-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Change default of antialias parameter from None to 'warn' #7160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
71283e8
d21e2c2
ca7932a
e5f619e
4e3624d
3eb3e30
c1d39e7
daacc46
911db0c
b4921e3
f6c8140
22fd450
09226ac
8e5e8b6
fc6375b
5a799fa
60c4165
058c6b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
import itertools | ||
import math | ||
import os | ||
import warnings | ||
from functools import partial | ||
from typing import Sequence | ||
|
||
|
@@ -483,8 +484,8 @@ def test_resize(device, dt, size, max_size, interpolation): | |
tensor = tensor.to(dt) | ||
batch_tensors = batch_tensors.to(dt) | ||
|
||
resized_tensor = F.resize(tensor, size=size, interpolation=interpolation, max_size=max_size) | ||
resized_pil_img = F.resize(pil_img, size=size, interpolation=interpolation, max_size=max_size) | ||
resized_tensor = F.resize(tensor, size=size, interpolation=interpolation, max_size=max_size, antialias=True) | ||
resized_pil_img = F.resize(pil_img, size=size, interpolation=interpolation, max_size=max_size, antialias=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: As we now doing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I'll merge now to unblock, and I'll open a PR to check the CI on a lower tol |
||
|
||
assert resized_tensor.size()[1:] == resized_pil_img.size[::-1] | ||
|
||
|
@@ -509,10 +510,12 @@ def test_resize(device, dt, size, max_size, interpolation): | |
else: | ||
script_size = size | ||
|
||
resize_result = script_fn(tensor, size=script_size, interpolation=interpolation, max_size=max_size) | ||
resize_result = script_fn(tensor, size=script_size, interpolation=interpolation, max_size=max_size, antialias=True) | ||
assert_equal(resized_tensor, resize_result) | ||
|
||
_test_fn_on_batch(batch_tensors, F.resize, size=script_size, interpolation=interpolation, max_size=max_size) | ||
_test_fn_on_batch( | ||
batch_tensors, F.resize, size=script_size, interpolation=interpolation, max_size=max_size, antialias=True | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("device", cpu_and_gpu()) | ||
|
@@ -547,7 +550,7 @@ def test_resize_antialias(device, dt, size, interpolation): | |
tensor = tensor.to(dt) | ||
|
||
resized_tensor = F.resize(tensor, size=size, interpolation=interpolation, antialias=True) | ||
resized_pil_img = F.resize(pil_img, size=size, interpolation=interpolation) | ||
resized_pil_img = F.resize(pil_img, size=size, interpolation=interpolation, antialias=True) | ||
|
||
assert resized_tensor.size()[1:] == resized_pil_img.size[::-1] | ||
|
||
|
@@ -596,6 +599,23 @@ def test_assert_resize_antialias(interpolation): | |
F.resize(tensor, size=(5, 5), interpolation=interpolation, antialias=True) | ||
|
||
|
||
def test_resize_antialias_default_warning(): | ||
|
||
img = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8) | ||
|
||
match = "The default value of the antialias" | ||
with pytest.warns(UserWarning, match=match): | ||
F.resize(img, size=(20, 20)) | ||
with pytest.warns(UserWarning, match=match): | ||
F.resized_crop(img, 0, 0, 10, 10, size=(20, 20)) | ||
|
||
# For modes that aren't bicubic or bilinear, don't throw a warning | ||
with warnings.catch_warnings(): | ||
warnings.simplefilter("error") | ||
F.resize(img, size=(20, 20), interpolation=NEAREST) | ||
F.resized_crop(img, 0, 0, 10, 10, size=(20, 20), interpolation=NEAREST) | ||
|
||
|
||
@pytest.mark.parametrize("device", cpu_and_gpu()) | ||
@pytest.mark.parametrize("dt", [torch.float32, torch.float64, torch.float16]) | ||
@pytest.mark.parametrize("size", [[10, 7], [10, 42], [42, 7]]) | ||
|
@@ -924,7 +944,9 @@ def test_resized_crop(device, mode): | |
# 1) resize to the same size, crop to the same size => should be identity | ||
tensor, _ = _create_data(26, 36, device=device) | ||
|
||
out_tensor = F.resized_crop(tensor, top=0, left=0, height=26, width=36, size=[26, 36], interpolation=mode) | ||
out_tensor = F.resized_crop( | ||
tensor, top=0, left=0, height=26, width=36, size=[26, 36], interpolation=mode, antialias=True | ||
) | ||
assert_equal(tensor, out_tensor, msg=f"{out_tensor[0, :5, :5]} vs {tensor[0, :5, :5]}") | ||
|
||
# 2) resize by half and crop a TL corner | ||
|
@@ -939,7 +961,14 @@ def test_resized_crop(device, mode): | |
|
||
batch_tensors = _create_data_batch(26, 36, num_samples=4, device=device) | ||
_test_fn_on_batch( | ||
batch_tensors, F.resized_crop, top=1, left=2, height=20, width=30, size=[10, 15], interpolation=NEAREST | ||
batch_tensors, | ||
F.resized_crop, | ||
top=1, | ||
left=2, | ||
height=20, | ||
width=30, | ||
size=[10, 15], | ||
interpolation=NEAREST, | ||
) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1050,5 +1050,25 @@ def test_raft(model_fn, scripted): | |
_assert_expected(flow_pred.cpu(), name=model_fn.__name__, atol=1e-2, rtol=1) | ||
|
||
|
||
def test_presets_antialias(): | ||
|
||
img = torch.randint(0, 256, size=(1, 3, 224, 224), dtype=torch.uint8) | ||
|
||
match = "The default value of the antialias parameter" | ||
with pytest.warns(UserWarning, match=match): | ||
models.ResNet18_Weights.DEFAULT.transforms()(img) | ||
with pytest.warns(UserWarning, match=match): | ||
models.segmentation.DeepLabV3_ResNet50_Weights.DEFAULT.transforms()(img) | ||
|
||
with warnings.catch_warnings(): | ||
warnings.simplefilter("error") | ||
models.ResNet18_Weights.DEFAULT.transforms(antialias=True)(img) | ||
models.segmentation.DeepLabV3_ResNet50_Weights.DEFAULT.transforms(antialias=True)(img) | ||
|
||
models.detection.FasterRCNN_ResNet50_FPN_Weights.DEFAULT.transforms()(img) | ||
models.video.R3D_18_Weights.DEFAULT.transforms()(img) | ||
models.optical_flow.Raft_Small_Weights.DEFAULT.transforms()(img, img) | ||
Comment on lines
+1068
to
+1070
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For these 3, AFAICT, |
||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main([__file__]) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import itertools | ||
import re | ||
import warnings | ||
from collections import defaultdict | ||
|
||
import numpy as np | ||
|
@@ -94,7 +95,7 @@ def parametrize_from_transforms(*transforms): | |
class TestSmoke: | ||
@parametrize_from_transforms( | ||
transforms.RandomErasing(p=1.0), | ||
transforms.Resize([16, 16]), | ||
transforms.Resize([16, 16], antialias=True), | ||
transforms.CenterCrop([16, 16]), | ||
transforms.ConvertDtype(), | ||
transforms.RandomHorizontalFlip(), | ||
|
@@ -210,7 +211,7 @@ def test_normalize(self, transform, input): | |
@parametrize( | ||
[ | ||
( | ||
transforms.RandomResizedCrop([16, 16]), | ||
transforms.RandomResizedCrop([16, 16], antialias=True), | ||
itertools.chain( | ||
make_images(extra_dims=[(4,)]), | ||
make_vanilla_tensor_images(), | ||
|
@@ -1991,6 +1992,70 @@ def test__transform(self, inpt): | |
assert output.dtype == inpt.dtype | ||
|
||
|
||
# TODO: remove this test in 0.17 when the default of antialias changes to True | ||
def test_antialias_warning(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm mixing the functional and class tests here... Hope that's fine (I found it to be a awkward to scatter them across so many files as done in the V1 tests) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine by me. Test is self-contained enough that there shouldn't be any confusion about it later. |
||
pil_img = PIL.Image.new("RGB", size=(10, 10), color=127) | ||
tensor_img = torch.randint(0, 256, size=(3, 10, 10), dtype=torch.uint8) | ||
tensor_video = torch.randint(0, 256, size=(2, 3, 10, 10), dtype=torch.uint8) | ||
|
||
match = "The default value of the antialias parameter" | ||
with pytest.warns(UserWarning, match=match): | ||
transforms.Resize((20, 20))(tensor_img) | ||
with pytest.warns(UserWarning, match=match): | ||
transforms.RandomResizedCrop((20, 20))(tensor_img) | ||
with pytest.warns(UserWarning, match=match): | ||
transforms.ScaleJitter((20, 20))(tensor_img) | ||
with pytest.warns(UserWarning, match=match): | ||
transforms.RandomShortestSize((20, 20))(tensor_img) | ||
with pytest.warns(UserWarning, match=match): | ||
transforms.RandomResize(10, 20)(tensor_img) | ||
|
||
with pytest.warns(UserWarning, match=match): | ||
transforms.functional.resize(tensor_img, (20, 20)) | ||
with pytest.warns(UserWarning, match=match): | ||
transforms.functional.resize_image_tensor(tensor_img, (20, 20)) | ||
|
||
with pytest.warns(UserWarning, match=match): | ||
transforms.functional.resize(tensor_video, (20, 20)) | ||
with pytest.warns(UserWarning, match=match): | ||
transforms.functional.resize_video(tensor_video, (20, 20)) | ||
|
||
with pytest.warns(UserWarning, match=match): | ||
datapoints.Image(tensor_img).resize((20, 20)) | ||
with pytest.warns(UserWarning, match=match): | ||
datapoints.Image(tensor_img).resized_crop(0, 0, 10, 10, (20, 20)) | ||
|
||
with pytest.warns(UserWarning, match=match): | ||
datapoints.Video(tensor_video).resize((20, 20)) | ||
with pytest.warns(UserWarning, match=match): | ||
datapoints.Video(tensor_video).resized_crop(0, 0, 10, 10, (20, 20)) | ||
|
||
with warnings.catch_warnings(): | ||
warnings.simplefilter("error") | ||
transforms.Resize((20, 20))(pil_img) | ||
transforms.RandomResizedCrop((20, 20))(pil_img) | ||
transforms.ScaleJitter((20, 20))(pil_img) | ||
transforms.RandomShortestSize((20, 20))(pil_img) | ||
transforms.RandomResize(10, 20)(pil_img) | ||
transforms.functional.resize(pil_img, (20, 20)) | ||
|
||
transforms.Resize((20, 20), antialias=True)(tensor_img) | ||
transforms.RandomResizedCrop((20, 20), antialias=True)(tensor_img) | ||
transforms.ScaleJitter((20, 20), antialias=True)(tensor_img) | ||
transforms.RandomShortestSize((20, 20), antialias=True)(tensor_img) | ||
transforms.RandomResize(10, 20, antialias=True)(tensor_img) | ||
|
||
transforms.functional.resize(tensor_img, (20, 20), antialias=True) | ||
transforms.functional.resize_image_tensor(tensor_img, (20, 20), antialias=True) | ||
transforms.functional.resize(tensor_video, (20, 20), antialias=True) | ||
transforms.functional.resize_video(tensor_video, (20, 20), antialias=True) | ||
|
||
datapoints.Image(tensor_img).resize((20, 20), antialias=True) | ||
datapoints.Image(tensor_img).resized_crop(0, 0, 10, 10, (20, 20), antialias=True) | ||
datapoints.Video(tensor_video).resize((20, 20), antialias=True) | ||
datapoints.Video(tensor_video).resized_crop(0, 0, 10, 10, (20, 20), antialias=True) | ||
|
||
|
||
@pytest.mark.parametrize("image_type", (PIL.Image, torch.Tensor, datapoints.Image)) | ||
@pytest.mark.parametrize("label_type", (torch.Tensor, int)) | ||
@pytest.mark.parametrize("dataset_return_type", (dict, tuple)) | ||
|
Uh oh!
There was an error while loading. Please reload this page.