简体   繁体   English

Pytorch 子模块 output 形状

[英]Pytorch submodules output shape

How does the output shape of submodules in pytorch is determined? pytorch中子模块的output形状是如何确定的? why is the output shape of a certain sub-module is modified in the code below?为什么在下面的代码中修改了某个子模块的 output 形状?

When I separate the head of a classical classifier from its backbone in the following way:当我通过以下方式将经典分类器的头部与其主干分开时:

import torch, torchvision
from torchsummary import summary

effnet = torchvision.models.efficientnet_b0(num_classes = 2)

backbone = torch.nn.Sequential(*(list(effnet.children())[0]))
adaptive_pool = list(effnet.children())[1]
head = list(effnet.children())[2]

model = torch.nn.Sequential(*[backbone, adaptive_pool, head])
summary(model, (3,256,256), device = 'cpu') # <== Error

I get the following error:我收到以下错误:

RuntimeError: mat1 and mat2 shapes cannot be multiplied (2560x1 and 1280x2)

This error is due to modified output shape of the sub-module adaptive_pool .此错误是由于修改了子模块adaptive_pool的 output 形状。 To workaround this problem, flatten can be used as follows:要解决此问题,可以按如下方式使用展平:

class flatten(torch.nn.Module):
    def forward(self, input):
        return input.view(input.size(0), -1)

model = torch.nn.Sequential(*[backbone, adaptive_pool,  flatten(), head])
summary(model, (3,256,256), device = 'cpu')  

Why is the output shape of the sub-module adaptive_pool is modified?为什么修改子模块adaptive_pool的output形状?

The output of an nn.AdaptiveAvgPool2d is 4D even if the average is computed globally i.e output_size=1 . nn.AdaptiveAvgPool2d 的nn.AdaptiveAvgPool2d是 4D,即使平均值是全局计算的,output_size=1 In other words, the output shape of your global pooling layer is (N, C, 1, 1) .换句话说,全局池化层的 output 形状是(N, C, 1, 1) This means you indeed need to flatten it for the layer which is fully connected.这意味着您确实需要为完全连接的层将其展平。

In the referenced original efficient.net classification.network, the implementation of the flattening operation is done directly in the forward logic without the use of a dedicated layer.在引用的原始efficient.net classification.network中,扁平化操作的实现直接在前向逻辑中完成,没有使用专用层。 See this line .看到这一行

Instead of implementing your own flattening layer, you can use the built-in nn.Flatten .您可以使用内置的nn.Flatten ,而不是实现自己的展平层。 More details about this module can be found here .可以在此处找到有关此模块的更多详细信息。

>>> model = nn.Sequential(backbone, adaptive_pool, nn.Flatten(1), head)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM