Last Updated on 22/04/2026 by Eran Feit
How to classify images using MobileNet V2 ? Want to turn any JPG into a set of top-5 predictions in under 5 minutes ?
In this hands-on tutorial I’ll walk you line-by-line through loading MobileNetV2, preparing an image with OpenCV, and decoding the results—all in pure Python.
Perfect for beginners who need a lightweight model or anyone looking to add instant AI super-powers to an app.
What You’ll Learn 🔍:
Loading MobileNetV2 pretrained on ImageNet (1000 classes) Reading images with OpenCV and converting BGR → RGB Resizing to 224×224 & batching with np.expand_dims Using preprocess_input (scales pixels to -1…1 ) Running inference on CPU/GPU (model.predict) Grabbing the single highest class with np.argmax Getting human-readable labels & probabilities via decode_predictions Check out our tutorial here : https://youtu.be/Nhe7WrkXnpM
You can find the code here : https://eranfeit.lemonsqueezy.com/buy/dffc5a8b-3e2a-4893-98a3-d8bb45d8ceeb or here : https://ko-fi.com/s/f8e806c381
You can find more tutorials, and join my newsletter here : https://eranfeit.net/
Master Computer Vision
Follow my latest tutorials and AI insights on my
Personal Blog .
Beginner Complete CV Bootcamp
Foundation using PyTorch & TensorFlow.
Get Started → Interactive Deep Learning with PyTorch
Hands-on practice in an interactive environment.
Start Learning → Advanced Modern CV: GPT & OpenCV4
Vision GPT and production-ready models.
Go Advanced → Image Classification code Part 1 : Install Lists the exact package and Python versions the viewer should install before running the script. Nothing is executed here.
# ───────────────────────────────────────────── # PART 1 – Environment requirements (read-only) # ───────────────────────────────────────────── # pip install tensorflow==2.10 # deep-learning framework # pip install numpy # numerical arrays # pip install opencv-python # image I/O + basic CV # Python version : 3.9.16 You can find the code here : https://ko-fi.com/s/f8e806c381
Part 2 : Import TensorFlow utilities & load the model Loads MobileNetV2 already trained on ImageNet and prints its layer structure so viewers know exactly what network they’re about to use.
# ───────────────────────────────────────────── # PART 2 – Import TensorFlow utilities & load the model # ───────────────────────────────────────────── from tensorflow . keras . applications . mobilenet_v2 import ( MobileNetV2 , # pretrained network architecture preprocess_input # matching preprocessing helper ) model = MobileNetV2 ( weights = " imagenet " ) # load ImageNet weights (1 000 classes) print ( model . summary ()) # optional: dump model architecture You can find the code here : https://ko-fi.com/s/f8e806c381
PART 3 — Read an image and resize it Loads a JPEG from disk, converts the color channels from OpenCV’s default BGR to RGB, and resizes it to 224 × 224 px — exactly the input size MobileNetV2 was trained on.
Here is the “Dori” image :
Super-Quick Image Classification with MobileNetV2 8 # ───────────────────────────────────────────── # PART 3 – Read an image and resize it # ───────────────────────────────────────────── import numpy as np # array math import cv2 # OpenCV for image handling img = cv2 . imread ( " Best-image-classification-models/Classify-images-MobileNet-V2/Dori.jpg " ) # read file as BGR print ( " Original shape : " , img . shape ) img = cv2 . cvtColor ( img , cv2 . COLOR_BGR2RGB ) # convert BGR → RGB img = cv2 . resize ( img , ( 224 , 224 )) # MobileNetV2 expects 224×224 print ( " Resized shape : " , img . shape ) You can find the code here : https://ko-fi.com/s/f8e806c381
PART 4 — Create a batch and preprocess the pixels Wraps the single RGB image in a 4-D batch tensor and scales every pixel to the range −1…1 using preprocess_input, matching the distribution expected by the pretrained network.
# ───────────────────────────────────────────── # PART 4 – Create a batch and preprocess the pixels # ───────────────────────────────────────────── data = np . empty (( 1 , 224 , 224 , 3 ), dtype = np . float32 ) # allocate batch of size 1 data [ 0 ] = img # insert the image print ( " Batch shape : " , data . shape ) # should be (1, 224, 224, 3) data = preprocess_input ( data ) # scale pixel values from [0,255] → [-1,1] You can find the code here : https://ko-fi.com/s/f8e806c381
PART 5 — Run inference and inspect raw scores Feeds the batch through MobileNetV2, prints the entire softmax vector (1 000 probabilities), finds the single best class index, and shows how to access any specific class score by ID.
# ───────────────────────────────────────────── # PART 5 – Run inference and inspect raw scores # ───────────────────────────────────────────── predictions = model . predict ( data ) # returns 1 × 1000 soft-max scores print ( predictions ) # long vector of class probabilities top_index = np . argmax ( predictions , axis = 1 ) # index of highest probability print ( " Top class index : " , top_index ) print ( " Score for class 155 : " , predictions [ 0 ][ 155 ]) # example: raw score lookup You can find the code here : https://ko-fi.com/s/f8e806c381
PART 6 — Decode the top-5 predictions Converts the numeric class indices into human-readable ImageNet labels and prints the top five guesses with their probabilities, giving an easily understandable result to the end user.
# ───────────────────────────────────────────── # PART 6 – Decode the top-5 predictions # ───────────────────────────────────────────── from tensorflow . keras . applications . mobilenet_v2 import decode_predictions for _ , label , score in decode_predictions ( predictions , top = 5 )[ 0 ]: print ( f " { label } : { score :.2% } " ) You can find the code here : https://ko-fi.com/s/f8e806c381
Connect : ☕ Buy me a coffee — https://ko-fi.com/eranfeit
🖥️ Email : feitgemel@gmail.com
🌐 https://eranfeit.net
🤝 Fiverr : https://www.fiverr.com/s/mB3Pbb
Planning a trip and want ideas you can copy fast? Here are three detailed guides from our travels:
• 5-Day Ireland Itinerary: Cliffs, Castles, Pubs & Wild Atlantic Viewshttps://eranfeit.net/unforgettable-trip-to-ireland-full-itinerary/
• My Kraków Travel Guide: Best Places to Eat, Stay & Explorehttps://eranfeit.net/my-krakow-travel-guide-best-places-to-eat-stay-explore/
• Northern Greece: Athens, Meteora, Tzoumerka, Ioannina & Nafpaktos (7 Days)https://eranfeit.net/my-amazing-trip-to-greece/
Each guide includes maps, practical tips, and family-friendly stops—so you can plan in minutes, not hours.
Enjoy,
Eran