How to load .pth file which contains only 'model' and 'opt'

I have .pth file which when loaded with torch.load() gives only ‘model’ and ‘opt’.
File is actually build with fastai unet with resnet34.
I don’t know the complete architecture of the model to re-create the model instance.
I tried with
model = torchvision.models.resnet34()
state_dict = torch.load(‘file.pth’)

It’s not working, pth file is not matching with model


If the dict returned by torch.load('file.pth') has two keys (you mention model and opt), you have to extract the value of the key model and then pass it to the model’s load_state_dict like that:

model = torchvision.models.resnet34()
state_dict = torch.load('file.pth')

Alternatively, you can pass the whole state_dict to load_state_dict and set the flag strict to False:

model.load_state_dict(state_dict['model'], strict=False)

It should work unless the structures of the models differ (the saved model is not an instance of resnet).