Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Fix aspect ratio sampling for RandomResizedCrop #14585

Merged
merged 5 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()