-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Fix hardcoded 255 #6830
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
Merged
Merged
Fix hardcoded 255 #6830
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
3e81aef
fix prototype kernels
pmeier 33852be
fix stable kernels
pmeier 3a92412
fix tests
pmeier e13613a
make test more robust
pmeier a400225
Merge branch 'main' into fix-hardcoded-255
pmeier e053125
Merge branch 'main' into fix-hardcoded-255
pmeier 3327e04
improve invert for signed integers
pmeier 91e8c66
Merge branch 'main' into fix-hardcoded-255
datumbox bdd8127
Merge branch 'main' into fix-hardcoded-255
pmeier c672425
improve invert
pmeier 6375627
fix posterize
pmeier 6895f71
Merge branch 'main' into fix-hardcoded-255
pmeier c0236fc
Revert "assume that integer images are [0, 255] in equalize (#6859)"
pmeier 8713528
Merge branch 'fix-hardcoded-255' of https://github.com/pmeier/vision …
pmeier 9acf2f4
Merge branch 'main' into fix-hardcoded-255
pmeier 402b01f
fix solarize in AA
pmeier d0394b7
Merge branch 'main' into fix-hardcoded-255
pmeier 5f33f4a
fix resize
pmeier 3a13a08
Revert "fix resize"
pmeier 7765a47
Merge branch 'main' into fix-hardcoded-255
pmeier 2d0549d
Merge branch 'main' into fix-hardcoded-255
pmeier f594ceb
Merge branch 'main' into fix-hardcoded-255
pmeier 48603b0
add comment to float max value
pmeier a61d44f
Merge branch 'fix-hardcoded-255' of https://github.com/pmeier/vision …
pmeier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,12 +15,6 @@ def _assert_image_tensor(img: Tensor) -> None: | |
| raise TypeError("Tensor is not a torch image.") | ||
|
|
||
|
|
||
| def _assert_threshold(img: Tensor, threshold: float) -> None: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was only used once so I inlined it. |
||
| bound = 1 if img.is_floating_point() else 255 | ||
| if threshold > bound: | ||
| raise TypeError("Threshold should be less than bound of img.") | ||
|
|
||
|
|
||
| def get_dimensions(img: Tensor) -> List[int]: | ||
| _assert_image_tensor(img) | ||
| channels = 1 if img.ndim == 2 else img.shape[-3] | ||
|
|
@@ -212,19 +206,15 @@ def adjust_hue(img: Tensor, hue_factor: float) -> Tensor: | |
| return img | ||
|
|
||
| orig_dtype = img.dtype | ||
| if img.dtype == torch.uint8: | ||
| img = img.to(dtype=torch.float32) / 255.0 | ||
| img = convert_image_dtype(img, torch.float32) | ||
|
|
||
| img = _rgb2hsv(img) | ||
| h, s, v = img.unbind(dim=-3) | ||
| h = (h + hue_factor) % 1.0 | ||
| img = torch.stack((h, s, v), dim=-3) | ||
| img_hue_adj = _hsv2rgb(img) | ||
|
|
||
| if orig_dtype == torch.uint8: | ||
| img_hue_adj = (img_hue_adj * 255.0).to(dtype=orig_dtype) | ||
|
|
||
| return img_hue_adj | ||
| return convert_image_dtype(img_hue_adj, orig_dtype) | ||
|
|
||
|
|
||
| def adjust_saturation(img: Tensor, saturation_factor: float) -> Tensor: | ||
|
|
@@ -263,7 +253,7 @@ def adjust_gamma(img: Tensor, gamma: float, gain: float = 1) -> Tensor: | |
|
|
||
| def _blend(img1: Tensor, img2: Tensor, ratio: float) -> Tensor: | ||
| ratio = float(ratio) | ||
| bound = 1.0 if img1.is_floating_point() else 255.0 | ||
| bound = _max_value(img1.dtype) | ||
| return (ratio * img1 + (1.0 - ratio) * img2).clamp(0, bound).to(img1.dtype) | ||
|
|
||
|
|
||
|
|
@@ -775,8 +765,7 @@ def invert(img: Tensor) -> Tensor: | |
|
|
||
| _assert_channels(img, [1, 3]) | ||
|
|
||
| bound = torch.tensor(1 if img.is_floating_point() else 255, dtype=img.dtype, device=img.device) | ||
| return bound - img | ||
| return _max_value(img.dtype) - img | ||
|
|
||
|
|
||
| def posterize(img: Tensor, bits: int) -> Tensor: | ||
|
|
@@ -802,7 +791,8 @@ def solarize(img: Tensor, threshold: float) -> Tensor: | |
|
|
||
| _assert_channels(img, [1, 3]) | ||
|
|
||
| _assert_threshold(img, threshold) | ||
| if threshold > _max_value(img.dtype): | ||
| raise TypeError("Threshold should be less than bound of img.") | ||
|
|
||
| inverted_img = invert(img) | ||
| return torch.where(img >= threshold, inverted_img, img) | ||
|
|
@@ -849,7 +839,7 @@ def autocontrast(img: Tensor) -> Tensor: | |
|
|
||
| _assert_channels(img, [1, 3]) | ||
|
|
||
| bound = 1.0 if img.is_floating_point() else 255.0 | ||
| bound = _max_value(img.dtype) | ||
| dtype = img.dtype if torch.is_floating_point(img) else torch.float32 | ||
|
|
||
| minimum = img.amin(dim=(-2, -1), keepdim=True).to(dtype) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of doing the conversion manually, I've opted to use our kernel for this. Note that this also implicitly converts to
float32since the divisor is afloat.