From f6eadce91d292e3b60dbd3a1ab64f9bdc60c5c08 Mon Sep 17 00:00:00 2001 From: Soumya Snigdha Kundu Date: Tue, 30 Jun 2026 17:26:53 +0100 Subject: [PATCH 1/2] Clarify RandWeightedCrop(d) weight map is not cropped (#7851) The w_key/weight_map is used only to compute patch sample locations and is left unchanged at its original spatial size. When the weight map is loaded as a separate key and the source images have different shapes, the uncropped maps have mismatched shapes and DataLoader collation fails. Document that the weight map is not cropped, and that w_key should be added to keys to obtain a cropped weight map aligned with the image patches. Signed-off-by: Soumya Snigdha Kundu --- monai/transforms/croppad/array.py | 1 + monai/transforms/croppad/dictionary.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/monai/transforms/croppad/array.py b/monai/transforms/croppad/array.py index b23fbac7d9..4b18c74b2d 100644 --- a/monai/transforms/croppad/array.py +++ b/monai/transforms/croppad/array.py @@ -965,6 +965,7 @@ class RandWeightedCrop(Randomizable, TraceableTransform, LazyTransform, MultiSam weight_map: weight map used to generate patch samples. The weights must be non-negative. Each element denotes a sampling weight of the spatial location. 0 indicates no sampling. It should be a single-channel array in shape, for example, `(1, spatial_dim_0, spatial_dim_1, ...)`. + The weight map is only used to compute the patch sample locations; it is not cropped itself. lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False. """ diff --git a/monai/transforms/croppad/dictionary.py b/monai/transforms/croppad/dictionary.py index 510ff72938..3bab4eb479 100644 --- a/monai/transforms/croppad/dictionary.py +++ b/monai/transforms/croppad/dictionary.py @@ -943,7 +943,11 @@ class RandWeightedCropd(Randomizable, MapTransform, LazyTransform, MultiSampleTr keys: keys of the corresponding items to be transformed. See also: :py:class:`monai.transforms.compose.MapTransform` w_key: key for the weight map. The corresponding value will be used as the sampling weights, - it should be a single-channel array in size, for example, `(1, spatial_dim_0, spatial_dim_1, ...)` + it should be a single-channel array in size, for example, `(1, spatial_dim_0, spatial_dim_1, ...)`. + The weight map is only used to compute the patch sample locations; it is not cropped itself. + To obtain the cropped weight map (e.g. to batch it alongside the image), include ``w_key`` in + ``keys`` so it is cropped with the same sample centers; otherwise it is passed through unchanged + at its original spatial size. spatial_size: the spatial size of the image patch e.g. [224, 224, 128]. If its components have non-positive values, the corresponding size of `img` will be used. num_samples: number of samples (image patches) to take in the returned list. From c319dec22ce9ae64e82284a7386b5e8b58792009 Mon Sep 17 00:00:00 2001 From: Soumya Snigdha Kundu Date: Tue, 30 Jun 2026 17:41:57 +0100 Subject: [PATCH 2/2] Fix "in size" to "with shape" in RandWeightedCropd w_key docstring Signed-off-by: Soumya Snigdha Kundu --- monai/transforms/croppad/dictionary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/transforms/croppad/dictionary.py b/monai/transforms/croppad/dictionary.py index 3bab4eb479..7c82fe065b 100644 --- a/monai/transforms/croppad/dictionary.py +++ b/monai/transforms/croppad/dictionary.py @@ -943,7 +943,7 @@ class RandWeightedCropd(Randomizable, MapTransform, LazyTransform, MultiSampleTr keys: keys of the corresponding items to be transformed. See also: :py:class:`monai.transforms.compose.MapTransform` w_key: key for the weight map. The corresponding value will be used as the sampling weights, - it should be a single-channel array in size, for example, `(1, spatial_dim_0, spatial_dim_1, ...)`. + it should be a single-channel array with shape, for example, `(1, spatial_dim_0, spatial_dim_1, ...)`. The weight map is only used to compute the patch sample locations; it is not cropped itself. To obtain the cropped weight map (e.g. to batch it alongside the image), include ``w_key`` in ``keys`` so it is cropped with the same sample centers; otherwise it is passed through unchanged