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

Fix flaky test_gluon_data_vision.test_random_transforms #17492

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions tests/python/unittest/test_gluon_data_vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ def test_to_tensor():
out_nd = transforms.ToTensor()(nd.array(data_in, dtype='uint8'))
assert_almost_equal(out_nd.asnumpy(), np.transpose(
data_in.astype(dtype=np.float32) / 255.0, (0, 3, 1, 2)))

# Invalid Input
invalid_data_in = nd.random.uniform(0, 255, (5, 5, 300, 300, 3)).astype(dtype=np.uint8)
transformer = transforms.ToTensor()
assertRaises(MXNetError, transformer, invalid_data_in)

# Bounds (0->0, 255->1)
data_in = np.zeros((10, 20, 3)).astype(dtype=np.uint8)
out_nd = transforms.ToTensor()(nd.array(data_in, dtype='uint8'))
Expand Down Expand Up @@ -126,7 +126,7 @@ def _test_resize_with_diff_type(dtype):
assertRaises(MXNetError, invalid_transform, data_in)

for dtype in ['uint8', 'float32', 'float64']:
_test_resize_with_diff_type(dtype)
_test_resize_with_diff_type(dtype)


@with_seed()
Expand Down Expand Up @@ -159,7 +159,7 @@ def _test_crop_resize_with_diff_type(dtype):
# test with resize height and width should be greater than 0
transformer = transforms.CropResize(0, 0, 100, 50, (-25, 25), 1)
assertRaises(MXNetError, transformer, data_in)
# test height and width should be greater than 0
# test height and width should be greater than 0
transformer = transforms.CropResize(0, 0, -100, -50)
assertRaises(MXNetError, transformer, data_in)
# test cropped area is bigger than input data
Expand All @@ -168,7 +168,7 @@ def _test_crop_resize_with_diff_type(dtype):
assertRaises(MXNetError, transformer, data_bath_in)

for dtype in ['uint8', 'float32', 'float64']:
_test_crop_resize_with_diff_type(dtype)
_test_crop_resize_with_diff_type(dtype)

# test nd.image.crop backward
def test_crop_backward(test_nd_arr, TestCase):
Expand Down Expand Up @@ -288,18 +288,18 @@ def test_random_rotation():
@with_seed()
def test_random_transforms():
from mxnet.gluon.data.vision import transforms

tmp_t = transforms.Compose([transforms.Resize(300), transforms.RandomResizedCrop(224)])
transform = transforms.Compose([transforms.RandomApply(tmp_t, 0.5)])

img = mx.nd.ones((10, 10, 3), dtype='uint8')
iteration = 1000
iteration = 2000
num_apply = 0
for _ in range(iteration):
out = transform(img)
if out.shape[0] == 224:
num_apply += 1
assert_almost_equal(num_apply/float(iteration), 0.5, 0.1)
assert_almost_equal(np.array(num_apply/float(iteration)), np.array(0.5), rtol=0.1, atol=1e-2)


if __name__ == '__main__':
Expand Down