How to Use EfficientNet for Image Classification

Apr 28, 2023 | Educational

EfficientNet has become a leading model for image classification tasks, and with the pruning technique applied in the efficientnet_b1_pruned.in1k model, its performance is enhanced while keeping the resource usage low. In this article, we will explore how to use this model effectively for various image classification tasks.

Model Overview

Before diving into the usage, let’s look at the key stats and details of the model:

  • Model Type: Image classification feature backbone
  • Model Stats:
    • Params (M): 6.3
    • GMACs: 0.4
    • Activations (M): 6.2
    • Image size: 240 x 240
  • Papers:
  • Dataset: ImageNet-1k

Using the Model for Image Classification

Here’s how you can utilize the efficientnet_b1_pruned model for image classification:


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

# 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('efficientnet_b1_pruned.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)

# Perform classification
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)

Understanding the Code with an Analogy

Think of this code as a recipe for baking a cake. Each line of code is a step in your recipe that builds upon the previous one:

  • Gathering Ingredients: The first part imports necessary libraries (like importing flour, sugar, and eggs). This is where we prepare what we need.
  • Preparing Your Cake: Loading the image is like preheating the oven and preparing your cake mold (in this case, the model).
  • Merging Ingredients: Creating the model corresponds to mixing your ingredients thoroughly – here, we’re using the EfficientNet as our mixture.
  • Shaping: Applying transforms to the image is akin to pouring your batter into the mold to take the desired shape.
  • Baking: Running the model on the image is like placing the cake in the oven – you’re now waiting for it to bake and take form.
  • Tasting: Finally, top5 probabilities give you the most likely ingredients (or classes) your cake could represent!

Feature Map Extraction

If you’re interested in extracting feature maps from the EfficientNet model, here’s how you can do it:


model = timm.create_model(
    'efficientnet_b1_pruned.in1k',
    pretrained=True,
    features_only=True,
)
model = model.eval()

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

Obtaining Image Embeddings

To extract image embeddings, you can modify the model slightly as follows:


model = timm.create_model(
    'efficientnet_b1_pruned.in1k',
    pretrained=True,
    num_classes=0,  # Remove classifier nn.Linear
)
model = model.eval()

output = model(transforms(img).unsqueeze(0))  # Output is shaped tensor

Troubleshooting

If you encounter any issues while implementing the model, consider the following troubleshooting tips:

  • Check your internet connection for resolving images; it could fail to load if connectivity is poor.
  • Ensure that you have all necessary libraries installed and updated, particularly torch and timm.
  • If the model does not perform as expected, verify that the input image is in the correct format and size (240 x 240 pixels).
  • For common issues, exploring the model’s GitHub repository may provide answers.

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

Comparison and Further Exploration

To further explore the dataset and runtime metrics of this model, you can visit the model results.

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