Load the weights from pretrained model into modified model

algeriapy: <p>I am trying to use inception model as extractor in different layers<br>
So I implemented a class like follow:</p>
<pre><code class="lang-auto">class InceptExt(nn.Module):
    def __init__(self, inception):
        super(InceptExt, self).__init__()
        self.Conv2d_1a_3x3 = inception.Conv2d_1a_3x3
        self.Conv2d_2a_3x3 = inception.Conv2d_2a_3x3
        self.Conv2d_2b_3x3 = inception.Conv2d_2b_3x3
        self.Conv2d_3b_1x1 = inception.Conv2d_3b_1x1
        self.Conv2d_4a_3x3 = inception.Conv2d_4a_3x3
        self.Mixed_5b = inception.Mixed_5b
        self.Mixed_5c = inception.Mixed_5c
        self.Mixed_5d = inception.Mixed_5d
        self.Mixed_6a = inception.Mixed_6a
        self.Mixed_6b = inception.Mixed_6b
        self.Mixed_6c = inception.Mixed_6c
        self.Mixed_6d = inception.Mixed_6d
        self.Mixed_6e = inception.Mixed_6e
    
        self.maxpool1 = nn.MaxPool2d(kernel_size=3, stride=2)
        self.maxpool2 = nn.MaxPool2d(kernel_size=3, stride=2)

        self.Mixed_7a = inception.Mixed_7a
        self.Mixed_7b = inception.Mixed_7b
        self.Mixed_7c = inception.Mixed_7c

    def forward(self, x):
        # N x 3 x 299 x 299
        x = self.Conv2d_1a_3x3(x)
        # N x 32 x 149 x 149
        x = self.Conv2d_2a_3x3(x)
        # N x 32 x 147 x 147
        x = self.Conv2d_2b_3x3(x)
        # N x 64 x 147 x 147
        x = self.maxpool1(x)
        # N x 64 x 73 x 73
        x = self.Conv2d_3b_1x1(x)
        # N x 80 x 73 x 73
        x = self.Conv2d_4a_3x3(x)
        # N x 192 x 71 x 71
        x = self.maxpool2(x)
        # N x 192 x 35 x 35
        x = self.Mixed_5b(x)
        # N x 256 x 35 x 35
        x = self.Mixed_5c(x)
        # N x 288 x 35 x 35
        x = self.Mixed_5d(x)
        # N x 288 x 35 x 35
        x = self.Mixed_6a(x)
        # N x 768 x 17 x 17
        x = self.Mixed_6b(x)
        # N x 768 x 17 x 17
        x = self.Mixed_6c(x)
        # N x 768 x 17 x 17
        x = self.Mixed_6d(x)
        # N x 768 x 17 x 17
        x = self.Mixed_6e(x)        
        # N x 768 x 17 x 17
        x = self.Mixed_7a(x)
        # N x 1280 x 8 x 8
        x = self.Mixed_7b(x)
        # N x 2048 x 8 x 8
        x = self.Mixed_7c(x)
        # N x 2048 x 8 x 8

        return x
    
    
inception = torchvision.models.inception_v3(pretrained=True)
my_inception = InceptExt(inception)
</code></pre>
<p>I am wondering if the above code takes the pretrained weights to the preserved layers or not<br>
If it does not, could you suggest a way how to load the weights from the pretrained model to the modified one ?</p>

pfloat: <p>Hello,</p>
<p>yes the weights are the same.<br>
If needed, you can try to look at the weights like this for example:</p>
<pre><code class="lang-auto">my_weights = list(my_inception.Conv2d_1a_3x3.named_children())[0][1].weight
original_weights = list(inception.Conv2d_1a_3x3.named_children())[0][1].weight
same = torch.allclose(my_weights, original_weights)
</code></pre>
<p>if <code>same</code> is <code>True</code> then the weights are the same.</p>

algeriapy: <aside class="quote no-group" data-username="pfloat" data-post="2" data-topic="95428">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pfloat/48/28529_2.png" class="avatar"> pfloat:</div>
<blockquote>
<pre><code class="lang-auto">my_weights = list(my_inception.Conv2d_1a_3x3.named_children())[0][1].weight
original_weights = list(inception.Conv2d_1a_3x3.named_children())[0][1].weight
same = torch.allclose(my_weights, original_weights)
</code></pre>
</blockquote>
</aside>
<p>Thanks a lot you answer make me sure about<br>
Best Regards</p>