How to load/read the pretrained model

i have a pretrained model and want to see list of the names (parameters, netwoeks, etc) in my model
how I can do that?

pretrain = torch.load(address.pth')
checkpoint = pretrain['model']
how to know the name of the weights, so i can use them later for loading?

if my question is vague let me explain what im looking for

lets say i have a model including network like:

Conv1 = conv2d(...)
Conv2 = conv2d(...)

after saving the model, how i can see the list of names, such as Conv1, and Conv2

I used visual code software and with this IDE, it is very easy to check them by adding in “watch” section.

there should be a way to do it in pytorch,

Hi,

Conv1, Conv2 are names for layers (or in fact modules). Names of parameters look like Conv1.weight, Conv2.bias.

Sometimes your layer names are just numbers (e.g when you are using Sequential without specifying the names). Assuming what you want are the names of parameters (for loading or something), you could use model.state_dict().keys() to get all the names.

Best

@kaixin
Thats exactly my issue.
They are name of the layers.

hmmm so i have a pth file, is there a way to find it in my pth file before loading the model?

@isalirezag
Not sure what you mean by “before loading the model”, at least you need torch.load() to load the .pth file so you can inspect it.

sorry for my confusing question.
This is what i was looking for:

a=torch.load('*.pth')
a_key=a['model'].keys() # so a_key is the name of all parameters that i have

If a contains only the pre-trained weights, you could simply call a.keys() to get the names of all parameters because a is actually an OrderedDict object.

1 Like

Hi while doing the torch.load() i am getting an error that MainModule is missing and its points the error to a serialization.py. I am using weights from https://github.com/ox-vgg/vgg_face2

Hii isalirezag,
Im new on torch and I loaded a model from .pth file and I have an OrderedDict object. My question is how can I use this object to do an object detection?
Thanks