0
0
PytorchHow-ToBeginner · 4 min read

How to Use ResNet in PyTorch: Simple Guide with Example

To use ResNet in PyTorch, import it from torchvision.models, load a pretrained model or initialize a new one, then pass your input tensor through it to get predictions. You can fine-tune the model by replacing the final layer and training on your data.
📐

Syntax

Use torchvision.models.resnet18() or other ResNet variants like resnet34(), resnet50(). Set pretrained=True to load weights trained on ImageNet. Pass an input tensor of shape (batch_size, 3, height, width) to the model to get output logits.

  • resnet18(pretrained=True): loads ResNet-18 with pretrained weights.
  • model(input_tensor): runs forward pass and returns predictions.
  • Replace model.fc to customize output classes.
python
import torchvision.models as models

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

# Replace final layer for 10 classes
import torch.nn as nn
model.fc = nn.Linear(model.fc.in_features, 10)

# Example input tensor (batch of 1, 3 color channels, 224x224 image)
import torch
input_tensor = torch.randn(1, 3, 224, 224)

# Forward pass
output = model(input_tensor)
💻

Example

This example shows loading a pretrained ResNet-18, replacing the last layer for 5 classes, running a dummy input through it, and printing the output shape.

python
import torch
import torchvision.models as models
import torch.nn as nn

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

# Replace final fully connected layer for 5 output classes
model.fc = nn.Linear(model.fc.in_features, 5)

# Create a dummy input tensor (batch size 2, 3 channels, 224x224 image)
input_tensor = torch.randn(2, 3, 224, 224)

# Run forward pass
output = model(input_tensor)

# Print output shape
print('Output shape:', output.shape)
Output
Output shape: torch.Size([2, 5])
⚠️

Common Pitfalls

  • Not resizing input images to 224x224 pixels, which ResNet expects.
  • Forgetting to normalize input images with ImageNet mean and std before passing to the model.
  • Not switching model to eval() mode during inference, which affects layers like batch normalization and dropout.
  • Replacing the final layer but not updating the optimizer or loss function accordingly.
python
import torchvision.models as models
import torch.nn as nn

# Wrong: Not replacing final layer for new classes
model = models.resnet18(pretrained=True)
# model.fc still outputs 1000 classes

# Right: Replace final layer for 3 classes
model.fc = nn.Linear(model.fc.in_features, 3)

# Remember to call model.eval() during inference
model.eval()
📊

Quick Reference

ActionCode ExampleNotes
Load pretrained ResNet-50model = models.resnet50(pretrained=True)Use other variants like resnet34, resnet101 similarly
Replace final layermodel.fc = nn.Linear(model.fc.in_features, num_classes)Change output classes for your task
Prepare inputinput = torch.randn(batch, 3, 224, 224)Images must be resized and normalized
Switch to eval modemodel.eval()Use during inference to disable dropout
Forward passoutput = model(input)Get raw predictions (logits)

Key Takeaways

Import ResNet models from torchvision.models and load pretrained weights with pretrained=True.
Replace the final fully connected layer to match your number of output classes before training.
Resize and normalize input images to 224x224 with ImageNet stats before passing to ResNet.
Use model.eval() mode during inference to get correct predictions.
Pass a tensor shaped (batch_size, 3, 224, 224) to the model for forward passes.