Skip to content

Commit

Permalink
2022-11-10 nightly release (ffd5a56)
Browse files Browse the repository at this point in the history
  • Loading branch information
chronos_secgrp_pytorch_oss_ci_oncall committed Nov 10, 2022
1 parent b24133e commit 797e1ac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 2 additions & 0 deletions test/test_prototype_transforms_consistency.py
Expand Up @@ -251,6 +251,8 @@ def __init__(
ArgsKwargs(p=0),
ArgsKwargs(p=1),
],
# Use default tolerances of `torch.testing.assert_close`
closeness_kwargs=dict(rtol=None, atol=None),
),
ConsistencyConfig(
prototype_transforms.RandomAdjustSharpness,
Expand Down
29 changes: 20 additions & 9 deletions torchvision/prototype/transforms/functional/_color.py
Expand Up @@ -79,9 +79,14 @@ def adjust_contrast_image_tensor(image: torch.Tensor, contrast_factor: float) ->
c = image.shape[-3]
if c not in [1, 3]:
raise TypeError(f"Input image tensor permitted channel values are {[1, 3]}, but found {c}")
dtype = image.dtype if torch.is_floating_point(image) else torch.float32
grayscale_image = _rgb_to_gray(image) if c == 3 else image
mean = torch.mean(grayscale_image.to(dtype), dim=(-3, -2, -1), keepdim=True)
fp = image.is_floating_point()
if c == 3:
grayscale_image = _rgb_to_gray(image, cast=False)
if not fp:
grayscale_image = grayscale_image.floor_()
else:
grayscale_image = image if fp else image.to(torch.float32)
mean = torch.mean(grayscale_image, dim=(-3, -2, -1), keepdim=True)
return _blend(image, mean, contrast_factor)


Expand Down Expand Up @@ -372,17 +377,23 @@ def autocontrast_image_tensor(image: torch.Tensor) -> torch.Tensor:
return image

bound = _FT._max_value(image.dtype)
dtype = image.dtype if torch.is_floating_point(image) else torch.float32
fp = image.is_floating_point()
float_image = image if fp else image.to(torch.float32)

minimum = image.amin(dim=(-2, -1), keepdim=True).to(dtype)
maximum = image.amax(dim=(-2, -1), keepdim=True).to(dtype)
minimum = float_image.amin(dim=(-2, -1), keepdim=True)
maximum = float_image.amax(dim=(-2, -1), keepdim=True)

scale = bound / (maximum - minimum)
eq_idxs = maximum == minimum
inv_scale = maximum.sub_(minimum).div_(bound)
minimum[eq_idxs] = 0.0
scale[eq_idxs] = 1.0
inv_scale[eq_idxs] = 1.0

if fp:
diff = float_image.sub(minimum)
else:
diff = float_image.sub_(minimum)

return (image - minimum).mul_(scale).clamp_(0, bound).to(image.dtype)
return diff.div_(inv_scale).clamp_(0, bound).to(image.dtype)


autocontrast_image_pil = _FP.autocontrast
Expand Down
8 changes: 5 additions & 3 deletions torchvision/prototype/transforms/functional/_meta.py
Expand Up @@ -213,10 +213,12 @@ def _gray_to_rgb(grayscale: torch.Tensor) -> torch.Tensor:
return grayscale.repeat(repeats)


def _rgb_to_gray(image: torch.Tensor) -> torch.Tensor:
def _rgb_to_gray(image: torch.Tensor, cast: bool = True) -> torch.Tensor:
r, g, b = image.unbind(dim=-3)
l_img = (0.2989 * r).add_(g, alpha=0.587).add_(b, alpha=0.114)
l_img = l_img.to(image.dtype).unsqueeze(dim=-3)
l_img = r.mul(0.2989).add_(g, alpha=0.587).add_(b, alpha=0.114)
if cast:
l_img = l_img.to(image.dtype)
l_img = l_img.unsqueeze(dim=-3)
return l_img


Expand Down

0 comments on commit 797e1ac

Please sign in to comment.