Skip to content
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

[prototype] Speed improvement for adjust gamma op #6820

Merged
merged 4 commits into from
Oct 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions torchvision/prototype/transforms/functional/_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from torchvision.prototype import features
from torchvision.transforms import functional_pil as _FP, functional_tensor as _FT

from ._meta import _rgb_to_gray, get_dimensions_image_tensor, get_num_channels_image_tensor
from ._meta import _rgb_to_gray, convert_dtype_image_tensor, get_dimensions_image_tensor, get_num_channels_image_tensor


def _blend(image1: torch.Tensor, image2: torch.Tensor, ratio: float) -> torch.Tensor:
Expand Down Expand Up @@ -257,7 +257,28 @@ def adjust_hue(inpt: features.InputTypeJIT, hue_factor: float) -> features.Input
return adjust_hue_image_pil(inpt, hue_factor=hue_factor)


adjust_gamma_image_tensor = _FT.adjust_gamma
def adjust_gamma_image_tensor(image: torch.Tensor, gamma: float, gain: float = 1.0) -> torch.Tensor:
if not (isinstance(image, torch.Tensor)):
raise TypeError("Input img should be Tensor image")

if gamma < 0:
raise ValueError("Gamma should be a non-negative real number")

# The input image is either assumed to be at [0, 1] scale (if float) or is converted to that scale (if integer).
# Since the gamma is non-negative, the output remains at [0, 1] scale.
if not torch.is_floating_point(image):
output = convert_dtype_image_tensor(image, torch.float32).pow_(gamma)
else:
output = image.pow(gamma)

if gain != 1.0:
# The clamp operation is needed only if multiplication is performed. It's only when gain != 1, that the scale
# of the output can go beyond [0, 1].
output = output.mul_(gain).clamp_(0.0, 1.0)

return convert_dtype_image_tensor(output, image.dtype)


adjust_gamma_image_pil = _FP.adjust_gamma


Expand Down