|
14 | 14 | cpu_and_gpu,
|
15 | 15 | assert_equal,
|
16 | 16 | )
|
| 17 | +from PIL import Image |
17 | 18 | from torchvision import transforms as T
|
18 | 19 | from torchvision.transforms import InterpolationMode
|
19 | 20 | from torchvision.transforms import functional as F
|
| 21 | +from torchvision.transforms.autoaugment import _apply_op |
20 | 22 |
|
21 | 23 | NEAREST, BILINEAR, BICUBIC = InterpolationMode.NEAREST, InterpolationMode.BILINEAR, InterpolationMode.BICUBIC
|
22 | 24 |
|
@@ -725,6 +727,48 @@ def test_autoaugment_save(augmentation, tmpdir):
|
725 | 727 | s_transform.save(os.path.join(tmpdir, "t_autoaugment.pt"))
|
726 | 728 |
|
727 | 729 |
|
| 730 | +@pytest.mark.parametrize("interpolation", [F.InterpolationMode.NEAREST, F.InterpolationMode.BILINEAR]) |
| 731 | +@pytest.mark.parametrize("mode", ["X", "Y"]) |
| 732 | +def test_autoaugment__op_apply_shear(interpolation, mode): |
| 733 | + # We check that torchvision's implementation of shear is equivalent |
| 734 | + # to official CIFAR10 autoaugment implementation: |
| 735 | + # https://github.com/tensorflow/models/blob/885fda091c46c59d6c7bb5c7e760935eacc229da/research/autoaugment/augmentation_transforms.py#L273-L290 |
| 736 | + image_size = 32 |
| 737 | + |
| 738 | + def shear(pil_img, level, mode, resample): |
| 739 | + if mode == "X": |
| 740 | + matrix = (1, level, 0, 0, 1, 0) |
| 741 | + elif mode == "Y": |
| 742 | + matrix = (1, 0, 0, level, 1, 0) |
| 743 | + return pil_img.transform((image_size, image_size), Image.AFFINE, matrix, resample=resample) |
| 744 | + |
| 745 | + t_img, pil_img = _create_data(image_size, image_size) |
| 746 | + |
| 747 | + resample_pil = { |
| 748 | + F.InterpolationMode.NEAREST: Image.NEAREST, |
| 749 | + F.InterpolationMode.BILINEAR: Image.BILINEAR, |
| 750 | + }[interpolation] |
| 751 | + |
| 752 | + level = 0.3 |
| 753 | + expected_out = shear(pil_img, level, mode=mode, resample=resample_pil) |
| 754 | + |
| 755 | + # Check pil output vs expected pil |
| 756 | + out = _apply_op(pil_img, op_name=f"Shear{mode}", magnitude=level, interpolation=interpolation, fill=0) |
| 757 | + assert out == expected_out |
| 758 | + |
| 759 | + if interpolation == F.InterpolationMode.BILINEAR: |
| 760 | + # We skip bilinear mode for tensors as |
| 761 | + # affine transformation results are not exactly the same |
| 762 | + # between tensors and pil images |
| 763 | + # MAE as around 1.40 |
| 764 | + # Max Abs error can be 163 or 170 |
| 765 | + return |
| 766 | + |
| 767 | + # Check tensor output vs expected pil |
| 768 | + out = _apply_op(t_img, op_name=f"Shear{mode}", magnitude=level, interpolation=interpolation, fill=0) |
| 769 | + _assert_approx_equal_tensor_to_pil(out, expected_out) |
| 770 | + |
| 771 | + |
728 | 772 | @pytest.mark.parametrize("device", cpu_and_gpu())
|
729 | 773 | @pytest.mark.parametrize(
|
730 | 774 | "config",
|
|
0 commit comments