How to Use MobileNet V3 for Image Classification

May 1, 2023 | Educational

MobileNet V3 is a powerful image classification model trained on the ImageNet-1k dataset, designed specifically for speed and efficiency. In this article, we will explore how to use the MobileNet V3 model using the timm library. We will cover image classification, feature map extraction, and generating image embeddings, along with troubleshooting tips.

Understanding MobileNet V3

Think of MobileNet V3 as a highly skilled chef in a fast-paced kitchen. This chef has learned to prepare meals (classifications) efficiently while maintaining quality, minimizing wasted ingredients (resources), and adapting to different cooking styles (tasks). The recipe it follows has been carefully honed using the best techniques available, ensuring that it can deliver speedy and excellent results with limited resources.

Getting Started with Image Classification

To classify images using the MobileNet V3 model, follow these steps:

  • Load the necessary libraries.
  • Open the image you want to classify from a URL.
  • Create the MobileNet V3 model, specifying that it should use the pretrained weights.
  • Apply the model-specific transformations to the image.
  • Run the image through the model to get the classification output.

Sample Code for Image Classification

Here’s a step-by-step Python code example for classifying an image:

python
from urllib.request import urlopen
from PIL import Image
import timm
import torch

# Load image
img = Image.open(urlopen('https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'))

# Create model
model = timm.create_model('mobilenetv3_small_075.lamb_in1k', pretrained=True)
model = model.eval()

# Get model specific transforms
data_config = timm.data.resolve_model_data_config(model)
transforms = timm.data.create_transform(**data_config, is_training=False)

# Forward pass
output = model(transforms(img).unsqueeze(0))  # unsqueeze to create batch size of 1
top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5)

Extracting Feature Maps

Feature maps are essential for understanding the layers of information processed by the model. Get feature maps by using the following steps:

  • Load the image and model as before.
  • Set the model to extract features only.
  • Run the model to get the output, which includes the shapes of each feature map.

Sample Code for Feature Map Extraction

python
# Load image and model
model = timm.create_model('mobilenetv3_small_075.lamb_in1k', pretrained=True, features_only=True)
model = model.eval()

# Forward pass
output = model(transforms(img).unsqueeze(0))  # unsqueeze to create batch size of 1

# Print shape of each feature map
for o in output:
    print(o.shape)

Generating Image Embeddings

Image embeddings represent the features extracted from the image in a condensed format. Follow these steps:

  • Load the image and model, this time setting `num_classes=0` to skip the classifier.
  • Perform a forward pass to generate the embeddings.

Sample Code for Image Embeddings

python
# Load image and model for embeddings
model = timm.create_model('mobilenetv3_small_075.lamb_in1k', pretrained=True, num_classes=0)
model = model.eval()

# Forward pass to obtain embeddings
output = model.forward_features(transforms(img).unsqueeze(0))
output = model.forward_head(output, pre_logits=True)  # Output tensor shaped (1, num_features)

Troubleshooting Tips

While working with MobileNet V3, you might encounter some issues. Here are some common troubleshooting strategies:

  • Ensure that the image URL is accessible and the image is in a supported format (JPEG, PNG).
  • If you run into issues with library imports, verify that the timm library is installed correctly.
  • If the model fails to run, check if you are using a compatible version of Python and PyTorch.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

MobileNet V3 is an efficient choice for image classification tasks and can easily adapt to various applications. Whether you want to classify images, extract feature maps, or generate embeddings, understanding how to use this model will enhance your image processing capabilities.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox