Skip to content Skip to sidebar Skip to footer

How Do I Get Probability/confidence As Output For A Cnn Using Keras In Python?

So, I'm new to deep learning and I've started with cats and dogs dataset for a CNN Model using Keras. In my code, I'm unable to get probabilities as output for both classifier.pred

Solution 1:

I think I found the mistake. You are rescaling your train and test data with the ImageDataGenerator. But you are not doing that when testing a single image. Try this:

# Making new Predictionsimport numpy as np
from keras.preprocessing import image

test_image_luna = image.load_img('D:\\NR\\data\\live2013\\caps.bmp', target_size=(64,64))
test_image2 = image.img_to_array(test_image_luna)/255.
test_image2 = np.expand_dims(test_image2, axis=0)
luna = classifier.predict_proba(test_image2)

The high input values lead to very high output values. Since you are using softmax activation these values are leading to predictions very close to 0 and 1.

Solution 2:

I'm looking for something like, [[0.4,0.6]], [[0.89,0.11]]

classifier.predict is the method you should use to get probabilities. Could you check again, considering the following tips?

There are two ways to build a binary classifier:

  1. NN with one output neuron with sigmoid activation. The output a is interpreted as the probability for class 1, thus the probability for class 2 is 1-a.
  2. NN with two output neurons using softmax activation. Each neuron is then interpreted as the probability of one class.

Both are valid options, but since you are doing 2. you should use softmax activation.

I've tried changing loss function from binary_crossentropy to categorical_crossentropy.

This should not make a difference, it's basically the same formula.

I think I might be going wrong with the data type, as the type of array of output is float32. But even if that is the error, I don't know how to change it though.

This is also not the cause of the error, since the type float32 is right for probability outputs.

Solution 3:

Either predict() or predict_generator() would work.

import numpy as np
from keras.preprocessing import image

test_image_luna=image.load_img('dataset/single/SkilletLuna.JPG',target_size=(64,64))
test_image2=image.img_to_array(test_image_luna)
test_image2=np.expand_dims(test_image2,axis=0)
luna=classifier.predict(test_image2)

print(luna)

If you'd want prediction probabilities on 'n' images (or 'n' subsets of an image as in your case), you could try predict_generator():

from keras.preprocessing.image import ImageDataGenerator

test_datagen=ImageDataGenerator(rescale=1./255)
test_set = test_datagen.flow_from_directory('dataset/test_set',
                                             target_size=(64,64),
                                             batch_size=32,
                                             class_mode='categorical')

predicted_probabilities = classifier.predict_generator(test_set)
print(predicted_probabilities)

Use the following to print in percentage rounded to 2 decimal places:

print(np.round(luna*100,2))
print(np.round(predicted_probabilities*100,2))

Let me know if this works for you!

Post a Comment for "How Do I Get Probability/confidence As Output For A Cnn Using Keras In Python?"