masahi commented on a change in pull request #4584: [TOPI] add 3D upsampling Op.
URL: https://github.com/apache/incubator-tvm/pull/4584#discussion_r361419440
 
 

 ##########
 File path: topi/tests/python/test_topi_upsampling.py
 ##########
 @@ -86,5 +86,71 @@ def test_upsampling():
     verify_upsampling(2, 2, 32, 32, 3.0, 3.0, layout="NHWC", method="bilinear")
     verify_upsampling(1, 64, 22, 32,  3.0, 3.0, layout="NHWC", 
method="bilinear")
 
+def verify_upsampling3d(batch, in_channel, in_depth, in_height, in_width, 
scale_d, scale_h, scale_w,
+                        layout='NCDHW', method="nearest_neighbor"):
+    if layout == 'NCDHW':
+        A = tvm.placeholder((batch, in_channel, in_depth, in_height, 
in_width), name='A')
+        dtype = A.dtype
+        out_shape = (batch, in_channel, int(round(in_depth*scale_d)), 
int(round(in_height*scale_h)),
+                     int(round(in_width*scale_w)))
+        a_np = np.random.uniform(size=(batch, in_channel, in_depth, in_height, 
in_width)).astype(dtype)
+    elif layout == 'NDHWC':
+        A = tvm.placeholder((batch, in_depth, in_height, in_width, 
in_channel), name='A')
+        dtype = A.dtype
+        out_shape = (batch, int(round(in_depth*scale_d)), 
int(round(in_height*scale_h)),
+                     int(round(in_width*scale_w)), in_channel)
+        a_np = np.random.uniform(size=(batch, in_depth, in_height, in_width, 
in_channel)).astype(dtype)
+    else:
+        raise NotImplementedError(
+            'Layout not supported {} '.format(layout))
+
+    B = topi.nn.upsampling3d(A, scale_d, scale_h, scale_w, layout=layout, 
method=method, align_corners=False)
+
+    if method == "trilinear":
+        out_size = (int(round(in_depth*scale_d)), 
int(round(in_height*scale_h)), int(round(in_width*scale_w)))
+        b_np = topi.testing.trilinear_resize3d_python(a_np, out_size, layout, 
align_corners=False)
+    else:
+        b_np = topi.testing.upsampling3d_python(a_np, (scale_d, scale_h, 
scale_w), layout)
+
+    def check_device(device):
+        ctx = tvm.context(device, 0)
+        if not ctx.exist:
+            print("Skip because %s is not enabled" % device)
+            return
+        print("Running on target: %s" % device)
+        with tvm.target.create(device):
+            s = topi.generic.schedule_injective(B)
+        a = tvm.nd.array(a_np, ctx)
+        b = tvm.nd.array(np.zeros(out_shape, dtype=dtype), ctx)
+        f = tvm.build(s, [A, B], device)
+        f(a, b)
+
+        tvm.testing.assert_allclose(b.asnumpy(), b_np, rtol=1e-5, atol=1e-5)
+
+    for device in get_all_backend():
+        check_device(device)
+
+def test_upsampling3d():
+    # nearest_neighbor - NCDHW
+    verify_upsampling3d(8, 8, 16, 16, 16, 2.0, 2.0, 2.0)
+    verify_upsampling3d(2, 16, 32, 32, 32, 3.0, 3.0, 3.0)
+    verify_upsampling3d(1, 8, 11, 16, 6, 1.954545497894287, 2.0, 1.5)
+
+    ## nearest_neighbor - NDHWC
+    verify_upsampling3d(8, 8, 16, 16, 16, 2.0, 2.0, 2.0, layout="NDHWC")
+    verify_upsampling3d(2, 16, 32, 32, 32, 3.0, 3.0, 3.0, layout="NDHWC")
+    verify_upsampling3d(1, 8, 11, 16, 6, 1.954545497894287, 2.0, 1.5, 
layout="NDHWC")
+
+    # trilinear - NCDHW
+    verify_upsampling3d(2, 2, 16, 16, 16, 2.0, 2.0, 2.0, method="trilinear")
+    verify_upsampling3d(2, 2, 32, 32, 32, 3.0, 3.0, 3.0, method="trilinear")
+    verify_upsampling3d(1, 2, 11, 16, 6, 1.954545497894287, 2.0, 1.5, 
method="trilinear")
+
+    # trilinear - NHWC
 
 Review comment:
   NDHWC

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to