How to beginner · 3 min read

How to use ResNet in PyTorch

Quick answer
Use torchvision.models.resnet to load pretrained ResNet models in PyTorch. Instantiate the model, optionally modify the final layer for your task, and run inference or training with standard PyTorch workflows.

PREREQUISITES

  • Python 3.8+
  • pip install torch torchvision
  • Basic knowledge of PyTorch tensors and models

Setup

Install torch and torchvision libraries to access ResNet models and utilities.

bash
pip install torch torchvision

Step by step

This example loads a pretrained ResNet-18 model, modifies the final fully connected layer for 10 classes, and runs a dummy input through it.

python
import torch
import torchvision.models as models

# Load pretrained ResNet-18
model = models.resnet18(pretrained=True)

# Modify the final layer for 10 classes (e.g., CIFAR-10)
num_features = model.fc.in_features
model.fc = torch.nn.Linear(num_features, 10)

# Set model to evaluation mode
model.eval()

# Create a dummy input tensor (batch_size=1, 3 color channels, 224x224 image)
dummy_input = torch.randn(1, 3, 224, 224)

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

print("Output shape:", output.shape)  # Should be [1, 10]
output
Output shape: torch.Size([1, 10])

Common variations

  • Use other ResNet variants like resnet34, resnet50, resnet101 by replacing resnet18.
  • Load models without pretrained weights by setting pretrained=False.
  • Fine-tune the model by training on your dataset after modifying the final layer.
  • Use GPU by moving model and inputs to CUDA device with model.to('cuda') and dummy_input.to('cuda').

Troubleshooting

  • If you get a shape mismatch error, ensure the final layer matches your dataset's number of classes.
  • If CUDA is not available, remove or modify to('cuda') calls.
  • For slow inference, switch model to eval() mode and disable gradients with torch.no_grad().

Key Takeaways

  • Use torchvision.models to easily load pretrained ResNet models.
  • Modify the final fully connected layer to match your classification task.
  • Always set the model to eval() mode for inference to disable dropout and batchnorm updates.
  • Use torch.no_grad() context to save memory and computation during inference.
  • Move model and data to GPU with to('cuda') for faster training and inference if available.
Verified 2026-04 · resnet18, resnet34, resnet50, resnet101
Verify ↗