...

Super-Quick Image Classification with MobileNetV2

Image Classification with MobileNetV2

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://ko-fi.com/s/f8e806c381

You can find more tutorials, and join my newsletter here : https://eranfeit.net/


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 :

1*ZkPLjwoOAXObzgmx z15vg
# ───────────────────────────────────────────── # 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

Enjoy,

Eran

error: Content is protected !!
Eran Feit