Project-MONAI / MONAI

AI Toolkit for Healthcare Imaging
https://monai.io/
Apache License 2.0
5.92k stars 1.09k forks source link

Add RandCropByPosPercent transform #586

Open Nic-Ma opened 4 years ago

Nic-Ma commented 4 years ago

Is your feature request related to a problem? Please describe. Currently, the balance sampling’s positive window is defined by the corresponding label of the window’s central location. We need the flexibility to define the positive windows by thresholding the percentage of positive pixels in a window instead of only central data. Consider as "positive" a patch that has some foreground voxels anywhere above percentage, and "negative" a patch that has none (rather than looking at the central voxel only).

Nic-Ma commented 4 years ago

Mark: @yiheng-wang-nv is working on this task, I will refactor the PosNegCrop related transforms when it's merged. Thanks.

Nic-Ma commented 3 years ago

Hi @wyli and @yiheng-wang-nv ,

I have a draft proposal in my simple mind: We can enhance or develop another utility for https://github.com/Project-MONAI/MONAI/blob/master/monai/transforms/utils.py#L191

  1. Computes convolution with the all 1 kernel(size = crop size, stride = 1) on the label data
  2. Then we get the "score" for every voxel as the crop center
  3. Use a percentage bar to define positive or negative centers, for example, 0% is negative, >50% is positive
  4. Cache the results, and randomly select center in RandCropByPosNegLabeld transform

Maybe we can convert to Tensor and execute on GPU to accelerate it?

Thanks.

ramchandracheke commented 3 years ago

Hi,

Thank you for this wonderful project.

File "/home/ramchand/.local/lib/python3.7/site-packages/monai/transforms/utils.py", line 334, in generate_pos_neg_label_crop_centers raise ValueError("The size of the proposed random crop ROI is larger than the image size.")

ValueError: The size of the proposed random crop ROI is larger than the image size. RuntimeError: applying transform <monai.transforms.croppad.dictionary.RandCropByPosNegLabeld object at 0x7ffecef93550>

Could you please help me to solve this error. as My image size is variable(no of slices are different in each nifti file.eg. (342, 342, 91) and I am using Unet. (128,128,128). I am using following combination of transform.

train_transforms = Compose( [ LoadImaged(keys=["image", "label"]), AddChanneld(keys=["image", "label"]), Spacingd(keys=["image", "label"], pixdim=( 1.5, 1.5, 1.0), mode=("bilinear", "nearest")), Orientationd(keys=["image", "label"], axcodes="RAS"), ScaleIntensityRanged( keys=["image"], a_min=-400, a_max=1500, b_min=0.0, b_max=1.0, clip=True, ), CropForegroundd(keys=["image", "label"], source_key="image"), RandCropByPosNegLabeld( keys=["image", "label"], label_key="label", spatial_size=(96, 96, 96), pos=1, neg=1, num_samples=4, image_key="image", image_threshold=0, ),ToTensord(keys=["image", "label"]), ] )

Thanks a million!

rijobro commented 3 years ago

hi @ramchandracheke, could you create a new discussion and we'll try and help you there: https://github.com/Project-MONAI/MONAI/discussions.

ramchandracheke commented 3 years ago

Thanks @rijobro for your reply. Yes I will create it.

luciaroccaro commented 2 years ago

Is your feature request related to a problem? Please describe. Currently, the balance sampling’s positive window is defined by the corresponding label of the window’s central location. We need the flexibility to define the positive windows by thresholding the percentage of positive pixels in a window instead of only central data. Consider as "positive" a patch that has some foreground voxels anywhere above percentage, and "negative" a patch that has none (rather than looking at the central voxel only).

Hi, I am having the same issue described above. I would like to crop patches containing a certain percentage of positive or negative labels, instead of considering the center voxel only. Has this been solved somehow? Thanks