RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 1. Got 38 and 37 in dimension 3

Hello everybody,
I am trying to do image segmentation task, but I got an error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-1-1b5cddef5c98> in <module>
    172 
    173 if __name__ == "__main__":
--> 174     train()

<ipython-input-1-1b5cddef5c98> in train()
    133             model.train()
    134             optimizer.zero_grad()
--> 135             outputs = model(images)
    136             loss = cross_entropy2d(input=outputs, target=labels)
    137             loss.backward()

D:\Anaconda\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--> 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

D:\Anaconda\lib\site-packages\torch\nn\parallel\data_parallel.py in forward(self, *inputs, **kwargs)
    136     def forward(self, *inputs, **kwargs):
    137         if not self.device_ids:
--> 138             return self.module(*inputs, **kwargs)
    139         inputs, kwargs = self.scatter(inputs, kwargs, self.device_ids)
    140         if len(self.device_ids) == 1:

D:\Anaconda\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--> 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

~\Desktop\unet\unet.py in forward(self, x)
    124         print(center)
    125 
--> 126         dec5 = self.dec5(torch.cat([center, conv5], 1))
    127         dec4 = self.dec4(torch.cat([dec5,   conv4], 1))
    128         dec3 = self.dec3(torch.cat([dec4,   conv3], 1))

RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 1. Got 38 and 37 in dimension 3 at c:\a\w\1\s\tmp_conda_3.7_061434\conda\conda-bld\pytorch_1544163540495\work\aten\src\th\generic/THTensorMoreMath.cpp:1333

Here is whrer the error located:

def forward(self, x):
    conv1 = self.conv1(x)
    conv2 = self.conv2(self.pool(conv1))
    conv3 = self.conv3(self.pool(conv2))
    conv4 = self.conv4(self.pool(conv3))
    conv5 = self.conv5(self.pool(conv4))

    center = self.center(self.pool(conv5))
    
    print(conv5)
    print(center)

    dec5 = self.dec5(torch.cat([center, conv5], 1))
    dec4 = self.dec4(torch.cat([dec5,   conv4], 1))
    dec3 = self.dec3(torch.cat([dec4,   conv3], 1))
    dec2 = self.dec2(torch.cat([dec3,   conv2], 1))
    dec1 = self.dec1(torch.cat([dec2,   conv1], 1))

    #if self.num_classes > 1:
    #    x_out = F.log_softmax(self.final(dec1), dim=1)
    #else:
    x_out = self.final(dec1)

    return x_out

any idea for this error?
Thanks for every one!

I don’t know what’s wrong, but here’s a general approach for debugging:

  1. find the exact line in the forward pass where this is wrong
  2. Think about the semantics of the model: are the input tensors to the function correctly shaped?
  3. Print out sizes for each tensor before and after each op in the forward pass until something looks fishy