Skip to content

Commit 7fd2491

Browse files
authored
F_t add factor nonnegativity checks for adjust_* (#2356)
* F_t add factor nonnegativity checks for adjust_* * Update functional_tensor.py * Update functional_tensor.py * Update functional_tensor.py * Update functional_tensor.py
1 parent 6fe11d5 commit 7fd2491

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

torchvision/transforms/functional_tensor.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ def adjust_brightness(img: Tensor, brightness_factor: float) -> Tensor:
9292
Returns:
9393
Tensor: Brightness adjusted image.
9494
"""
95+
if brightness_factor < 0:
96+
raise ValueError('brightness_factor ({}) is not non-negative.'.format(brightness_factor))
97+
9598
if not _is_tensor_a_torch_image(img):
9699
raise TypeError('tensor is not a torch image.')
97100

@@ -110,6 +113,9 @@ def adjust_contrast(img: Tensor, contrast_factor: float) -> Tensor:
110113
Returns:
111114
Tensor: Contrast adjusted image.
112115
"""
116+
if contrast_factor < 0:
117+
raise ValueError('contrast_factor ({}) is not non-negative.'.format(contrast_factor))
118+
113119
if not _is_tensor_a_torch_image(img):
114120
raise TypeError('tensor is not a torch image.')
115121

@@ -143,7 +149,7 @@ def adjust_hue(img, hue_factor):
143149
Returns:
144150
Tensor: Hue adjusted image.
145151
"""
146-
if not(-0.5 <= hue_factor <= 0.5):
152+
if not (-0.5 <= hue_factor <= 0.5):
147153
raise ValueError('hue_factor ({}) is not in [-0.5, 0.5].'.format(hue_factor))
148154

149155
if not _is_tensor_a_torch_image(img):
@@ -171,13 +177,16 @@ def adjust_saturation(img: Tensor, saturation_factor: float) -> Tensor:
171177
172178
Args:
173179
img (Tensor): Image to be adjusted.
174-
saturation_factor (float): How much to adjust the saturation. 0 will
175-
give a black and white image, 1 will give the original image while
176-
2 will enhance the saturation by a factor of 2.
180+
saturation_factor (float): How much to adjust the saturation. Can be any
181+
non negative number. 0 gives a black and white image, 1 gives the
182+
original image while 2 enhances the saturation by a factor of 2.
177183
178184
Returns:
179185
Tensor: Saturation adjusted image.
180186
"""
187+
if saturation_factor < 0:
188+
raise ValueError('saturation_factor ({}) is not non-negative.'.format(saturation_factor))
189+
181190
if not _is_tensor_a_torch_image(img):
182191
raise TypeError('tensor is not a torch image.')
183192

0 commit comments

Comments
 (0)