How to Perform Image Classification Using EfficientNet with TIMM

Apr 29, 2023 | Educational

Image classification is a critical component in many modern applications of artificial intelligence, including computer vision tasks. In this blog, we will walk you through the steps to use the EfficientNet model, specifically the tf_efficientnet_b5.ns_jft_in1k, leveraging the TIMM library. Following this guide will help you unlock the power of EfficientNet for image classification, feature extraction, and generating image embeddings.

What is EfficientNet?

EfficientNet is an advanced image classification model known for its efficient scaling. Developed through a structured approach to model expanding, it provides remarkable accuracy while maintaining efficiency. This specific model has been trained on the ImageNet-1k dataset and incorporates semi-supervised learning techniques.

Getting Started: Installation and Setup

  • First, ensure you have Python installed on your system.
  • Install the TIMM library using pip by running the command:
    pip install timm
  • Furthermore, you will need the Pillow library for image handling:
    pip install Pillow

Image Classification Example

Let’s walk through the code that enables us to classify an image. Imagine you are a librarian tasked with organizing a vast collection of books. Instead of reading each book, you have a smart assistant (the model) that can classify each book based on its cover image based on what it has learned from other books (the training data).

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

# Load the image from a URL
img = Image.open(urlopen("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignet-task-guide.png"))

# Create the EfficientNet model
model = timm.create_model("tf_efficientnet_b5.ns_jft_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)

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

In the above code, we:

  • Load an image from a URL.
  • Create an EfficientNet model using TIMM.
  • Transform the image via model-specific transformations.
  • Pass the image through the model to get the top 5 classifications.

Feature Map Extraction

If you wish to extract feature maps (think of extracting bookmarks from books instead of whole books), here is how:

python
model = timm.create_model("tf_efficientnet_b5.ns_jft_in1k", pretrained=True, features_only=True)
model = model.eval()

# Extract features
output = model(transforms(img).unsqueeze(0))  # unsqueeze single image into a batch of 1
for o in output:
    print(o.shape)  # Print the shape of each feature map

Here, we configure the model to extract features instead of making classifications. The shapes printed represent the dimensions of each feature map, reflecting a condensed representation of the input image.

Generating Image Embeddings

Generating image embeddings is akin to summarizing the essence of a book in a few key phrases. Here’s how to obtain embeddings from the EfficientNet model:

python
model = timm.create_model("tf_efficientnet_b5.ns_jft_in1k", pretrained=True, num_classes=0)  # remove classifier
model = model.eval()

# Generate embeddings
output = model.forward_features(transforms(img).unsqueeze(0))  # Output is a feature tensor
output = model.forward_head(output, pre_logits=True)  # Final embeddings

This code will yield a tensor that represents the image in a lower-dimensional space, encapsulating its important characteristics.

Troubleshooting Tips

In case you run into issues while implementing the above steps, consider the following:

  • Ensure that all required packages are installed correctly.
  • Confirm the image URL is reachable and valid.
  • Check your network connection if you’re experiencing problems loading images.
  • To find more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Concluding Remarks

By following this guide, you should be able to utilize the EfficientNet model effectively for image classification. Whether you’re summarizing large datasets or extracting features, the process opens doors to numerous applications in AI and machine learning.

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