Skip to content

RandomWeightedCrop doesn't randomly generate crops for a weightmap of integers #7949

@gregspangenberg

Description

@gregspangenberg

Describe the bug
monai.transforms.RandomWeightedCrop uses the function monai.transforms.utils.weighted_patch_samples. At line 576 in utils the weighted_patch_samples function converts the datatype of the randomly generate number in the range of (0,1) to match the datatype of the input weightmap.
"""
r, *_ = convert_to_dst_type(r_state.random(n_samples), v)
"""
If the weightmap consisted of integers then the random value will always be the integer of 0, which disables random sampling and instead samples from the first valid region continuously.

To Reproduce
Steps to reproduce the behavior:

  1. Create a numpy weightmap with integer datatype
  2. Pass weightmap into RandomWeightedCrop
  3. Observe the "crop_center" value in the meta data of the cropped image after multiple iterations it will remain constant

Expected behavior
The random factor "r" should never be an converted to an integer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions