0
0
PytorchHow-ToBeginner · 4 min read

How to Use Pretrained Models in PyTorch: Simple Guide

To use a pretrained model in PyTorch, import the model from torchvision.models with pretrained=True. Then, set it to evaluation mode using model.eval() before running predictions on new data.
📐

Syntax

Here is the basic syntax to load a pretrained model in PyTorch:

  • import torchvision.models as models: Imports the models module.
  • model = models.resnet18(pretrained=True): Loads the ResNet18 model with pretrained weights.
  • model.eval(): Sets the model to evaluation mode to disable training behaviors like dropout.
python
import torchvision.models as models

# Load pretrained ResNet18 model
model = models.resnet18(pretrained=True)

# Set model to evaluation mode
model.eval()
💻

Example

This example loads a pretrained ResNet18 model, prepares a sample image, and runs a prediction to get the top class index.

python
import torch
from torchvision import models, transforms
from PIL import Image
import requests
from io import BytesIO

# Load pretrained model
model = models.resnet18(pretrained=True)
model.eval()

# Image preprocessing pipeline
preprocess = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                         std=[0.229, 0.224, 0.225])
])

# Load an example image from the web
url = 'https://upload.wikimedia.org/wikipedia/commons/2/26/YellowLabradorLooking_new.jpg'
response = requests.get(url)
img = Image.open(BytesIO(response.content)).convert('RGB')

# Preprocess the image
input_tensor = preprocess(img)
input_batch = input_tensor.unsqueeze(0)  # create a mini-batch as expected by the model

# Run inference
with torch.no_grad():
    output = model(input_batch)

# Get predicted class index
predicted_idx = torch.argmax(output[0]).item()
print(f'Predicted class index: {predicted_idx}')
Output
Predicted class index: 208
⚠️

Common Pitfalls

Common mistakes when using pretrained models include:

  • Not calling model.eval() before inference, which keeps dropout and batch normalization in training mode and leads to inconsistent results.
  • Feeding input images without proper preprocessing (resize, crop, normalize), causing poor predictions.
  • Forgetting to add a batch dimension to the input tensor, which the model expects.
python
import torchvision.models as models

# Wrong: missing eval mode
model = models.resnet18(pretrained=True)

# Wrong: input tensor missing batch dimension
# input_tensor = preprocess(img)  # shape [3, 224, 224]

# Right way:
model.eval()
input_batch = input_tensor.unsqueeze(0)  # add batch dimension

# Always preprocess images correctly before inference
📊

Quick Reference

Summary tips for using pretrained models in PyTorch:

  • Import model from torchvision.models with pretrained=True.
  • Always call model.eval() before inference.
  • Preprocess input images with resize, crop, tensor conversion, and normalization.
  • Add batch dimension to input tensors.
  • Use torch.no_grad() context to save memory during inference.

Key Takeaways

Load pretrained models with pretrained=True from torchvision.models.
Set model.eval() before running predictions to disable training behaviors.
Preprocess input images correctly with resize, crop, and normalization.
Add a batch dimension to input tensors before feeding to the model.
Use torch.no_grad() during inference to save memory and speed up.