Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More than one prediction in multi-classification in Keras?

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!

like image 421
TMS Avatar asked Aug 04 '17 14:08

TMS


People also ask

How do you predict multiple labels?

Multi-label classification involves predicting zero or more class labels. Unlike normal classification tasks where class labels are mutually exclusive, multi-label classification requires specialized machine learning algorithms that support predicting multiple mutually non-exclusive classes or “labels.”

Which Optimizer is best for multiclass classification?

Multiclass Classification Neural Network using Adam Optimizer.

Does smote work for multiclass classification?

But SMOTE seems to work only on binary label data not with multiclass data. So how to use this SMOTE technique to balance my multiclass data. Apart from SMOTE is there any another technique to balance the data. Before you can post on Kaggle, you'll need to create an account or log in.


1 Answers

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.

like image 85
Dr. Snoopy Avatar answered Sep 20 '22 09:09

Dr. Snoopy