Skip to content

Instantly share code, notes, and snippets.

@remi-or
Created June 4, 2025 18:07
Show Gist options
  • Save remi-or/eb8936ca093d54c186fb5b67f15334eb to your computer and use it in GitHub Desktop.
Save remi-or/eb8936ca093d54c186fb5b67f15334eb to your computer and use it in GitHub Desktop.
Manual resize comparaison
from typing import Tuple
import torch
from torch import Tensor
from torchvision.transforms.v2 import functional as F
def reference_resize(image: Tensor, new_size: Tuple[int, int]) -> Tensor:
image = F.resize(image, new_size, interpolation=F.InterpolationMode.BICUBIC, antialias=True)
return image
def new_resize(image: Tensor, new_size: Tuple[int, int], normalizing_constant: int) -> Tensor:
image = image.float() / normalizing_constant
image = F.resize(image, new_size, interpolation=F.InterpolationMode.BICUBIC, antialias=True)
image = image * normalizing_constant
image = image.masked_fill(image > 255, 255)
image = image.masked_fill(image < 0, 0)
image = image.round().to(torch.uint8)
return image
if __name__ == "__main__":
torch.manual_seed(0)
input_size = (3, 41, 70)
image = torch.randn(input_size).mul(256).to(torch.uint8).to("cuda:0")
y_ref = reference_resize(image, (384, 384))
y_255 = new_resize(image, (384, 384), normalizing_constant=255)
y_256 = new_resize(image, (384, 384), normalizing_constant=256)
print(f"Max difference with 255: {y_ref.float().sub(y_255.float()).abs().max().item()}")
print(f"Max difference with 256: {y_ref.float().sub(y_256.float()).abs().max().item()}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment