Addressing Shape Mismatch Error in TensorFlow Code for (None, 224, 224, 3) vs. (TensorSpec(shape=(None, None, 224, 224, 3)) Shape: Troubleshooting and Resolution

**code **

from google.colab import drive

drive.mount(‘/content/gdrive’)
Mounted at /content/gdrive

import tensorflow as tf

import numpy as np

import matplotlib.pyplot as plt

import tensorflow_datasets as tfds

from tensorflow.keras.layers import Conv2D , MaxPool2D,Dense,Flatten,InputLayer,BatchNormalization

from tensorflow.keras.losses import BinaryCrossentropy

from tensorflow.keras.optimizers import Adam

dataset_path = ‘/content/gdrive/MyDrive/Dataset malaria/Train’

dataset_info = tfds.builder(“malaria”)

dataset_info = dataset_info.info

dataset = tf.keras.preprocessing.image_dataset_from_directory(
dataset_path,
validation_split=0.2,
subset=“training”,
seed=1337,
image_size=(124, 121),
batch_size=32,

)

Found 416 files belonging to 2 classes.
Using 333 files for training.

train_dataset = dataset
val_dataset = tf.keras.preprocessing.image_dataset_from_directory(
dataset_path,
validation_split=0.2,
subset=“validation”,
seed=1337,
image_size=(224, 224),
batch_size=32,
)

Found 416 files belonging to 2 classes.
Using 83 files for validation.

test_dataset = tf.keras.preprocessing.image_dataset_from_directory(
dataset_path,
validation_split=0.1, # Adjust as needed
subset=“validation”,
seed=1337,
image_size=(124, 121),
batch_size=32,
)

Found 416 files belonging to 2 classes.
Using 41 files for validation.

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

train_dataset, val_dataset, test_dataset = splits(train_dataset, 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()))

**[(array([[[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
…,
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]],


   [[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]],


   [[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]],


   ...,


   [[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]],


   [[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]],


   [[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]]], dtype=float32), array([0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1,
   0, 0, 0, 1, 0, 1, 1, 0, 0, 1], dtype=int32))] [(array([[[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]],


   [[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]],


   [[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]],


   ...,


   [[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]],


   [[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]],


   [[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]]], dtype=float32), array([0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0,
   0, 1, 0, 0, 0, 0, 0, 1, 0, 1], dtype=int32))] [(array([[[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]],


   [[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]],


   [[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]],


   ...,


   [[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]],


   [[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]],


   [[[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    ...,

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]],

    [[0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.],
     ...,
     [0., 0., 0.],
     [0., 0., 0.],
     [0., 0., 0.]]]], dtype=float32), array([1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1,
   1, 1, 1, 0, 1, 1, 0, 1, 1, 0], dtype=int32))]

[ ] **

for i, (images, labels) in enumerate(train_dataset.take(1)): # Taking 1 batch for simplicity
for j in range(16): # Displaying 16 images
ax = plt.subplot(4, 4, j + 1)
plt.imshow(images[j].numpy().astype(“uint8”)) # Convert to NumPy array and adjust data type
plt.title(dataset_info.features[‘label’].int2str(labels[j].numpy())) # Convert label to NumPy array
plt.axis(‘off’)

plt.show()

dataset_info.features[‘label’].int2str(1)
uninfected

for data in dataset.take(4):
print(data)

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

** MapDataset element_spec=(TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name=None), TensorSpec(shape=(None,), dtype=tf.int32, name=None))**

BATCH_SIZE = 32

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

BATCH_SIZE = 32

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

train_dataset
<_PrefetchDataset element_spec=(TensorSpec(shape=(None, None, 224, 224, 3), dtype=tf.float32, name=None), TensorSpec(shape=(None, None), dtype=tf.int32, name=None))>

** ERROR OCCUR here is mismatch shape input need 4 tensorshape but code provide 5 tensor shape (code run fine but error occur on last epoche line) **

val_dataset
<_PrefetchDataset element_spec=(TensorSpec(shape=(None, None, 224, 224, 3), dtype=tf.float32, name=None), TensorSpec(shape=(None, None), dtype=tf.int32, name=None))>

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

Conv2D(filters = 6 , kernel_size = 5, 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()

**Model: “sequential_6”


Layer (type) Output Shape Param #

conv2d_12 (Conv2D) (None, 220, 220, 6) 456

batch_normalization_24 (Ba (None, 220, 220, 6) 24
tchNormalization)

max_pooling2d_12 (MaxPooli (None, 110, 110, 6) 0
ng2D)

conv2d_13 (Conv2D) (None, 108, 108, 16) 880

batch_normalization_25 (Ba (None, 108, 108, 16) 64
tchNormalization)

max_pooling2d_13 (MaxPooli (None, 54, 54, 16) 0
ng2D)

flatten_6 (Flatten) (None, 46656) 0

dense_18 (Dense) (None, 100) 4665700

batch_normalization_26 (Ba (None, 100) 400
tchNormalization)

dense_19 (Dense) (None, 10) 1010

batch_normalization_27 (Ba (None, 10) 40
tchNormalization)

dense_20 (Dense) (None, 1) 11

=================================================================
Total params: 4668585 (17.81 MB)
Trainable params: 4668321 (17.81 MB)
Non-trainable params: 264 (1.03 KB)**

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)

<tf.Tensor: shape=(), dtype=float32, numpy=4.9340706>

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

ONLY ERROR IN LAST LINE BECAUSE OF SHAPE MISMATCH

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

** ValueError: Input 0 of layer “sequential_6” is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(None, None, 224, 224, 3)**

provide me solution please

Hi @Palak_Talreja
The formatting of your post/question is awful, and makes it difficult to help (at least for me!)
Maybe you can share the Google Colab?
Thank you.

1 Like

can you send me your mail I will send you

@tagoma provide your E-mail

@tagoma here is the link to open my google colab file please solve the problem

Hi @Palak_Talreja, I have gone through your code and i have a few suggestions, while loading the dataset you can pass the desired shape (224,224) so that you don’t need to use the resize_rescale function. For rescaling you can pass the rescaling layer in the model

lenet_model = tf.keras.Sequential([
    InputLayer(input_shape = (224,224, 3)),
    Rescaling(1./127.5,offset=-1),
    Conv2D(filters = 6 , kernel_size = 5, 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()

And also in the colab i can see the Image shape before resizing: (8, 32, 224, 224, 3) but there is no code mentioned for this output.

while training the model, the model expects the training data to be in (Batch_size,height,width,channels). But you are passing the dataset of shape (8, 32, 224, 224, 3) to the model so you are getting the error.

I have trained the model with the above mentioned changes and did not face any error. Please refer to this gist for working code. Thank You.

its great code is working now thankyou so much dear for helping us :smiling_face_with_three_hearts: :heart: :heart: :heart: