A Guide to Using the MobileNet-v3 Image Classification Model

May 1, 2023 | Educational

In this article, we will explore how to effectively use the MobileNet-v3 image classification model, specifically the tf_mobilenetv3_small_100.in1k variant, trained on the ImageNet-1k dataset. This guide covers the steps for image classification, feature extraction, and obtaining image embeddings, along with troubleshooting tips.

Understanding the Model

The MobileNet-v3 model is designed to perform image classification with efficiency in mind. To explain this, let’s use an analogy: imagine you have a highly skilled chef who is renowned for creating delicious dishes with a limited number of ingredients. The chef can transform simple ingredients into gourmet meals, just as the MobileNet-v3 model can classify complex images using a small number of parameters.

Model Details

  • Model Type: Image classification feature backbone
  • Parameters (M): 2.5
  • GMACs: 0.1
  • Activations (M): 1.4
  • Image Size: 224 x 224

For further reading:

How to Use the Model

1. Image Classification

To classify an image using the MobileNet-v3 model, you can follow these steps:

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

img = Image.open(urlopen("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png"))
model = timm.create_model("tf_mobilenetv3_small_100.in1k", pretrained=True)
model = model.eval()

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

output = model(transforms(img).unsqueeze(0))  # unsqueeze single image into batch of 1
top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5)

This block of code fetches an image, initializes the MobileNet-v3 model, and classifies the image, providing the top 5 class probabilities and indices.

2. Feature Map Extraction

If you want to extract feature maps from the model, use the following:

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

img = Image.open(urlopen("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png"))
model = timm.create_model("tf_mobilenetv3_small_100.in1k", pretrained=True, features_only=True)
model = model.eval()

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

output = model(transforms(img).unsqueeze(0))  # unsqueeze single image into batch of 1
for o in output:
    print(o.shape)

This code will print the shape of each feature map, allowing you to observe the depth and dimensions of the features extracted at various layers.

3. Image Embeddings

To obtain embeddings from the images, follow this code:

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

img = Image.open(urlopen("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png"))
model = timm.create_model("tf_mobilenetv3_small_100.in1k", pretrained=True, num_classes=0)  # remove classifier nn.Linear
model = model.eval()

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

output = model(transforms(img).unsqueeze(0))  # output is (batch_size, num_features) shaped tensor

This snippet removes the classifier and yields the raw features from the model, which can be useful for further analyses.

Model Comparison

Explore the dataset and runtime metrics of this model on the timm model results repository.

Troubleshooting Tips

If you encounter issues while working with the MobileNet-v3 model, consider the following troubleshooting ideas:

  • Ensure you have all the necessary dependencies installed, including PyTorch and timm.
  • Double-check the model name and URL of the image you are trying to use for classification.
  • Verify that your input images are in the correct format (RGB) and size (224 x 224).
  • Check for compatibility issues if using multiple libraries; consider updating them.

If you still face problems, feel free to reach out for help or insights. For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

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