Activity 7 - Plotting an Image's predicted Category
Copy and Paste the following code into Google Colab:
# Grab an image from the test dataset. This shows the resolution of the image. #NOTE: This index will be changed and its corresponding plot would be displayed in the next few steps img = test_images print(img.shape)
# Add the image to a batch where it's the only member. img = (np.expand_dims(img,0)) print(img.shape)
predictions_single = probability_model.predict(img) print(predictions_single)
plot_value_array(0, predictions_single, test_labels) #plot the graph containing all the class names _ = plt.xticks(range(10), class_names, rotation=45)
To verify the index value with the highest probability, we use
np.argmax(predictions_single) #Verifying the index value with highest probability
In the first code segment of this activity, change the index value for the
test_images array to any number of your choice.
plot_value_array, change the first parameter to the same index number used previously. What class name has the highest probability?
Verify your answer by running the code below to display the specified image and the plot of the category that the model predicted.
Is this consistent with the answer from your previous question?
i = your_desired_value #We can see that this image detects the right class name for the image plt.figure(figsize=(6,3)) plt.subplot(1,2,1) plot_image(i, predictions[i], test_labels, test_images) plt.subplot(1,2,2) plot_value_array(i, predictions[i], test_labels) plt.show()