-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Make ColorJitter torchscriptable #2298
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
bdb50a2
ff441d4
200bef4
2b064d2
69f6184
6db8c9f
5d07ea4
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 |
---|---|---|
|
@@ -865,7 +865,7 @@ def __repr__(self): | |
return format_string | ||
|
||
|
||
class ColorJitter(object): | ||
class ColorJitter(torch.nn.Module): | ||
"""Randomly change the brightness, contrast and saturation of an image. | ||
|
||
Args: | ||
|
@@ -882,20 +882,23 @@ class ColorJitter(object): | |
hue_factor is chosen uniformly from [-hue, hue] or the given [min, max]. | ||
Should have 0<= hue <= 0.5 or -0.5 <= min <= max <= 0.5. | ||
""" | ||
|
||
def __init__(self, brightness=0, contrast=0, saturation=0, hue=0): | ||
super().__init__() | ||
self.brightness = self._check_input(brightness, 'brightness') | ||
self.contrast = self._check_input(contrast, 'contrast') | ||
self.saturation = self._check_input(saturation, 'saturation') | ||
self.hue = self._check_input(hue, 'hue', center=0, bound=(-0.5, 0.5), | ||
clip_first_on_zero=False) | ||
|
||
@torch.jit.unused | ||
def _check_input(self, value, name, center=1, bound=(0, float('inf')), clip_first_on_zero=True): | ||
if isinstance(value, numbers.Number): | ||
if value < 0: | ||
raise ValueError("If {} is a single number, it must be non negative.".format(name)) | ||
value = [center - value, center + value] | ||
value = [center - float(value), center + float(value)] | ||
if clip_first_on_zero: | ||
value[0] = max(value[0], 0) | ||
value[0] = max(value[0], 0.0) | ||
elif isinstance(value, (tuple, list)) and len(value) == 2: | ||
if not bound[0] <= value[0] <= value[1] <= bound[1]: | ||
raise ValueError("{} values should be between {}".format(name, bound)) | ||
|
@@ -909,6 +912,7 @@ def _check_input(self, value, name, center=1, bound=(0, float('inf')), clip_firs | |
return value | ||
|
||
@staticmethod | ||
@torch.jit.unused | ||
def get_params(brightness, contrast, saturation, hue): | ||
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. 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. For backwards-compatibility. The way it was implemented was pretty non-canonical though, but I would be ok if we made a BC-breaking change to use it again by maybe returning a list of transform name / params somehow, but this seems lower priority for me I think 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 can give a shot to that as I'm working on 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. Sounds good to me, but can you do it in a separate PR which only does this? 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. No problems, can do it in a separate PR |
||
"""Get a randomized transform to be applied on image. | ||
|
||
|
@@ -941,17 +945,37 @@ def get_params(brightness, contrast, saturation, hue): | |
|
||
return transform | ||
|
||
def __call__(self, img): | ||
def forward(self, img): | ||
""" | ||
Args: | ||
img (PIL Image): Input image. | ||
img (PIL Image or Tensor): Input image. | ||
|
||
Returns: | ||
PIL Image: Color jittered image. | ||
PIL Image or Tensor: Color jittered image. | ||
""" | ||
transform = self.get_params(self.brightness, self.contrast, | ||
self.saturation, self.hue) | ||
return transform(img) | ||
fn_idx = torch.randperm(4) | ||
for fn_id in fn_idx: | ||
if fn_id == 0 and self.brightness is not None: | ||
brightness = self.brightness | ||
brightness_factor = torch.tensor(1.0).uniform_(brightness[0], brightness[1]).item() | ||
img = F.adjust_brightness(img, brightness_factor) | ||
|
||
if fn_id == 1 and self.contrast is not None: | ||
contrast = self.contrast | ||
contrast_factor = torch.tensor(1.0).uniform_(contrast[0], contrast[1]).item() | ||
img = F.adjust_contrast(img, contrast_factor) | ||
|
||
if fn_id == 2 and self.saturation is not None: | ||
saturation = self.saturation | ||
saturation_factor = torch.tensor(1.0).uniform_(saturation[0], saturation[1]).item() | ||
img = F.adjust_saturation(img, saturation_factor) | ||
|
||
if fn_id == 3 and self.hue is not None: | ||
hue = self.hue | ||
hue_factor = torch.tensor(1.0).uniform_(hue[0], hue[1]).item() | ||
img = F.adjust_hue(img, hue_factor) | ||
|
||
return img | ||
|
||
def __repr__(self): | ||
format_string = self.__class__.__name__ + '(' | ||
|
Uh oh!
There was an error while loading. Please reload this page.