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

"after converting the dataset to the tfrecord file format, I tried to train the model I created with it, but I couldn't convert it to the input format suitable for the model. image shape (224,224.3). The model has already passed through the pre-processing layers The image cannot be converted to the correct shape for the model's input


import matplotlib.pylab as plt
import numpy as np
import tensorflow as tf
import keras

IMG_HEIGHT = 224
IMG_WIDTH = 224
IMG_CHANNELS = 3
   
def decode_image(image, shape=None):
    image = tf.image.decode_jpeg(image, channels=3)
    image = tf.cast(image, dtype=tf.float32)
    image = tf.convert_to_tensor(image, dtype=np.float32)
    return image

def parse_tfrecord(example):
    feature_description = {
        "image" : tf.io.FixedLenFeature([], tf.string),
        "height" : tf.io.FixedLenFeature([], tf.int64),
        "width" : tf.io.FixedLenFeature([], tf.int64),
        "depht" : tf.io.FixedLenFeature([], tf.int64),
        "shape" : tf.io.VarLenFeature(tf.int64),
        "label" : tf.io.FixedLenFeature([], tf.string, default_value=""),
        "label_index" : tf.io.FixedLenFeature([], tf.int64)
    }
    example = tf.io.parse_single_example(example, feature_description)
    # tf.sparse.to_dense(example["shape"]

    image = decode_image(example["image"], [224,224,3])
    # image = tf.image.decode_jpeg(example["image"], [224,224,3])
    label_index = tf.cast(example["label_index"], tf.int32)
    image = tf.reshape(image, (224,224,3))
    # label_indx = rec["label_index"]
    return image, label_index


def preprocess(image, label_index):
    prepro_image = tf.expand_dims(image, 0)
    prepro_image = keras.layers.Resizing(224 , 224, crop_to_aspect_ratio=True, input_shape=(224,224,3))(prepro_image)
    prepro_image = keras.layers.Rescaling(1./255)(prepro_image)
    prepro_image = keras.layers.RandomFlip(mode="horizontal_and_vertical")(prepro_image)
    prepro_image = tf.squeeze(prepro_image, 0)
    return prepro_image, label_index

batch_size = 32
epochs = 1
steps_per_epoch = 50
AUTOTUNE = tf.data.AUTOTUNE

def create_preproc_dataset(pattern):
    trainds = tf.data.TFRecordDataset(pattern).map(parse_tfrecord).map(preprocess).shuffle(25)
    return trainds
  
trainds = create_preproc_dataset("valid_tfrecords")


NUM_EPOCHS = 25
IMG_HEIGHT = 224
IMG_WIDTH = 224
IMG_CHANNELS = 3

CLASS_NAMES = 'bluebel buttercup crocus daffodil daisy dandelion iris lilyvalley pansy snowdrop sunflowers tulip'.split()

def train_and_evaluate(batch_size = 32,
                       lrate = 0.001,
                       l1 = 0.,
                       l2 = 0.,
                       num_hidden = 16):
    regularizer = tf.keras.regularizers.l1_l2(l1, l2)
    
    train_dataset = create_preproc_dataset(
        'train_tfrecord')
    eval_dataset = create_preproc_dataset(
        'valid_tfrecord')
    


    layers = [
    # input layer

    # image preprocessing layers
    # keras.layers.Resizing(height=224, width=224,crop_to_aspect_ratio=True, input_shape=(224,224,3)),
    #keras.layers.RandomFlip(mode="horizontal_and_vertical", name='random_lr_flip/none'),
    #RandomColorDistortion(name='random_contrast_brightness/none'),

    # core layers
    keras.layers.Conv2D(32, 3, activation="relu", input_shape=(224, 224, 3)),
    keras.layers.MaxPooling2D(pool_size=2),

    keras.layers.Conv2D(64, 3, activation="relu"),
    keras.layers.MaxPooling2D(pool_size=2),

    keras.layers.Conv2D(128, 3, activation="relu"),
    keras.layers.MaxPooling2D(pool_size=2),

    keras.layers.Conv2D(256, 3, activation="relu"),
    keras.layers.MaxPooling2D(pool_size=2),

    # flatten layers
    keras.layers.Flatten(),

    keras.layers.Dense(num_hidden, activation="relu"),
    keras.layers.Dropout(0.5),

    # output layers
    keras.layers.Dense(12, activation="softmax")
]

    # checkpoint and early stopping callbacks
    model_checkpoint_cb = keras.callbacks.ModelCheckpoint(
        filepath="\work_images\chkpts",monitor = "val_accuracy", mode="max",
        save_best_only=True)
    early_stopping_cb = keras.callbacks.EarlyStopping(
        monitor="val_accuracy", mode="max",
        patience=2)

    # model training
    model = tf.keras.Sequential(layers, name='flower_classification')
    model.build([ 224, 224, 3])
    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=lrate),
                loss=tf.keras.losses.CategoricalCrossentropy(
                    from_logits=False),
                metrics=['accuracy'])
           
    print(model.summary())
    history = model.fit(train_dataset, validation_data=eval_dataset, 
    epochs=NUM_EPOCHS,
    callbacks=[model_checkpoint_cb, early_stopping_cb])

    training_plot(['loss', "accuracy"], history)
    return model


model = train_and_evaluate()


Solution 1:[1]

Replace this

def create_preproc_dataset(pattern):    
    trainds = tf.data.TFRecordDataset(pattern).map(parse_tfrecord).map(preprocess).shuffle(25)
    return trainds

with this

def create_preproc_dataset(pattern):    
        trainds = tf.data.TFRecordDataset(pattern).map(parse_tfrecord).map(preprocess).shuffle(25).batch(32)
        return trainds

and changing the loss to SparseCategoricalCrossentropy will help.

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=lrate),
                loss=tf.keras.losses.SparseCategoricalCrossentropy(
                    from_logits=False),
                metrics=['accuracy'])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1