0

I would like to ask you how to solve this problem. I have two folders of pictures, one as train set and the other as validation. What i've done is to use ImageDataGenerator:

train_datagen = ImageDataGenerator(
        rescale=1./255,
        rotation_range=40, #integer degree range for random rotations
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)


# to have a better performance in accuracy measure I just rescale the test
test_datagen = ImageDataGenerator(rescale=1./255)

# apply ImageDataGenerator on requierd folde 
train_generator = train_datagen.flow_from_directory(
        '/content/drive/MyDrive/NN_HW2/training/training/',  # this is the target directory
        target_size=(150, 150),  # all images will be resized to 150x150
        batch_size=batch_size,
        class_mode='categorical')  # since I am in multicalss calssification 

# this is a similar generator, for validation data
validation_generator = test_datagen.flow_from_directory(
        '/content/drive/MyDrive/NN_HW2/validation/validation/',
        target_size=(150, 150),
        batch_size=batch_size,
        class_mode='categorical')

Then, I defined my model like this :

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(150, 150, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('softmax'))


model.compile(loss='categorical_crossentropy',
              optimizer='Adam',
              metrics=['accuracy']
              )

So, when I try to fit the model, colab raise me the following error:

model.fit_generator(
        train_generator,
        steps_per_epoch=1000//batch_size,
        epochs=25,
        validation_data=validation_generator,
        steps_per_epoch=500//batch_size
        )
Matrix size-incompatible: In[0]: [16,10], In[1]: [64,1]
     [[node gradient_tape/sequential_3/dense_7/MatMul (defined at <ipython-input-22-f61b6c381681>:7) ]] [Op:__inference_train_function_3405]

Function call stack:
train_function

Thank you for the time

1 Answers1

0

in your code below

model.fit_generator(
        train_generator,
        steps_per_epoch=1000//batch_size,
        epochs=25,
        validation_data=validation_generator,
        steps_per_epoch=500//batch_size
        )

You defined steps_per_epoch twice. What you want to do is leave the first one, delete the second one and replace it with

 validation_steps=500//batch_size 

Alternatively since you are using generators, you can leave steps_per_epoch=None and validation_steps=None. model.fit_generator is being depreciate so just use model.fit.

Gerry P
  • 7,662
  • 3
  • 10
  • 20