
Pytorch ResNet18 low performance with CIFAR100 Dataset
When using PyTorch’s provided ResNet18, the performance on the CIFAR-100 dataset is significantly lower than expected—around 30-40% accuracy, which is much lower than its reported benchmark.
Why does this happen?
PyTorch’s ResNet18 is originally designed for the ImageNet dataset, where images are typically resized to 224×224 or 256×256. The model architecture is optimized for these larger images, particularly in the first convolutional layer, which uses a large kernel size:
Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
Since CIFAR-100 images are much smaller (32×32), using this large kernel and stride results in excessive downsampling, negatively impacting performance.
How to Fix It?
To better adapt ResNet18 for CIFAR-100, we need to:
- Modify the first convolutional layer to use a smaller kernel size and stride.
- Remove the max pooling layer (since CIFAR images are already small, additional pooling isn’t necessary).
Here’s the code snippet to make these adjustments:
import torch.nn as nn
from torchvision.models import resnet18
# Load ResNet18 without pre-trained weights
model = resnet18(pretrained=False)
# Adjust the first convolutional layer for 32x32 images
model.conv1 = nn.Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
# Remove the max pooling layer
model.maxpool = nn.Identity()
With these modifications, ResNet18 is better suited for CIFAR-100 and should yield improved performance. 🚀
References: