-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: add channel_wise option for ShiftIntensity
#6789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6f2b6c6
ce343e8
256139c
b489aab
52d5de0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -227,13 +227,15 @@ class ShiftIntensity(Transform): | |
| offset: offset value to shift the intensity of image. | ||
| safe: if `True`, then do safe dtype convert when intensity overflow. default to `False`. | ||
| E.g., `[256, -12]` -> `[array(0), array(244)]`. If `True`, then `[256, -12]` -> `[array(255), array(0)]`. | ||
| channel_wise: if `True`, shift the intensity for each channel of image with random offset | ||
| """ | ||
|
|
||
| backend = [TransformBackends.TORCH, TransformBackends.NUMPY] | ||
|
|
||
| def __init__(self, offset: float, safe: bool = False) -> None: | ||
| def __init__(self, offset: float, safe: bool = False, channel_wise: bool = False) -> None: | ||
| self.offset = offset | ||
| self.safe = safe | ||
| self.channel_wise = channel_wise | ||
|
|
||
| def __call__(self, img: NdarrayOrTensor, offset: float | None = None) -> NdarrayOrTensor: | ||
| """ | ||
|
|
@@ -242,10 +244,16 @@ def __call__(self, img: NdarrayOrTensor, offset: float | None = None) -> Ndarray | |
|
|
||
| img = convert_to_tensor(img, track_meta=get_track_meta()) | ||
| offset = self.offset if offset is None else offset | ||
| out = img + offset | ||
| out, *_ = convert_data_type(data=out, dtype=img.dtype, safe=self.safe) | ||
|
|
||
| return out | ||
| if self.channel_wise: | ||
| for i, d in enumerate(img): | ||
| out = d + offset | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, but if you don't randomize offset here, the output won't make any difference since offset is a constant.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh yes good catch, any suggestions on how I do this ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @SauravMaheshkar, I would suggest you refer to this PR. |
||
| out, *_ = convert_data_type(data=out, dtype=d.dtype, safe=self.safe) | ||
| img[i] = out | ||
| return img | ||
| else: | ||
| out = img + offset | ||
| out, *_ = convert_data_type(data=out, dtype=img.dtype, safe=self.safe) | ||
| return out # type: ignore | ||
|
|
||
|
|
||
| class RandShiftIntensity(RandomizableTransform): | ||
|
|
@@ -255,13 +263,16 @@ class RandShiftIntensity(RandomizableTransform): | |
|
|
||
| backend = [TransformBackends.TORCH, TransformBackends.NUMPY] | ||
|
|
||
| def __init__(self, offsets: tuple[float, float] | float, safe: bool = False, prob: float = 0.1) -> None: | ||
| def __init__( | ||
| self, offsets: tuple[float, float] | float, safe: bool = False, channel_wise: bool = False, prob: float = 0.1 | ||
| ) -> None: | ||
| """ | ||
| Args: | ||
| offsets: offset range to randomly shift. | ||
| if single number, offset value is picked from (-offsets, offsets). | ||
| safe: if `True`, then do safe dtype convert when intensity overflow. default to `False`. | ||
| E.g., `[256, -12]` -> `[array(0), array(244)]`. If `True`, then `[256, -12]` -> `[array(255), array(0)]`. | ||
| channel_wise: if `True`, shift the intensity for each channel of image with random offset | ||
| prob: probability of shift. | ||
| """ | ||
| RandomizableTransform.__init__(self, prob) | ||
|
|
@@ -272,7 +283,7 @@ def __init__(self, offsets: tuple[float, float] | float, safe: bool = False, pro | |
| else: | ||
| self.offsets = (min(offsets), max(offsets)) | ||
| self._offset = self.offsets[0] | ||
| self._shifter = ShiftIntensity(self._offset, safe) | ||
| self._shifter = ShiftIntensity(self._offset, safe, channel_wise) | ||
|
|
||
| def randomize(self, data: Any | None = None) -> None: | ||
| super().randomize(None) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, please also update the corresponding APIs in https://github.com/Project-MONAI/MONAI/blob/dev/monai/transforms/intensity/dictionary.py and add test cases to the existing unit tests such as https://github.com/Project-MONAI/MONAI/blob/dev/tests/test_shift_intensity.py