Dissecting a Resnet101

Aseem: <p>I have used a predefined resnet101 and trained it. I am now trying to implement gradcam for which I have dissected the model to insert a hook after the last convolution layer.</p>
<p>‘’’<br>
class my_resnet101(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(my_resnet101, self).<strong>init</strong>()<br>
self.resnet = model</p>
<pre><code>    # disect the network to access its last convolutional layer
    self.part1 = nn.Sequential(*list(model.children())[:-2])
    
    # get the max pool of the features stem
    self.part2 = nn.Sequential(*list(model.children())[-2:])
    
    self.gradients = None

# hook for the gradients of the activations
def activations_hook(self, grad):
    self.gradients = grad
    
def forward(self, x):
    x = self.part1(x)
    
    # register the hook
    h = x.register_hook(self.activations_hook)
    
    # apply the remaining pooling
    x = self.part2(x)
    return x

# method for the gradient extraction
def get_activations_gradient(self):
    return self.gradients

# method for the activation exctraction
def get_activations(self, x):
    return self.features_conv(x)
</code></pre>
<p>‘’’</p>
<p>Now when I do pred = resnet(img), i get the error <strong>mat1 and mat2 shapes cannot be multiplied (2048x1 and 2048x3)</strong>. The dimension of img are 1<em>3</em>48*48. Below is a screenshot of the part of the model where I am engineering this customization. The model has been trained with images of 3 channels and so there should not be this discrepancy. Not sure what am I missing here.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/0/90ad729aa218123dbf6cae2c2ba0ee1e7c4752be.png" data-download-href="https://discuss.pytorch.org/uploads/default/90ad729aa218123dbf6cae2c2ba0ee1e7c4752be" title="Screen Shot 2020-12-14 at 2.58.48 am"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/0/90ad729aa218123dbf6cae2c2ba0ee1e7c4752be_2_690x194.png" alt="Screen Shot 2020-12-14 at 2.58.48 am" data-base62-sha1="kDSmFCWQzx43ZtzZAxS33WvNjVY" width="690" height="194" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/0/90ad729aa218123dbf6cae2c2ba0ee1e7c4752be_2_690x194.png, https://discuss.pytorch.org/uploads/default/original/3X/9/0/90ad729aa218123dbf6cae2c2ba0ee1e7c4752be.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/9/0/90ad729aa218123dbf6cae2c2ba0ee1e7c4752be.png 2x" data-dominant-color="F3F3F3"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2020-12-14 at 2.58.48 am</span><span class="informations">1022×288 42.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>Wrapping the submodules in <code>nn.Sequential</code> blocks can easily fail, if functional calls are used in the original <code>forward</code> method.<br>
In particular, <a href="https://github.com/pytorch/vision/blob/4eab7a67f9f134c810eb7d1977e3ef9aa3fa1b26/torchvision/models/resnet.py#L243">this <code>torch.flatten()</code> operation</a> will be missing in your code and might cause the shape mismatch.</p>