I have a CNN built in Keras. I have saved it and am now using the model.predict() function to make predictions from it. Whenever I run the following code,

def prediction(path):
    import keras
    from keras.preprocessing.image import load_img, img_to_array 
    from keras.models import load_model
    import PIL
    import numpy as np
    img = load_img(path)
    img = img.resize((224, 224))
    img = img_to_array(img) 

    img = img.reshape( -1,224, 224,3)

    model = load_model('model1.h5')
    pred = model.predict(img)

    return pred


I get an output like this:

[[7.578206e-37 1.000000e+00 0.000000e+00 0.000000e+00]]

I am doing transfer learning using resnet50 with imagenet weights and here is the model.summary().

I have 4 classes. How do I find out where each prediction belongs?

I have looked here as well but it doesn't seem to help me.


  • $\begingroup$ In your last dense layer, I show that dense layer output shape 4. But you mention that you have 2 classes. $\endgroup$
    – AIFahim
    Feb 20 '21 at 17:02
  • $\begingroup$ Oops, I was doing 2 classes earlier, then 4. The 2 just stuck to my mind. Sorry for that, I'll edit it. $\endgroup$
    – snookso
    Feb 21 '21 at 6:27

Model prediction output is a bunch of probabilities. In order to get category name you need use following snippet. It calculates the argmax of predicions and give it to CLASSES list:


Keras included in their library to predict the class label. You can get the class label directly by using model.predict_classes(img).
Ref: https://datascience.stackexchange.com/a/40415/109134


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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