Final assignment - ImageDataGenerator - flow() - how to set 'categorical' class mode

Dear DeapLearning.ai tem,

The final assignment of the course requires to classify among 25(or 26) classes, representing Englsh alphabet.

  1. ImageDataGenerator. It was suggested to use the flow() method of ImageDataGenerator class.
    In the examples in the course, we used other method - flow_from_directory() with parameter ‘class_mode’ one can set to ‘categorical’. The flow() method however does not have such a parameter.
  2. create_model(). Because of 25 classes, the last layer shall be tf.keras.layers.Dense(25, activation=‘softmax’)

This threw ValueError: Shapes (None, 1) and (None, 25) are incompatible.

I was able to overcome the problem by using one-hot-encoding (tf.keras.utils.to_categorical()) of both training_labels and validation_labels when creating ImageDataGenerator.
This solution however diverted from the expected outocme of generators
My result “Labels of training generator have shape: (27455, 25)” - instead of expected “Labels of training generator have shape: (27455,)

What other approach exist to fix the ValueError shown above?

Thank you in advance for your explanation,
Sergey

This is the culprit change the unit of last dense layer to 1

Thank you for the suggestion.
However, we need to classify among 25 classes.
tf.keras.layers.Dense(1,
Such a change will restrict to a binary classification (or regression), won’t it?

Is your assignment name Multi-class classification?

Then in your parse data

You need to append label to (row[0])
Images append to (row[1])

Then convert the images into numpy array with use of correct data type
Then reshape the images using the correct images.shape[0] (HINTS AR GIVEN IN THE INSTRUCTIONS BEFORE THE GRADE CELL)

NOW convert labels into numpy array with use of correct data type

Note all of this instructions are given before grade cell.

Now comes to your last dense layer unit it should be 26 and not 25 as input shape is (28, 28, 1)

Actually I have the same problem. While parsing data and creating Generators I have all dimensions right, but I’m not able to train the model even with loss function SparseCategoricalCrossentropy.

Nevermind, I forgot the Flatten layer :frowning:

1 Like