Implementing image_dataset_from directory instead of flow_from_directory

Hello!
I was writing a transfer learning model where I wanted to do preprocessing and take input images so i wrote the following code

 train_batches = ImageDataGenerator().flow_from_directory(train_path,target_size=(224,224),batch_size=10)
 valid_batches = ImageDataGenerator().flow_from_directory(valid_path,target_size=(224,224),batch_size=30)
 test_batches= ImageDataGenerator().flow_from_directory(test_path,target_size=(224,224), batch_size=50, shuffle=False)

I proceeded to write the entire model and it ran successfully
However I then realised that from_from_directory is deprecated and we are required to use
image_from_dataset instead
So I tried to write the equivalent code for it ( Below)

train_dataset = tf.keras.preprocessing.image_dataset_from_directory(train_path, image_size=(224, 224), batch_size=10)
# valid_dataset = tf.keras.preprocessing.image_dataset_from_directory(valid_path, image_size=(224, 224), batch_size=30)
# test_dataset = tf.keras.preprocessing.image_dataset_from_directory(test_path, image_size=(224, 224), batch_size=50, shuffle=False)

However this time while training the model, it is giving the following error

 target.shape.assert_is_compatible_with(output.shape)

    ValueError: Shapes (None, 1) and (None, 10) are incompatible

How to recitfy this?

Hi @Harshwardhan_Fartale

Welcome to the TensorFlow Forum!

This error shows your dataset has different output what you have defined in the model definition. Please provide us the detail of model definition, model compilation code and how many classes you have in the dataset to understand the issue. Thank you.

1 Like