How to Use the GC-EfficientNet-v2 Image Classification Model

Apr 29, 2023 | Educational

The GC-EfficientNet-v2 image classification model is a sophisticated tool designed to classify images with remarkable efficiency and accuracy. Developed using the timm library and trained on the ImageNet-1k dataset, this model leverages advanced techniques like Global Context attention. In this guide, we’ll explore how to use this model for image classification, extract feature maps, and generate image embeddings.

Understanding the Ingredients

To understand how this model works, let’s compare it to a professional chef preparing a gourmet dish. The chef carefully selects their ingredients (features) which include:

  • Parameters (M): 13.7
  • GMACs: 1.9
  • Activations (M): 10.0
  • Image sizes: Train = 224 x 224, Test = 288 x 288

Just like a chef needs specific steps and tools, this model requires certain configurations detailed through the provided recipes, enabling it to deliver a perfectly crafted dish (or classification result).

Model Usage

Let’s dive into how to invoke this powerful model. We’ll break it down into three main tasks: Image Classification, Feature Map Extraction, and Image Embeddings.

1. Image Classification

Here’s how you can classify an image using the GC-EfficientNet-v2 model:

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("gc_efficientnetv2_rw_t.agc_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)

2. Feature Map Extraction

Extracting feature maps is straightforward and you can do it as follows:

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("gc_efficientnetv2_rw_t.agc_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)

3. Image Embeddings

Finally, for generating image embeddings, you can leverage the following 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("gc_efficientnetv2_rw_t.agc_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
# Or equivalently (without needing to set num_classes=0)
output = model.forward_features(transforms(img).unsqueeze(0))  # Output is unpooled, a (1, 1024, 7, 7) shaped tensor
output = model.forward_head(output, pre_logits=True)  # Output is a (1, num_features) shaped tensor

Model Comparison

For a deeper analysis and comparison of this model with others, you can explore the dataset and runtime metrics in the model results.

Troubleshooting Tips

Should you run into any obstacles while using the GC-EfficientNet-v2 model, here are some helpful tips:

  • Ensure you have the latest version of the timm library installed.
  • Double-check that the image URL is accessible and correctly formatted.
  • If there’s an issue with the model loading, confirm you’re using the exact model name gc_efficientnetv2_rw_t.agc_in1k.
  • For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

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