0
0
Computer-visionHow-ToBeginner · 3 min read

How to Use MobileNet for Image Classification in Computer Vision

Use MobileNet by loading a pre-trained model from a deep learning library like TensorFlow Keras, then input your images to get classification predictions. MobileNet is efficient for mobile and edge devices, providing fast and accurate image classification with minimal resources.
📐

Syntax

To use MobileNet for classification, you typically load the pre-trained model, preprocess your input images to the required size and format, then run the model to get predictions.

  • mobilenet.MobileNet(weights='imagenet'): Loads MobileNet with pre-trained ImageNet weights.
  • preprocess_input(): Prepares images to match MobileNet's expected input.
  • model.predict(): Runs the model on input data to get class probabilities.
  • decode_predictions(): Converts model output to human-readable class names.
python
from tensorflow.keras.applications import MobileNet
from tensorflow.keras.applications.mobilenet import preprocess_input, decode_predictions
from tensorflow.keras.preprocessing import image
import numpy as np

# Load MobileNet with pretrained ImageNet weights
model = MobileNet(weights='imagenet')

# Load and preprocess image
img = image.load_img('elephant.jpg', target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

# Predict classes
preds = model.predict(x)

# Decode predictions
print(decode_predictions(preds, top=3)[0])
Output
[('n02504458', 'African_elephant', 0.85), ('n01871265', 'tusker', 0.10), ('n02504013', 'Indian_elephant', 0.03)]
💻

Example

This example shows how to classify an image using MobileNet. It loads a sample image, preprocesses it, runs the model, and prints the top 3 predicted classes with probabilities.

python
from tensorflow.keras.applications import MobileNet
from tensorflow.keras.applications.mobilenet import preprocess_input, decode_predictions
from tensorflow.keras.preprocessing import image
import numpy as np

# Load MobileNet model with pretrained weights
model = MobileNet(weights='imagenet')

# Load an example image (replace 'elephant.jpg' with your image path)
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))

# Convert image to array and preprocess
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

# Predict the class probabilities
preds = model.predict(x)

# Decode and print top 3 predictions
results = decode_predictions(preds, top=3)[0]
for i, (imagenet_id, label, prob) in enumerate(results):
    print(f"{i+1}. {label}: {prob:.2%}")
Output
1. African_elephant: 85.00% 2. tusker: 10.00% 3. Indian_elephant: 3.00%
⚠️

Common Pitfalls

  • Not resizing images to 224x224 pixels, which MobileNet expects.
  • Failing to preprocess input images with preprocess_input(), causing wrong predictions.
  • Using the wrong input shape or data format (should be 4D tensor: batch, height, width, channels).
  • Forgetting to expand image dimensions to include batch size.
  • Not decoding predictions, resulting in numeric outputs instead of readable labels.
python
from tensorflow.keras.applications import MobileNet
from tensorflow.keras.applications.mobilenet import preprocess_input, decode_predictions
from tensorflow.keras.preprocessing import image
import numpy as np

model = MobileNet(weights='imagenet')

# WRONG: Not resizing image
# img = image.load_img('elephant.jpg')  # Missing target_size

# CORRECT: Resize image to 224x224
img = image.load_img('elephant.jpg', target_size=(224, 224))

x = image.img_to_array(img)

# WRONG: Not expanding dims
# preds = model.predict(x)  # x shape is (224,224,3), should be (1,224,224,3)

# CORRECT: Expand dims for batch
x = np.expand_dims(x, axis=0)

# WRONG: Not preprocessing input
# preds = model.predict(x)

# CORRECT: Preprocess input
x = preprocess_input(x)
preds = model.predict(x)

# WRONG: Not decoding predictions
# print(preds)

# CORRECT: Decode predictions
print(decode_predictions(preds, top=3)[0])
Output
[('n02504458', 'African_elephant', 0.85), ('n01871265', 'tusker', 0.10), ('n02504013', 'Indian_elephant', 0.03)]
📊

Quick Reference

Key points to remember when using MobileNet for classification:

  • Input images must be resized to 224x224 pixels.
  • Use preprocess_input() to prepare images.
  • Expand image dimensions to include batch size before prediction.
  • Use decode_predictions() to get readable class names.
  • MobileNet is optimized for speed and low resource use, ideal for mobile and edge devices.

Key Takeaways

Always resize input images to 224x224 pixels before feeding them to MobileNet.
Preprocess images with MobileNet's specific preprocess_input function for correct predictions.
Expand image dimensions to include batch size (shape should be (1, 224, 224, 3)).
Use decode_predictions to convert model outputs into human-readable class labels.
MobileNet is a lightweight model designed for efficient classification on limited hardware.