Removing layers from ResNet pretrained model

username: learningpytorch
cooked: <p>Hi everyone! I am trying to do what I did (see below) in VGG16 but in ResNet and GoogleNet. I basically want to remove some of the last layers of these models. I get an error. I would like to use them for feature extraction. I have read that the error could be located in the flatten, but I honestly do not know how to implement it. Please, could anyone help me? Thanks in advance.</p>
<pre><code class="lang-auto">class resnet18_fe(nn.Module):
            def __init__(self):
                super(resnet18_fe, self).__init__()
                self.features = nn.Sequential(
                    # removing the last convolutional layer
                    *list(original_model_resnet.features.children())[:-3]
                )
            def forward(self, x):
                x = self.features(x)
                return x

model_3 = resnet18_fe().to(device=device)
</code></pre>
<p>The error I get is:</p>
<hr>
<p>AttributeError                            Traceback (most recent call last)<br>
 in ()<br>
10                 return x<br>
11<br>
—&gt; 12 model_3 = resnet18_fe().to(device=device)<br>
13<br>
14 print(model_3)</p>
<p>1 frames<br>
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in <strong>getattr</strong>(self, name)<br>
946                 return modules[name]<br>
947         raise AttributeError("’{}’ object has no attribute ‘{}’".format(<br>
 → 948             type(self).<strong>name</strong>, name))<br>
949<br>
950     def <strong>setattr</strong>(self, name: str, value: Union[Tensor, ‘Module’]) → None:</p>
<p>AttributeError: ‘ResNet’ object has no attribute ‘features’</p>

username: sio277
cooked: <aside class="quote no-group" data-username="learningpytorch" data-post="1" data-topic="116166">
<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/l/e95f7d/48.png" class="avatar"> learningpytorch:</div>
<blockquote>
<p>AttributeError: ‘ResNet’ object has no attribute ‘features’</p>
</blockquote>
</aside>
<aside class="quote no-group" data-username="learningpytorch" data-post="1" data-topic="116166">
<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/l/e95f7d/48.png" class="avatar"> learningpytorch:</div>
<blockquote>
<p><code>original_model_resnet.features</code></p>
</blockquote>
</aside>
<p>Check if your ResNet model (<code>original_model_resnet</code>) has <code>features</code> attribute.</p>

username: learningpytorch
cooked: <p>Thanks for your reply. No, it has not that. I guess that may be the failure but not sure how to implement it.</p>

username: sio277
cooked: <p>Can you upload your ResNet code?</p>

username: learningpytorch
cooked: <p>My model is just an imported version:</p>
<pre><code class="lang-auto">original_model_resnet = models.resnet18(pretrained=True).to(device=device)
</code></pre>

username: sio277
cooked: <p>Based on torch ResNet here,<a href="https://github.com/pytorch/vision/blob/a75fdd4180683f7953d97ebbcc92d24682690f96/torchvision/models/resnet.py" class="inline-onebox" rel="noopener nofollow ugc">vision/resnet.py at a75fdd4180683f7953d97ebbcc92d24682690f96 · pytorch/vision · GitHub</a>, try inheritance, such as,</p>
<pre><code class="lang-auto">class resnet18_fe(models.resnet.ResNet):
    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        
        # erase layers you want
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.fc(x)

        return x
</code></pre>

username: learningpytorch
cooked: <p>Thanks, I will try it!</p>