7

I am learning about designing Convolutional Neural Networks using Keras. I have developed a simple model using VGG16 as the base. I have about 6 classes of images in the dataset. Here are the code and description of my model.

model = models.Sequential()
conv_base = VGG16(weights='imagenet' ,include_top=False, input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3))
conv_base.trainable = False
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu', kernel_regularizer=regularizers.l2(0.001)))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(6, activation='sigmoid'))

1

Here is the code for compiling and fitting the model:

model.compile(loss='categorical_crossentropy',
        optimizer=optimizers.RMSprop(lr=1e-4),
         metrics=['acc'])
model.summary()

callbacks = [
    EarlyStopping(monitor='acc', patience=1, mode='auto'),
    ModelCheckpoint(monitor='val_loss', save_best_only=True, filepath=model_file_path)
]

history = model.fit_generator(
    train_generator,
    steps_per_epoch=10,
    epochs=EPOCHS,
    validation_data=validation_generator,
    callbacks = callbacks,
    validation_steps=10)

Here is the code for prediction of a new image

img = image.load_img(img_path, target_size=(IMAGE_SIZE, IMAGE_SIZE))
plt.figure(index)
imgplot = plt.imshow(img)

x = image.img_to_array(img)
x = x.reshape((1,) + x.shape)
prediction = model.predict(x)[0]
# print(prediction)

Often model.predict() method predicts more than one class.

[0 1 1 0 0 0]

I have a couple of questions

  1. Is it normal for a multiclass classification model to predict more than one output?
  2. How is accuracy measured during training time if more than one class was predicted?
  3. How can I modify the neural network so that only one class is predicted?

Any help is appreciated. Thank you so much!

TMS
  • 1,201
  • 1
  • 13
  • 20

1 Answers1

7

You are not doing multi-class classification, but multi-label. This is caused by the use of a sigmoid activation at the output layer. To do multi-class classification properly, use a softmax activation at the output, which will produce a probability distribution over classes. Taking the class with the biggest probability (argmax) will produce a single class prediction, as expected.

Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140
  • Thank you for the explanation. That was it!. Can you explain how accuracy is measured by Keras train function? For example, during training, suppose the model predicted [0, 0.2, 0.4, 0.7, 0.1, 0]. Does keras look at class 3, with the accuracy of 0.7 and consider that as the output of the model to compare with the truth? – TMS Aug 04 '17 at 16:27
  • This approach is not library specific. The general multi-class classification probability is to use softmax activation with n output classes, taking the "pick" to be the one of the highest probability. So in your case, yes class 3 is considered to be the selected class. Accuracy on a single sample is binary and averaged over your input. @TMS. – modesitt Aug 04 '17 at 16:54