Skip to content

Add clip_range args in ShiftIntensity and AdjustContrast - #5535

Closed
KumoLiu wants to merge 2 commits into
Project-MONAI:devfrom
KumoLiu:clip-args
Closed

KumoLiu wants to merge 2 commits into
Project-MONAI:devfrom
KumoLiu:clip-args

Conversation

@KumoLiu

@KumoLiu KumoLiu commented Nov 16, 2022 •

Copy link
Copy Markdown
Contributor

Signed-off-by: KumoLiu [email protected]

Part of #5517.

Description

If we have an RGB image, after shifting intensity, the intensity will go beyond the original range. Users may want to clip the data to the 0-255 range. This PR adds clip_range argument in ShiftIntensity and AdjustContrast to help do that.

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

and shift_intensity

Signed-off-by: KumoLiu <[email protected]>
@KumoLiu
KumoLiu requested review from Nic-Ma, ericspod, rijobro and wyli and removed request for Nic-Ma November 16, 2022 10:27
@Nic-Ma

Nic-Ma commented Nov 16, 2022

Copy link
Copy Markdown
Contributor

Should we consider channel-wise values to clip?

Thanks.

@wyli

wyli commented Nov 16, 2022

Copy link
Copy Markdown
Contributor

I think the range 0-255 comes from the dtype uint8, perhaps what's missing is a dtype argument instead of clipping? torchvision preserves the input dtype:

>>> torchvision.transforms.functional.adjust_contrast((torch.rand([1, 3, 3])*512).to(torch.uint8), 1)
tensor([[[232,  40, 211],
         [ 65, 238, 172],
         [198,   2,  93]]], dtype=torch.uint8)

@ericspod

Copy link
Copy Markdown
Member

I think the range 0-255 comes from the dtype uint8, perhaps what's missing is a dtype argument instead of clipping? torchvision preserves the input dtype:

Casting to uint8 would effectively clip but we want to be able to do it in floating point anyway. There's a number of times where I've used ScaleIntensity to ensure a particular value range but having a ClipIntensity transform perhaps makes more sense than adding clipping to just these transforms.

@KumoLiu

KumoLiu commented Nov 16, 2022

Copy link
Copy Markdown
Contributor Author

Hi @wyli, thanks for your suggestions, I'm confused about why casting to uint8 can replace clipping. It seems the return values of the two operations are different. Am I missing anything?

>>> a = torch.tensor([260, 250])
>>> b = a.to(torch.uint8)
a: tensor([260, 250])
b: tensor([  4, 250], dtype=torch.uint8)
clip a: tensor([255, 250])

Thanks!

@KumoLiu

KumoLiu commented Nov 16, 2022

Copy link
Copy Markdown
Contributor Author

I think the range 0-255 comes from the dtype uint8, perhaps what's missing is a dtype argument instead of clipping? torchvision preserves the input dtype:

Casting to uint8 would effectively clip but we want to be able to do it in floating point anyway. There's a number of times where I've used ScaleIntensity to ensure a particular value range but having a ClipIntensity transform perhaps makes more sense than adding clipping to just these transforms.

Hi @ericspod, thanks for your suggestions! I think having a ClipIntensity transform would be a good solution. Just one thing I am concerned that if the user has several transforms that need to clip at the same time (Just like what I did in the HoVerNet pipeline :) ). It may need to call ClipIntensity several times. So maybe we can keep this PR and create another one to add ClipIntensity. What do you think?
Thanks!

@ericspod

Copy link
Copy Markdown
Member

It may need to call ClipIntensity several times. So maybe we can keep this PR and create another one to add ClipIntensity. What do you think?

I don't think having a transform multiple times in a sequence is a problem unless there's performance reasons making that slow. I have had multiples of the same transform in the past. So if there's no overriding performance concern motivating keeping clip_range in the transforms you've modified here, I would put this into a new transform. Thanks!

@wyli

wyli commented Nov 16, 2022

Copy link
Copy Markdown
Contributor

sure, just to clarify my previous comment, the main motivation of clipping 0-255 is to simulate training examples in uint8, as a data augmentation method. if we add a dtype preserving option, then the problem is solved.

preserving the types is not necessarily just to(uint8), we can use similar logic like this one from torchvision:

https://github.com/pytorch/vision/blob/ad128b753c7e8cc0c600dfddac22ff48fc73c9d9/torchvision/transforms/functional_tensor.py#L258-L259

    bound = _max_value(img1.dtype)
    return (ratio * img1 + (1.0 - ratio) * img2).clamp(0, bound).to(img1.dtype)

@Nic-Ma

Nic-Ma commented Nov 16, 2022

Copy link
Copy Markdown
Contributor

@ericspod @KumoLiu ,

Maybe you can also consider the existing ThresholdIntensity transform?
https://github.com/Project-MONAI/MONAI/blob/dev/monai/transforms/intensity/array.py#L708

Thanks.

@KumoLiu

KumoLiu commented Nov 16, 2022

Copy link
Copy Markdown
Contributor Author

Hi @wyli, I see your point!
Maybe we could use a preserve_range argument to help users to choose whether preserve the original dtype. Not sure if there are any user cases that do not want to preserve the original dtype.
And for ClipIntensity, maybe calling the existing ThresholdIntensity one twice can help do it.
What do you think? @wyli @ericspod @Nic-Ma

@wyli

wyli commented Nov 16, 2022

Copy link
Copy Markdown
Contributor

Not sure if there are any user cases that do not want to preserve the original dtype.

it seems the current transform doesn't preserve the original dtype, and it's not possible to specify output dtype...maybe we can improve this, but at the moment the resampling backend doesn't support many dtypes anyway. so I don't have a strong opinion about this PR, was just mentioning the potential of adding dtype option.

@myron

myron commented Nov 17, 2022

Copy link
Copy Markdown
Collaborator

I think the range 0-255 comes from the dtype uint8, perhaps what's missing is a dtype argument instead of clipping? torchvision preserves the input dtype:

Casting to uint8 would effectively clip but we want to be able to do it in floating point anyway. There's a number of times where I've used ScaleIntensity to ensure a particular value range but having a ClipIntensity transform perhaps makes more sense than adding clipping to just these transforms.

I agree with @ericspod , I recommend have a separate clipping transform, instead of cluttering existing transforms. Adding clipping option only to 2 of these augmentation transforms seems somewhat arbitrary, and adding it to All augmentation transforms is too much.

@KumoLiu

KumoLiu commented Nov 17, 2022

Copy link
Copy Markdown
Contributor Author

I think in ShiftIntensity, we need to add a preserve_range flag or clip_range args since now it forced converts data dtype to the original one which will cause an issue when the intensity goes beyond the original range. But for AdjustContrast and other intensity transforms, we can discuss later whether add another clip transform.

out, *_ = convert_data_type(data=out, dtype=img.dtype)

Thanks!

@wyli

wyli commented Nov 17, 2022

Copy link
Copy Markdown
Contributor

MONAI/monai/transforms/intensity/array.py

agreed, I think this is a bug

out, *_ = convert_data_type(data=out, dtype=img.dtype)
which doesn't take into consideration the dtype overflow...

I think we should do similarly this logic: https://github.com/pytorch/vision/blob/ad128b753c7e8cc0c600dfddac22ff48fc73c9d9/torchvision/transforms/functional_tensor.py#L64

@bhashemian

bhashemian commented Nov 18, 2022 •

Copy link
Copy Markdown
Member

@KumoLiu these are great improvements that you are making. Thanks for putting effort on this, but for hovernet pipeline, please use colorjitter instead as it should include all these color augmentations: https://github.com/Project-MONAI/tutorials/blob/7eccb858f0286a40fdd0a2fd4bfbb0702af8e6ef/pathology/tumor_detection/ignite/camelyon_train_evaluate.py#L89-L91

@KumoLiu

KumoLiu commented Nov 18, 2022

Copy link
Copy Markdown
Contributor Author

@KumoLiu these are great improvements that you are making. Thanks for putting effort on this, but for hovernet pipeline, please use colorjitter instead as it should include all these color augmentations: https://github.com/Project-MONAI/tutorials/blob/7eccb858f0286a40fdd0a2fd4bfbb0702af8e6ef/pathology/tumor_detection/ignite/camelyon_train_evaluate.py#L89-L91

Yes, I have used it. Thanks!

@KumoLiu

KumoLiu commented Nov 22, 2022

Copy link
Copy Markdown
Contributor Author

After discussion, I will close this PR and create another PR later.

@function2-llx

function2-llx commented Feb 20, 2023 •

Copy link
Copy Markdown
Contributor

FYI, torchvision clips value to [0, 1] for floating points or [0, 255] for uint8 type for any blending operation and adjust_gamma (which is what actually AdjustContrast does in MONAI).

@KumoLiu
KumoLiu deleted the clip-args branch February 20, 2023 06:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants