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

[proto] Speed improvement for autocontrast op #6811

Merged
merged 3 commits into from
Oct 24, 2022
Merged
Changes from 2 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
29 changes: 28 additions & 1 deletion torchvision/prototype/transforms/functional/_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,34 @@ def solarize(inpt: features.InputTypeJIT, threshold: float) -> features.InputTyp
return solarize_image_pil(inpt, threshold=threshold)


autocontrast_image_tensor = _FT.autocontrast
def autocontrast_image_tensor(image: torch.Tensor) -> torch.Tensor:

if not (isinstance(image, torch.Tensor)):
raise TypeError("Input img should be Tensor image")

c = get_num_channels_image_tensor(image)

if c not in [1, 3]:
raise TypeError(f"Input image tensor permitted channel values are {[1, 3]}, but found {c}")

if image.numel() == 0:
# exit earlier on empty images
return image

bound = 1.0 if image.is_floating_point() else 255.0
dtype = image.dtype if torch.is_floating_point(image) else torch.float32

minimum = image.amin(dim=(-2, -1), keepdim=True).to(dtype)
maximum = image.amax(dim=(-2, -1), keepdim=True).to(dtype)
Comment on lines +231 to +232
Copy link
Collaborator Author

@vfdev-5 vfdev-5 Oct 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use torch.aminmax here as it does not support tuple of dims. Reshaping the input does not improve runtime.


scale = bound / (maximum - minimum)
eq_idxs = maximum == minimum
datumbox marked this conversation as resolved.
Show resolved Hide resolved
minimum[eq_idxs] = 0.0
scale[eq_idxs] = 1.0

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


autocontrast_image_pil = _FP.autocontrast


Expand Down