How to Utilize the Vision Transformer (ViT) for Image Classification

Feb 13, 2024 | Educational

If you are venturing into the realm of image classification and you’ve come across the Vision Transformer (ViT), specifically the vit_large_patch16_224.orig_in21k model, then you are in for a treat! This powerhouse has been pretrained on the ImageNet-21k dataset, and it’s perfect for feature extraction and fine-tuning tasks. In this guide, we will walk through how to implement it step-by-step.

Model Overview

This model leverages the power of transformers for image classification:

  • Model Type: Image classification feature backbone
  • Params: 303.3 million
  • GMACs: 59.7
  • Activations: 43.8 million
  • Image Size: 224 x 224

For a deeper understanding, you might want to check the original paper titled “An Image is Worth 16×16 Words: Transformers for Image Recognition at Scale”.

How to Implement Image Classification

Let’s break down how to use the ViT model for image classification. Imagine you’re hosting a new art gallery opening, and you need to categorize artworks into various sections. Similarly, we will categorize images using our model.

If you want to classify images, here’s how you can do it:

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("vit_large_patch16_224.orig_in21k", 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)

How to Obtain Image Embeddings

In the world of art, you may want to capture the essence of each artwork without categorizing it directly. This is where embeddings come into play—providing a unique representation.

To achieve this, here are the 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("vit_large_patch16_224.orig_in21k", pretrained=True, num_classes=0)  # Remove classifier
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)
# Alternatively (without needing to set num_classes=0)
output = model.forward_features(transforms(img).unsqueeze(0))  # Unpooled tensor
output = model.forward_head(output, pre_logits=True)  # Final shaped tensor

Troubleshooting

As you embark on your image classification journey, you may encounter some bumps along the way. Here are a few troubleshooting tips:

  • Ensure all required libraries are installed (PIL, timm, etc.).
  • Verify the image URL is correct; an invalid URL will hinder the image loading.
  • If encountering issues with model inference, check that the input dimensions match the model’s expected input size.

If you are still facing challenges, don’t hesitate to reach out. For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

With this roadmap, you’re now equipped to wield the power of the Vision Transformer for image classification and embedding extraction. 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