Skip to content

Sizes of tensors must match except in dimension 4. Got 18 and 17 #1754

@itruonghai

Description

@itruonghai

I try to train Monai with Unet (the architecture is the same as the tutorial at
https://github.com/Project-MONAI/tutorials/blob/master/3d_segmentation/spleen_segmentation_3d.ipynb

Please help me to fix this bug

Traceback (most recent call last):
  File "/home/pqdung/segmentation_3d.py", line 247, in <module>
    outputs = model(inputs)
  File "/home/pqdung/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/pqdung/.local/lib/python3.6/site-packages/monai/networks/nets/unet.py", line 189, in forward
    x = self.model(x)
  File "/home/pqdung/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/pqdung/.local/lib/python3.6/site-packages/torch/nn/modules/container.py", line 119, in forward
    input = module(input)
  File "/home/pqdung/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/pqdung/.local/lib/python3.6/site-packages/monai/networks/layers/simplelayers.py", line 125, in forward
    y = self.submodule(x)
  File "/home/pqdung/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/pqdung/.local/lib/python3.6/site-packages/torch/nn/modules/container.py", line 119, in forward
    input = module(input)
  File "/home/pqdung/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/pqdung/.local/lib/python3.6/site-packages/monai/networks/layers/simplelayers.py", line 125, in forward
    y = self.submodule(x)
  File "/home/pqdung/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/pqdung/.local/lib/python3.6/site-packages/torch/nn/modules/container.py", line 119, in forward
    input = module(input)
  File "/home/pqdung/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/pqdung/.local/lib/python3.6/site-packages/monai/networks/layers/simplelayers.py", line 128, in forward
    return torch.cat([x, y], dim=self.dim)
RuntimeError: Sizes of tensors must match except in dimension 4. Got 18 and 17 (The offending index is 0)

 

Process finished with exit code 1

The U-net architecture is defined as

# standard PyTorch program style: create UNet, DiceLoss and Adam optimizer
device = torch.device("cuda:0")
model = UNet(
    dimensions=3,
    in_channels=1,
    out_channels=2,
    channels=(16, 32, 64, 128, 256),
    strides=(2, 2, 2, 2),
    num_res_units=2,
    norm=Norm.BATCH,
).to(device)
loss_function = DiceLoss(to_onehot_y=True, softmax=True)
optimizer = torch.optim.Adam(model.parameters(), 1e-4)

I try to segment 1 class (tumor only)

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions