torch.nn.modules.module.ModuleAttributeError: 'EncoderCNN' object has no attribute 'encoderCNN' : Class Looking for itself as argument

username: csblacknet
cooked: <pre><code class="lang-auto">import torch
import torch.nn as nn
import torchvision.models as models
import torchvision.transforms as transforms

from get_loader import get_loader

class EncoderCNN(nn.Module):
    def __init__(self, embed_size, trainCNN = False):
        super(EncoderCNN, self).__init__()

        self.trainCNN = trainCNN
        self.inception = models.inception_v3(pretrained=True, aux_logits=False)
        self.inception.fc == nn.Linear(self.inception.fc.in_features, embed_size)
        self.relu = nn.ReLU()
        self.dropout = nn.Dropout(p=0.5)

    
    def forward(self, images):
        features = self.inception(images)

        for name,param  in self.encoderCNN.inception.parameters():
            if "fc.weight" in name or "fc.bias" in name:
                param.require_grad = True
            else:
                param.require_grad = self.trainCNN

        return self.dropout(self.relu(features))

transform = transforms.Compose([
    transforms.Resize((356, 356)),
    transforms.RandomCrop((299, 299)),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5,0.5,0.5))
])

train_loader, dataset = get_loader(
    root_folder = 'flickr8k/images',
    annotation_file = 'flickr8k/captions.txt',
    transform=transform,
    num_workers = 2
)

encoderCNN = EncoderCNN(256)

for epoch in range(10):
    for idx, (images, captions) in enumerate(train_loader):
        outputs = encoderCNN(images)
</code></pre>
<p>Hey I was just creating a simple Encoder using pretrained inception_v3 from torchvision.models but as soon as I pass input to the created model the following error trace comes up:</p>
<p><strong>Error Trace</strong>:<br>
Traceback (most recent call last):<br>
File “problem.py”, line 48, in <br>
outputs = encoderCNN(images)<br>
File “/home/chirag/Codes/image_captionate_pytorch/venv/lib/python3.8/site-packages/torch/nn/modules/module.py”, line 727, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “problem.py”, line 22, in forward<br>
for name,param  in self.encoderCNN.inception.parameters():<br>
File “/home/chirag/Codes/image_captionate_pytorch/venv/lib/python3.8/site-packages/torch/nn/modules/module.py”, line 778, in <strong>getattr</strong><br>
raise ModuleAttributeError("’{}’ object has no attribute ‘{}’".format(<br>
torch.nn.modules.module.ModuleAttributeError: ‘EncoderCNN’ object has no attribute ‘encoderCNN’</p>
<p>Is this the latest version problem or I am messing with some OOPS logic? I saw something similar ModuleAttributeError recently in yolo_v5 probably but not sure how much it is related to it : <a href="https://github.com/ultralytics/yolov5/issues/58" rel="noopener nofollow ugc">https://github.com/ultralytics/yolov5/issues/58</a></p>

username: csblacknet
cooked: <p>Sorry I have fixed it. It was rather embarrasing:</p>
<p>It should be</p>
<pre><code class="lang-auto">for name,param  in self.inception.parameters():
</code></pre>
<p>instead of</p>
<pre><code class="lang-auto">for name,param  in self.encoderCNN.inception.parameters():
</code></pre>
<p>and I shouldn’t do require_grad for each forward pass</p>