RuntimeError: adaptive_max_pool2d

Hi all, I’m using MONAI to build a U-Net for a segmentation task. I want to crop the input images with “CropForegroundd” as follows:

    monai_load = [
      LoadImaged(keys=["image","segmentation"],image_only=False,reader=PILReader()),
      EnsureTyped(keys=["image", "segmentation"], data_type="numpy"),
      AddChanneld(keys=["segmentation","image"]),
      RepeatChanneld(keys=["image"],repeats=3),
      AsChannelFirstd(keys=["image"], channel_dim = 0), 
      ]
      
    monai_transforms =[
      AsDiscreted(keys=["segmentation"],threshold=0.5),
      CropForegroundd(keys=["image","segmentation"], source_key="image", margin=20),
      Resized(keys=["image","segmentation"],spatial_size=(240,240)),
      ToTensord(keys=["image","segmentation"]),
      ]
      
    train_transforms = Compose(monai_load  + monai_transforms)
    train_ds = IterableDataset(data = train_data, transform = train_transforms)
    train_loader = DataLoader(dataset = train_ds, batch_size = batch_size, num_workers = 0, pin_memory = True)

During training, it stops everytime at 7th iteration (no matter starting from 0) and returns the following error:

RuntimeError: adaptive_avg_pool2d(): Expected input to have non-zero size for non-batch dimensions, but input has sizes [1, 3, 0, 0] with dimension 2 being empty

My input images are MRI images (240x240).

Thanks in advance for your feedback

Federico