2
$\begingroup$

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(MaxPool2D(pool_size=(2, 2)))

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

classifier.add(Flatten())

classifier.add(Dense(units=128, activation='relu'))

classifier.add(Dense(units=2, activation='softmax'))

# Metrics will be categorical_accuracy
classifier.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['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)
$\endgroup$
2
  • $\begingroup$ 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 $\endgroup$
    – Aditya
    Mar 10, 2018 at 6:31
  • $\begingroup$ 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. $\endgroup$ May 14, 2018 at 10:52

4 Answers 4

1
$\begingroup$

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).
$\endgroup$
1
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Yep either the probability is e or 1-e $\endgroup$
    – Aditya
    Mar 10, 2018 at 16:59
0
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.