Skip to content

Commit

Permalink
WIP Added antialias option to transforms.functional.resize
Browse files Browse the repository at this point in the history
  • Loading branch information
vfdev-5 committed May 3, 2021
1 parent 730c5e1 commit 9257c2e
Show file tree
Hide file tree
Showing 6 changed files with 577 additions and 4 deletions.
44 changes: 44 additions & 0 deletions test/test_functional_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,50 @@ def test_resize(self):
with self.assertRaisesRegex(ValueError, "max_size = 32 must be strictly greater"):
F.resize(img, size=32, max_size=32)

def test_resize_antialias(self):
script_fn = torch.jit.script(F.resize)
tensor, pil_img = self._create_data(320, 290, device=self.device)

for dt in [None, torch.float32, torch.float64, torch.float16]:

if dt == torch.float16 and torch.device(self.device).type == "cpu":
# skip float16 on CPU case
continue

if dt is not None:
# This is a trivial cast to float of uint8 data to test all cases
tensor = tensor.to(dt)

for size in [[96, 72], ]:
for interpolation in [BILINEAR, ]:
resized_tensor = F.resize(tensor, size=size, interpolation=interpolation, antialias=True)
resized_pil_img = F.resize(pil_img, size=size, interpolation=interpolation)

self.assertEqual(
resized_tensor.size()[1:], resized_pil_img.size[::-1],
msg=f"{size}, {interpolation}, {dt}"
)

resized_tensor_f = resized_tensor
# we need to cast to uint8 to compare with PIL image
if resized_tensor_f.dtype == torch.uint8:
resized_tensor_f = resized_tensor_f.to(torch.float)

self.approxEqualTensorToPIL(
resized_tensor_f, resized_pil_img, tol=0.5, msg=f"{size}, {interpolation}, {dt}"
)
self.approxEqualTensorToPIL(
resized_tensor_f, resized_pil_img, tol=1.0 + 1e-5, agg_method="max", msg=f"{size}, {interpolation}, {dt}"
)

if isinstance(size, int):
script_size = [size, ]
else:
script_size = size

resize_result = script_fn(tensor, size=script_size, interpolation=interpolation, antialias=True)
self.assertTrue(resized_tensor.equal(resize_result), msg="{}, {}".format(size, interpolation))

def test_resized_crop(self):
# test values of F.resized_crop in several cases:
# 1) resize to the same size, crop to the same size => should be identity
Expand Down
Loading

0 comments on commit 9257c2e

Please sign in to comment.