Error during forward pass: Sizes of tensors must match except in dimension 1. Expected size 39 but got size 40 for tensor number 1 in the list

import os
from glob import glob
import shutil
from tqdm import tqdm
import dicom2nifti
import numpy as np
import nibabel as nib
from monai.transforms import (
Compose,
EnsureChannelFirstD,
LoadImaged,
Resized,
ToTensord,
Spacingd,
Orientationd,
ScaleIntensityRanged,
CropForegroundd,

)
from monai.data import DataLoader, Dataset, CacheDataset
from monai.utils import set_determinism
from monai.utils import first
import matplotlib.pyplot as plt

def prepare(in_dir, pixdim=(1.5, 1.5, 1.0), a_min=0, a_max=2235.0, spatial_size=[128, 128, 155], cache=True):
“”"
This function is for preprocessing, it contains only the basic transforms, but you can add more operations that you
find in the Monai documentation.
MONAI - Docs
“”"

set_determinism(seed=0)

path_train_volumes = sorted(glob(os.path.join(in_dir, "Train_Data", "*.nii.gz")))
path_train_segmentation = sorted(glob(os.path.join(in_dir, "Train_Label", "*.nii.gz")))

path_test_volumes = sorted(glob(os.path.join(in_dir, "Test_Data", "*.nii.gz")))
path_test_segmentation = sorted(glob(os.path.join(in_dir, "Test_Label", "*.nii.gz")))

train_files = [{"vol": image_name, "seg": label_name} for image_name, label_name in
               zip(path_train_volumes, path_train_segmentation)]
test_files = [{"vol": image_name, "seg": label_name} for image_name, label_name in
              zip(path_test_volumes, path_test_segmentation)]

train_transforms = Compose(
    [
        LoadImaged(keys=["vol", "seg"]),
        EnsureChannelFirstD(keys=["vol", "seg"]),
        Spacingd(keys=["vol", "seg"], pixdim=pixdim, mode=("bilinear", "nearest")),
        Orientationd(keys=["vol", "seg"], axcodes="RAS"),
        ScaleIntensityRanged(keys=["vol"], a_min=a_min, a_max=a_max, b_min=0.0, b_max=1.0, clip=True),
        CropForegroundd(keys=["vol", "seg"], source_key="vol"),
        Resized(keys=["vol", "seg"], spatial_size=spatial_size),
        ToTensord(keys=["vol", "seg"]),

    ]
)

test_transforms = Compose(
    [
        LoadImaged(keys=["vol", "seg"]),
        EnsureChannelFirstD(keys=["vol", "seg"]),
        Spacingd(keys=["vol", "seg"], pixdim=pixdim, mode=("bilinear", "nearest")),
        Orientationd(keys=["vol", "seg"], axcodes="RAS"),
        ScaleIntensityRanged(keys=["vol"], a_min=a_min, a_max=a_max, b_min=0.0, b_max=1.0, clip=True),
        CropForegroundd(keys=['vol', 'seg'], source_key='vol'),
        Resized(keys=["vol", "seg"], spatial_size=spatial_size),
        ToTensord(keys=["vol", "seg"]),

    ]
)

if cache:
    train_ds = CacheDataset(data=train_files, transform=train_transforms, cache_rate=1.0)
    train_loader = DataLoader(train_ds, batch_size=1)

    test_ds = CacheDataset(data=test_files, transform=test_transforms, cache_rate=1.0)
    test_loader = DataLoader(test_ds, batch_size=1)

    return train_loader, test_loader

else:
    train_ds = Dataset(data=train_files, transform=train_transforms)
    train_loader = DataLoader(train_ds, batch_size=1)

    test_ds = Dataset(data=test_files, transform=test_transforms)
    test_loader = DataLoader(test_ds, batch_size=1)

    return train_loader, test_loader

from monai.utils import first
import matplotlib.pyplot as plt
import torch
import os
import numpy as np
from monai.losses import DiceLoss
from tqdm import tqdm

def dice_metric(predicted, target):
‘’’
In this function we take predicted and target (label) to calculate the dice coeficient then we use it
to calculate a metric value for the training and the validation.
‘’’
dice_value = DiceLoss(to_onehot_y=True, sigmoid=True, squared_pred=True)
value = 1 - dice_value(predicted, target).item()
return value

def calculate_weights(val1, val2):
‘’’
In this function we take the number of the background and the forgroud pixels to return the weights
for the cross entropy loss values.
‘’’
count = np.array([val1, val2])
summ = count.sum()
weights = count/summ
weights = 1/weights
summ = weights.sum()
weights = weights/summ
return torch.tensor(weights, dtype=torch.float32)

def train(model, data_in, loss, optim, max_epochs, model_dir, test_interval=1 , device=torch.device(“cpu”)):
best_metric = -1
best_metric_epoch = -1
save_loss_train =
save_loss_test =
save_metric_train =
save_metric_test =
train_loader, test_loader = data_in

for epoch in range(max_epochs):
    print("-" * 10)
    print(f"epoch {epoch + 1}/{max_epochs}")
    model.train()
    train_epoch_loss = 0
    train_step = 0
    epoch_metric_train = 0
    for batch_data in train_loader:
        
        train_step += 1

        volume = batch_data["vol"]
        label = batch_data["seg"]
        label = label != 0
        volume, label = (volume.to(device), label.to(device))

        optim.zero_grad()
        outputs = model(volume)
        
        train_loss = loss(outputs, label)
        
        train_loss.backward()
        optim.step()

        train_epoch_loss += train_loss.item()
        print(
            f"{train_step}/{len(train_loader) // train_loader.batch_size}, "
            f"Train_loss: {train_loss.item():.4f}")

        train_metric = dice_metric(outputs, label)
        epoch_metric_train += train_metric
        print(f'Train_dice: {train_metric:.4f}')

    print('-'*20)
    
    train_epoch_loss /= train_step
    print(f'Epoch_loss: {train_epoch_loss:.4f}')
    save_loss_train.append(train_epoch_loss)
    np.save(os.path.join(model_dir, 'loss_train.npy'), save_loss_train)
    
    epoch_metric_train /= train_step
    print(f'Epoch_metric: {epoch_metric_train:.4f}')

    save_metric_train.append(epoch_metric_train)
    np.save(os.path.join(model_dir, 'metric_train.npy'), save_metric_train)

    if (epoch + 1) % test_interval == 0:

        model.eval()
        with torch.no_grad():
            test_epoch_loss = 0
            test_metric = 0
            epoch_metric_test = 0
            test_step = 0

            for test_data in test_loader:

                test_step += 1

                test_volume = test_data["vol"]
                test_label = test_data["seg"]
                test_label = test_label != 0
                test_volume, test_label = (test_volume.to(device), test_label.to(device),)
                
                test_outputs = model(test_volume)
                
                test_loss = loss(test_outputs, test_label)
                test_epoch_loss += test_loss.item()
                test_metric = dice_metric(test_outputs, test_label)
                epoch_metric_test += test_metric
                
            
            test_epoch_loss /= test_step
            print(f'test_loss_epoch: {test_epoch_loss:.4f}')
            save_loss_test.append(test_epoch_loss)
            np.save(os.path.join(model_dir, 'loss_test.npy'), save_loss_test)

            epoch_metric_test /= test_step
            print(f'test_dice_epoch: {epoch_metric_test:.4f}')
            save_metric_test.append(epoch_metric_test)
            np.save(os.path.join(model_dir, 'metric_test.npy'), save_metric_test)

            if epoch_metric_test > best_metric:
                best_metric = epoch_metric_test
                best_metric_epoch = epoch + 1
                torch.save(model.state_dict(), os.path.join(
                    model_dir, "best_metric_model.pth"))
            
            print(
                f"current epoch: {epoch + 1} current mean dice: {test_metric:.4f}"
                f"\nbest mean dice: {best_metric:.4f} "
                f"at epoch: {best_metric_epoch}"
            )


print(
    f"train completed, best_metric: {best_metric:.4f} "
    f"at epoch: {best_metric_epoch}")

def show_patient(data, SLICE_NUMBER=1, train=True, test=False):
“”"
This function is to show one patient from your datasets, so that you can si if the it is okay or you need
to change/delete something.

`data`: this parameter should take the patients from the data loader, which means you need to can the function
prepare first and apply the transforms that you want after that pass it to this function so that you visualize 
the patient with the transforms that you want.
`SLICE_NUMBER`: this parameter will take the slice number that you want to display/show
`train`: this parameter is to say that you want to display a patient from the training data (by default it is true)
`test`: this parameter is to say that you want to display a patient from the testing patients.
"""

check_patient_train, check_patient_test = data

view_train_patient = first(check_patient_train)
view_test_patient = first(check_patient_test)


if train:
    plt.figure("Visualization Train", (12, 6))
    plt.subplot(1, 2, 1)
    plt.title(f"vol {SLICE_NUMBER}")
    plt.imshow(view_train_patient["vol"][0, 0, :, :, SLICE_NUMBER], cmap="gray")

    plt.subplot(1, 2, 2)
    plt.title(f"seg {SLICE_NUMBER}")
    plt.imshow(view_train_patient["seg"][0, 0, :, :, SLICE_NUMBER])
    plt.show()

if test:
    plt.figure("Visualization Test", (12, 6))
    plt.subplot(1, 2, 1)
    plt.title(f"vol {SLICE_NUMBER}")
    plt.imshow(view_test_patient["vol"][0, 0, :, :, SLICE_NUMBER], cmap="gray")

    plt.subplot(1, 2, 2)
    plt.title(f"seg {SLICE_NUMBER}")
    plt.imshow(view_test_patient["seg"][0, 0, :, :, SLICE_NUMBER])
    plt.show()

def calculate_pixels(data):
val = np.zeros((1, 2))

for batch in tqdm(data):
    batch_label = batch["seg"] != 0
    _, count = np.unique(batch_label, return_counts=True)

    if len(count) == 1:
        count = np.append(count, 0)
    val += count

print('The last values:', val)
return val

from monai.networks.nets import UNet
from monai.networks.layers import Norm
from monai.losses import DiceLoss, DiceCELoss

import torch

data_dir = ‘D:/New folder (5)/Task01_BrainTumour/New folder (2)’
model_dir = ‘D:/New folder (5)/Task01_BrainTumour/New folder (2)’
data_in = prepare(data_dir, cache=True)

device = torch.device(“cpu”)
model = UNet(
spatial_dims=3,
in_channels=4,
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 = DiceCELoss(to_onehot_y=True, sigmoid=True, squared_pred=True, ce_weight=calculate_weights(1792651250,2510860).to(device))
loss_function = DiceLoss(to_onehot_y=True, sigmoid=True, squared_pred=True)
optimizer = torch.optim.Adam(model.parameters(), 1e-5, weight_decay=1e-5, amsgrad=True)

if name == ‘main’:
train(model, data_in, loss_function, optimizer, 600, model_dir)

I am getting this errior
Sample segmentation dimensions: torch.Size([1, 1, 128, 128, 155])
Sample volume dimensions: torch.Size([1, 4, 128, 128, 155])
Sample segmentation dimensions: torch.Size([1, 1, 128, 128, 155])
Checking dimensions for Training:
Training volume dimensions: torch.Size([1, 4, 128, 128, 155])
Training label dimensions: torch.Size([1, 1, 128, 128, 155])
Checking dimensions for Testing:
Testing volume dimensions: torch.Size([1, 4, 128, 128, 155])
Testing label dimensions: torch.Size([1, 1, 128, 128, 155])
Train volume dimensions: torch.Size([1, 4, 128, 128, 155])
Train label dimensions: torch.Size([1, 1, 128, 128, 155])
Train volume dimensions: torch.Size([1, 4, 128, 128, 155])
Train label dimensions: torch.Size([1, 1, 128, 128, 155])

epoch 1/600
Error during forward pass: Sizes of tensors must match except in dimension 1. Expected size 39 but got size 40 for tensor number 1 in the list.

What is this, which course are you taking? You need to post under the course to get any help from mentors here!

I am making a 3d unet tumor detection model using monai and pytorch.It isnt from a specific course.Its for my own learning and understanding.

1 Like