This is an automated email from the ASF dual-hosted git repository. wkcn pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git
The following commit(s) were added to refs/heads/master by this push: new b64e00a Fix wrong description of output range of ToTensor (#14794) b64e00a is described below commit b64e00a26dd6e00a3c057880ec41b0f46827fcfb Author: Alexander Grund <flamef...@users.noreply.github.com> AuthorDate: Sun Jun 9 07:12:05 2019 +0200 Fix wrong description of output range of ToTensor (#14794) * Fix wrong description of output range of ToTensor The range is actually including 1 due to division by 255 (not 256) * Add ToTensor tests with boundary values * retrigger CI --- python/mxnet/gluon/data/vision/transforms.py | 2 +- src/operator/image/image_random.cc | 2 +- tests/python/gpu/test_gluon_transforms.py | 11 ++++++++++- tests/python/unittest/test_gluon_data_vision.py | 9 +++++++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/python/mxnet/gluon/data/vision/transforms.py b/python/mxnet/gluon/data/vision/transforms.py index dff7f66..955f2b2 100644 --- a/python/mxnet/gluon/data/vision/transforms.py +++ b/python/mxnet/gluon/data/vision/transforms.py @@ -100,7 +100,7 @@ class ToTensor(HybridBlock): Converts an image NDArray of shape (H x W x C) in the range [0, 255] to a float32 tensor NDArray of shape (C x H x W) in - the range [0, 1). + the range [0, 1]. If batch input, converts a batch image NDArray of shape (N x H x W x C) in the range [0, 255] to a float32 tensor NDArray of shape (N x C x H x W). diff --git a/src/operator/image/image_random.cc b/src/operator/image/image_random.cc index 0b95b19..34f4cb4 100644 --- a/src/operator/image/image_random.cc +++ b/src/operator/image/image_random.cc @@ -41,7 +41,7 @@ DMLC_REGISTER_PARAMETER(RandomColorJitterParam); NNVM_REGISTER_OP(_image_to_tensor) .describe(R"code(Converts an image NDArray of shape (H x W x C) or (N x H x W x C) with values in the range [0, 255] to a tensor NDArray of shape (C x H x W) or (N x C x H x W) -with values in the range [0, 1) +with values in the range [0, 1] Example: .. code-block:: python diff --git a/tests/python/gpu/test_gluon_transforms.py b/tests/python/gpu/test_gluon_transforms.py index 599a02c..e303008 100644 --- a/tests/python/gpu/test_gluon_transforms.py +++ b/tests/python/gpu/test_gluon_transforms.py @@ -24,7 +24,7 @@ from mxnet import gluon from mxnet.base import MXNetError from mxnet.gluon.data.vision import transforms from mxnet.test_utils import assert_almost_equal, set_default_context -from mxnet.test_utils import almost_equal +from mxnet.test_utils import almost_equal, same curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__))) sys.path.insert(0, os.path.join(curr_path, '../unittest')) from common import assertRaises, setup_module, with_seed, teardown @@ -90,6 +90,15 @@ def test_to_tensor(): 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')) + assert same(out_nd.asnumpy(), np.transpose(np.zeros(data_in.shape, dtype=np.float32), (2, 0, 1))) + + data_in = np.full((10, 20, 3), 255).astype(dtype=np.uint8) + out_nd = transforms.ToTensor()(nd.array(data_in, dtype='uint8')) + assert same(out_nd.asnumpy(), np.transpose(np.ones(data_in.shape, dtype=np.float32), (2, 0, 1))) + @with_seed() def test_resize(): # Test with normal case 3D input float type diff --git a/tests/python/unittest/test_gluon_data_vision.py b/tests/python/unittest/test_gluon_data_vision.py index cc15bec..627567c 100644 --- a/tests/python/unittest/test_gluon_data_vision.py +++ b/tests/python/unittest/test_gluon_data_vision.py @@ -47,6 +47,15 @@ def test_to_tensor(): 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')) + assert same(out_nd.asnumpy(), np.transpose(np.zeros(data_in.shape, dtype=np.float32), (2, 0, 1))) + + data_in = np.full((10, 20, 3), 255).astype(dtype=np.uint8) + out_nd = transforms.ToTensor()(nd.array(data_in, dtype='uint8')) + assert same(out_nd.asnumpy(), np.transpose(np.ones(data_in.shape, dtype=np.float32), (2, 0, 1))) @with_seed()