Pre trained VGG 19 maxpool layer output

username: Abhishek_Das
cooked: <p>I need to extract the Maxpool2d layer (36).<br>
I used the below code:</p>
<p>vggnet = models.vgg19(pretrained=True)<br>
modules = list(vggnet.children())[:-1]<br>
vggnet = nn.Sequential(*modules)</p>
<p>Now , I want the output from<br>
(36): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) and use this output through a Linear layer.</p>
<p>I am resizing the input to 3x448x448 and I am aware of the fact that this pooling layer would give me 512x14x14.<br>
I want this output.<br>
Please help.</p>
<p>Sequential(<br>
(0): Sequential(<br>
(0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(1): ReLU(inplace)<br>
(2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(3): ReLU(inplace)<br>
(4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(6): ReLU(inplace)<br>
(7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(8): ReLU(inplace)<br>
(9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(11): ReLU(inplace)<br>
(12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(13): ReLU(inplace)<br>
(14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(15): ReLU(inplace)<br>
(16): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(17): ReLU(inplace)<br>
(18): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(19): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(20): ReLU(inplace)<br>
(21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(22): ReLU(inplace)<br>
(23): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(24): ReLU(inplace)<br>
(25): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(26): ReLU(inplace)<br>
(27): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(29): ReLU(inplace)<br>
(30): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(31): ReLU(inplace)<br>
(32): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(33): ReLU(inplace)<br>
(34): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(35): ReLU(inplace)<br>
(36): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
)<br>
)</p>

username: ptrblck
cooked: <p>You could just use the <code>.features</code>:</p>
<pre><code class="lang-python">model = vggnet.features
out = model(torch.randn(1, 3, 448, 448))
</code></pre>

username: Abhishek_Das
cooked: <p>Peter,<br>
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.<br>
I want to use this tensor given by the Maxpool2d layer (36).<br>
Please help.<br>
Do I need to use register hook. If so how?</p>

username: ptrblck
cooked: <p><code>out</code> in my example will be the output of the maxpool layer (36), so you can just use it without registering hooks.</p>

username: Abhishek_Das
cooked: <p>Yup I got the output.<br>
What if I want the output of layer 32?<br>
Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))</p>

username: ptrblck
cooked: <p>In that case you could slice the sequential model and create a new one containing only all submodules to index 32 or use a <a href="https://discuss.pytorch.org/t/how-can-l-load-my-best-model-as-a-feature-extractor-evaluator/17254/6">forward_hook</a>.</p>