-1

I'm trying to load a pre-trained model and pass an image to model.predict() in keras to predict the probabilities of each class. However, instead of probabilities, I get something like one hot encoding:

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

Code Snippet

What should I do to return the probabilities of each class instead of one hot encoding?

Here is the source code for the pre-trained model:

https://github.com/usmannazir/ChestX/blob/master/4Cnn_keras.ipynb

Please someone help me to solve this issue.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Are you sure that there's a problem? If the task (data) isn't ambiguous, then a well-trained model will output probabilities very close to a one-hot encoding; and when printing 0.000001 will be rounded to 0.0 and 0.9999999 will be rounded to 1.0. – Peteris Feb 24 '19 at 02:15
  • @Peteris It displays the same all the time irrespective of whichever image I pass to model.predict(). You can try it out on your system as well, as it is a small code. I just tried with dog image it displays the same result . I just wonder what could be the issue. – rella sirisha Feb 24 '19 at 02:24
  • http://idownvotedbecau.se/imageofcode ; plus, you say you use `model.predict()`, while in your (extremely hard to read) image-of-code you seem to use `model.predict_proba` - please **be consistent** – desertnaut Feb 24 '19 at 10:58
  • 1
    You are probably forgetting to normalize the input images by dividing by 255. – Dr. Snoopy Feb 24 '19 at 12:24

2 Answers2

1

For better understanding of possible reasons for this, please refer to this answer.

However quick things you can check are:

  • Whether you are preprocessing the images for prediction in the same way as the training image samples were preprocessed (for instance: normalising pixel values within [0,1] range)
  • Whether the model is actually trained correctly (to ensure this, try training the same model again and monitor accuracy if it is fluctuating or not)
bazinga
  • 136
  • 1
  • 10
0

print (classes[int (one_hot_predictions[2][2])])

Should do the trick to return a class name instead of integers