From e0874c7e966175a6b2c35c3cf58e9b52995ee930 Mon Sep 17 00:00:00 2001 From: Francisco Massa Date: Fri, 30 Aug 2019 13:21:10 +0200 Subject: [PATCH 1/3] Fix flakiness of test_randomresized_params --- 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 964504eb9dc..034029c0111 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -639,7 +639,7 @@ def get_params(img, scale, ratio): for attempt in range(10): target_area = random.uniform(*scale) * area - log_ratio = (math.log(ratio[0]), math.log(ratio[1])) + log_ratio = (math.log(min(ratio)), math.log(max(ratio))) aspect_ratio = math.exp(random.uniform(*log_ratio)) w = int(round(math.sqrt(target_area * aspect_ratio))) From 9971465a39f9098ab54fb196d10c2684d8a70c83 Mon Sep 17 00:00:00 2001 From: Francisco Massa Date: Fri, 30 Aug 2019 14:41:16 +0200 Subject: [PATCH 2/3] Real fix --- test/test_transforms.py | 5 +++-- torchvision/transforms/transforms.py | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/test/test_transforms.py b/test/test_transforms.py index 54aae796301..4099b12a694 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -141,8 +141,9 @@ def test_randomresized_params(self): img = to_pil_image(img) size = 100 epsilon = 0.05 - for _ in range(10): - scale_min = round(random.random(), 2) + min_scale = 0.25 + for _ in range(1000): + scale_min = max(round(random.random(), 2), min_scale) scale_range = (scale_min, scale_min + round(random.random(), 2)) aspect_min = max(round(random.random(), 2), epsilon) aspect_ratio_range = (aspect_min, aspect_min + round(random.random(), 2)) diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index 034029c0111..203dae345cd 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -639,13 +639,13 @@ def get_params(img, scale, ratio): for attempt in range(10): target_area = random.uniform(*scale) * area - log_ratio = (math.log(min(ratio)), math.log(max(ratio))) + log_ratio = (math.log(ratio[0]), math.log(ratio[1])) aspect_ratio = math.exp(random.uniform(*log_ratio)) w = int(round(math.sqrt(target_area * aspect_ratio))) h = int(round(math.sqrt(target_area / aspect_ratio))) - if w <= img.size[0] and h <= img.size[1]: + if 0 < w <= img.size[0] and 0 < h <= img.size[1]: i = random.randint(0, img.size[1] - h) j = random.randint(0, img.size[0] - w) return i, j, h, w From 3c05a0845b76cbb603130b92b610710cfab3ce7f Mon Sep 17 00:00:00 2001 From: Francisco Massa Date: Fri, 30 Aug 2019 14:53:15 +0200 Subject: [PATCH 3/3] Reduce number of iters --- 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 4099b12a694..7e8320d6d6c 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -142,7 +142,7 @@ def test_randomresized_params(self): size = 100 epsilon = 0.05 min_scale = 0.25 - for _ in range(1000): + for _ in range(10): scale_min = max(round(random.random(), 2), min_scale) scale_range = (scale_min, scale_min + round(random.random(), 2)) aspect_min = max(round(random.random(), 2), epsilon)