How to access parameters in a certain layer of pretrained network?

Hi, i am recently trying to try out some style transfer codes facilitating activations of pretrained network in torchvision.
Reference: Accessing intermediate layers of a pretrained network forward?

The issue is that I wish to try using an object detection network such as Faster R-CNN, in which case the definition of network is kind of of different e.g. there is no more ‘.features’ attribute.
How to access parameters (weight and activation in multiple layers if possible) in such case?

1 Like

Hi @pinata1337,

An easy way to find out it is to print your model:

>>> print(frcnn)
FasterRCNN(
  (transform): GeneralizedRCNNTransform()
  (backbone): Sequential(
    (0): ConvBNReLU(
      (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (2): ReLU6(inplace=True)
    )
[...]

You can always access the submodules of a module by it’s named attribute (or index when the Module is a Sequential). So, in the case of FasterR-CNN, let’s say you want to access the first Conv2D in the first ConvBNReLU in backbone:

>>> frcnn.backbone[0][0]
Conv2d(3, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
1 Like

Much obliged for the help! Now I know how to access the layers in that network.

However I still encountered other obstacles while trying to get activations for certain data, at a certain layer of the network(e.g. the output) .
When I tried to obtain activation of a layer of the network with the code:

net = models.detection.fasterrcnn_resnet50_fpn(pretrained=True).eval()
for inputs, targets in trainloader:
aaaa = net(inputs)
bbbb = net(targets)
loss = nn.MSELoss(aaaa, bbbb)

Errors pop out:
RuntimeError: The expanded size of the tensor (128) must match the existing size (800) at non-singleton dimension 2. Target sizes: [3, 128, 128]. Tensor sizes: [3, 800, 800]

I do not fully understand the meaning of this error because cnn should work for any size of image(128 in my case) as long as they’re consistent…

I think I might need to read some kind of manual about how to access parameters of the network given input data and location of the wanted parameters, still looking just yet.
Is there any insights you could share on this issue?

That’s because the tensor(or images in your case) shapes passed on to MSELoss is different.
One tensor is of shape (3, 128, 128) and the other is (3, 800, 800). Make sure ‘aaaa’ and ‘bbbb’ shapes’ match.