Unverified Commit 06a5858b authored by clint (woonhyuk baek)'s avatar clint (woonhyuk baek) Committed by GitHub
Browse files

bugfix aspect ratio sampling in transforms.RandomErasing (#3344)

* aspect ratio must be a sampling from log scale.
reference from: https://github.com/pytorch/vision/blob/8317295c1d272e0ba7b2ce31e3fd2c048235fc73/torchvision/transforms/transforms.py#L833-L836



* add random erasing unittest code

* Increased threshold for difference in sampling rate

* move outside of the loop

* log_ratio move outside of the loop. RandomResizedCrop also
Co-authored-by: default avatarVasilis Vryniotis <datumbox@users.noreply.github.com>
parent 17e1ec4e
...@@ -1955,6 +1955,25 @@ class Tester(unittest.TestCase): ...@@ -1955,6 +1955,25 @@ class Tester(unittest.TestCase):
img = transform(img) img = transform(img)
transform.__repr__() 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, 1)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -828,9 +828,9 @@ class RandomResizedCrop(torch.nn.Module): ...@@ -828,9 +828,9 @@ class RandomResizedCrop(torch.nn.Module):
width, height = F._get_image_size(img) width, height = F._get_image_size(img)
area = height * width area = height * width
log_ratio = torch.log(torch.tensor(ratio))
for _ in range(10): for _ in range(10):
target_area = area * torch.empty(1).uniform_(scale[0], scale[1]).item() target_area = area * torch.empty(1).uniform_(scale[0], scale[1]).item()
log_ratio = torch.log(torch.tensor(ratio))
aspect_ratio = torch.exp( aspect_ratio = torch.exp(
torch.empty(1).uniform_(log_ratio[0], log_ratio[1]) torch.empty(1).uniform_(log_ratio[0], log_ratio[1])
).item() ).item()
...@@ -1576,9 +1576,12 @@ class RandomErasing(torch.nn.Module): ...@@ -1576,9 +1576,12 @@ class RandomErasing(torch.nn.Module):
img_c, img_h, img_w = img.shape[-3], img.shape[-2], img.shape[-1] img_c, img_h, img_w = img.shape[-3], img.shape[-2], img.shape[-1]
area = img_h * img_w area = img_h * img_w
log_ratio = torch.log(torch.tensor(ratio))
for _ in range(10): for _ in range(10):
erase_area = area * torch.empty(1).uniform_(scale[0], scale[1]).item() erase_area = area * torch.empty(1).uniform_(scale[0], scale[1]).item()
aspect_ratio = torch.empty(1).uniform_(ratio[0], ratio[1]).item() aspect_ratio = torch.exp(
torch.empty(1).uniform_(log_ratio[0], log_ratio[1])
).item()
h = int(round(math.sqrt(erase_area * aspect_ratio))) h = int(round(math.sqrt(erase_area * aspect_ratio)))
w = int(round(math.sqrt(erase_area / aspect_ratio))) w = int(round(math.sqrt(erase_area / aspect_ratio)))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment