From 1b276dd629f2cdf923c59f429131be2093a59a08 Mon Sep 17 00:00:00 2001 From: Howard Su Date: Tue, 7 Feb 2017 22:40:12 +0800 Subject: [PATCH 1/2] set target_shape to 0 instead of leaving with random --- src/operator/upsampling.cc | 2 ++ src/operator/upsampling.cu | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/operator/upsampling.cc b/src/operator/upsampling.cc index 284afc57e856..dd35a581e0ca 100644 --- a/src/operator/upsampling.cc +++ b/src/operator/upsampling.cc @@ -33,6 +33,8 @@ Operator *CreateOp(UpSamplingParam param, int dtype) { p.stride = TShape(shape, shape + 2); shape[0] = shape[1] = pad; p.pad = TShape(shape, shape + 2); + shape[0] = shape[1] = 0; + p.target_shape = TShape(shape, shape + 2); op = new DeconvolutionOp(p); } else { LOG(FATAL) << "Unknown sample type"; diff --git a/src/operator/upsampling.cu b/src/operator/upsampling.cu index 95864e430010..1a96091472d5 100644 --- a/src/operator/upsampling.cu +++ b/src/operator/upsampling.cu @@ -32,6 +32,8 @@ Operator *CreateOp(UpSamplingParam param, int dtype) { p.stride = TShape(shape, shape + 2); shape[0] = shape[1] = pad; p.pad = TShape(shape, shape + 2); + shape[0] = shape[1] = 0; + p.target_shape = TShape(shape, shape + 2); op = new DeconvolutionOp(p); } else { LOG(FATAL) << "Unknown sample type"; From ccaf00a7452dc02b9a3d5822e56daa6616a3b062 Mon Sep 17 00:00:00 2001 From: Howard Su Date: Tue, 7 Feb 2017 22:56:48 +0800 Subject: [PATCH 2/2] Add test cases for bilinear upsampling --- tests/python/gpu/test_operator_gpu.py | 11 ++++++++++- tests/python/unittest/test_operator.py | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/python/gpu/test_operator_gpu.py b/tests/python/gpu/test_operator_gpu.py index 19a2709c27a9..a8fd78c42932 100644 --- a/tests/python/gpu/test_operator_gpu.py +++ b/tests/python/gpu/test_operator_gpu.py @@ -145,7 +145,16 @@ def test_pooling_with_type(): check_consistency(sym, ctx_list) def test_upsampling_with_type(): - sym = mx.sym.UpSampling(scale=2, num_filter=2, name='up', sample_type = 'nearest', num_args=1) + sym = mx.sym.UpSampling(scale=2, num_filter=2, name='up', sample_type='nearest', num_args=1) + ctx_list = [{'ctx': mx.gpu(0), 'up_arg0': (2, 2, 2, 10), 'type_dict': {'up_arg0': np.float64}}, + {'ctx': mx.gpu(0), 'up_arg0': (2, 2, 2, 10), 'type_dict': {'up_arg0': np.float32}}, + {'ctx': mx.gpu(0), 'up_arg0': (2, 2, 2, 10), 'type_dict': {'up_arg0': np.float16}}, + {'ctx': mx.cpu(0), 'up_arg0': (2, 2, 2, 10), 'type_dict': {'up_arg0': np.float64}}, + {'ctx': mx.cpu(0), 'up_arg0': (2, 2, 2, 10), 'type_dict': {'up_arg0': np.float32}}] + check_consistency(sym, ctx_list) + +def test_upsampling_bilinear_with_type(): + sym = mx.sym.UpSampling(scale=2, num_filter=2, name='up', sample_type='bilinear', num_args=1) ctx_list = [{'ctx': mx.gpu(0), 'up_arg0': (2, 2, 2, 10), 'type_dict': {'up_arg0': np.float64}}, {'ctx': mx.gpu(0), 'up_arg0': (2, 2, 2, 10), 'type_dict': {'up_arg0': np.float32}}, {'ctx': mx.gpu(0), 'up_arg0': (2, 2, 2, 10), 'type_dict': {'up_arg0': np.float16}}, diff --git a/tests/python/unittest/test_operator.py b/tests/python/unittest/test_operator.py index 74490752b726..4e7e2215b7f2 100644 --- a/tests/python/unittest/test_operator.py +++ b/tests/python/unittest/test_operator.py @@ -786,6 +786,17 @@ def check_nearest_upsampling_with_shape(shapes, scale, root_scale): name = 'arg_%d'%k assert_allclose(arr[name].asnumpy()*root_scale**2*scale**(2*k), arr_grad[name].asnumpy(), rtol=1e-4) +def check_bilinear_upsampling_with_shape(shapes, scale, root_scale): + arr = {'arg_%d'%i: mx.random.uniform(-10.0, 10.0, shape, ctx=mx.cpu()).copyto(default_context()) for i, shape in zip(range(len(shapes)), shapes)} + arr_grad = {'arg_%d'%i: mx.nd.zeros(shape) for i, shape in zip(range(len(shapes)), shapes)} + + up = mx.sym.UpSampling(*[mx.sym.Variable('arg_%d'%i) for i in range(len(shapes))], sample_type='bilinear', scale=root_scale) + exe = up.bind(default_context(), args=arr, args_grad=arr_grad) + exe.forward(is_train=True) + exe.backward(exe.outputs) + for k in range(len(shapes)): + name = 'arg_%d'%k + assert_allclose(arr[name].asnumpy()*root_scale**2*scale**(2*k), arr_grad[name].asnumpy(), rtol=1e-4) def test_nearest_upsampling(): for root_scale in [1,2,3]: