How to Fine Tune own pytorch model

Hello there am a new to pytorch , my problem is I have to fine tune my own model . I have seen example of fine tuning the Torch Vision Models , like downloading the .pth and start training it.

Like wise I have my own .pth file and Neural Network model , I want to do fine tuning .
I kindly request you help with an example for my own model.

If you have your own .pth model file then just load it and finetune for the number of epochs you want.

import torch

model = get_model()
checkpoint = torch.load(path_to_your_pth_file)

finetune_epochs = 10 # number of epochs you want to finetune
for epoch in range(finetune_epochs):
         # assuming you have functions for training and validating models 
1 Like

hello there and thanks for the immediate reply .
I tried your above steps , I get the following error ,please help on this .

model = torch.hub.load(‘pytorch/vision:v0.5.0’, ‘resnext50_32x4d’, pretrained=False)
for param in model.parameters():
param.requires_grad = False

model.fc = nn.Linear(2048, num_classes)

And the error is
KeyError: ‘state_dict’

How to modify the number of categories in the last layer?

You would access the fully connected layer and just replace it with the output argument being the number of classes that your new task has. For example:

model = get_model()
checkpoint = torch.load(path_to_your_pth_file)
model.fc = nn.Linear(2048, 10) #input is whatever the output of prior layer is and output is the number of classes that you have

You can then decide how you would like to proceed with the training step since your last layer has random weights and the rest of your model has trained weights. You can either just train the last layer or the whole network.

This is not working for me.

‘key error: state_dict’???

Any idea?

checkpoint['state_dict'] assumes that checkpoint is a dict with the state_dict key.
This doesn’t seem to be the case in your setup, so check what checkpoint is and what it contains.