5
$\begingroup$

tensorflow version = '1.12.0'

keras version = '2.1.6-tf'

I'm using keras with tensorflow backend.

I want to get the probabilities values of the prediction. I want the probabilities to sum up to 1. I tried using 'softmax' and 'categorical_crossentropy' but nothing works.

This is my model:

X = pickle.load(open("X.pickle", "rb"))
y = pickle.load(open("y.pickle", "rb"))

number_of_gestures = 5 
y = to_categorical(y, num_classes=number_of_gestures) #to_categorical is a function from keras - np_utils.

model = Sequential()
model.add(Conv2D(16, (2,2), input_shape=(IMG_SIZE, IMG_SIZE, 1), activation='relu'))

model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))

model.add(Conv2D(32, (5,5), activation='relu'))

model.add(MaxPooling2D(pool_size=(5, 5), strides=(5, 5), padding='same'))

model.add(Conv2D(64, (5,5), activation='relu'))

model.add(Flatten())

model.add(Dense(128, activation='relu'))

model.add(Dropout(0.2))

model.add(Dense(number_of_gestures, activation='softmax'))

sgd = optimizers.SGD(lr=1e-2)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
model.fit(X, y, batch_size=500, epochs=40, validation_split=0.1)

The probabilities looks like that:

[1. 0. 0. 0. 0.]

And I want it to look like this:

[0.897. 0.023. 0.158. 0.780. 0.1021]

I know it does not sum up to 1 but this is just an example.

$\endgroup$
2
  • 1
    $\begingroup$ A couple of questions: When you say that "the probabilities look like this", how do you get them right now? What is the size of your datasets? $\endgroup$
    – Mark.F
    Feb 12, 2019 at 7:25
  • $\begingroup$ Can you please provide the code for your prediction cycles? The output that you're posting is quite telling. It's possible that you're already getting the probabilities you want but your prediction code is using some sort of argmax() function that is creating the [1,0,0,0] output. $\endgroup$ Feb 12, 2019 at 22:52

4 Answers 4

2
$\begingroup$

I faced such a problem using CNN in Keras. Even, I thought that the output is being processed in the such a way like the argmax().

  • But, after investigating the model, I found that the model are learned and generalised so well that the labels got binarized ( 0s and 1s ). Remember, that we give the label of an image as a one hot vector ( like [ 1 0 0 0 ] ). The NN learned it so well that the output, usually being a probability vector , was now a one hot vector on which it was trained.

  • Another way to prove this is that you are using a softmax activation function at the output layer. A softmax function produces probabilities which sum up to 1. The sum of all class probabilities is 1. In the vector [ 1 0 0 0 0 ]. The sum of all numbers is 1. Hence the output is valid.

This is just a different phase in training your model. You can try to increase the size of your dataset or lower the learning rate to 0.0001 or even smaller.

$\endgroup$
0
$\begingroup$

My guess is that you are calling predict when you need to call predict_proba.

$\endgroup$
0
$\begingroup$

As I found on stackoverflow something similar that did the job for me, here is the link [StackOverflow Link][1]. Which I'm citing "

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 Predictions
    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)/255 #add this one
    test_image2=np.expand_dims(test_image2,axis=0)
    luna=classifier.predict_proba(test_image2)


  [1]: https://stackoverflow.com/questions/54513742/how-do-i-get-probability-confidence-as-output-for-a-cnn-using-keras-in-python
$\endgroup$
0
$\begingroup$

To get probabilities, you have to use something like this:

probabilities = tf.nn.softmax(prediction_results).numpy()
$\endgroup$

Your Answer

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

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