Skip to content

Improved consistency tests for AA with all policies #7274

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions test/test_prototype_transforms_consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,18 +1021,37 @@ def test_augmix(self, inpt, interpolation, mocker):
PIL.Image.NEAREST,
],
)
def test_aa(self, inpt, interpolation):
aa_policy = legacy_transforms.AutoAugmentPolicy("imagenet")
@pytest.mark.parametrize(
"policy", ["imagenet", "cifar10", "svhn"]
)
def test_aa(self, inpt, interpolation, policy, mocker):
aa_policy = legacy_transforms.AutoAugmentPolicy(policy)
t_ref = legacy_transforms.AutoAugment(aa_policy, interpolation=interpolation)
t = v2_transforms.AutoAugment(aa_policy, interpolation=interpolation)

torch.manual_seed(12)
expected_output = t_ref(inpt)
le = len(t._AUGMENTATION_SPACE)

torch.manual_seed(12)
output = t(inpt)
policy_ids = []
for i in range(le):
policy_ids.append(i)
policy_ids.append(i)
policy_ids = iter(policy_ids)

assert_equal(expected_output, output)
def torch_randint_side_effect(*args, **kwargs):
arg0 = args[0]
assert isinstance(arg0, int)
if arg0 > 2:
v = next(policy_ids)
return torch.tensor(v)
return torch.zeros(*args[1:], **kwargs)

mocker.patch("torch.randint", side_effect=torch_randint_side_effect)
mocker.patch("torch.rand", side_effect=lambda *args, **kwargs: torch.zeros(*args, **kwargs))

for _ in range(le):
expected_output = t_ref(inpt)
output = t(inpt)
assert_equal(expected_output, output, atol=2, rtol=0) # set atol=2 as tests are flaky


def import_transforms_from_references(reference):
Expand Down