Welcome to the world of image classification! In this guide, we will explore how to effectively use the MobileNet-v3 image classification model, trained on the ImageNet-1k dataset. We will walk through the process step by step, making it user-friendly along the way. So, let’s dive in!
Understanding the Basics
The MobileNet-v3 model is like a well-trained waiter in a restaurant, efficiently serving customers (images) by recognizing their orders (classifications) with quick accuracy. This model has about 5.5M parameters, representing its knowledge of various classifications, processed efficiently to provide a faster response (only 0.2 GMACs). As you prepare to deploy this model, remember, an effective waiter needs the right tools—similarly, we need the correct libraries and setup for the MobileNet-v3 model.
Setting Up Your Environment
- Ensure you have Python installed on your machine.
- Install necessary libraries:
pip install timm torch pillow
Image Classification
Now, let’s see how to classify an image using the MobileNet-v3 model:
python
from urllib.request import urlopen
from PIL import Image
import timm
import torch
# Load an image
img = Image.open(urlopen('https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'))
# Create the model
model = timm.create_model('tf_mobilenetv3_large_100.in1k', pretrained=True)
model = model.eval()
# Get model transforms
data_config = timm.data.resolve_model_data_config(model)
transforms = timm.data.create_transform(**data_config, is_training=False)
# Make predictions
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)
Feature Map Extraction
If you want to visualize the inner workings of the model—similar to watching the chef prepare a meal—you can extract feature maps:
python
# Load an image
img = Image.open(urlopen('https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'))
# Create the model with features_only set to True
model = timm.create_model('tf_mobilenetv3_large_100.in1k', pretrained=True, features_only=True)
model = model.eval()
# Get model transforms
data_config = timm.data.resolve_model_data_config(model)
transforms = timm.data.create_transform(**data_config, is_training=False)
# Extract feature maps
output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
for o in output:
print(o.shape)
Generating Image Embeddings
Lastly, let’s say you want a refined representation (“embeddings”) of an image, which serves as a compact description of the image’s features:
python
# Load an image
img = Image.open(urlopen('https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'))
# Create the model to retrieve embeddings
model = timm.create_model('tf_mobilenetv3_large_100.in1k', pretrained=True, num_classes=0)
model = model.eval()
# Get model transforms
data_config = timm.data.resolve_model_data_config(model)
transforms = timm.data.create_transform(**data_config, is_training=False)
# Get embeddings
output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor
Troubleshooting
If you encounter issues during installation or usage, consider the following:
- Ensure PyTorch is properly installed.
- Confirm that the image URL is accessible and correct.
- Check for proper library versions compatible with Python.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
Congratulations! You’ve now learned how to utilize the MobileNet-v3 model for image classification, extract feature maps, and generate embeddings. Just like a chef perfects their dish, your journey in mastering AI image classification is just beginning.
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.

