Skip to content

Commit

Permalink
Fix aspect ratio sampling for RandomResizedCrop (apache#14585)
Browse files Browse the repository at this point in the history
* added log sampling for aspect ratio

* added test

* added comments

* fix test

* remove math, fix test
  • Loading branch information
abhinavs95 authored and haohuw committed Jun 23, 2019
1 parent 15153a7 commit 547d623
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 2 additions & 4 deletions python/mxnet/image/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,12 @@ def random_size_crop(src, size, area, ratio, interp=2, **kwargs):
area = (area, 1.0)
for _ in range(10):
target_area = random.uniform(area[0], area[1]) * src_area
new_ratio = random.uniform(*ratio)
log_ratio = (np.log(ratio[0]), np.log(ratio[1]))
new_ratio = np.exp(random.uniform(*log_ratio))

new_w = int(round(np.sqrt(target_area * new_ratio)))
new_h = int(round(np.sqrt(target_area / new_ratio)))

if random.random() < 0.5:
new_h, new_w = new_w, new_h

if new_w <= w and new_h <= h:
x0 = random.randint(0, w - new_w)
y0 = random.randint(0, h - new_h)
Expand Down
13 changes: 13 additions & 0 deletions tests/python/unittest/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,19 @@ def test_det_augmenters(self):
for batch in det_iter:
pass

@with_seed()
def test_random_size_crop(self):
# test aspect ratio within bounds
width = np.random.randint(100, 500)
height = np.random.randint(100, 500)
src = np.random.rand(height, width, 3) * 255.
ratio = (0.75, 1)
out, (x0, y0, new_w, new_h) = mx.image.random_size_crop(mx.nd.array(src), size=(width, height), area=0.08, ratio=ratio)
_, pts = mx.image.center_crop(mx.nd.array(src), size=(width, height))
if (x0, y0, new_w, new_h) != pts:
assert ratio[0] <= float(new_w)/new_h <= ratio[1]


if __name__ == '__main__':
import nose
nose.runmodule()

0 comments on commit 547d623

Please sign in to comment.