Monai : RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 7 but got size 8 for tensor number 1 in the list
I am using Monai for the 3D Multilabel segmentation task. My input image size is 512x496x49 and my label size is 512x496x49. An Image can have 3 labels in one image. With transform, I have converted the image in size 1x512x512x49 and Label in 3x512x512x49
My Transform
# Setting tranform for train and test data
a_min=6732
a_max=18732

train_transform = Compose(
    [
      LoadImaged(keys=[&quot;image&quot;, &quot;label&quot;]),
      EnsureChannelFirstd(keys=&quot;image&quot;),
      ConvertToMultiChannelBasedOnBratsClassesd(keys=&quot;label&quot;),
      ScaleIntensityRanged(keys='image', a_min=a_min, a_max=a_max, b_min=0.0, b_max=1.0, clip=False),
      Orientationd(keys=[&quot;image&quot;, &quot;label&quot;], axcodes=&quot;RAS&quot;),
      # Spacingd(keys=[&quot;image&quot;, &quot;label&quot;], pixdim=(
      #     1.5, 1.5, 2.0), mode=(&quot;bilinear&quot;, &quot;nearest&quot;)),
      RandFlipd(keys=[&quot;image&quot;, &quot;label&quot;], prob=0.5, spatial_axis=0),
      RandFlipd(keys=[&quot;image&quot;, &quot;label&quot;], prob=0.5, spatial_axis=1),
      RandFlipd(keys=[&quot;image&quot;, &quot;label&quot;], prob=0.5, spatial_axis=2),
      CropForegroundd(keys=[&quot;image&quot;, &quot;label&quot;], source_key=&quot;image&quot;),
      NormalizeIntensityd(keys=&quot;image&quot;, nonzero=True, channel_wise=True),
      SpatialPadd(keys=['image', 'label'], spatial_size= [512, 512, 49]),# it will result in 512x512x49
      EnsureTyped(keys=[&quot;image&quot;, &quot;label&quot;]),
    ]
)
val_transform = Compose(
    [
      LoadImaged(keys=[&quot;image&quot;, &quot;label&quot;]),
      EnsureChannelFirstd(keys=&quot;image&quot;),
      ConvertToMultiChannelBasedOnBratsClassesd(keys=&quot;label&quot;),
      ScaleIntensityRanged(keys='image', a_min=a_min, a_max=a_max, b_min=0.0, b_max=1.0, clip=False),
      Orientationd(keys=[&quot;image&quot;, &quot;label&quot;], axcodes=&quot;RAS&quot;),
      # Spacingd(keys=[&quot;image&quot;, &quot;label&quot;], pixdim=(
      #     1.5, 1.5, 2.0), mode=(&quot;bilinear&quot;, &quot;nearest&quot;)),
      CropForegroundd(keys=[&quot;image&quot;, &quot;label&quot;], source_key=&quot;image&quot;),
      NormalizeIntensityd(keys=&quot;image&quot;, nonzero=True, channel_wise=True),
      SpatialPadd(keys=['image', 'label'], spatial_size= [512, 512, 49]),# it will result in 512x512x49
      EnsureTyped(keys=[&quot;image&quot;, &quot;label&quot;]),
    ]
)

Dataloader for training and val
train_ds = CacheDataset(data=train_files, transform=train_transform,cache_rate=1.0, num_workers=4)
train_loader = DataLoader(train_ds, batch_size=2, shuffle=True, num_workers=4,collate_fn=pad_list_data_collate)

val_ds = CacheDataset(data=val_files, transform=val_transform, cache_rate=1.0, num_workers=4)
val_loader = DataLoader(val_ds, batch_size=1, num_workers=4)

3D U-Net Network from Monai
# standard PyTorch program style: create UNet, DiceLoss and Adam optimizer

device = torch.device(&quot;cuda:0&quot;)
model = UNet(
    spatial_dims=3,
    in_channels=1,
    out_channels=4,
    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, sigmoid=True)
optimizer = torch.optim.Adam(model.parameters(), 1e-4)
dice_metric = DiceMetric(include_background=True, reduction=&quot;mean&quot;)

Training
max_epochs = 5
val_interval = 2
best_metric = -1
best_metric_epoch = -1
epoch_loss_values = []
metric_values = []
post_pred = Compose([EnsureType(), AsDiscrete(argmax=True, to_onehot=4)])
post_label = Compose([EnsureType(), AsDiscrete(to_onehot=4)])

for epoch in range(max_epochs):
    print(&quot;-&quot; * 10)
    print(f&quot;epoch {epoch + 1}/{max_epochs}&quot;)
    model.train()
    epoch_loss = 0
    step = 0
    for batch_data in train_loader:
        step += 1
        inputs, labels = (
            batch_data[&quot;image&quot;].to(device),
            batch_data[&quot;label&quot;].to(device),
        )
        optimizer.zero_grad()
        print(&quot;Size of inputs :&quot;, inputs.shape)
        print(&quot;Size of inputs[0] :&quot;, inputs[0].shape)
        # print(&quot;Size of inputs[1] :&quot;, inputs[1].shape)
        # print(&quot;printing of inputs :&quot;, inputs)
        outputs = model(inputs)
        loss = loss_function(outputs, labels)
        loss.backward()
        optimizer.step()
        epoch_loss += loss.item()
        print(
            f&quot;{step}/{len(train_ds) // train_loader.batch_size}, &quot;
            f&quot;train_loss: {loss.item():.4f}&quot;)
    epoch_loss /= step
    epoch_loss_values.append(epoch_loss)
    print(f&quot;epoch {epoch + 1} average loss: {epoch_loss:.4f}&quot;)

    if (epoch + 1) % val_interval == 0:
        model.eval()
        with torch.no_grad():
            for val_data in val_loader:
                val_inputs, val_labels = (
                    val_data[&quot;image&quot;].to(device),
                    val_data[&quot;label&quot;].to(device),
                )
                roi_size = (160, 160, 160)
                sw_batch_size = 4
                val_outputs = sliding_window_inference(
                    val_inputs, roi_size, sw_batch_size, model)
                val_outputs = [post_pred(i) for i in decollate_batch(val_outputs)]
                val_labels = [post_label(i) for i in decollate_batch(val_labels)]
                # compute metric for current iteration
                dice_metric(y_pred=val_outputs, y=val_labels)

            # aggregate the final mean dice result
            metric = dice_metric.aggregate().item()
            # reset the status for next validation round
            dice_metric.reset()

            metric_values.append(metric)
            if metric &gt; best_metric:
                best_metric = metric
                best_metric_epoch = epoch + 1
                torch.save(model.state_dict(), os.path.join(
                    root_dir, &quot;best_metric_model.pth&quot;))
                print(&quot;saved new best metric model&quot;)
            print(
                f&quot;current epoch: {epoch + 1} current mean dice: {metric:.4f}&quot;
                f&quot;\nbest mean dice: {best_metric:.4f} &quot;
                f&quot;at epoch: {best_metric_epoch}&quot;
            )

While training I am getting this error
RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 7 but got size 8 for tensor number 1 in the list.

I followed the 3D Segmentation Monai tutorial but this was only for 2 classes (including background) therefore I followed the discussion at https://github.com/Project-MONAI/MONAI/issues/415 but even though I changed what was recommended in this discussion still am getting errors while training.

Your images have a depth of 49, but due to the 4 downsampling steps, each with stride 2, your images need to be divisible by a factor of 2**4=16. Adding in DivisiblePadd([&quot;image&quot;, &quot;label&quot;], 16) should solve it.
