RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 48 but got size 47 for tensor number 1 in the list

Hassan_Ghavidel: <p>Hi, I am training 181 images with .png format for the purpose of image registration.<br>
and received this error:</p>
<blockquote>
<p>File “/project/med/Hassan_Ghavidel/transformer_target_localization/code/main_train_unsup_TransMorph.py”, line 228, in <br>
train_val_test.train_val_model_unsupervised(model, train_loader, optimizer, config.loss_name, config.loss_weights,<br>
File “/project/med/Hassan_Ghavidel/transformer_target_localization/code/auxiliary/train_val_test.py”, line 61, in train_val_model_unsupervised<br>
train_outputs, train_ddf = model(torch.cat((train_inputs, train_targets), dim=1))<br>
File “/project/med/Hassan_Ghavidel/TransMorph_Transformer_for_Medical_Image_Registration/myenv/lib/python3.8/site-packages/torch/nn/modules/module.py”, line 1194, in _call_impl<br>
return forward_call(*input, **kwargs)<br>
File “/project/med/Hassan_Ghavidel/transformer_target_localization/code/models/TransMorph.py”, line 838, in forward<br>
out = self.up1(out, f2)<br>
File “/project/med/Hassan_Ghavidel/TransMorph_Transformer_for_Medical_Image_Registration/myenv/lib/python3.8/site-packages/torch/nn/modules/module.py”, line 1194, in _call_impl<br>
return forward_call(*input, **kwargs)<br>
File “/project/med/Hassan_Ghavidel/transformer_target_localization/code/models/TransMorph.py”, line 707, in forward<br>
x = torch.cat([x, skip], dim=1)<br>
File “/project/med/Hassan_Ghavidel/TransMorph_Transformer_for_Medical_Image_Registration/myenv/lib/python3.8/site-packages/monai/data/meta_tensor.py”, line 282, in <strong>torch_function</strong><br>
ret = super().<strong>torch_function</strong>(func, types, args, kwargs)<br>
File “/project/med/Hassan_Ghavidel/TransMorph_Transformer_for_Medical_Image_Registration/myenv/lib/python3.8/site-packages/torch/_tensor.py”, line 1279, in <strong>torch_function</strong><br>
ret = func(*args, *<em>kwargs)<br>
RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 48 but got size 47 for tensor number 1 in the list.</em></p>
</blockquote>
<p>the line that code is complaining is<br>
<strong>x = torch.cat([x, skip], dim=1)</strong><br>
located in the following picture:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/8/28eb05c908198f6e9bcdb486dc740e53c9bda762.png" data-download-href="https://discuss.pytorch.org/uploads/default/28eb05c908198f6e9bcdb486dc740e53c9bda762" title="Screenshot from 2024-08-03 19-32-56"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/8/28eb05c908198f6e9bcdb486dc740e53c9bda762_2_690x490.png" alt="Screenshot from 2024-08-03 19-32-56" data-base62-sha1="5PYDcZQww5MziNI6Gw3f2mVKwDg" width="690" height="490" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/8/28eb05c908198f6e9bcdb486dc740e53c9bda762_2_690x490.png, https://discuss.pytorch.org/uploads/default/optimized/3X/2/8/28eb05c908198f6e9bcdb486dc740e53c9bda762_2_1035x735.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/2/8/28eb05c908198f6e9bcdb486dc740e53c9bda762.png 2x" data-dominant-color="232423"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2024-08-03 19-32-56</span><span class="informations">1285×914 124 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>May you give me a suggestion to solve this issue?</p>

OEtzi007: <p>Hi Hassan,<br>
it seems your two tensors <code>x</code> and <code>skip</code> do not match up in their dimensions/shape.<br>
You can check those in the debugger by hovering over their variable names. From the error message I suspect that <code>x</code> has a shape of <code>[48, n, ...]</code> and <code>skip</code> has a shape of <code>[47, m, ...]</code>. When you <code>cat</code> two tensors along dimensions <code>1</code>, that means all other dimensions (specifically dimension 0) need to match in size, e.g. <code>skip</code> should also have shape <code>[48, m, ...]</code> such that the resulting tensor has shape <code>[48, n+m, ...]</code>.<br>
Perhaps you want to share the shapes of <code>x</code> and <code>skip</code> (and perhaps also from <code>x</code> before the <code>self.up</code> function call) in case you have still difficulties in understanding the problem.</p>

Hassan_Ghavidel: <p>Thanks OEtzi for your response.<br>
in main_train_unsup_TransMorph.py script i had written a class to make all input images with same channels: this is the function:</p>
<pre><code class="lang-auto">class EnsureSingleChannel:
    def __init__(self, keys):
        self.keys = keys
    def __call__(self, data):
        for key in self.keys:
            img = data[key]
            if not isinstance(img, torch.Tensor):
                img = torch.tensor(img)
            if img.shape[0] &gt; 1:
                img = img[0:1]
            elif img.shape[0] &lt; 1:
                raise ValueError("Image has less than 1 channel, cannot be processed")
            data[key] = img
        return data
</code></pre>
<p>then i used it here:</p>
<pre><code class="lang-auto">train_transforms = Compose(
    [
        LoadImaged(keys=["fixed_image", "moving_image"]),
        EnsureChannelFirstd(keys=("fixed_image", "moving_image")),
        EnsureSingleChannel(keys=["fixed_image", "moving_image"]),
        ScaleIntensityRanged(
            keys=["fixed_image", "moving_image"],
            a_min=0,
            a_max=1000,
            b_min=0.0,
            b_max=1.0,
            clip=False,
        ),
</code></pre>
<p>if i dont use it i will receive imges with two different channels<br>
RuntimeError: Given groups=1, weight of size [48, 2, 3, 3], expected input[2, 4, 185, 185] to have 2 channels, but got 4 channels instead<br>
this EnsureSingleChannel function worked for some other input images that some of them were greyscales but for new set of images i receive : RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 48 but got size 47 for tensor number 1 in the list. error</p>
<p>i am thinking maybe i should define a different function for these new images…<br>
or do you think i should try to manually make <code>x</code> and <code>skip</code> tensors same size?</p>

ptrblck: <p>Double post from <a href="https://discuss.pytorch.org/t/sizes-of-tensors-must-match-except-in-dimension-1/147785/7">here</a> with follow up.</p>