Pre trained VGG 19 maxpool layer output

I need to extract the Maxpool2d layer (36).
I used the below code:

vggnet = models.vgg19(pretrained=True)
modules = list(vggnet.children())[:-1]
vggnet = nn.Sequential(*modules)

Now , I want the output from
(36): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) and use this output through a Linear layer.

I am resizing the input to 3x448x448 and I am aware of the fact that this pooling layer would give me 512x14x14.
I want this output.
Please help.

Sequential(
(0): Sequential(
(0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): ReLU(inplace)
(2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(3): ReLU(inplace)
(4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(6): ReLU(inplace)
(7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(8): ReLU(inplace)
(9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(11): ReLU(inplace)
(12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(13): ReLU(inplace)
(14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(15): ReLU(inplace)
(16): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(17): ReLU(inplace)
(18): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(19): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(20): ReLU(inplace)
(21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(22): ReLU(inplace)
(23): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(24): ReLU(inplace)
(25): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(26): ReLU(inplace)
(27): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(29): ReLU(inplace)
(30): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(31): ReLU(inplace)
(32): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(33): ReLU(inplace)
(34): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(35): ReLU(inplace)
(36): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
)

You could just use the .features:

model = vggnet.features
out = model(torch.randn(1, 3, 448, 448))

Peter,
I extracted the Maxpool2d layer (36) and I m aware of the fact that if I give a 3x448x448 image I will get back the 512x14x14 tensor.
I want to use this tensor given by the Maxpool2d layer (36).
Please help.
Do I need to use register hook. If so how?

out in my example will be the output of the maxpool layer (36), so you can just use it without registering hooks.

Yup I got the output.
What if I want the output of layer 32?
Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

In that case you could slice the sequential model and create a new one containing only all submodules to index 32 or use a forward_hook.

1 Like