How to do multi-class image classification in keras?

For multi-class classification, the last dense layer must have a number of nodes equal to the number of classes, followed by softmax activation, i.e. the last two layers of your model should be:

model.add(Dense(num_classes))
model.add(Activation('softmax'))

Additionally, your labels (both train and test) must be one-hot encoded; so, assuming that your initial cats and dogs were labeled as integers (0/1), and your new category (airplane) is initially similarly labeled as '2', you should convert them as follows:

train_labels = keras.utils.to_categorical(train_labels, num_classes)
test_labels = keras.utils.to_categorical(test_labels, num_classes)

Finally, on a terminology level, what you are doing is multi-class, and not multi-label classification (I have edited the title of your post) - the last term is used for problems where a sample might belong to more than one categories at the same time.


For the multi-class classification, the size of the last layer of a NN must be equal the number of classes.

F.i. for your problem (3 Classes), the code should look like this:

model = Sequential()

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

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

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

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(3))
model.add(Activation('softmax'))