How to decode predictions?

I trained my model on some set of labeled images, where I have ‘label_1’, ‘label_2’, ‘label_3’
Then, I’m trying to predict labels of unknown images with features = model.predict(images)
but here I only obtain feature maps, not labels themselves. How can I do label = decode_predictions(features) to get actual name of labels?

Hi @Ashley,

If you are using softmax activation function at the last classification dense layer, model.predict(images) is going to return the prediction scores for all classes/labels.

You can get the prediction scores by model.predict(image)[0], and then get the index of the maximum score by using np.argmax() or tf.math.argmax().

Here is a typical example from loading an image to getting its predicted label

image = tf.keras.preprocessing.image.load_img(image_path, target_size=(224,224))
image = tf.keras.preprocessing.image.img_to_array(image)
image = image/255.0
image = tf.expand_dims(image, 0) 

# predict the image

pred = model.predict(image)

# Get the label/class

im_class = tf.argmax(pred[0], axis=-1) #either tf.math.argmax() or tf.argmax will work

If you are using a specific pre-trained model from Keras directly, you can refer to some examples from the documentation on how to get the class using

Hope that helps!


yes it helps a lot thank you!!!

1 Like

You’re welcome, Ashley. I am glad it does.