# Keras/Tensorflow: model.predict() returns a list. How do I match the output with my class names?

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
import PIL
import numpy as np

img = img.resize((224, 224))
img = img_to_array(img)

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

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

• In your last dense layer, I show that dense layer output shape 4. But you mention that you have 2 classes. – AIFahim Feb 20 at 17:02
• Oops, I was doing 2 classes earlier, then 4. The 2 just stuck to my mind. Sorry for that, I'll edit it. – Amay Agarwal Feb 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:

print(CLASSES[np.argmax(predictions)])


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