Randomcropbyposneglabel issues with dimensions.. the size of tensor a (118210248) must match the size of tensor b (101117952) at non-singleton dimension 0 monai randcropposneg error #6783
-
from monai.utils import first, set_determinism from monai.handlers import TensorBoardStatsHandler from monai.networks.nets import UNet data_dir = “/home/hpc/iwi5/iwi5131h” train_images = sorted(glob.glob(os.path.join(data_dir, “traindata”, “.nii.gz"))) val_images = sorted(glob.glob(os.path.join(data_dir, ‘valdata’, ‘.nii.gz’))) train_files = [{“image”: image_name, ‘label’: label_name} for image_name, label_name in zip(train_images, train_labels)] train_transforms = Compose( user can also add other random transformsRandAffined(keys=[‘image’, ‘label’],mode=(‘bilinear’, ‘nearest’),prob=1.0, spatial_size=(96, 96, 96),rotate_range=(0, 0, np.pi/15),scale_range=(0.1, 0.1, 0.1)),] train_loader = DataLoader(train_ds, batch_size=1, shuffle=True, num_workers=0) val_ds = CacheDataset( val_loader = DataLoader(val_ds, batch_size=1, num_workers=0) device = torch.device(“cuda:0”) dice_metric = DiceMetric(include_background=False, reduction=“mean”) surf_metric = HausdorffDistanceMetric(include_background=False, distance_metric=‘euclidean’, reduction=“mean”, max_epochs = 1000 for epoch in range(max_epochs): print(“Size of inputs[1] :”, inputs[1].shape)print(“printing of inputs :”, inputs)outputs = model(inputs) if (epoch + 1) % val_interval == 0:
print( import numpy as np np.save(‘loss_resunet_trained_010102.npy’, epoch_loss_values) # save ERROR The above exception was the direct cause of the following exception: Traceback (most recent call last):
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Hi @ibrahimmanzoor, could you please check the data shape before Hope it helps, thanks! |
Beta Was this translation helpful? Give feedback.
Hi @ibrahimmanzoor, it's not that
RandCropByPosNegLabel
can't fix the size for you; it's that your inputs have different sizes, which causes the error to be reported when you get fg and bg indices. Second, it is true thatspatial_size
does not guarantee that the output will be the same, and you must usually follow it with aResizeWithPadorCrop
.MONAI/monai/transforms/croppad/array.py
Lines 1075 to 1079 in 11546e8