Skip to content

Fix resize_mask to honor NEAREST_EXACT interpolation#9497

Open
joannemiki57 wants to merge 1 commit into
pytorch:mainfrom
joannemiki57:fix/resize-mask-nearest-exact
Open

Fix resize_mask to honor NEAREST_EXACT interpolation#9497
joannemiki57 wants to merge 1 commit into
pytorch:mainfrom
joannemiki57:fix/resize-mask-nearest-exact

Conversation

@joannemiki57
Copy link
Copy Markdown

Closes #9188.

The interpolation argument was effectively ignored for tv_tensors.Mask inputs. resize_mask hardcoded InterpolationMode.NEAREST when calling resize_image, so F.resize(mask, size, interpolation=InterpolationMode.NEAREST_EXACT) produced the same output as plain NEAREST, contrary to what the docs imply.

Reproduction (before this PR):

import torch
from torchvision import tv_tensors
from torchvision.transforms.v2 import functional as F, InterpolationMode

mask = tv_tensors.Mask(torch.randint(0, 4, (1, 7, 11)))
a = F.resize(mask, size=[14, 22], interpolation=InterpolationMode.NEAREST)
b = F.resize(mask, size=[14, 22], interpolation=InterpolationMode.NEAREST_EXACT)
assert torch.equal(a, b)   # passes — bug: NEAREST_EXACT was a no-op

After this PR, b is computed with nearest-exact and the two tensors differ on inputs where the two algorithms disagree.

What changed

  • Add an interpolation parameter to resize_mask and _resize_mask_dispatch, in the position expected by F.resize (mask, size, interpolation, max_size) and with the same annotation and default (Union[str, InterpolationMode, int] = InterpolationMode.BILINEAR) so test_functional_signature[F.resize_mask-Mask] passes.
  • Validate through the existing _check_interpolation helper. Anything other than NEAREST / NEAREST_EXACT is silently coerced to NEAREST, 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.
  • Extend TestResize.test_kernel_mask to parametrize over NEAREST and NEAREST_EXACT, exercising eager / scripted / batched / CUDA via check_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)" -v covers the new parametrization and the signature contract (to be exercised on CI).
  • Existing call sites of resize_mask (resized_crop_mask at _geometry.py:2845) pass (mask, size) positionally and are backward-compatible with the new signature.

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>
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 22, 2026

🔗 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.

@meta-cla meta-cla Bot added the cla signed label May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InterpolationMode.NEAREST_EXACT and InterpolationMode.NEAREST are the same for tv_tensors.Mask

1 participant