Project-MONAI / Project-MONAI/MONAI

RandSpatialCropSamplesd

Open
#7,828 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
8.7k
Forks
1.6k
Avg merge
5d 1h
Merged PRs (30d)
20

Description

Hi,I want to use batch_size = 2 for training. When using CenterSpatialCropd(keys=["image", "label"],roi_size=(224, 224, 224)), I can train the network, but when I use RandSpatialCropSamplesd(keys=["image", "label"],roi_size=(224, 224, 224), random_size=False, num_samples=2),ResizeWithPadOrCropd( keys=["image", "label"], spatial_size=224, mode='constant', ), the following error occurred.

collate/stack a list of tensors
E: stack expects each tensor to be equal size, but got [1, 275, 386, 386] at entry 0 and [1, 275, 380, 380] at entry 2, shape [torch.Size([1, 275, 386, 386]), torch.Size([1, 275, 386, 386]), torch.Size([1, 275, 380, 380]), torch.Size([1, 275, 380, 380])] in collate([tensor([[[[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]],

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     ...,

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]]]]), tensor([[[[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     ...,

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]]]]), tensor([[[[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     ...,

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]]]]), tensor([[[[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     ...,

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]],

     [[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]]]])])

collate dict key "image_meta_dict" out of 4 keys

collate dict key "sizeof_hdr" out of 43 keys

collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "extents" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "session_error" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "dim_info" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "dim" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "intent_p1" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "intent_p2" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "intent_p3" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "intent_code" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "datatype" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "bitpix" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "slice_start" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "pixdim" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "vox_offset" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "scl_slope" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "scl_inter" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "slice_end" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "slice_code" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "xyzt_units" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "cal_max" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "cal_min" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "slice_duration" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "toffset" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "glmax" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "glmin" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "qform_code" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "sform_code" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "quatern_b" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "quatern_c" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "quatern_d" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "qoffset_x" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "qoffset_y" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "qoffset_z" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "srow_x" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "srow_y" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "srow_z" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "affine" out of 43 keys
collate/stack a list of tensors
collate dict key "original_affine" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "as_closest_canonical" out of 43 keys
collate dict key "spatial_shape" out of 43 keys
collate/stack a list of numpy arrays
collate/stack a list of tensors
collate dict key "space" out of 43 keys
collate dict key "original_channel_dim" out of 43 keys
collate dict key "filename_or_obj" out of 43 keys
Traceback (most recent call last):
File "/mnt/store/zhengsch/Ostia_landmark/tools/train.py", line 572, in
Trainer().run()
File "/mnt/store/zhengsch/Ostia_landmark/tools/train.py", line 332, in run
self.train_step(epoch)
File "/mnt/store/zhengsch/Ostia_landmark/tools/train.py", line 392, in train_step
for i, batch_data in enumerate(self.trn_dl):
File "/opt/miniforge3/envs/monai/lib/python3.9/site-packages/torch/utils/data/dataloader.py", line 681, in next
data = self._next_data()
File "/opt/miniforge3/envs/monai/lib/python3.9/site-packages/torch/utils/data/dataloader.py", line 1376, in _next_data
return self._process_data(data)
File "/opt/miniforge3/envs/monai/lib/python3.9/site-packages/torch/utils/data/dataloader.py", line 1402, in _process_data
data.reraise()
File "/opt/miniforge3/envs/monai/lib/python3.9/site-packages/torch/_utils.py", line 461, in reraise
raise exception
RuntimeError: Caught RuntimeError in DataLoader worker process 0.
Original Traceback (most recent call last):
File "/home/zhengsch/.local/lib/python3.9/site-packages/monai/data/utils.py", line 516, in list_data_collate
ret = collate_fn(data)
File "/home/zhengsch/.local/lib/python3.9/site-packages/monai/data/utils.py", line 478, in collate_meta_tensor
return {k: collate_meta_tensor([d[k] for d in batch]) for k in elem_0}
File "/home/zhengsch/.local/lib/python3.9/site-packages/monai/data/utils.py", line 478, in
return {k: collate_meta_tensor([d[k] for d in batch]) for k in elem_0}
File "/home/zhengsch/.local/lib/python3.9/site-packages/monai/data/utils.py", line 483, in collate_meta_tensor
return default_collate(batch)
File "/opt/miniforge3/envs/monai/lib/python3.9/site-packages/torch/utils/data/utils/collate.py", line 140, in default_collate
out = elem.new(storage).resize
(len(batch), *list(elem.size()))
RuntimeError: Trying to resize storage that is not resizable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/opt/miniforge3/envs/monai/lib/python3.9/site-packages/torch/utils/data/_utils/worker.py", line 302, in _worker_loop
data = fetcher.fetch(index)
File "/opt/miniforge3/envs/monai/lib/python3.9/site-packages/torch/utils/data/_utils/fetch.py", line 52, in fetch
return self.collate_fn(data)
File "/home/zhengsch/.local/lib/python3.9/site-packages/monai/data/utils.py", line 696, in pad_list_data_collate
return PadListDataCollate(method=method, mode=mode, **kwargs)(batch)
File "/home/zhengsch/.local/lib/python3.9/site-packages/monai/transforms/croppad/batch.py", line 114, in call
return list_data_collate(batch)
File "/home/zhengsch/.local/lib/python3.9/site-packages/monai/data/utils.py", line 529, in list_data_collate
raise RuntimeError(re_str) from re
RuntimeError: Trying to resize storage that is not resizable

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported pipeline using RandSpatialCropSamplesd, ResizeWithPadOrCropd, and batch_size=2, then inspect the collate error and the differing tensor shapes. Determine the expected behavior for the sampled outputs and verify that the reported batch configuration either produces consistently shaped tensors or is clearly documented as unsupported.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
data, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.