AttributeError: 'ResNet' object has no attribute 'get_features'

Mahmoud_Ghorbel: <p>Hello<br>
i defined a method inside a class but i don’t know why i’m getting the following error<br>
AttributeError: ‘ResNet’ object has no attribute ‘get_features’</p>
<p>this is my class:</p>
<pre><code class="lang-auto">class ResNet(nn.Module):
    def __init__(self, num_classes=1000):
        super(ResNet, self).__init__()
        resnet = resnet50(pretrained=True)
        self.backbone = nn.Sequential(
            resnet.conv1,
            resnet.bn1,
            resnet.relu,
            resnet.maxpool,
            resnet.layer1,  # res_conv2
            resnet.layer2,  # res_conv3
            resnet.layer3,  # res_conv4
        )
        self.res_part = nn.Sequential(
            Bottleneck(1024, 512, stride=1, downsample=nn.Sequential(
                nn.Conv2d(1024, 2048, kernel_size=1, stride=1, bias=False),
                nn.BatchNorm2d(2048),
            )),
            Bottleneck(2048, 512),
            Bottleneck(2048, 512),
        )
        self.res_part.load_state_dict(resnet.layer4.state_dict())
        reduction = nn.Sequential(
            nn.Conv2d(2048, 512, 1), 
            nn.BatchNorm2d(512), 
            nn.ReLU()
        )

        self.global_avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.global_softmax = nn.Linear(512, num_classes) 
        self.global_softmax.apply(weights_init_kaiming)
        self.global_reduction = copy.deepcopy(reduction)
        self.global_reduction.apply(weights_init_kaiming)
    def forward(self, x):
        """
        :param x: input image tensor of (N, C, H, W)
        :return: (prediction, triplet_losses, softmax_losses)
        """
        x = self.backbone(x)
        x = self.res_part(x)


        #global branch
        glob = self.global_avgpool(x)
        global_triplet_feature = self.global_reduction(glob).squeeze()
        global_softmax_class = self.global_softmax(global_triplet_feature)

        return global_softmax_class

    def get_features(self):
        return nn.Sequential(
            self.backbone,
            self.res_part,
        )
</code></pre>

ptrblck: <p>The <code>get_features</code> method is currently not used in your code and seems to work if I call it directly:</p>
<pre><code class="lang-python">model = ResNet()
model.get_features()
</code></pre>

Mahmoud_Ghorbel: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="98792">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<pre><code class="lang-auto">model = ResNet()
model.get_features()
</code></pre>
</blockquote>
</aside>
<p>my code works with other Classes, i added this new class because i want to use a specific resnet50</p>

ptrblck: <p>And you should be able to add this method to your custom module.<br>
However, it seems to be working if I’m using your code and call the method directly.<br>
What kind of error are you seeing?</p>

Mahmoud_Ghorbel: <aside class="quote no-group" data-username="Mahmoud_Ghorbel" data-post="1" data-topic="98792">
<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/mahmoud_ghorbel/48/23304_2.png" class="avatar"> Mahmoud_Ghorbel:</div>
<blockquote>
<p>AttributeError: ‘ResNet’ object has no attribute ‘get_features’</p>
</blockquote>
</aside>
<p>i only get this error:</p>
<p>AttributeError: ‘ResNet’ object has no attribute ‘get_features’</p>

ptrblck: <p>It still works for me after removing all undefined classes and functions:</p>
<pre><code class="lang-python">class ResNet(nn.Module):
    def __init__(self, num_classes=1000):
        super(ResNet, self).__init__()
        resnet = models.resnet50(pretrained=True)
        self.backbone = nn.Sequential(
            resnet.conv1,
            resnet.bn1,
            resnet.relu,
            resnet.maxpool,
            resnet.layer1,  # res_conv2
            resnet.layer2,  # res_conv3
            resnet.layer3,  # res_conv4
        )
        reduction = nn.Sequential(
            nn.Conv2d(2048, 512, 1), 
            nn.BatchNorm2d(512), 
            nn.ReLU()
        )

        self.global_avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.global_softmax = nn.Linear(512, num_classes) 
        self.global_reduction = copy.deepcopy(reduction)

    def forward(self, x):
        """
        :param x: input image tensor of (N, C, H, W)
        :return: (prediction, triplet_losses, softmax_losses)
        """
        x = self.backbone(x)
        #global branch
        glob = self.global_avgpool(x)
        global_triplet_feature = self.global_reduction(glob).squeeze()
        global_softmax_class = self.global_softmax(global_triplet_feature)

        return global_softmax_class

    def get_features(self):
        return nn.Sequential(
            self.backbone,
        )


model = ResNet()
model.get_features()
&gt; Sequential(
  (0): Sequential(
    (0): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
    (1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (2): ReLU(inplace=True)
...
</code></pre>

Mahmoud_Ghorbel: <p>here where i need to call get_features and it’s not working</p>
<pre><code class="lang-auto">self.features = getattr(resnet, net)(pretrained=pretrained).get_features()
</code></pre>

Mahmoud_Ghorbel: <p>problem solved, thank you</p>

AI-P-K: <p>How did you solved the issue?</p>