How to load/read the pretrained model

isalirezag: <p>i have a pretrained model and want to see list of the names (parameters, netwoeks, etc) in my model<br>
how I can do that?</p>
<pre><code class="lang-auto">pretrain = torch.load(address.pth')
checkpoint = pretrain['model']
how to know the name of the weights, so i can use them later for loading?
</code></pre>
<p>if my question is vague let me explain what im looking for</p>
<p>lets say i have a model including network like:</p>
<pre><code class="lang-auto">Conv1 = conv2d(...)
Conv2 = conv2d(...)
</code></pre>
<p>after saving the model, how i can see the list of names, such as <strong>Conv1, and Conv2</strong></p>

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

isalirezag: <p>there should be a way to do it in pytorch,</p>

kaixin: <p>Hi,</p>
<p><code>Conv1</code>, <code>Conv2</code> are names for layers (or in fact modules). Names of parameters look like <code>Conv1.weight</code>, <code>Conv2.bias</code>.</p>
<p>Sometimes your layer names are just numbers (e.g when you are using <code>Sequential</code> without specifying the names). Assuming what you want are the names of parameters (for loading or something), you could use <code>model.state_dict().keys()</code> to get all the names.</p>
<p>Best</p>

isalirezag: <p><a class="mention" href="/u/kaixin">@kaixin</a><br>
Thats exactly my issue.<br>
They are name of the layers.</p>
<p>hmmm so i have a <code>pth</code> file,  is there a way to find it in my pth file before loading the model?</p>

kaixin: <p><a class="mention" href="/u/isalirezag">@isalirezag</a><br>
Not sure what you mean by “before loading the model”, at least you need <code>torch.load()</code> to load the <code>.pth</code> file so you can inspect it.</p>

isalirezag: <p>sorry for my confusing question.<br>
This is what i was looking for:</p>
<pre><code class="lang-auto">a=torch.load('*.pth')
a_key=a['model'].keys() # so a_key is the name of all parameters that i have
</code></pre>

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

Hardik_Uppal: <p>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 <a href="https://github.com/ox-vgg/vgg_face2" rel="nofollow noopener">https://github.com/ox-vgg/vgg_face2</a></p>

Sodi16: <aside class="quote no-group" data-username="isalirezag" data-post="7" data-topic="28534">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/fbc32d/48.png" class="avatar"> isalirezag:</div>
<blockquote>
<pre><code class="lang-auto">a=torch.load('*.pth')
a_key=a['model'].keys() # so a_key is the name of all parameters that i have
</code></pre>
</blockquote>
</aside>
<p>Hii isalirezag,<br>
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?<br>
Thanks</p>