0
$\begingroup$

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

print(prediction('/path/to/image/')

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.

Thanks

$\endgroup$
2
  • $\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 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 at 6:27
0
$\begingroup$

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:

print(CLASSES[np.argmax(predictions)])
$\endgroup$
0
0
$\begingroup$

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

$\endgroup$
0

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.