Import adapted Resnet model

Hi everyone, I would like to use a pre-trained Resnet model, but with some modifications. I would like to change the input from 3 channels (RGB) to gray scale (1 input). I would also like to change the output from 1000 classes to 10. Furthermore, it would be interesting to implement a deconvnet to see the feature maps at the different filters.

I do all this changes in the original Resnet code, and save it as resnet_adapted in the same folder. How do I import this model pre-trained? Could I freeze certain layers, just like in normal transfer learning?

I have been advised to use:

import sys
sys.path.append(r'C:\Users\Jacobo\anaconda3\envs\deep learning 3\Lib\site-packages\torchvision\models')
import resnet_adapted

but I think I cannot obtain a pre-trained model from that, nor freeze specific layers.

Thanks in advance for the help!

Depending how much has changed in your custom Resnet implementation, you could either try to directly load the pretrained resnet state_dict with the strict=False option:

resnet_adapted = MyResnet()
pretrained = torchvision.models.resnet50(pretrained=True)
resnet_adapted.load_state_dict(pretrained.state_dict(), strict=False)

or load specific layers manually by iterating the keys in the pretrained state_dict.

Why do you think you cannot freeze specific layers in your model? Are you seeing any errors?