From a2b400c72e4069a2ee531a969fa874bef109a763 Mon Sep 17 00:00:00 2001 From: "Joshua Z. Zhang" Date: Wed, 12 Aug 2020 22:47:47 -0700 Subject: [PATCH] fix center element not being copied (#18917) --- src/operator/image/image_random-inl.h | 4 +++- .../python/unittest/test_gluon_data_vision.py | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/operator/image/image_random-inl.h b/src/operator/image/image_random-inl.h index f01b135aa209..fc4cd7899457 100644 --- a/src/operator/image/image_random-inl.h +++ b/src/operator/image/image_random-inl.h @@ -552,7 +552,9 @@ void FlipImpl(const mxnet::TShape &shape, DType *src, DType *dst) { for (int i = axis+1; i < shape.ndim(); ++i) tail *= shape[i]; for (int i = 0; i < head; ++i) { - for (int j = 0; j < (mid >> 1); ++j) { + // if inplace flip, skip the mid point in axis, otherwise copy is required + int mid2 = (src == dst) ? mid >> 1 : (mid + 1) >> 1; + for (int j = 0; j < mid2; ++j) { int idx1 = (i*mid + j) * tail; int idx2 = idx1 + (mid-(j << 1)-1) * tail; for (int k = 0; k < tail; ++k, ++idx1, ++idx2) { diff --git a/tests/python/unittest/test_gluon_data_vision.py b/tests/python/unittest/test_gluon_data_vision.py index 0546eb11c875..320b33d3e28a 100644 --- a/tests/python/unittest/test_gluon_data_vision.py +++ b/tests/python/unittest/test_gluon_data_vision.py @@ -194,18 +194,20 @@ def test_crop_backward(test_nd_arr, TestCase): @with_seed() def test_flip_left_right(): - data_in = np.random.uniform(0, 255, (300, 300, 3)).astype(dtype=np.uint8) - flip_in = data_in[:, ::-1, :] - data_trans = nd.image.flip_left_right(nd.array(data_in, dtype='uint8')) - assert_almost_equal(flip_in, data_trans.asnumpy()) + for width in range(3, 301, 7): + data_in = np.random.uniform(0, 255, (300, width, 3)).astype(dtype=np.uint8) + flip_in = data_in[:, ::-1, :] + data_trans = nd.image.flip_left_right(nd.array(data_in, dtype='uint8')) + assert_almost_equal(flip_in, data_trans.asnumpy()) @with_seed() def test_flip_top_bottom(): - data_in = np.random.uniform(0, 255, (300, 300, 3)).astype(dtype=np.uint8) - flip_in = data_in[::-1, :, :] - data_trans = nd.image.flip_top_bottom(nd.array(data_in, dtype='uint8')) - assert_almost_equal(flip_in, data_trans.asnumpy()) + for height in range(3, 301, 7): + data_in = np.random.uniform(0, 255, (height, 300, 3)).astype(dtype=np.uint8) + flip_in = data_in[::-1, :, :] + data_trans = nd.image.flip_top_bottom(nd.array(data_in, dtype='uint8')) + assert_almost_equal(flip_in, data_trans.asnumpy()) @with_seed() @@ -445,4 +447,3 @@ def test_bbox_crop(): im_out, im_bbox = transform(img, bbox) assert im_out.shape == (3, 3, 3) assert im_bbox[0][2] == 3 -