Introduction
This tutorial shows how to build a complete image classification pipeline in Python with a pretrained MobileNet model.
You will load the model, preprocess any input image to the required dimensions, run inference, decode the ImageNet labels, and render the predicted class directly on the image with OpenCV.
By the end, you will have a single, copy-paste script that takes an image path, returns the best prediction, and visualizes it.
The link for the video tutorial is here : https://youtu.be/xsBm_DTSbB0&list=UULFTiWJJhaH6BviSWKLJUM9sg
You can find the full code here : https://ko-fi.com/s/99e7970319
You can find more tutorial in my blog : https://eranfeit.net/blog/
Complete MobileNet Image Classification Script :
The following script loads a pretrained MobileNet (ImageNet), prepares an input image to the model’s expected size (224×224×3), runs model.predict
, decodes the top-5 classes with decode_predictions
, returns the top label, and overlays the prediction on the image using OpenCV.
### Import the MobileNet architecture from Keras Applications to use a pretrained CNN. from tensorflow.keras.applications import MobileNet ### Import Keras image utilities for array conversion and loading helpers. from tensorflow.keras.preprocessing import image ### Import the MobileNet specific preprocessing function to normalize inputs correctly. from tensorflow.keras.applications.mobilenet import preprocess_input ### Import Pillow to load and resize images safely and consistently. from PIL import Image , ImageFile ### Import NumPy for numerical arrays and tensor manipulation. import numpy as np ### Import OpenCV for rendering predicted text and showing the image window. import cv2 ### Instantiate a pretrained MobileNet with ImageNet weights and keep the classification head. model = MobileNet(weights="imagenet", include_top=True) ### Print a readable summary of the model layers and parameters for verification. print (model.summary() ) ### Note that the pretrained ImageNet head outputs probabilities across 1000 classes. # the model predicts 1000 classes ### Import the decoder utility to map logits to human readable ImageNet class names. from tensorflow.keras.applications.mobilenet import decode_predictions ### Define the input width expected by MobileNet. IMAGE_WIDTH = 224 ### Define the input height expected by MobileNet. IMAGE_HEIGHT = 224 ### Define the number of color channels for RGB images. IMAGE_CHANNELS = 3 ### Clarify that any source image must be transformed to 224x224x3 before inference. # any image should be converted to this dim : 224,224,3 ### Create a function that loads a file, preprocesses it, runs inference, and returns the top class text. # function for classify image ### Begin function definition that accepts a path to an image file. def classify_image (imageFile) : ### Prepare a container variable for intermediate data if needed. x= [] ### Open the image from disk using Pillow to ensure broad format support. img = Image.open(imageFile) ### Fully load image data to avoid deferred file operations later. img.load() ### Resize the image to the exact size MobileNet expects for inference. img = img.resize( (IMAGE_WIDTH,IMAGE_HEIGHT) , Image.ANTIALIAS) ### Convert the Pillow image to a NumPy array arranged as HxWxC for Keras. x = image.img_to_array(img) ### Add a batch dimension so the tensor shape becomes 1x224x224x3. x = np.expand_dims(x, axis=0) ### Normalize pixel values with MobileNet specific scaling for correct predictions. # process the image x = preprocess_input(x) ### Perform forward pass inference to obtain class probabilities. pred = model.predict(x) ### Report the index of the most probable class by argmax across the softmax vector. # we got 1000 results # lets print the highest score print(np.argmax(pred,axis=1)) ### The printed index helps quick debugging but the human label is decoded below. # so it is sea snake . ### Convert raw predictions into human readable tuples like (class_id, class_name, score). # get the best five results list = decode_predictions(pred , top=5) ### Iterate over the first example’s top five predictions to print label and confidence. for itm in list[0]: print(itm) ### Extract the top prediction tuple for return. result = list[0][0] ### Unpack the tuple to get the class text and probability for convenience. _ , classText , p = result ### Return the best class name as a simple string for downstream use. return classText ### Provide the path to the image you want to classify for a quick demo run. #imagePath = "transfer-learning/MobileNet/seaSnake.jpg" ### Optionally switch the test image by editing this line. imagePath = "transfer-learning/MobileNet/Hero-Volcano.jpg" ### Call the classification function and capture the best label text. resultText = classify_image(imagePath) ### Print the textual label so it is visible in the console logs as well. print(resultText) ### Load the same image via OpenCV for visualization purposes. # show the image img = cv2.imread(imagePath) ### Render the predicted label onto the image at a fixed position with a readable font. img = cv2.putText(img , resultText , (50,50), cv2.FONT_HERSHEY_SIMPLEX , 0.6 , (51,255,255), 1 ) ### Display the annotated image in a named window for quick inspection. cv2.imshow('img' , img) ### Keep the window open until any key is pressed to finish the demo. cv2.waitKey(0) ### Close the window and release resources gracefully. cv2.destroyAllWindows()
You can find the full code here : https://ko-fi.com/s/99e7970319
Deep Dive Into The MobileNet Classification Pipeline
This Keras MobileNet tutorial assembles a complete MobileNet image classification pipeline using a pretrained MobileNet Python setup in TensorFlow.
It is a practical Python computer vision walkthrough that covers image loading, resizing, normalization, inference, and result visualization end to end.
By loading weights="imagenet"
, the model is ready for TensorFlow image classification across the 1000 ImageNet categories without extra training.
Your MobileNet classify image script performs four core stages: image loading and resizing, normalization with preprocess_input
, model inference with model.predict
, and human-readable decoding via decode_predictions
.
Finally, it overlays the best class label on the original image using an OpenCV putText example to give immediate visual confirmation.
Why MobileNet For Quick Prototypes
MobileNet is a lightweight convolutional neural network optimized for speed and efficiency, ideal for laptops, edge devices, and CPU-only servers.
For tutorials, demos, or on-device inference, MobileNet reduces latency and memory usage while maintaining strong baseline accuracy for MobileNet image classification.
This trade-off is excellent when you need a fast baseline for TensorFlow image classification before moving to heavier architectures if necessary.
Loading The Pretrained Model
MobileNet(weights="imagenet", include_top=True)
initializes a pretrained MobileNet Python model with the final classification head attached.include_top=True
ensures the softmax layer with 1000 outputs is present, so you get class probabilities directly from model.predict
.
Because MobileNet expects a specific input shape, you standardize images to 224×224 with three channels to match the training setup for ImageNet top-5 predictions.
Image Loading, Resizing, And Data Types
Pillow image resize provides high-quality downscaling and broad format support for robust preprocessing.
The script uses LANCZOS (formerly Image.ANTIALIAS
) to achieve smoother results than nearest or bilinear filters for Python computer vision tasks.
After resizing to 224×224, you convert the image to a NumPy array using image.img_to_array
, producing a float32 array shaped (224, 224, 3)
.
You then expand the array to (1, 224, 224, 3)
with np.expand_dims
to create a batch dimension, as Keras models operate on batches in a typical Keras MobileNet tutorial workflow.
Normalization With preprocess_input
preprocess_input
applies the exact normalization MobileNet was trained with for TensorFlow image classification.
For MobileNet, this scaling maps pixel values from [0, 255]
into approximately [-1, 1]
, aligning the inputs with the model’s expectations.
Using the correct preprocessing is crucial because mismatched normalization can reduce accuracy even if the rest of the MobileNet classify image script is correct.
Model Inference And Argmax
model.predict(x)
runs a forward pass and returns a (1, 1000)
array of probabilities across ImageNet classes.np.argmax(pred, axis=1)
returns the index of the most probable class, which is useful for debugging and verifying behavior in a pretrained MobileNet Python pipeline.
The numeric index alone is not human-friendly, so you pair it with decoded labels for readability in Python computer vision projects.
Decoding Human-Readable Labels
decode_predictions(pred, top=5)
maps output indices to tuples like (class_id, class_name, score)
for ImageNet top-5 predictions.
Iterating through the top five provides transparency into the confidence distribution and supports quick diagnostics in a Keras MobileNet tutorial context.
Extract the top tuple and unpack the class text and probability for display and return, reflecting a clean MobileNet image classification UX.
Visualization With OpenCV
OpenCV reads the original image, overlays the predicted label with cv2.putText
, and opens a window to display the annotated result as an OpenCV putText example.
This immediate visual feedback is helpful for demos and for spotting issues like wrong image paths, unexpected colors, or preprocessing mismatches.
You can tune font scale, color, and thickness as needed, and the same approach applies when saving annotated images to disk for Python computer vision pipelines.
Connect :
☕ Buy me a coffee — https://ko-fi.com/eranfeit
🖥️ Email : feitgemel@gmail.com
🤝 Fiverr : https://www.fiverr.com/s/mB3Pbb
Enjoy,
Eran