AttributeError: 'ResNet50' object has no attribute 'conv1'

username: akschougule
cooked: <p>Basically I am trying to implement different learning rate for different layers in my modified ResNet50. My code below.</p>
<pre><code class="lang-auto">class ResNet50(nn.Module):
    def __init__(self, pretrained):
        super(ResNet50, self).__init__()
        if pretrained is True:
            self.model = models.resnet50(pretrained="imagenet")
        else:
            self.model = models.resnet50(pretrained=None)

        self.l0 = nn.Linear(2048, 2) 
        self.l1 = nn.Linear(2048, 2)  
        self.l2 = nn.Linear(2048, 2)  
  

    def forward(self, x):
        bs, _, _, _ = x.shape
        x = self.model.features(x)
        x = F.adaptive_avg_pool2d(x, 1).reshape(bs, -1)
        l0  = self.l0(x)
        l1  = self.l1(x)
        l2  = self.l2(x)

        return l0, l1, l2

model = ResNet50(pretrained=True)

for name, param in model.named_parameters():
    if param.requires_grad:
        print(name)
</code></pre>
<p>The output looks like:</p>
<pre><code class="lang-auto">model.conv1.weight
model.bn1.weight
model.bn1.bias
model.layer1.0.conv1.weight
model.layer1.0.bn1.weight
model.layer1.0.bn1.bias
model.layer1.0.conv2.weight
model.layer1.0.bn2.weight
model.layer1.0.bn2.bias
model.layer1.0.conv3.weight
model.layer1.0.bn3.weight
model.layer1.0.bn3.bias
model.layer1.0.downsample.0.weight
model.layer1.0.downsample.1.weight
model.layer1.0.downsample.1.bias
model.layer1.1.conv1.weight
model.layer1.1.bn1.weight
model.layer1.1.bn1.bias
model.layer1.1.conv2.weight
model.layer1.1.bn2.weight
model.layer1.1.bn2.bias
model.layer1.1.conv3.weight
model.layer1.1.bn3.weight
model.layer1.1.bn3.bias
model.layer1.2.conv1.weight
model.layer1.2.bn1.weight
model.layer1.2.bn1.bias
model.layer1.2.conv2.weight
model.layer1.2.bn2.weight
model.layer1.2.bn2.bias
model.layer1.2.conv3.weight
model.layer1.2.bn3.weight
model.layer1.2.bn3.bias
model.layer2.0.conv1.weight
model.layer2.0.bn1.weight
model.layer2.0.bn1.bias
model.layer2.0.conv2.weight
model.layer2.0.bn2.weight
model.layer2.0.bn2.bias
model.layer2.0.conv3.weight
model.layer2.0.bn3.weight
model.layer2.0.bn3.bias
model.layer2.0.downsample.0.weight
model.layer2.0.downsample.1.weight
model.layer2.0.downsample.1.bias
model.layer2.1.conv1.weight
model.layer2.1.bn1.weight
model.layer2.1.bn1.bias
model.layer2.1.conv2.weight
model.layer2.1.bn2.weight
model.layer2.1.bn2.bias
model.layer2.1.conv3.weight
model.layer2.1.bn3.weight
model.layer2.1.bn3.bias
model.layer2.2.conv1.weight
model.layer2.2.bn1.weight
model.layer2.2.bn1.bias
model.layer2.2.conv2.weight
model.layer2.2.bn2.weight
model.layer2.2.bn2.bias
model.layer2.2.conv3.weight
model.layer2.2.bn3.weight
model.layer2.2.bn3.bias
model.layer2.3.conv1.weight
model.layer2.3.bn1.weight
model.layer2.3.bn1.bias
model.layer2.3.conv2.weight
model.layer2.3.bn2.weight
model.layer2.3.bn2.bias
model.layer2.3.conv3.weight
model.layer2.3.bn3.weight
model.layer2.3.bn3.bias
model.layer3.0.conv1.weight
model.layer3.0.bn1.weight
model.layer3.0.bn1.bias
model.layer3.0.conv2.weight
model.layer3.0.bn2.weight
model.layer3.0.bn2.bias
model.layer3.0.conv3.weight
model.layer3.0.bn3.weight
model.layer3.0.bn3.bias
model.layer3.0.downsample.0.weight
model.layer3.0.downsample.1.weight
model.layer3.0.downsample.1.bias
model.layer3.1.conv1.weight
model.layer3.1.bn1.weight
model.layer3.1.bn1.bias
model.layer3.1.conv2.weight
model.layer3.1.bn2.weight
model.layer3.1.bn2.bias
model.layer3.1.conv3.weight
model.layer3.1.bn3.weight
model.layer3.1.bn3.bias
model.layer3.2.conv1.weight
model.layer3.2.bn1.weight
model.layer3.2.bn1.bias
model.layer3.2.conv2.weight
model.layer3.2.bn2.weight
model.layer3.2.bn2.bias
model.layer3.2.conv3.weight
model.layer3.2.bn3.weight
model.layer3.2.bn3.bias
model.layer3.3.conv1.weight
model.layer3.3.bn1.weight
model.layer3.3.bn1.bias
model.layer3.3.conv2.weight
model.layer3.3.bn2.weight
model.layer3.3.bn2.bias
model.layer3.3.conv3.weight
model.layer3.3.bn3.weight
model.layer3.3.bn3.bias
model.layer3.4.conv1.weight
model.layer3.4.bn1.weight
model.layer3.4.bn1.bias
model.layer3.4.conv2.weight
model.layer3.4.bn2.weight
model.layer3.4.bn2.bias
model.layer3.4.conv3.weight
model.layer3.4.bn3.weight
model.layer3.4.bn3.bias
model.layer3.5.conv1.weight
model.layer3.5.bn1.weight
model.layer3.5.bn1.bias
model.layer3.5.conv2.weight
model.layer3.5.bn2.weight
model.layer3.5.bn2.bias
model.layer3.5.conv3.weight
model.layer3.5.bn3.weight
model.layer3.5.bn3.bias
model.layer4.0.conv1.weight
model.layer4.0.bn1.weight
model.layer4.0.bn1.bias
model.layer4.0.conv2.weight
model.layer4.0.bn2.weight
model.layer4.0.bn2.bias
model.layer4.0.conv3.weight
model.layer4.0.bn3.weight
model.layer4.0.bn3.bias
model.layer4.0.downsample.0.weight
model.layer4.0.downsample.1.weight
model.layer4.0.downsample.1.bias
model.layer4.1.conv1.weight
model.layer4.1.bn1.weight
model.layer4.1.bn1.bias
model.layer4.1.conv2.weight
model.layer4.1.bn2.weight
model.layer4.1.bn2.bias
model.layer4.1.conv3.weight
model.layer4.1.bn3.weight
model.layer4.1.bn3.bias
model.layer4.2.conv1.weight
model.layer4.2.bn1.weight
model.layer4.2.bn1.bias
model.layer4.2.conv2.weight
model.layer4.2.bn2.weight
model.layer4.2.bn2.bias
model.layer4.2.conv3.weight
model.layer4.2.bn3.weight
model.layer4.2.bn3.bias
model.fc.weight
model.fc.bias
l0.weight
l0.bias
l1.weight
l1.bias
l2.weight
l2.bias
</code></pre>
<p>But when I try this</p>
<pre><code class="lang-auto">param_groups = [
    [model.conv1, model.bn1, model.layer1, model.layer2],
    [model.layer3, model.layer4],
    [model.fc.weight, model.fc.bias]
]

lrs = np.array([lr / 10, lr / 3, lr])
</code></pre>
<p>I get following error</p>
<pre><code class="lang-auto">AttributeError                            Traceback (most recent call last)
&lt;ipython-input-110-28b2b69ee3ca&gt; in &lt;module&gt;
      1 param_groups = [
----&gt; 2     [model.conv1, model.bn1, model.layer1, model.layer2],
      3     [model.layer3, model.layer4],
      4     [model.fc.weight, model.fc.bias]
      5 ]

~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in __getattr__(self, name)
    533                 return modules[name]
    534         raise AttributeError("'{}' object has no attribute '{}'".format(
--&gt; 535             type(self).__name__, name))
    536 
    537     def __setattr__(self, name, value):

AttributeError: 'ResNet50' object has no attribute 'conv1'

</code></pre>

username: simaiden
cooked: <p>It’s because your class does not have those attributes but self.model. So you have to use model.model.conv1 and with others attributes as well</p>

username: akschougule
cooked: <p>Thanks, that worked.</p>
<p>Is there any way I can avoid writing <code>model.model</code>?</p>
<p>Edit: In order to avoid writing <code>model.model</code>, while creating the instance I used<br>
<code>model = ResNet50(pretrained=True).model</code></p>

username: gyt.971027
cooked: <p>I code like it, but it said mobileNetV3 has no attribute ‘model’<br>
And my code below</p>
<blockquote>
<p>class MobileNetV3(MyNetwork):</p>
<pre><code>def __init__(self, first_conv, blocks, final_expand_layer, feature_mix_layer, classifier):
    super(MobileNetV3, self).__init__()
    
    self.first_conv = first_conv
    self.blocks = nn.ModuleList(blocks)
    self.final_expand_layer = final_expand_layer
    self.feature_mix_layer = feature_mix_layer
    self.classifier = classifier
    self.quant = torch.quantization.QuantStub()
    self.dequant = torch.quantization.DeQuantStub()

def forward(self, x):
    x.contiguous(memory_format=torch.channels_last)
    x = self.quant(x)
    x = self.first_conv(x)
    for block in self.blocks:
        x = block(x)
    x = self.final_expand_layer(x)
    x = x.mean(3, keepdim=True).mean(2, keepdim=True)  # global average pooling
    x = self.feature_mix_layer(x)
    x = torch.squeeze(x)
    x = self.classifier(x)
    x = self.dequant(x)
    return x

@property
def module_str(self):
    _str = self.first_conv.module_str + '\n'
    for block in self.blocks:
        _str += block.module_str + '\n'
    _str += self.final_expand_layer.module_str + '\n'
    _str += self.feature_mix_layer.module_str + '\n'
    _str += self.classifier.module_str
    return _str

@property
def config(self):
    return {
        'name': MobileNetV3.__name__,
        'bn': self.get_bn_param(),
        'first_conv': self.first_conv.config,
        'blocks': [
            block.config for block in self.blocks
        ],
        'final_expand_layer': self.final_expand_layer.config,
        'feature_mix_layer': self.feature_mix_layer.config,
        'classifier': self.classifier.config,
    }

@staticmethod
def build_from_config(config):
    first_conv = set_layer_from_config(config['first_conv'])
    final_expand_layer = set_layer_from_config(config['final_expand_layer'])
    feature_mix_layer = set_layer_from_config(config['feature_mix_layer'])
    classifier = set_layer_from_config(config['classifier'])

    blocks = []
    for block_config in config['blocks']:
        blocks.append(MobileInvertedResidualBlock.build_from_config(block_config))

    net = MobileNetV3(first_conv, blocks, final_expand_layer, feature_mix_layer, classifier)
    if 'bn' in config:
        net.set_bn_param(**config['bn'])
    else:
        net.set_bn_param(momentum=0.1, eps=1e-3)

    return net

def zero_last_gamma(self):
    for m in self.modules():
        if isinstance(m, MobileInvertedResidualBlock):
            if isinstance(m.mobile_inverted_conv, MBInvertedConvLayer) and isinstance(m.shortcut, IdentityLayer):
                m.mobile_inverted_conv.point_linear.bn.weight.data.zero_()
</code></pre>
</blockquote>
<p>But when I try to use</p>
<blockquote>
<p>torch.quantization.fuse_modules(model, [[‘conv’, ‘bn’, ‘relu’]], inplace=True)<br>
The error:<br>
‘MobileNetV3’ object has no attribute ‘conv’</p>
</blockquote>

username: gyt.971027
cooked: <p>And when I try to add ‘.model’. It said mobileNetv3 has no attribute ‘model’.</p>