-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Adding invert operator #3065
Adding invert operator #3065
Changes from 5 commits
d9c96e6
f896308
422c9f4
72daf4d
152717f
60fe2cd
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 |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
"CenterCrop", "Pad", "Lambda", "RandomApply", "RandomChoice", "RandomOrder", "RandomCrop", | ||
"RandomHorizontalFlip", "RandomVerticalFlip", "RandomResizedCrop", "RandomSizedCrop", "FiveCrop", "TenCrop", | ||
"LinearTransformation", "ColorJitter", "RandomRotation", "RandomAffine", "Grayscale", "RandomGrayscale", | ||
"RandomPerspective", "RandomErasing", "GaussianBlur", "InterpolationMode"] | ||
"RandomPerspective", "RandomErasing", "GaussianBlur", "InterpolationMode", "RandomInvert"] | ||
|
||
|
||
class Compose: | ||
|
@@ -1699,3 +1699,43 @@ def _setup_angle(x, name, req_sizes=(2, )): | |
_check_sequence_input(x, name, req_sizes) | ||
|
||
return [float(d) for d in x] | ||
|
||
|
||
class RandomInvert(torch.nn.Module): | ||
"""Inverts the colors of the given image randomly with a given probability. | ||
The image can be a PIL Image or a torch Tensor, in which case it is expected | ||
to have [..., H, W] shape, where ... means an arbitrary number of leading | ||
dimensions | ||
|
||
Args: | ||
p (float): probability of the image being color inverted. Default value is 0.5 | ||
""" | ||
|
||
def __init__(self, p=0.5): | ||
super().__init__() | ||
self.p = p | ||
|
||
@staticmethod | ||
def get_params() -> float: | ||
"""Choose value for random color inversion. | ||
|
||
Returns: | ||
float: Random value which is used to determine whether the random color inversion | ||
should occur. | ||
""" | ||
return torch.rand(1).item() | ||
|
||
def forward(self, img): | ||
""" | ||
Args: | ||
img (PIL Image or Tensor): Image to be inverted. | ||
|
||
Returns: | ||
PIL Image or Tensor: Randomly color inverted image. | ||
""" | ||
if self.get_params() < self.p: | ||
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. We have decided on getting random apply param with static 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. I've asked @fmassa and he recommended adding it in the 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. The output of 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. @fmassa OK and what to do if there are both to sample: transform random params and random apply one ? 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. I don't have a good solution for this one yet. It's a similar story as We might need to revisit our transforms story, maybe breaking it down into two types of transforms. |
||
return F.invert(img) | ||
return img | ||
|
||
def __repr__(self): | ||
return self.__class__.__name__ + '(p={})'.format(self.p) |
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.
If image dtype is float then max_val will be something very large right ? OK, probably, now I understand the questions asked about float dtype and its range as 0-1.
How about adding an option to the method such that user could specify its own max value. By default can be max dtype...
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.
Yep you are right. I opted for using the same approach as _bend() to determine the
bound
. I did not optin for passing it as a param to make sure the API is similar as other transformations.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.
Hum, this sounds fishy to me for floating point types. We should probably not be doing
1.79769e+308 - value
for doubles, nor3.40282e+38 - value
for floats.Is there any use-case for applying color transforms to native float images (like satellite or medical data)? If not, we should probably assume that float images are within 0-1 range
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.
For medical imagery I'd use MONAI as reference and it seems like it is possible: https://docs.monai.io/en/latest/transforms.html#intensity . They do not have any assumptions on the range of input data (uint8 or float).
As for satellite imagery, some intensity manipulations in visible spectrum can be also possible.
I think there are 2 ways possible for this op:
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.
That's a great reference, thanks @vfdev-5 !
I think that exposing a
min_value / max_value
arguments (which defaults toNone
and has sensible defaults for different data types) is a good trade-off to consider.We can go in two steps though if we want to be extra cautious, and first raise an exception and then in the future add those extra arguments.
Thoughts?