From 776af02b2c433398591c47c892b866fd5f9ecc9f Mon Sep 17 00:00:00 2001 From: Zhun Zhong Date: Fri, 5 Jul 2019 21:57:43 +1000 Subject: [PATCH 1/7] Fix bug to Random Erasing 1. Avoid forever loop for getting parameters of erase. 2. replace' img_b' by 'img_c', because it indicates the channel. 3. replace v = torch.rand([img_c, h, w]) by v = torch.empty([img_c, h, w], dtype=torch.float32).normal_(). Normally distributed achieves better performance. --- torchvision/transforms/transforms.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index e40baa6c274..25bbe377077 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -1250,10 +1250,10 @@ def get_params(img, scale, ratio, value=0): Returns: tuple: params (i, j, h, w, v) to be passed to ``erase`` for random erasing. """ - img_b, img_h, img_w = img.shape + img_c, img_h, img_w = img.shape area = img_h * img_w - while True: + for attempt in range(10): erase_area = random.uniform(scale[0], scale[1]) * area aspect_ratio = random.uniform(ratio[0], ratio[1]) @@ -1266,11 +1266,14 @@ def get_params(img, scale, ratio, value=0): if isinstance(value, numbers.Number): v = value elif isinstance(value, torch._six.string_classes): - v = torch.rand(img_b, h, w) + v = torch.empty([img_c, h, w], dtype=torch.float32).normal_() elif isinstance(value, (list, tuple)): v = torch.tensor(value, dtype=torch.float32).view(-1, 1, 1).expand(-1, h, w) return i, j, h, w, v + # Return original image + return 0, 0, img_h, img_w, img + def __call__(self, img): """ Args: From 9f0ad38d37b9a774d23a4e94cc6cd16f61f0085b Mon Sep 17 00:00:00 2001 From: Zhun Zhong Date: Fri, 5 Jul 2019 22:38:11 +1000 Subject: [PATCH 2/7] add test --- test/test_transforms.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/test_transforms.py b/test/test_transforms.py index 2968282cfc1..6061c9f0357 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -1378,6 +1378,11 @@ def test_random_erasing(self): # Test Set 5: Testing the inplace behaviour img_re = transforms.RandomErasing(value=(0.2), inplace=True)(img) assert torch.equal(img_re, img) + + # Test Set 6: Checking when no erased region is selected + img = torch.rand([3, 300, 1]) + img_re = transforms.RandomErasing(ratio=(0.3, 1.0), value='random')(img) + assert torch.equal(img_re, img) if __name__ == '__main__': From adba82b14d686b4da85e36d80717586bfa134762 Mon Sep 17 00:00:00 2001 From: Zhun Zhong Date: Fri, 5 Jul 2019 23:01:54 +1000 Subject: [PATCH 3/7] Update test_transforms.py --- 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 6061c9f0357..36702528717 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -1378,7 +1378,7 @@ def test_random_erasing(self): # Test Set 5: Testing the inplace behaviour img_re = transforms.RandomErasing(value=(0.2), inplace=True)(img) assert torch.equal(img_re, img) - + # Test Set 6: Checking when no erased region is selected img = torch.rand([3, 300, 1]) img_re = transforms.RandomErasing(ratio=(0.3, 1.0), value='random')(img) From 4f5cb6935c7aa25e44a2efa4cdda1481115e81c7 Mon Sep 17 00:00:00 2001 From: Zhun Zhong Date: Fri, 5 Jul 2019 23:02:53 +1000 Subject: [PATCH 4/7] Update transforms.py --- 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 25bbe377077..4dfc7334fe0 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -1223,7 +1223,7 @@ class RandomErasing(object): >>> ]) """ - def __init__(self, p=0.5, scale=(0.02, 0.33), ratio=(0.3, 1. / 0.3), value=0, inplace=False): + def __init__(self, p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3), value=0, inplace=False): assert isinstance(value, (numbers.Number, str, tuple, list)) if (scale[0] > scale[1]) or (ratio[0] > ratio[1]): warnings.warn("range should be of kind (min, max)") From e89746dfe134212b05450d70c847d5ddcba763c1 Mon Sep 17 00:00:00 2001 From: Zhun Zhong Date: Fri, 5 Jul 2019 23:49:07 +1000 Subject: [PATCH 5/7] Update test_transforms.py --- 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 36702528717..404b16cae08 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -1381,7 +1381,7 @@ def test_random_erasing(self): # Test Set 6: Checking when no erased region is selected img = torch.rand([3, 300, 1]) - img_re = transforms.RandomErasing(ratio=(0.3, 1.0), value='random')(img) + img_re = transforms.RandomErasing(ratio=(0.1, 0.2), value='random')(img) assert torch.equal(img_re, img) From aeb69753b48d715e9ae044f71bf37859fd072eb4 Mon Sep 17 00:00:00 2001 From: Zhun Zhong Date: Sat, 6 Jul 2019 02:18:35 +1000 Subject: [PATCH 6/7] Update transforms.py --- 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 4dfc7334fe0..964504eb9dc 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -1210,7 +1210,7 @@ class RandomErasing(object): erase all pixels. If a tuple of length 3, it is used to erase R, G, B channels respectively. If a str of 'random', erasing each pixel with random values. - inplace: boolean to make this transform inplace.Default set to False. + inplace: boolean to make this transform inplace. Default set to False. Returns: Erased Image. From 5751953b6b37fa87e7bd5ca5daed7a93b834eb33 Mon Sep 17 00:00:00 2001 From: Zhun Zhong Date: Sat, 6 Jul 2019 02:20:42 +1000 Subject: [PATCH 7/7] Update functional.py --- torchvision/transforms/functional.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/transforms/functional.py b/torchvision/transforms/functional.py index 14448e01b00..b11cac5a440 100644 --- a/torchvision/transforms/functional.py +++ b/torchvision/transforms/functional.py @@ -828,7 +828,7 @@ def erase(img, i, j, h, w, v, inplace=False): h (int): Height of the erased region. w (int): Width of the erased region. v: Erasing value. - inplace(bool,optional): For in-place operations. By default is set False. + inplace(bool, optional): For in-place operations. By default is set False. Returns: Tensor Image: Erased image.