From 2f28380eea7e934d8e33a8a5604dfaa9b80337bd Mon Sep 17 00:00:00 2001 From: clint Date: Wed, 3 Feb 2021 17:13:04 +0900 Subject: [PATCH 1/5] aspect ratio must be a sampling from log scale. reference from: https://github.com/pytorch/vision/blob/8317295c1d272e0ba7b2ce31e3fd2c048235fc73/torchvision/transforms/transforms.py#L833-L836 --- torchvision/transforms/transforms.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index 30911978558..adf46f22baa 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -1578,7 +1578,10 @@ def get_params( for _ in range(10): erase_area = area * torch.empty(1).uniform_(scale[0], scale[1]).item() - aspect_ratio = torch.empty(1).uniform_(ratio[0], ratio[1]).item() + log_ratio = torch.log(torch.tensor(ratio)) + aspect_ratio = torch.exp( + torch.empty(1).uniform_(log_ratio[0], log_ratio[1]) + ).item() h = int(round(math.sqrt(erase_area * aspect_ratio))) w = int(round(math.sqrt(erase_area / aspect_ratio))) From b3c3498a2e2c34bdd47160a70a509b55ce59d623 Mon Sep 17 00:00:00 2001 From: clint Date: Wed, 3 Feb 2021 17:49:43 +0900 Subject: [PATCH 2/5] add random erasing unittest code --- test/test_transforms.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/test_transforms.py b/test/test_transforms.py index 25d61fafeb4..53bf3e67637 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -1955,6 +1955,25 @@ def test_autoaugment(self): img = transform(img) transform.__repr__() + def test_random_erasing(self): + img = torch.ones(3, 128, 128) + + t = transforms.RandomErasing(scale=(0.1, 0.1), ratio=(1. / 3., 3. / 1.)) + y, x, h, w, v = t.get_params(img, t.scale, t.ratio, [t.value, ]) + aspect_ratio = h / w + self.assertTrue(aspect_ratio > 1. / 3. and aspect_ratio < 3. / 1.) + + aspect_ratios = [] + random.seed(42) + trial = 1000 + for _ in range(trial): + y, x, h, w, v = t.get_params(img, t.scale, t.ratio, [t.value, ]) + aspect_ratios.append(h / w) + + count_bigger_then_ones = len([1 for aspect_ratio in aspect_ratios if aspect_ratio > 1]) + count_smaller_then_ones = len([1 for aspect_ratio in aspect_ratios if aspect_ratio < 1]) + self.assertAlmostEqual(count_bigger_then_ones / trial, count_smaller_then_ones / trial, 2) + if __name__ == '__main__': unittest.main() From 78f129f34d03773575d7bacd337a0249424b38a3 Mon Sep 17 00:00:00 2001 From: clint Date: Wed, 3 Feb 2021 18:07:49 +0900 Subject: [PATCH 3/5] Increased threshold for difference in sampling rate --- test/test_transforms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_transforms.py b/test/test_transforms.py index 53bf3e67637..b92b73d21c7 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -1972,7 +1972,7 @@ def test_random_erasing(self): count_bigger_then_ones = len([1 for aspect_ratio in aspect_ratios if aspect_ratio > 1]) count_smaller_then_ones = len([1 for aspect_ratio in aspect_ratios if aspect_ratio < 1]) - self.assertAlmostEqual(count_bigger_then_ones / trial, count_smaller_then_ones / trial, 2) + self.assertAlmostEqual(count_bigger_then_ones / trial, count_smaller_then_ones / trial, 1) if __name__ == '__main__': From f7c88f4ec8ad2c30bff548e20bce4b4b33713b42 Mon Sep 17 00:00:00 2001 From: clint Date: Tue, 9 Feb 2021 10:47:45 +0900 Subject: [PATCH 4/5] move outside of the loop --- torchvision/transforms/transforms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index adf46f22baa..f6ae055a3a8 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -828,9 +828,9 @@ def get_params( width, height = F._get_image_size(img) area = height * width + log_ratio = torch.log(torch.tensor(ratio)) for _ in range(10): target_area = area * torch.empty(1).uniform_(scale[0], scale[1]).item() - log_ratio = torch.log(torch.tensor(ratio)) aspect_ratio = torch.exp( torch.empty(1).uniform_(log_ratio[0], log_ratio[1]) ).item() From 561b7b0038dc10cc16f369c5bcdd4d09f26bc7be Mon Sep 17 00:00:00 2001 From: clint Date: Tue, 9 Feb 2021 10:51:12 +0900 Subject: [PATCH 5/5] log_ratio move outside of the loop. RandomResizedCrop also --- torchvision/transforms/transforms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index f6ae055a3a8..916956e29fd 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -1576,9 +1576,9 @@ def get_params( img_c, img_h, img_w = img.shape[-3], img.shape[-2], img.shape[-1] area = img_h * img_w + log_ratio = torch.log(torch.tensor(ratio)) for _ in range(10): erase_area = area * torch.empty(1).uniform_(scale[0], scale[1]).item() - log_ratio = torch.log(torch.tensor(ratio)) aspect_ratio = torch.exp( torch.empty(1).uniform_(log_ratio[0], log_ratio[1]) ).item()