Working with multiple errors at once

I’m working with Malaria dataset and even though I extensively looked throughout the tutorial, documentations, searching over Stackoverflow and looking with chatGPT, I couldn’t find a solution

Here’s the file

`# -- coding: utf-8 --
“”"malaria_detection.ipynb

Automatically generated by Colaboratory.

Original file is located at

Importing Libraries

“”"

import tensorflow as tf### models
import numpy as np### math computations
import matplotlib.pyplot as plt### plots
import sklearn### machine learning library
import cv2## image processing
from sklearn.metrics import confusion_matrix, roc_curve### metrics
import seaborn as sns### visualizations
import datetime
import io
import os
import random
from google.colab import files
from PIL import Image
import albumentations as A
import tensorflow_datasets as tfds
import tensorflow_probability as tfp
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Layer
from tensorflow.keras.layers import Conv2D, MaxPool2D, Dense, Flatten, InputLayer, BatchNormalization, Input, Dropout, RandomFlip, RandomRotation, Resizing, Rescaling
from tensorflow.keras.losses import BinaryCrossentropy
from tensorflow.keras.metrics import BinaryAccuracy, FalsePositives, FalseNegatives, TruePositives, TrueNegatives, Precision, Recall, AUC, binary_accuracy
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import Callback, CSVLogger, EarlyStopping, LearningRateScheduler, ModelCheckpoint, ReduceLROnPlateau
from tensorflow.keras.regularizers import L2, L1
from tensorboard.plugins.hparams import api as hp
from google.colab import drive
from keras import Sequential

“”"# Data Preparation

Data Loading

“”"

dataset, dataset_info = tfds.load(‘malaria’, with_info = True, as_supervised = True, shuffle_files = True, split = [‘train’])

for data in dataset[0].take(4):
print(data)

dataset

dataset_info

def splits(dataset, TRAIN_RATIO, VAL_RATIO, TEST_RATIO):

DATASET_SIZE = len(dataset)

train_dataset = dataset.take(int(TRAIN_RATIO*DATASET_SIZE))

val_test_dataset = dataset.skip(int(TRAIN_RATIODATASET_SIZE))
val_dataset = val_test_dataset.take(int(VAL_RATIO
DATASET_SIZE))

test_dataset = val_test_dataset.skip(int(VAL_RATIO*DATASET_SIZE))
return train_dataset, val_dataset, test_dataset

TRAIN_RATIO = 0.8
VAL_RATIO = 0.1
TEST_RATIO = 0.1

#dataset = tf.data.Dataset.range(10)
train_dataset, val_dataset, test_dataset = splits(dataset[0], TRAIN_RATIO, VAL_RATIO, TEST_RATIO)
print(list(train_dataset.take(1).as_numpy_iterator()), list(val_dataset.take(1).as_numpy_iterator()), list(test_dataset.take(1).as_numpy_iterator()))

dataset

“”“# Data Visualization”“”

for i, (image, label) in enumerate(train_dataset.take(16)):
ax = plt.subplot(4, 4, i + 1)
plt.imshow(image)
plt.title(dataset_info.features[‘label’].int2str(label))
plt.axis(‘off’)

dataset_info.features[‘label’].int2str(0)

“”“# Data Preprocessing”“”

IM_SIZE = 224

def resize_rescale(image, label):
return tf.image.resize(image, (IM_SIZE, IM_SIZE))/255.0, label

train_dataset = train_dataset.map(resize_rescale)
val_dataset = val_dataset.map(resize_rescale)
test_dataset = test_dataset.map(resize_rescale)

train_dataset
val_dataset
test_dataset

for image, label in train_dataset.take(1):
print(image, label)

BATCH_SIZE = 32
train_dataset = train_dataset.shuffle(buffer_size = 8, reshuffle_each_iteration= True).batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)

val_dataset = val_dataset.shuffle(buffer_size = 8, reshuffle_each_iteration= True).batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)

train_dataset

val_dataset

“”“# Model Creation and Training”“”

IM_SIZE = 224
lenet_model = Sequential([
InputLayer(input_shape=(IM_SIZE, IM_SIZE, 3)),

Conv2D(filters = 6, kernel_size = 3, strides = 1, padding = 'valid', activation = 'relu'),
BatchNormalization(),
MaxPool2D(pool_size = 2, strides = 2),

Conv2D(filters = 16, kernel_size = 3, strides = 1, padding='valid', activation='relu'),
BatchNormalization(),
MaxPool2D(pool_size = 2, strides = 2),

Flatten(),

Dense(100, activation="relu"),
BatchNormalization(),

Dense(10, activation="relu"),
BatchNormalization(),

Dense(1, activation="sigmoid"),

])

lenet_model.summary()

“”“# Functional API”“”

IM_SIZE = 224
func_input = Input((IM_SIZE, IM_SIZE, 3), name = “Input Image”)

x = Conv2D(filters = 6, kernel_size = 3, strides = 1, padding = ‘valid’, activation = ‘relu’) (func_input)
x = BatchNormalization()(x)
x = MaxPool2D(pool_size = 2, strides = 2)(x)

x = Conv2D(filters = 16, kernel_size = 3, strides = 1, padding=‘valid’, activation=‘relu’)(x)
x = BatchNormalization()(x)
x = MaxPool2D(pool_size = 2, strides = 2)(x)

x = Flatten()(x)

x = Dense(100, activation=“relu”)(x)
x = BatchNormalization()(x)

x = Dense(10, activation=“relu”)(x)
x = BatchNormalization()(x)

func_output = Dense(1, activation=“sigmoid”)(x)

lenet_model = Model(func_input, func_output, name = “Lenet Model”)
lenet_model.summary()

“”“# Model Training”“”

y_true = [0, 1, 0, 0]
y_pred = [0.6, 0.51, 0.94, 1]
bce = tf.keras.losses.BinaryCrossentropy()
bce(y_true, y_pred)

lenet_model.compile(optimizer=Adam(learning_rate=0.01),
loss=BinaryCrossentropy(),
metrics = ‘accuracy’,)

history = lenet_model.fit(train_dataset, validation_data = val_dataset, epochs=10, verbose=1)

plt.plot(history.history[‘loss’])
plt.plot(history.history[‘val_loss’])
plt.title(‘Model loss’)
plt.ylabel(‘loss’)
plt.xlabel(‘epoch’)
plt.legend([‘train_loss’, ‘val_loss’])
plt.show()

plt.plot(history.history[‘accuracy’])
plt.plot(history.history[‘val_accuracy’])
plt.title(‘Model Accuracy’)
plt.ylabel(‘Accuracy’)
plt.xlabel(‘Epoch’)
plt.legend([‘train_accuracy’, ‘val_accuracy’])
plt.show()

“”“# Model Evaluation and Testing”“”

test_dataset = test_dataset.batch(1)

test_dataset

lenet_model.evaluate(test_dataset)

def parasite_or_not(x):
if x < 0.5:
return str(‘P’)
else:
return str(‘U’)

parasite_or_not(lenet_model.predict(test_dataset.take(1))[0][0])

for i, (image, label) in enumerate(test_dataset.take(9)):
ax = plt.subplot(3, 3, i + 1)
plt.imshow(image[0])
plt.title(str(parasite_or_not(label.numpy()[0])) + “:” + str(parasite_or_not(lenet_model.predict(image)[0][0])))
plt.axis(‘off’)

“”"# Saving and Loading

Saving and Loading from Google Drive

“”"

drive.mount(‘/content/drive/’)

!cp -r /content/lenet/ /content/drive/MyDrive/lenet_colab/

!cp -r /content/drive/MyDrive/lenet_colab/ /content/lenet_colab/
`

Errors

-ValueError Traceback (most recent call last)
in <cell line: 1>()
----> 1 history = lenet_model.fit(train_dataset, validation_data = val_dataset, epochs=10, verbose=1)

3 frames
/usr/lib/python3.10/contextlib.py in enter(self)
133 del self.args, self.kwds, self.func
134 try:
→ 135 return next(self.gen)
136 except StopIteration:
137 raise RuntimeError(“generator didn’t yield”) from None

ValueError: in user code:

File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1284, in train_function  *
    return step_function(self, iterator)
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1268, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1249, in run_step  **
    outputs = model.train_step(data)
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1050, in train_step
    y_pred = self(x, training=True)
File "/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
File "/usr/lib/python3.10/contextlib.py", line 492, in enter_context
    result = _cm_type.__enter__(cm)
File "/usr/lib/python3.10/contextlib.py", line 135, in __enter__
    return next(self.gen)

ValueError: 'Lenet Model/' is not a valid root scope name. A root scope name has to match the following pattern: ^[A-Za-z0-9.][A-Za-z0-9_.\\/>-]*$

ValueError: in user code:

File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 2169, in predict_function  *
    return step_function(self, iterator)
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 2155, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 2143, in run_step  **
    outputs = model.predict_step(data)
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 2111, in predict_step
    return self(x, training=False)
File "/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.10/dist-packages/keras/engine/input_spec.py", line 298, in assert_input_compatibility
    raise ValueError(

ValueError: Input 0 of layer "Lenet Model" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(None, None, None, 224, 224, 3)

I tried going through these errors but I couldn’t solve them and I get these errors multiple times

I appreciate all the help here

Hi @Ata_Tekeli, Lenet Model name is not valid as per the regular expression mentioned in the error([1][A-Za-z0-9_.\/>-]*$). could you please try with Lenet_Model or Lenet.Model.

This error suggests that the input shape of the data you are passing to the model does not match. Please make sure that the input data has the shape(224,224,3). Thank You.


  1. A-Za-z0-9. ↩︎