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 torchvisionBasic knowledge of PyTorch tensors and models
Setup
Install torch and torchvision libraries to access ResNet models and utilities.
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.
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,resnet101by replacingresnet18. - 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')anddummy_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 withtorch.no_grad().
Key Takeaways
- Use
torchvision.modelsto 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.