LeeJunHyun / Image_Segmentation

Pytorch implementation of U-Net, R2U-Net, Attention U-Net, and Attention R2U-Net.
2.68k stars 595 forks source link

if img_ch =3,RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0 #38

Closed mk123qwe closed 1 year ago

mk123qwe commented 5 years ago

if img_ch =3,RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0

If img_ch =3 ,sometimes this error occurs.

more detail information: batchsize = 1,the code can work batchsize = 4,it causes the problem ,RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0

linzhenyuyuchen commented 4 years ago

Downsample size is inconsistent with upsample size. Solution: line 58:

def forward(self, x1, x2):
        x1 = self.up(x1)
        # input is CHW
        diffY = x2.size()[2] - x1.size()[2]
        diffX = x2.size()[3] - x1.size()[3]

        x1 = F.pad(x1, [diffX // 2, diffX - diffX // 2,
                        diffY // 2, diffY - diffY // 2]) 
        x = torch.cat([x2, x1], dim = 1)
        return x

forward in U_net:

    def forward(self, x):
        # encoding path
        x1 = self.Conv1(x)

        x2 = self.Maxpool(x1)
        x2 = self.Conv2(x2)

        x3 = self.Maxpool(x2)
        x3 = self.Conv3(x3)

        x4 = self.Maxpool(x3)
        x4 = self.Conv4(x4)

        x5 = self.Maxpool(x4)
        x5 = self.Conv5(x5)

        # decoding + concat path
        d5 = self.Up5(x5,x4)
        d5 = self.Up_conv5(d5)

        d4 = self.Up4(d5,x3)
        d4 = self.Up_conv4(d4)

        d3 = self.Up3(d4,x2)
        d3 = self.Up_conv3(d3)

        d2 = self.Up2(d3,x1)
        d2 = self.Up_conv2(d2)

        d1 = self.Conv_1x1(d2)

        return d1