Fix resize_mask to honor NEAREST_EXACT interpolation#9497
Open
joannemiki57 wants to merge 1 commit into
Open
Conversation
Closes pytorch#9188. `resize_mask` previously hardcoded `InterpolationMode.NEAREST` when calling `resize_image`, so `F.resize` on a `tv_tensors.Mask` produced identical output regardless of whether the user passed `InterpolationMode.NEAREST` or `InterpolationMode.NEAREST_EXACT`. This made the two modes effectively the same for masks, which is inconsistent with the rest of the API and was flagged in pytorch#9188 as a bug. This change: - Adds `interpolation` to the `resize_mask` and `_resize_mask_dispatch` signatures in the order/position expected by `F.resize` (`mask, size, interpolation, max_size`). - Uses `Union[str, InterpolationMode, int]` annotation and `InterpolationMode.BILINEAR` default so the signature matches `F.resize` and `test_functional_signature[F.resize_mask-Mask]` passes. - Validates the interpolation via the existing `_check_interpolation` helper and coerces anything other than `NEAREST` / `NEAREST_EXACT` to `NEAREST`, preserving the historical behavior for callers that previously relied on the argument being ignored. - Uses chained `!=` / `and` comparisons (no set literals) so the function remains TorchScript-compatible. - Extends `TestResize.test_kernel_mask` to parametrize over `NEAREST` and `NEAREST_EXACT`, smoke-testing eager, scripted, batched, and CUDA paths for both modes. A prior attempt at this fix is open as pytorch#9384; the maintainer review there spelled out the signature, set-literal, and default-value issues. This PR addresses each of those notes from scratch. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9497
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #9188.
The
interpolationargument was effectively ignored fortv_tensors.Maskinputs.resize_maskhardcodedInterpolationMode.NEARESTwhen callingresize_image, soF.resize(mask, size, interpolation=InterpolationMode.NEAREST_EXACT)produced the same output as plainNEAREST, contrary to what the docs imply.Reproduction (before this PR):
After this PR,
bis computed withnearest-exactand the two tensors differ on inputs where the two algorithms disagree.What changed
interpolationparameter toresize_maskand_resize_mask_dispatch, in the position expected byF.resize(mask, size, interpolation, max_size) and with the same annotation and default (Union[str, InterpolationMode, int] = InterpolationMode.BILINEAR) sotest_functional_signature[F.resize_mask-Mask]passes._check_interpolationhelper. Anything other thanNEAREST/NEAREST_EXACTis silently coerced toNEAREST, preserving the historical behavior of callers that previously relied on the argument being ignored. Comparisons are chained!=/and(no set literals) so the function stays TorchScript-compatible.TestResize.test_kernel_maskto parametrize overNEARESTandNEAREST_EXACT, exercising eager / scripted / batched / CUDA viacheck_kernel.A prior attempt at the same fix is open as #9384; the maintainer review there spelled out the signature ordering, set-literal, and default-value issues that this PR addresses from scratch.
Test plan
pytest test/test_transforms_v2.py -k "TestResize and (test_kernel_mask or test_functional_signature)" -vcovers the new parametrization and the signature contract (to be exercised on CI).resize_mask(resized_crop_maskat_geometry.py:2845) pass(mask, size)positionally and are backward-compatible with the new signature.