vision icon indicating copy to clipboard operation
vision copied to clipboard

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

Open Alek96 opened this issue 4 months ago • 2 comments

🐛 Describe the bug

When resizing tensor wrapped in tv_tensors.Mask interpolation mode NEAREST_EXACT is changed to NEAREST

import torch
import torch.nn.functional as F
import torchvision.transforms.v2 as T
import torchvision.transforms.v2.functional as TF
from torchvision import tv_tensors


def compare(mask, size):
    mask_nearest = TF.resize(mask, size=size, interpolation=T.InterpolationMode.NEAREST)
    mask_nearest_exact = TF.resize(mask, size=size, interpolation=T.InterpolationMode.NEAREST_EXACT)
    mask_nearest_interpolate = F.interpolate(mask.unsqueeze(0), size=size, mode='nearest').squeeze(0)
    mask_nearest_exact_interpolate = F.interpolate(mask.unsqueeze(0), size=size, mode='nearest-exact').squeeze(0)
    
    print(torch.equal(mask_nearest, mask_nearest_exact))
    print(torch.equal(mask_nearest_interpolate, mask_nearest_exact_interpolate))
    print(torch.equal(mask_nearest, mask_nearest_interpolate))
    print(torch.equal(mask_nearest_exact, mask_nearest_exact_interpolate))
    

height = 64
width = 64
new_height = 16
new_width = 16
mask = torch.randint(low=0, high=2, size=(1, height, width)).float()

compare(mask, (new_height, new_width))
print()

mask = tv_tensors.Mask(mask)
compare(mask, (new_height, new_width))
False
False
True
True

True
False
True
False

Versions

torch = "2.8.0" torchvision = "0.23.0"

Alek96 avatar Aug 25 '25 13:08 Alek96

Thanks for the report @Alek96 . I don't know/remember why nearest-exact wasn't exposed for masks in v2, but we should probably enable it.

NicolasHug avatar Aug 26 '25 08:08 NicolasHug

For context on nearest vs nearest exact: https://github.com/pytorch/vision/issues/6645

Mode mode='nearest-exact' matches Scikit-Image and PIL nearest neighbours interpolation algorithms and fixes known issues with mode='nearest'. This mode is introduced to keep backward compatibility. Mode mode='nearest' matches buggy OpenCV's INTER_NEAREST interpolation algorithm.

NicolasHug avatar Aug 26 '25 08:08 NicolasHug