Stage 9: Use It on New Images
Keep your Colab notebook tab open all session. Open in a new tab — don’t use the buttons in this page to leave the course.
an inference tool that preprocesses uploaded images and prints top guesses
why prediction uses the same preparation rules as training
a tested upload-and-predict workflow with confidence interpretation
Upload one valid CIFAR-10 class image and one off-menu image, like pizza. Show that the model always chooses from its 10 known classes.
The big idea
Using a trained model on a new image is called inference. The model does not learn during inference. It only guesses.
The new image must be translated into the same format as training data: 32 by 32 pixels, RGB, values from 0 to 1, and a batch dimension.
- 1Photos / CIFAR-10labeled image examplesStage 1
- 2Notebook variablesx_train, y_train, class_namesSetup-2
- 3Prepared datanormalized pixels and fair pilesStage 3
- 4Keras modelCNN layers and summaryStage 4
- 5Training historyepochs, loss, accuracyStage 5
- 6Test evidencesealed score and mistakesStages 6-7
- 7Improved modelaugmentation comparisonStage 8
- 8Inferenceuploaded image to top-3 guessesStage 9
- 9Demo evidencetable, confidence, limitationStage 10
Stage 9 uses the trained model without changing it. Uploaded images must pass through the same preparation rules before the model can make top-3 guesses.
- inference
- using a trained model to make a prediction
- preprocess
- prepare new data in the same format as training data
- confidence
- the model's score for a predicted class
- top-3
- the three most likely classes according to the model
Use augmented_model from Stage 8 if it performed at least as well as baseline. Otherwise use model. Set final_model in Step 1.
Build it
Step 1 — Choose the model to demo
final_model = augmented_model
If your augmented model performed worse and you want to demo the baseline, use:
# final_model = model
Step 2 — Write the preprocessing function
The image helper loads a file and converts it to an array. np.expand_dims adds the batch dimension, turning one image into a batch of one image. The model was trained on batches, so even a single uploaded image must keep that shape.
def load_and_preprocess_image(image_path):
from tensorflow.keras.preprocessing import image
import numpy as np
img = image.load_img(image_path, target_size=(32, 32))
img_array = image.img_to_array(img)
img_array = img_array / 255.0
img_array = np.expand_dims(img_array, axis=0)
return img_array
This matches the training format exactly.
Step 3 — Predict top guesses
Upload an image in Colab, copy its path, and paste it below.
new_image_path = 'my_image.jpg'
new_image = load_and_preprocess_image(new_image_path)
predictions = final_model.predict(new_image)[0]
top_indexes = predictions.argsort()[-3:][::-1]
plt.imshow(plt.imread(new_image_path))
plt.axis('off')
plt.show()
for index in top_indexes:
print(f"{class_names[index]}: {predictions[index]:.0%}")
The top class is the model's answer. The next two show what it almost chose.
Step 4 — Test an off-menu image
Upload something not in CIFAR-10: pizza, phone, person, or shoe. Run the same cell.
The model still picks one of its 10 classes because it has no "none of these" output.
Step 5 — Write a prediction note
Prediction note:
- My in-class image was __________.
- Top guess: __________ at _____%.
- Off-menu image: __________.
- The model called it __________ because it only knows 10 classes.
Understand it
Preprocessing is not optional. A model trained on 32x32 normalized images expects every new image to look like that numerically.
Confidence is not the same as truth. A wrong model can be confident, especially when forced to choose from only 10 classes. Top-3 results give a more honest view of uncertainty.
Try this
Try this
Three short experiments. Predict before you run, then test your guess.
Predict whether a clear image or a blurry image will get higher confidence. Test both.
Compare top-3 guesses for an in-class image and an off-menu image. Which result looks more uncertain?
Which Stage 7 mistakes explain the new-image mistakes you saw today?
Test your stage
- You predicted top-3 classes for one in-class image.
- You tested one off-menu image.
- You wrote a prediction note.
- Workflow check. Point to this stage on the workflow map and explain why uploaded images need the same preparation rules.
- Evidence check. Your prediction note includes one top-3 result and one confidence value.
- Design check. Explain why the model cannot say "none of these."
If it breaks
FileNotFoundError. Re-copy the exact path from Colab Files.final_modelis undefined. Run Step 1.- The image is huge on screen. Add
plt.figure(figsize=(3, 3))beforeimshow.
The off-menu image is essential. It prevents students from thinking AI knows when it is outside its training world.