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.
This article was published as a part of the Data Science Blogathon.
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.
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.
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.
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.
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:
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
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.
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")
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
# 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.
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:
You can also check the model here: Link
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.
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:
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.
Ans. MobileNetV2 uses depthwise separable convolution and inverted residuals, making it more efficient for mobile and embedded systems compared to traditional CNNs.
Ans. MobileNetV2 is optimized for low-latency and real-time image classification tasks, making it suitable for mobile and edge devices.
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.
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.