Model._modules[] to find a layer

hkitano: <p>I’ve trained a DenseNet121 model, and am trying to get ahold of one of the final layers for a GradCAM implementation. The implementation uses model._modules[] as a dict from layer name to layer, but for some reason, my model has only one key-value pair in this dict: “module” to the entire DenseNet object. What should I do instead? My version is 1.2.0.</p>
<p>Thanks!</p>

ptrblck: <p>Which layer would you like to get?<br>
This code would index a specific conv layer inside the model:</p>
<pre><code class="lang-python">model = models.densenet121()
model.features.denseblock4.denselayer2.conv2
</code></pre>
<p>Your mentioned method is an internal method and thus shouldn’t generally be used.</p>

hkitano: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="71931">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>model.features.denseblock4.denselayer2.conv2</p>
</blockquote>
</aside>
<p>I’m looking for the a layer to create class activation maps, so probably one of the last ones like “module.densenet121.features.denseblock4.denselayer16.conv.2” or “module.densenet121.features.norm5”</p>

ptrblck: <p>So would the approach of directly accessing the modules work or are you still stuck?</p>

hkitano: <p>My model is wrapped in torch.nn.DataParallel(model). How do I get “under the hood” to access the Densenet? I think this is probably the issue with the first part of the issue</p>

ptrblck: <p>You could access the model via <code>model.module.features...</code></p>

hkitano: <p>Oops! silly mistake on my part. Appreciate it!</p>

Liron_Mor_Yosef: <aside class="quote no-group" data-username="ptrblck" data-post="6" data-topic="71931">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p><code>model.module.features</code></p>
</blockquote>
</aside>
<p>what about Sequential, getting AttributeError: ‘Sequential’ object has no attribute ‘module’.</p>

ptrblck: <p>The <code>.module</code> attribute will be added by <code>nn.DataParallel</code> for all models including <code>nn.Seuqential</code>:</p>
<pre><code class="lang-python">model = nn.Sequential(
    nn.Linear(1, 1),
    nn.ReLU(),
    nn.Linear(1, 1))

model = nn.DataParallel(model)
print(model.module)
&gt; Sequential(
  (0): Linear(in_features=1, out_features=1, bias=True)
  (1): ReLU()
  (2): Linear(in_features=1, out_features=1, bias=True)
)
</code></pre>

anil_sarode: <p>I am working on classification of five different types of images of skin and I trained the AlexNet model and GoogleNet Model. Now I wanted to implement the Grad-CAM on the final layer of the these models and want to get the heat-map.</p>
<p>after reading many blogs and coding material I reach <a href="https://github.com/jacobgil/pytorch-grad-cam/blob/master/gradcam.py" rel="noopener nofollow ugc">here</a>. This code is specifically use resnet50 model.</p>
<pre><code>model = models.resnet50(pretrained=True)
grad_cam = GradCam(model=model, feature_module=model.layer4, \
                   target_layer_names=["2"], use_cuda=args.use_cuda)
</code></pre>
<p>How should I pass the <code>feature_module</code> and <code>target_layer_names</code> to constructor of the <code>grad_cam</code> class for AlexNet and for GoogleNet.</p>
<p>I realized I need to get the final layer from the Pretrain Network by using the following code.</p>
<pre><code> new_classifier = nn.Sequential(*list(model.classifier.children())[:-1])
 model.classifier = new_classifier  
</code></pre>
<p>How can I use this way to pass as parameter to the <code>grad_cam</code> class.</p>