Unable to get the module of pre-trained VGG model

Eugene_Chan: <p>Hi, I am new to the PyTorch community, so sorry if I ask some questions that look dump.<br>
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.</p>
<p>Here is what I have with model.eval():<br>
In[6]:model.eval()<br>
Out[6]:<br>
VGGReg(<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; (vgg): VGG(<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp; (features): Sequential(<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;  (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;  (1): ReLU(inplace=True)<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;   (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; …<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;    (29): ReLU(inplace=True)<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;       (30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;  )<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;   (avgpool): AdaptiveAvgPool2d(output_size=(7, 7))<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;    (classifier): Sequential(<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;      (0): Linear(in_features=25088, out_features=4096, bias=True)<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;      (1): ReLU(inplace=True)<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;     (2): Dropout(p=0.5, inplace=False)<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;   (3): Linear(in_features=4096, out_features=4096, bias=True)<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;      (4): ReLU(inplace=True)<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;      (5): Dropout(p=0.5, inplace=False)<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;     (6): Sequential(<br>
&nbsp; &nbsp;  &nbsp;  &nbsp;   &nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;       (0): Linear(in_features=4096, out_features=1, bias=True)<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;      (1): Sigmoid()<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;    )<br>
&nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp;  &nbsp;     )<br>
&nbsp; &nbsp;  &nbsp;  &nbsp;   )<br>
)</p>
<p>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.</p>
<p>(function in use):<br>
&nbsp; &nbsp;  &nbsp;  &nbsp;        for name, module in self.model._modules.items():<br>
&nbsp; &nbsp;  &nbsp;  &nbsp;&nbsp; &nbsp;  &nbsp;  &nbsp;            if module == self.feature_module:<br>
&nbsp; &nbsp;  &nbsp;  &nbsp;&nbsp; &nbsp;  &nbsp;  &nbsp;&nbsp; &nbsp;  &nbsp;  &nbsp;               target_activations, x = self.feature_extractor(x)<br>
&nbsp; &nbsp;  &nbsp;  &nbsp;&nbsp; &nbsp;  &nbsp;  &nbsp;        elif “avgpool” in name.lower():<br>
&nbsp; &nbsp;  &nbsp;  &nbsp;&nbsp; &nbsp;  &nbsp;  &nbsp;&nbsp; &nbsp;  &nbsp;  &nbsp;            x = module(x)<br>
&nbsp; &nbsp;  &nbsp;  &nbsp;&nbsp; &nbsp;  &nbsp;  &nbsp;&nbsp; &nbsp;  &nbsp;  &nbsp;            x = x.view(x.size(0), -1)<br>
&nbsp; &nbsp;  &nbsp;  &nbsp;&nbsp; &nbsp;  &nbsp;  &nbsp;      else:<br>
&nbsp; &nbsp;  &nbsp;  &nbsp;&nbsp; &nbsp;  &nbsp;  &nbsp;&nbsp; &nbsp;  &nbsp;  &nbsp;       x = module(x)<br>
&nbsp; &nbsp;  &nbsp;  &nbsp;    return target_activations, x</p>
<p>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.</p>
<p>I appreciate any help!</p>

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

Eugene_Chan: <p>It works, appreciated! It was for a customized GradCAM method.</p>