Exploring the Efficiency of Image Classification With MobileNetV2

Maigari David Last Updated : 19 Feb, 2025
6 min read

MobileNet is an open-source model created to support the emergence of smartphones. It uses a CNN architecture to perform computer vision tasks such as image classification and object detection. Models using this architecture usually require a lot of computational cost and hardware resources, but MobileNet was made to work with mobile devices and embedding. 

Over the years, this model has been used for various real-world applications. It also has some capabilities, like reducing parameters using depthwise separation convolution. So, with the limited hardware resources of mobile devices, this technique can help make the model functional. 

We will discuss how this model efficiently classifies images using pre-trained predicted class to Classifier images with depth. 

Learning Objectives

  • Learn about MobileNet and its working principle. 
  • Gain insight into the architecture of MobileNet. 
  • Run inference on MobileNet to perform image classification. 
  • Explore the Real-life applications of MobileNet.

This article was published as a part of the Data Science Blogathon.

Working Principle of MobileNet 

The working principle of MobileNet is one of the most important parts of this model’s structure. It outlines the techniques and methods employed to build this model and make it adaptable to mobile and embedded devices. This model’s design leverages Convolution Neural Network (CNN) architecture to allow it to operate on mobile devices. 

However, a crucial part of this architecture is the use of depthwise separable convolution to reduce the number of parameters. This method uses two operations: Depthwise and Pointwise convolution. 

Standard Covulation 

A standard convolution process starts with the filter (kernel); this step is where image features such as edges, textures, or patterns are detected in images. This is followed by sliding the filter across the width and height of the image. And each step involves an element-wise multiplication and summation. The sum of this gives a single number that results in the formation of a feature map. It represents the presence and strength of the features detected by the filter. 

However, this comes with a high computational cost and increased parameter count, hence the need for depthwise and point-wise convolution.

Depthwise separable convolution block
Source: viso.ai

How Does the Depthwise and Pointwise Convolution Work?

The depthwise convolution applies a single filter to the input channel, while the pointwise combines the output from depthwise convolution to create new features from the image. 

So, the difference here is that with depthwise applying just a single filter, the multiplication task is reduced, which means the output has the same number of channels as the input. This leads to the pointwise convolution. 

Tbe pointwise convolution uses a 1×1 filter that combines or expands features. This helps the model to learn different patterns dispensing on the channel features to create a new feature map. This enables pointwise convolution to increase or decrease the number of channels in the output feature map.

MobileNet Architecure  

This computer vision model is built on CNN architecture to perform image classification and object detection tasks. The use of Depthwise separable convolution is to adapt this model to mobile and embedded devices, as it permits the building of lightweight deep neural networks. 

This mechanism brings in the reduction of parameter counts and latency to meet the resource constraints. The architecture enables efficiency and accuracy in the output of the model. 

The second version of this model (MobileNetV2) was built with an enhancement. MobileNet v2 introduced a special type of building block called inverted residuals with bottlenecks. These blocks help reduce the number of processed channels, making the model more efficient. It also includes shortcuts between bottleneck layers to improve the flow of information. Instead of using a standard activation function (ReLU) in the last layer, it uses a linear activation, which works better because the data has a lower spatial size at that stage.

How to Run this Model?

Using this model for image classification requires a few steps. The model receives and classifies an input image using its inbuilt prediction class. Let’s dive into the steps on how to run MobileNet:

Importing Necessary Libraries For Image Classification

You need to import some essential modules to run this model. This starts with importing the image processor and image classification module from the transformer library. They help to preprocess images and load a pre-trained model, respectively. Also, PIL is used to manipulate images, while ‘requests’ allows you to fetch images from the web.

from transformers import AutoImageProcessor, AutoModelForImageClassification
from PIL import Image
import requests

Loading Input Image

 image = Image.open('/content/imagef-ishfromunsplash-ezgif.com-webp-to-jpg-converter.jpg')

The function ‘Image.open’ is used from the PIL library to load the image from a file path, which, in this case, was uploaded from our local device. Another alternative would be to fetch the image using its URL. 

"
Source: Unsplash

Loading the Pre-trained Model For Image Classification

The code below initializes the process ‘AutoImageProcessor’ from the mobilenetv2 pre-trained model. This part handles the image pre-processing before feeding it to the model.  Also, as seen in the second line, the code loads the corresponding MobileNetV2 model for image classification.

preprocessor = AutoImageProcessor.from_pretrained("google/mobilenet_v2_1.0_224")
model = AutoModelForImageClassification.from_pretrained("google/mobilenet_v2_1.0_224")

Input Processing

This step is where the preprocessed image is converted into a format suitable for PyTorch. Then, it is passed through the model to generate output logits, which will be converted into probabilities using softmax.

inputs = preprocessor(images=image, return_tensors="pt")
 
outputs = model(**inputs)
logits = outputs.logits

Output

 # model predicts one of the 1000 ImageNet classes
predicted_class_idx = logits.argmax(-1).item()
print("Predicted class:", model.config.id2label[predicted_class_idx])

This code finds the class with the highest prediction score from the model’s output (logits) and retrieves its corresponding label from the model’s configuration. The predicted class label is then printed.

Here is the output:

"

Here is a link to the colab file.

Application of this Model

MobileNet has found applications in various real-life use cases. And it has been used across various fields including healthcare. Here are some of the applications of this model: 

  • During the COVID pandemic, MobileNet was used to categorise chest X-ray into three: Normal, COVID, and viral pneumonia. The result also came with a very high accuracy. 
  • MobileNetV2 was also efficient in detecting two major forms of skin cancer. This innovation was significant as healthcare in areas that could not afford stable internet connectivity leaverged this model. 
  • In Agriculture, this model was also crucial in detecting leaf disease in tomato crops. So, with a mobile application, this model can help detect 10 common leaf diseases. 

You can also check the model here: Link

Wrapping Up

MobileNet is the result of a masterclass by Google researchers in bringing models with high computational costs down to mobile devices without interfering with their efficiency. This model was built on an architecture that allows the creation of image classification and detection just from mobile applications. The use cases in healthcare and Agriculture are evidence of the capacities of this model. 

Key Takeaway

There are a few talking points about how this model works, from the architecture to applications. Here are some of the highlights of this article: 

  • Enhanced Architecture: The second version of MobileNet came with inverted residuals and bottleneck layers in MobileNetV2. This development improved efficiency and accuracy while maintaining lightweight performance.
  • Efficient Mobile Optimization: This model’s design for mobile and embedded design means that it serves low computational resources while offering effective performance. 
  • Real-World Applications: MobileNet has been successfully used in healthcare (e.g., COVID-19 and skin cancer detection) and agriculture (e.g., detecting leaf diseases in crops).

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Frequently Asked Questions

Q1. What makes MobileNetV2 different from other CNN models?

Ans. MobileNetV2 uses depthwise separable convolution and inverted residuals, making it more efficient for mobile and embedded systems compared to traditional CNNs.

Q2. Can MobileNetV2 be used for real-time applications? 

Ans. MobileNetV2 is optimized for low-latency and real-time image classification tasks, making it suitable for mobile and edge devices.

Q3. How accurate is MobileNetV2 compared to larger models? 

Ans. While MobileNetV2 is optimized for efficiency, it maintains a high accuracy close to larger models, making it a strong choice for mobile AI applications.

Q4. How accurate is MobileNetV2 compared to larger models?

Ans. While MobileNetV2 is optimized for efficiency, it maintains a high accuracy close to larger models, making it a strong choice for mobile AI applications.

Hey there! I'm David Maigari a dynamic professional with a passion for technical writing writing, Web Development, and the AI world. David is an also enthusiast of data science and AI innovations.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details