Transfer Learning With Resnet18 on CIFAR10: Poor Training Accuracy

Hi,

I am playing around with the Pytorch library and trying to use Transfer Learning.

My code is as follows:

# get the model with pre-trained weights
resnet18 = models.resnet18(pretrained=True)

# freeze all the layers
for param in resnet18.parameters():
    param.requires_grad = False
    
# print and check what the last FC layer is: 
# Linear(in_features=512, out_features=1000, bias=True)
print(resnet18)

# set the final FC layer to what we require for our problem
resnet18.fc = nn.Linear(512, 10)

# unfreeze the last layer so it learns on our dataset
for param in resnet18.fc.parameters():
  param.requires_grad = True

I set the optimizer as:

# set optimizer
lr = 1e-2
optimizer = torch.optim.SGD(resnet18.parameters(), lr=lr, momentum=0.5)

Training this model on CIFAR10 gives me a very poor training accuracy of 44%.

Am I doing transfer learning correctly here? I would have expected much better results.

I followed the tutorial here:

https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html

Are you using the same transformations as used in the tutorial?
Also, have you played around with some hyperparameters, e.g. did you try to lower the learning rate?

Hi did lower the learning rate for the last layer but it didn’t perform much better. If I unfreeze the layers and set a very low learning rate the model performs much better. In terms of transformations I simply did normalization and toTensor.