From cc32826a8a806146590e254d9dc28fa0b77706c9 Mon Sep 17 00:00:00 2001 From: Tongzhou Wang Date: Fri, 26 Jun 2020 23:49:27 -0400 Subject: [PATCH 1/5] F_t add factor nonnegativity checks for adjust_* --- torchvision/transforms/functional_tensor.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/torchvision/transforms/functional_tensor.py b/torchvision/transforms/functional_tensor.py index 56703d0a1fd..72a78f39f90 100644 --- a/torchvision/transforms/functional_tensor.py +++ b/torchvision/transforms/functional_tensor.py @@ -91,6 +91,9 @@ def adjust_brightness(img, brightness_factor): Returns: Tensor: Brightness adjusted image. """ + if not brightness_factor < 0: + raise ValueError('brightness_factor ({}) is not non-negative.'.format(brightness_factor)) + if not _is_tensor_a_torch_image(img): raise TypeError('tensor is not a torch image.') @@ -110,6 +113,9 @@ def adjust_contrast(img, contrast_factor): Returns: Tensor: Contrast adjusted image. """ + if not contrast_factor < 0: + raise ValueError('contrast_factor ({}) is not non-negative.'.format(contrast_factor)) + if not _is_tensor_a_torch_image(img): raise TypeError('tensor is not a torch image.') @@ -172,13 +178,16 @@ def adjust_saturation(img, saturation_factor): Args: img (Tensor): Image to be adjusted. - saturation_factor (float): How much to adjust the saturation. 0 will - give a black and white image, 1 will give the original image while - 2 will enhance the saturation by a factor of 2. + saturation_factor (float): How much to adjust the saturation. Can be any + non negative number. 0 gives a black and white image, 1 gives the + original image while 2 enhances the saturation by a factor of 2. Returns: Tensor: Saturation adjusted image. """ + if not saturation_factor < 0: + raise ValueError('saturation_factor ({}) is not non-negative.'.format(saturation_factor)) + if not _is_tensor_a_torch_image(img): raise TypeError('tensor is not a torch image.') @@ -282,7 +291,7 @@ def ten_crop(img, size, vertical_flip=False): def _blend(img1, img2, ratio): # type: (Tensor, Tensor, float) -> Tensor bound = 1 if img1.dtype in [torch.half, torch.float32, torch.float64] else 255 - return (ratio * img1 + (1 - ratio) * img2).clamp(0, bound).to(img1.dtype) + return torch.lerp(ratio, img2, img1).clamp(0, bound).to(img1.dtype) def _rgb2hsv(img): From d053a77a023a7c4e437a77811962e92088c731ad Mon Sep 17 00:00:00 2001 From: Tongzhou Wang Date: Sat, 27 Jun 2020 01:27:20 -0400 Subject: [PATCH 2/5] Update functional_tensor.py --- torchvision/transforms/functional_tensor.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/torchvision/transforms/functional_tensor.py b/torchvision/transforms/functional_tensor.py index 72a78f39f90..5cd19f39a86 100644 --- a/torchvision/transforms/functional_tensor.py +++ b/torchvision/transforms/functional_tensor.py @@ -91,7 +91,7 @@ def adjust_brightness(img, brightness_factor): Returns: Tensor: Brightness adjusted image. """ - if not brightness_factor < 0: + if brightness_factor < 0: raise ValueError('brightness_factor ({}) is not non-negative.'.format(brightness_factor)) if not _is_tensor_a_torch_image(img): @@ -113,7 +113,7 @@ def adjust_contrast(img, contrast_factor): Returns: Tensor: Contrast adjusted image. """ - if not contrast_factor < 0: + if contrast_factor < 0: raise ValueError('contrast_factor ({}) is not non-negative.'.format(contrast_factor)) if not _is_tensor_a_torch_image(img): @@ -149,7 +149,7 @@ def adjust_hue(img, hue_factor): Returns: Tensor: Hue adjusted image. """ - if not(-0.5 <= hue_factor <= 0.5): + if not (-0.5 <= hue_factor <= 0.5): raise ValueError('hue_factor ({}) is not in [-0.5, 0.5].'.format(hue_factor)) if not _is_tensor_a_torch_image(img): @@ -185,7 +185,7 @@ def adjust_saturation(img, saturation_factor): Returns: Tensor: Saturation adjusted image. """ - if not saturation_factor < 0: + if saturation_factor < 0: raise ValueError('saturation_factor ({}) is not non-negative.'.format(saturation_factor)) if not _is_tensor_a_torch_image(img): From 6b2485bd825b65cd711cb6b61d1c314abb575c22 Mon Sep 17 00:00:00 2001 From: Tongzhou Wang Date: Sat, 27 Jun 2020 12:09:14 -0400 Subject: [PATCH 3/5] Update functional_tensor.py --- torchvision/transforms/functional_tensor.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/torchvision/transforms/functional_tensor.py b/torchvision/transforms/functional_tensor.py index 5cd19f39a86..cc7eaed7d3b 100644 --- a/torchvision/transforms/functional_tensor.py +++ b/torchvision/transforms/functional_tensor.py @@ -119,7 +119,7 @@ def adjust_contrast(img, contrast_factor): if not _is_tensor_a_torch_image(img): raise TypeError('tensor is not a torch image.') - mean = torch.mean(rgb_to_grayscale(img).to(torch.float)) + mean = rgb_to_grayscale(img).mean() return _blend(img, mean, contrast_factor) @@ -290,8 +290,10 @@ def ten_crop(img, size, vertical_flip=False): def _blend(img1, img2, ratio): # type: (Tensor, Tensor, float) -> Tensor - bound = 1 if img1.dtype in [torch.half, torch.float32, torch.float64] else 255 - return torch.lerp(ratio, img2, img1).clamp(0, bound).to(img1.dtype) + if img1.dtype in [torch.half, torch.float32, torch.float64]: + return torch.lerp(ratio, img2, img1).clamp(0, 1).to(img1.dtype) + else: + return torch.lerp(ratio, img2.float(), img1.float()).clamp(0, 255).to(img1.dtype) def _rgb2hsv(img): From 963fec517ffad52f8053126e1297e5f26d39031c Mon Sep 17 00:00:00 2001 From: Tongzhou Wang Date: Mon, 29 Jun 2020 10:23:02 -0400 Subject: [PATCH 4/5] Update functional_tensor.py --- torchvision/transforms/functional_tensor.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/torchvision/transforms/functional_tensor.py b/torchvision/transforms/functional_tensor.py index cc7eaed7d3b..f97209c1baf 100644 --- a/torchvision/transforms/functional_tensor.py +++ b/torchvision/transforms/functional_tensor.py @@ -290,10 +290,8 @@ def ten_crop(img, size, vertical_flip=False): def _blend(img1, img2, ratio): # type: (Tensor, Tensor, float) -> Tensor - if img1.dtype in [torch.half, torch.float32, torch.float64]: - return torch.lerp(ratio, img2, img1).clamp(0, 1).to(img1.dtype) - else: - return torch.lerp(ratio, img2.float(), img1.float()).clamp(0, 255).to(img1.dtype) + bound = 1 if img1.dtype in [torch.half, torch.float32, torch.float64] else 255 + return (ratio * img1 + (1 - ratio) * img2).clamp(0, bound).to(img1.dtype) def _rgb2hsv(img): From 8676c896bb7b9d8b231de002ee8f3fdb9bde62f1 Mon Sep 17 00:00:00 2001 From: Tongzhou Wang Date: Mon, 29 Jun 2020 12:12:53 -0400 Subject: [PATCH 5/5] Update functional_tensor.py --- torchvision/transforms/functional_tensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/transforms/functional_tensor.py b/torchvision/transforms/functional_tensor.py index f97209c1baf..95c955c7a5e 100644 --- a/torchvision/transforms/functional_tensor.py +++ b/torchvision/transforms/functional_tensor.py @@ -119,7 +119,7 @@ def adjust_contrast(img, contrast_factor): if not _is_tensor_a_torch_image(img): raise TypeError('tensor is not a torch image.') - mean = rgb_to_grayscale(img).mean() + mean = torch.mean(rgb_to_grayscale(img).to(torch.float)) return _blend(img, mean, contrast_factor)