bug for "RandCropByLabelClassesd" on multiple datapoints for each batch #6015
Replies: 3 comments
-
Hi @DorisxinDU, from the error message, it looks like you didn't specify the length of the ratios or indices correctly. |
Beta Was this translation helpful? Give feedback.
-
Hi @KumoLiu , thanks for reply. Please see the following error using classes based error:ValueError Traceback (most recent call last) File ~/anaconda3/envs/transform/lib/python3.9/site-packages/monai/transforms/croppad/dictionary.py:999, in RandCropByLabelClassesd.call(self, data) File ~/anaconda3/envs/transform/lib/python3.9/site-packages/monai/transforms/croppad/dictionary.py:991, in RandCropByLabelClassesd.randomize(self, label, indices, image) File ~/anaconda3/envs/transform/lib/python3.9/site-packages/monai/transforms/croppad/array.py:1219, in RandCropByLabelClasses.randomize(self, label, indices, image) File ~/anaconda3/envs/transform/lib/python3.9/site-packages/monai/transforms/utils.py:575, in generate_label_classes_crop_centers(spatial_size, num_samples, label_spatial_shape, indices, ratios, rand_state, allow_smaller) File ~/anaconda3/envs/transform/lib/python3.9/site-packages/monai/transforms/utils.py:444, in correct_crop_centers(centers, spatial_size, label_spatial_shape, allow_smaller) File ~/anaconda3/envs/transform/lib/python3.9/site-packages/monai/utils/misc.py:201, in fall_back_tuple(user_provided, default, func) File ~/anaconda3/envs/transform/lib/python3.9/site-packages/monai/utils/misc.py:158, in ensure_tuple_rep(tup, dim) ValueError: Sequence must have length 4, got 3. |
Beta Was this translation helpful? Give feedback.
-
Hi @DorisxinDU, I see your questions. I think 8 is your batch size. The images and labels should be channel-first. MONAI/monai/transforms/croppad/array.py Lines 1236 to 1237 in 11745a6
Hope it can help you, thanks! Just convert it to discussions. |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
i am trying to use RandCropByLabelClassesd for image and label with following shape, but shows below bug. please can you help me. thanks.
(8, 1, 224, 224, 64) (8, 9, 224, 224, 64)
(8, 1, 224, 224, 64) (8, 9, 224, 224, 64)
(8, 1, 224, 224, 64) (8, 9, 224, 224, 64)
ValueError Traceback (most recent call last)
Cell In [110], line 12
8 # data=resizer(data)
9 # data=resizer2(data)
10 # data=reshape(data)
11 print(data['image'].shape,data['label'].shape)
---> 12 result = cropper(data)
13 print(result)
File ~/anaconda3/envs/transform/lib/python3.9/site-packages/monai/transforms/croppad/dictionary.py:999, in RandCropByLabelClassesd.call(self, data)
996 image = d[self.image_key] if self.image_key else None
997 indices = d.pop(self.indices_key, None) if self.indices_key is not None else None
--> 999 self.randomize(label, indices, image)
1001 # initialize returned list with shallow copy to preserve key ordering
1002 ret: List = [dict(d) for _ in range(self.cropper.num_samples)]
File ~/anaconda3/envs/transform/lib/python3.9/site-packages/monai/transforms/croppad/dictionary.py:991, in RandCropByLabelClassesd.randomize(self, label, indices, image)
988 def randomize(
989 self, label: torch.Tensor, indices: Optional[List[NdarrayOrTensor]] = None, image: Optional[torch.Tensor] = None
990 ) -> None:
--> 991 self.cropper.randomize(label=label, indices=indices, image=image)
File ~/anaconda3/envs/transform/lib/python3.9/site-packages/monai/transforms/croppad/array.py:1219, in RandCropByLabelClasses.randomize(self, label, indices, image)
1217 else:
1218 indices_ = indices
-> 1219 self.centers = generate_label_classes_crop_centers(
1220 self.spatial_size, self.num_samples, label.shape[1:], indices_, self.ratios, self.R, self.allow_smaller
1221 )
File ~/anaconda3/envs/transform/lib/python3.9/site-packages/monai/transforms/utils.py:558, in generate_label_classes_crop_centers(spatial_size, num_samples, label_spatial_shape, indices, ratios, rand_state, allow_smaller)
556 ratios_: List[Union[float, int]] = ([1] * len(indices)) if ratios is None else ratios
557 if len(ratios_) != len(indices):
--> 558 raise ValueError("random crop ratios must match the number of indices of classes.")
559 if any(i < 0 for i in ratios_):
560 raise ValueError("ratios should not contain negative number.")
ValueError: random crop ratios must match the number of indices of classes.
Beta Was this translation helpful? Give feedback.
All reactions