# Multi-Class classification with CNN using keras - trained model predicts object even in a fully white picture

I built an multi classification in CNN using keras with Tensorflow in the backend. It nicely predicts cats and dogs. However, when it comes to an image which does not have any object-white background image-, it still finds a dog ( lets say probability for dog class 0.75…, cats 0.24… ). I am a quite newbie learner in learning built with neural network.

Sorry if I am asking a silly question, even though I have searched the internet I could not find any answer.

What is my exception from the case of the white background image as an input to prediction method, is 0 probability for dog and cat classes.

Any suggestion would make me so happy.

The below is how I implemented the training.

classifier = Sequential()

classifier.add(Conv2D(32, 3, 3, input_shape=(64, 64, 3), activation='relu'))

classifier.add(Conv2D(32, 3, 3, activation='relu'))

# Metrics will be categorical_accuracy

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1. / 255)
training_set = train_datagen.flow_from_directory(
'/Users/ozercevikaslan/Desktop/Convolutional_Neural_Networks/dataset/training_set',
target_size=(64, 64),
batch_size=32,
class_mode='categorical')

test_set = test_datagen.flow_from_directory(
'/Users/ozercevikaslan/Desktop/Convolutional_Neural_Networks/dataset/test_set',
target_size=(64, 64),
batch_size=32,
class_mode='categorical')

classifier.fit_generator(
training_set,
steps_per_epoch=8000,
epochs=25,
validation_data=test_set,
validation_steps=2000)

• It will be something like this because the model has to either predict a dog or a cat, We don't have any other options – Aditya Mar 10 '18 at 6:31
• i have a similar question. what Should i I do if i have lets say 3 classes (cat, Dog, Cow). Then in this case how should i build the model. – aniket shrivastav May 14 '18 at 10:52

in categorical_crossentropy the sum of predictions are equal to one, in your case either cat or dog,

• maybe using accuracy threshold, (or) adding third class for unknowns (other images than dog/cat).

As I mentioned in my question post, the post is a bit silly even for a new learner. In this case, the world has only 2 classes which are dogs and cats so the output must be either a dog or a cat.

• Yep either the probability is e or 1-e – Aditya Mar 10 '18 at 16:59

As others have already said, you force the model to choose one or another. If you do not want this, then you can use sigmoid instead of softmax as the activation function in your final layer.

But beware, in addition to assigning low probabilities to both classes, the model could assign high probabilities to both classes. You will need to think about how you will handle and interpret that.

I think one possible solution could be to train a model trying to predict between three classes, i.e. between "cats", "dogs" and "others".

Of course in that case you need to train the model using examples for the third class also, for example making use of random pictures (that may comprise also white background images).