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

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)

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:

Error Trace:
Traceback (most recent call last):
File “problem.py”, line 48, in
outputs = encoderCNN(images)
File “/home/chirag/Codes/image_captionate_pytorch/venv/lib/python3.8/site-packages/torch/nn/modules/module.py”, line 727, in _call_impl
result = self.forward(*input, **kwargs)
File “problem.py”, line 22, in forward
for name,param in self.encoderCNN.inception.parameters():
File “/home/chirag/Codes/image_captionate_pytorch/venv/lib/python3.8/site-packages/torch/nn/modules/module.py”, line 778, in getattr
raise ModuleAttributeError("’{}’ object has no attribute ‘{}’".format(
torch.nn.modules.module.ModuleAttributeError: ‘EncoderCNN’ object has no attribute ‘encoderCNN’

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 : https://github.com/ultralytics/yolov5/issues/58

Sorry I have fixed it. It was rather embarrasing:

It should be

for name,param  in self.inception.parameters():

instead of

for name,param  in self.encoderCNN.inception.parameters():

and I shouldn’t do require_grad for each forward pass