Inception_v4
pre-trained models
deep learning
computer vision
neural networks

Using pre-trained Inception_v4 model

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Using a pre-trained Inception-v4 model is a transfer-learning problem, not a model-design problem. The goal is to reuse features learned on a large dataset such as ImageNet and adapt them to your own image task with less data and less training time.

What You Get from a Pre-Trained Model

Inception-v4 is a deep convolutional network built around multi-branch inception blocks and reduction blocks. You do not usually load it because you want to study every layer. You load it because the early and middle layers already know how to detect edges, textures, shapes, and higher-level image patterns.

That gives you two common strategies:

  • use the model as a frozen feature extractor
  • fine-tune some or all layers on a new dataset

Feature extraction is safer when you have little data. Fine-tuning can improve accuracy when your dataset is large enough and similar enough to the new target task.

Loading Inception-v4 in Practice

A practical way to use a pre-trained Inception-v4 model in PyTorch today is the timm library:

python
1import timm
2import torch
3
4model = timm.create_model("inception_v4", pretrained=True, num_classes=3)
5model.eval()
6
7x = torch.randn(1, 3, 299, 299)
8with torch.no_grad():
9    y = model(x)
10
11print(y.shape)

This example creates a model with ImageNet-pretrained weights and replaces the classifier head with three output classes.

The input size matters. Inception-v4 is commonly used with 299 x 299 images, so passing 224 x 224 images without checking preprocessing can silently hurt results.

Freezing Layers for Transfer Learning

For a small dataset, start by freezing most of the model:

python
1import timm
2import torch.nn as nn
3
4model = timm.create_model("inception_v4", pretrained=True)
5
6for param in model.parameters():
7    param.requires_grad = False
8
9model.last_linear = nn.Linear(model.num_features, 3)

Now only the final classifier learns. This reduces overfitting and cuts training cost.

A minimal training step looks like this:

python
1import torch
2
3criterion = torch.nn.CrossEntropyLoss()
4optimizer = torch.optim.Adam(model.last_linear.parameters(), lr=1e-3)
5
6images = torch.randn(8, 3, 299, 299)
7labels = torch.randint(0, 3, (8,))
8
9logits = model(images)
10loss = criterion(logits, labels)
11loss.backward()
12optimizer.step()

This is runnable, but in a real project you would replace the random tensors with a properly normalized dataset pipeline.

Preprocessing Matters as Much as the Model

A pre-trained network expects input prepared the same way it was trained. In timm, you can derive the recommended transform configuration from the model metadata and build the transform automatically.

If you skip normalization or use the wrong resize and crop strategy, transfer learning can underperform badly even when the architecture is correct.

That is why debugging a pre-trained model usually starts with:

  • image size
  • channel order
  • normalization values
  • train and eval mode

Fine-Tuning Safely

Once the classifier head is stable, unfreeze a few deeper layers and use a smaller learning rate. That lets the model adapt features without destroying the useful ImageNet initialization.

A common pattern is:

  • train the new head first
  • unfreeze the last block
  • continue training with a lower learning rate

This staged approach is usually more stable than unfreezing everything on day one.

Common Pitfalls

The most common mistake is using the wrong image size. Inception-v4 is not as forgiving as smaller backbones that are often trained on 224 x 224.

Another problem is forgetting model.eval() during inference. Batch normalization and dropout behave differently between training and inference modes.

Teams also replace the classifier but leave every layer trainable on a tiny dataset, which quickly overfits and wastes compute.

Finally, a pre-trained model is not magic. If your target images are far from ImageNet-style natural images, you may need stronger adaptation, more data, or a different backbone entirely.

Summary

  • A pre-trained Inception-v4 model is mainly useful for transfer learning.
  • Use timm to load the model and adjust the classifier head.
  • Keep the expected 299 x 299 input size and preprocessing pipeline consistent.
  • Freeze most layers first, then fine-tune gradually if needed.
  • Check preprocessing and train-versus-eval mode before blaming the architecture.

Course illustration
Course illustration

All Rights Reserved.