Search code examples
pythontensorflowmachine-learningimage-processingkeras

Keras: printing out the predicted class label


I'm testing a CNN model based on the cifar10 keras dataset. So, basically

prediction = mymodel.predict(x)
print(prediction)

prints out the prediction itself:

[[3.3675440e-04 5.7650192e-07 2.5850117e-02 5.4446888e-01 7.0444457e-02
  1.7459875e-01 3.5874096e-03 1.8062484e-01 1.2066155e-06 8.6996079e-05]]

What's the correct syntax, given the cifar10 class labels

classes = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

to print the predicted label instead?


Solution

  • In your case it should be something like that:

    prediction = mymodel.predict(x)
    # return position of max:
    max_position = np.argmax(prediction)  
    prediction_label = classes[max_position]
    print(prediction_label)