0

I am receiving a value error when attempting to fit my model to the training set and am unsure how to reshape either my labels or logits to resolve this issue.

The code for the model is below, and for reference:

train_images.image_shape = (256, 256, 3), total of 1611 images

train_images.labels.shape = (1611,)

val_images.image_shape = (256, 256, 3), total of 537 images

val_images.labels.shape = (537,)

I used flow_from_directory to create the DirectoryIterator objects for the training/validation sets and set the class_mode to 'binary'.

model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(16, (3,3), activation = 'relu', input_shape = (256, 256, 3)),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(32, (3,3), activation = 'relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(64, (3,3), activation = 'relu'),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512, activation = 'relu'),
    tf.keras.layers.Dense(1, activation = 'sigmoid')
])

model.compile(optimizer = RMSprop(), loss = 'binary_crossentropy', metrics = ['accuracy'])

demo = model.fit(train_images, epochs = 5, validation_data = val_images)

Any advice would be much appreciated, thank you!

EDIT - adding generator code

Generator Code:

train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
    rescale = 1/255,
    validation_split = 0.25
)

train_images = train_datagen.flow_from_directory(
    directory = dir_path,
    class_mode = 'binary',
    target_size=(256, 256),
    shuffle = True,
    batch_size = 128,
    subset = 'training'
)

val_images = train_datagen.flow_from_directory(
    directory = dir_path,
    class_mode = 'binary',
    target_size=(256, 256),
    shuffle = True,
    batch_size = 16,
    subset = 'validation'
)

Found 1611 images belonging to 2 classes.

Found 537 images belonging to 2 classes.

Paul Song
  • 25
  • 5

1 Answers1

0

Hmm I copied you code and ran it locally on a data set having 2 classes and it worked fine. So only thing I can think of is there might be something strange in your data set. To check run the code below after you create your generators

images, labels=next(train_images)
print (images.shape, labels.shape)
images, labels=next(val_images)
print (images.shape, labels.shape)

you should get the printed result

(128, 256, 256, 3) (128,)
(16, 256, 256, 3) (16,)

If that comes out correct check if anywhere in your code you do anything with the train_images or val_images before you fit the model. Make sure you dataset is structured as

dir_path
----------class 0 directory (if doing cats and dogs classification this is cats)
          ----- image file 0 (cat image 0)
          ----- image file 1
          ----- images file N
---------class 1 directory (if doing cats and dogs classification this is dogs)
          ----- image file 0 (dog image 0)
          ----- image file 1
          ----- images file N
Gerry P
  • 7,662
  • 3
  • 10
  • 20
  • so I just noticed there is a txt file in each of the class directories. does the ImageDataGenerator/Directory Iterator objects automatically bypass any non-image files? – Paul Song Aug 07 '22 at 21:32
  • To test it I put a .txt file in each of the class directories and ran the code again . Apparently train_datagen.flow_from_directory ignores files that are not image files as the notebook ran properly – Gerry P Aug 07 '22 at 23:37