Repository navigation
fix(filters): sharpen integer images in float32 in unsharp_mask - #5946
Conversation
ducha-aiki
left a comment
There was a problem hiding this comment.
Thanks @RavSinghChandan, this is the right fix for the unsharp_mask entry of #5155. gaussian_blur2d already promotes an integer image, so torch.lerp got a float32 blur and the original integer tensor and raised. Promoting with _to_floating first follows the same rule as the other filters: integer and bool images give float32 on their own scale, and floating images keep their dtype.
On this head merged with current main: uint8, int8, int16, int32, int64 and bool inputs return float32 equal to the float32 call, and float16, bfloat16 and float64 keep their dtype. A fullgraph aot_eager compile of the uint8 call matches eager. tests/filters/test_unsharp_mask.py passes on CPU in float32, float64 and float16 and on MPS in float32. With main's unsharp.py, the new test fails for all three dtypes. The fragment is right as fixed.
One request: the description has no "How did AI help?" section from the pull request template. Please add one, even if the answer is that no AI was used. Heads-up: #5974 moves spatial_gradient and sobel within the same conventions.rst sentence. Whichever merges second gets a conflict, which we will resolve. Approving.
Posted on behalf of @ducha-aiki by Claude (Opus 5.5).
What does this pull request do?
unsharp_maskandUnsharpMaskraisedRuntimeError: expected dtype float for end but got dtype unsigned charfor a uint8 image, becausetorch.lerpmixes the float32 blur fromgaussian_blur2dwith the integer input. The image is now promoted with the existing_to_floatinghelper first, likeguided_blur(#5890), so an integer or bool image is sharpened in float32 and returned as float32 on its own scale, matchinggaussian_blur2d. Floating images are unchanged. The conventions page now listsunsharp_maskwith the filters that follow this rule.Related issue or discussion
Relates to #5155 (
unsharp_maskwas listed as not following the integer convention yet).What did you check?
New
test_integer_input_matches_float32_5155(uint8, int16, bool) fails before the change and passes after.