Unable to get the module of pre-trained VGG model

Hi, I am new to the PyTorch community, so sorry if I ask some questions that look dump.
I am working on a pre-trained VGG for a regression problem, in which I replace the last output layer of VGG with a linear activation for the regression.

Here is what I have with model.eval():
In[6]:model.eval()
Out[6]:
VGGReg(
        (vgg): VGG(
                (features): Sequential(
                (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
                (1): ReLU(inplace=True)
                (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        …
                (29): ReLU(inplace=True)
                (30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
                )
                (avgpool): AdaptiveAvgPool2d(output_size=(7, 7))
                (classifier): Sequential(
                (0): Linear(in_features=25088, out_features=4096, bias=True)
                (1): ReLU(inplace=True)
                (2): Dropout(p=0.5, inplace=False)
                (3): Linear(in_features=4096, out_features=4096, bias=True)
                (4): ReLU(inplace=True)
                (5): Dropout(p=0.5, inplace=False)
                (6): Sequential(
                        (0): Linear(in_features=4096, out_features=1, bias=True)
                        (1): Sigmoid()
                        )
                )
        )
)

My problem is when I used the following function to get modules from the model (i.e. ‘features’, ‘avgpool’,‘classifier’), it treats all the modules as one single module, therefore no module actually fits the IF statements.

(function in use):
        for name, module in self.model._modules.items():
               if module == self.feature_module:
                      target_activations, x = self.feature_extractor(x)
               elif “avgpool” in name.lower():
                      x = module(x)
                      x = x.view(x.size(0), -1)
               else:
                      x = module(x)
        return target_activations, x

I did check the original VGG and it went through the function with proper results. I realize that my module ‘structure’ is unlike the original VGG model - it looks like the module ‘VGG’ is wrapped by VGGReg(). So, is there a way/function to ‘extract’ the module ‘VGG’ from ‘VGGReg’ in my model? I hope by extracting the module it will work as the original VGG.

I appreciate any help!

You could probably just access the vgg module via: self.model.vgg._modules.
What is your use case that you would need this loop and the conditions? Your model’s forward method should properly execute the computation or do you see any issues?

It works, appreciated! It was for a customized GradCAM method.