IntermediateLayerGetter get layer result not desired #3048

jinfagang: <h2>
<img src="https://discuss.pytorch.org/images/emoji/apple/bug.png?v=9" title=":bug:" class="emoji" alt=":bug:"> Bug</h2>
<pre><code class="lang-python">self.backbone = mobilenetv2.mobilenet_v2(
            pretrained=None, output_stride=output_stride)
        # print(backbone.features)
        # rename layers
        # self.backbone.p3 = self.backbone.features[0:2]
        self.backbone.low_level_features = self.backbone.features[0:4]
        self.backbone.p3 = self.backbone.features[4:7]
        self.backbone.p4 = self.backbone.features[7:11]
        self.backbone.p5 = self.backbone.features[11:14] # 64
        self.backbone.high_level_features = self.backbone.features[14:-1]
        # self.backbone.p6 = self.backbone.features[-1:]
        self.backbone.features = None
        self.backbone.classifier = None

        self.backbone = torchvision.models._utils.IntermediateLayerGetter(self.backbone, {
            'low_level_features': 'low',
            'p3': 0,
            'p4': 1,
            'p5': 2,
            # 'p6': 2,
            'high_level_features': 'out',
        })
</code></pre>
<p>the output p3 p4 p5 w and h not desired:</p>
<pre><code class="lang-auto">low torch.Size([1, 24, 128, 184])
0 torch.Size([1, 32, 64, 92])
1 torch.Size([1, 64, 64, 92])
2 torch.Size([1, 96, 64, 92])
out torch.Size([1, 320, 64, 92])

</code></pre>
<p>If I forward mobilenet, the layer I extracted from should w, h all be half:</p>
<pre><code class="lang-auto">from torchvision.models.mobilenet import mobilenet_v2

def main():
    m = mobilenet_v2()
    a = torch.rand([2, 3, 512, 1024])
    for l in m.features:
        a = l(a)
        print(a.shape)

</code></pre>
<pre><code class="lang-auto">torch.Size([2, 32, 256, 512])
torch.Size([2, 16, 256, 512])
torch.Size([2, 24, 128, 256])
torch.Size([2, 24, 128, 256])
torch.Size([2, 32, 64, 128])
torch.Size([2, 32, 64, 128])  &lt;--- get this layer
torch.Size([2, 32, 64, 128])
torch.Size([2, 64, 32, 64])
torch.Size([2, 64, 32, 64])
torch.Size([2, 64, 32, 64])
torch.Size([2, 64, 32, 64]) &lt;--- get this layer
torch.Size([2, 96, 32, 64])
torch.Size([2, 96, 32, 64])
torch.Size([2, 96, 32, 64]) &lt;--- get this layer
torch.Size([2, 160, 16, 32])
torch.Size([2, 160, 16, 32])
torch.Size([2, 160, 16, 32])
torch.Size([2, 320, 16, 32])
torch.Size([2, 1280, 16, 32])

</code></pre>
<p>As you can see, p4 layer, which is index with 1, should with size of <code>2, 64, 32, 64</code> but  I got it size <code>[1, 64, 64, 92]</code></p>
<p>To be clear, this layer w, and h should be half then last layer, but it is same.</p>
<p>What the mistake I got here or it’s a bug a IntermediateLayerGetter?</p>
<p>Can anyone help me out??? Need help！</p>

ptrblck: <p>Double post from <a href="https://github.com/pytorch/vision/issues/3048#issuecomment-734262321">here</a> with a follow-up.</p>

mohit117: <p>Thanks for the link.</p>
<p>The Github code for <code>IntermediateLayerGetter</code> comes with this <a href="https://github.com/pytorch/vision/blob/4ef8e6bc73368c21654b9cae95fcfbab04777bf8/torchvision/models/_utils.py#L13" rel="noopener nofollow ugc">warning</a> and is repeated below for convenience,</p>
<pre><code class="lang-auto">It has a strong assumption that the modules have been registered
    into the model in the same order as they are used.
    This means that one should **not** reuse the same nn.Module
    twice in the forward if you want this to work.
</code></pre>
<p>Could you please elaborate this warning with possibly a very short dummy code where this approach should not be used.</p>
<p>Thank you.</p>