Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions monai/transforms/intensity/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.offset = offset
self.safe = safe
self.channel_wise = channel_wise

def __call__(self, img: NdarrayOrTensor, offset: float | None = None) -> NdarrayOrTensor:
"""
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

@SauravMaheshkar SauravMaheshkar Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yes good catch, any suggestions on how I do this ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @SauravMaheshkar, I would suggest you refer to this PR.
Thanks!

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):
Expand All @@ -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)
Expand All @@ -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)
Expand Down