-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Add Color transforms #275
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
Add Color transforms #275
Conversation
@alykhantejani just to confirm, gamma correction should be done on V channel of HSV right? Edit: looks like not. RGB channels are corrected together. Ref |
Also * Change adjust_saturation to use pillow implementation * Documentation made clear
I'm not using code from #27 because implementations are on tensors not on PIL images. |
Hey @alykhantejani, I have actually implemented all the transforms: brightness, contrast, saturation and gamma. I need to implement a transform class (and tests) using these functions and PR would be complete. I think this makes #27 redundant actually. |
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.
LGTM, thanks!
I have only one comment that I think should be addressed (the overflow warning), the rest are minor details.
Also, could you add basic tests to those transforms?
torchvision/transforms.py
Outdated
|
||
np_h = np.array(h, dtype='uint8') | ||
# uint8 addition take cares of rotation across boundaries | ||
np_h += np.uint8(hue_factor * 255) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torchvision/transforms.py
Outdated
|
||
h, s, v = img.convert('HSV').split() | ||
|
||
np_h = np.array(h, dtype='uint8') |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torchvision/transforms.py
Outdated
|
||
img = img.convert('RGB') | ||
|
||
np_img = np.array(img) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
np.random.shuffle(transforms) | ||
transform = Compose(transforms) | ||
|
||
return transform |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
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.
Thanks @chsasank!
LGTM once we change the requirement for input images to be RGB.
torchvision/transforms.py
Outdated
|
||
|
||
def adjust_contrast(img, contrast_factor): | ||
"""Adjust brightness of an Image. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torchvision/transforms.py
Outdated
2 will enhance the saturation by a factor of 2. | ||
|
||
Returns: | ||
Adjusted image(s), same shape and DType as `image`. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torchvision/transforms.py
Outdated
"""Adjust hue of an image. | ||
|
||
`image` is an RGB image. The image hue is adjusted by converting the | ||
image to HSV and cyclically shifting the intensities in hue channel (H). |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torchvision/transforms.py
Outdated
Returns: | ||
PIL.Image: Hue adjusted image. | ||
""" | ||
if not(hue_factor <= 0.5 and hue_factor >= -0.5): |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torchvision/transforms.py
Outdated
PIL.Image: Hue adjusted image. | ||
""" | ||
if not(hue_factor <= 0.5 and hue_factor >= -0.5): | ||
raise ValueError('hue_factor {} out of range.'.format(hue_factor)) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torchvision/transforms.py
Outdated
def adjust_hue(img, hue_factor): | ||
"""Adjust hue of an image. | ||
|
||
`image` is an RGB image. The image hue is adjusted by converting the |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torchvision/transforms.py
Outdated
np_img = 255 * gain * ((np_img / 255) ** gamma) | ||
np_img = np.uint8(np.clip(np_img, 0, 255)) | ||
|
||
img = Image.fromarray(np_img, 'RGB') |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
Can somebody guide me how to write tests for these? As in what do I check for etc. |
torchvision/transforms.py
Outdated
input_mode = img.mode | ||
img = img.convert('RGB') | ||
|
||
np_img = np.array(img, dtype='float32') |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
Thanks a lot @chsasank ! |
Quick! Thanks @chsasank! Would it be possible to also document these on the readme? |
@Kaixhin I think we actually should solve the root issue and get these docs autogenerated from the docstrings themselves, like it's done in the pytorch repo |
Please see #271 for the discussion.
Currently added Hue and saturation colour transforms based on tf code: https://github.com/tensorflow/tensorflow/blob/r1.3/tensorflow/python/ops/image_ops_impl.py