如何修复''输入大小:(64x176x64)。计算的输出大小:(64x1x0)。输出尺寸太小

时间:2020-07-29 12:20:24

标签: python deep-learning pytorch

我正在尝试使用colab来训练一个类似segdecNet的github https://github.com/mengcius/Surface-Defect-Detection

        # SE layers
        self.fc1 = nn.Conv2d(64, 64//16, kernel_size=1)
        self.fc2 = nn.Conv2d(64//16, 64, kernel_size=1)    

        self.layer4 = nn.Sequential(
                            nn.Conv2d(64, 1024, 15, stride=1, padding=7),
                            nn.BatchNorm2d(1024),
                            nn.ReLU(inplace=True)
                        )

        self.layer5 = nn.Sequential(
                            nn.Conv2d(1024, 1, 1),
                            nn.ReLU(inplace=True)
                        )


        if init_weights == True:
            pass

    def forward(self, x):
        x1 = self.layer1(x)
        x2 = self.layer2(x1)
        x3 = self.layer3(x2)

        # Squeeze
        w = F.avg_pool2d(x3, x3.size(2))
        w = F.relu(self.fc1(w))
        w = torch.sigmoid(self.fc2(w))
        # Excitation
        x3 = x3 * w

        x4 = self.layer4(x3)
        x5 = self.layer5(x4)
        # print('x:', x4.shape, x5.shape) 

        return {"f":x4, "seg":x5}

我收到此错误:

Namespace(b1=0.5, b2=0.999, batch_size=2, begin_epoch=0, cuda=True, end_epoch=101, gpu_num=1, img_height=1408, img_width=512, lr=0.0001, need_save=True, need_test=True, save_interval=10, test_interval=10, worker_num=4)
Traceback (most recent call last):
  File "train_segment.py", line 140, in <module>
    rst = segment_net(img)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/content/models.py", line 87, in forward
    w = F.avg_pool2d(x3, x3.size(2))
RuntimeError: Given input size: (64x176x64). Calculated output size: (64x1x0). Output size is too small

我猜我在通道大小或池大小上犯了一个错误,但我不知道如何解决。我没有更改github中的数据集,但出现错误 我尝试更改torch1.2可以正常工作。但是我想知道我是否没有更改torch我该如何解决我的代码

1 个答案:

答案 0 :(得分:0)

您的平均池操作会导致您的输入减少为标量。

w = F.avg_pool2d(x3, x3.size(2))

之所以会这样,是因为平均池操作的第二个参数决定了输出尺寸将减小多少倍。您基本上是将大小除以自身。尝试改用较小的数字(例如2)。

w = F.avg_pool2d(x3, 2)

编辑:

根据您得到的错误

x3 = x3 * w RuntimeError: The size of tensor a (32) must 
match the size of tensor b (16) at non-singleton 
dimension 3

我建议完全不使用平均池。只是尝试将所有注释一起注释掉。因为似乎您应该匹配x3张量的大小。