You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by mb...@apache.org on 2021/02/26 21:01:15 UTC
[tvm] branch main updated: [BUG_FIX][TOPI] Allow topi resize to
accept more options (#7532)
This is an automated email from the ASF dual-hosted git repository.
mbrookhart pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git
The following commit(s) were added to refs/heads/main by this push:
new 5c5aea6 [BUG_FIX][TOPI] Allow topi resize to accept more options (#7532)
5c5aea6 is described below
commit 5c5aea620bb940fd4fb7106602ae51111c7af03a
Author: Lily Orth-Smith <li...@gmail.com>
AuthorDate: Fri Feb 26 16:00:54 2021 -0500
[BUG_FIX][TOPI] Allow topi resize to accept more options (#7532)
* Make topi more permissive
* Remove testing stuff
* lint
* Downsampling tests
---
python/tvm/topi/image/resize.py | 6 +-----
tests/python/frontend/onnx/test_forward.py | 12 ++++++++++++
tests/python/relay/test_op_level5.py | 20 +++++++++++++-------
tests/python/topi/python/test_topi_image.py | 20 +++++++++++---------
4 files changed, 37 insertions(+), 21 deletions(-)
diff --git a/python/tvm/topi/image/resize.py b/python/tvm/topi/image/resize.py
index 103850d..433a920 100644
--- a/python/tvm/topi/image/resize.py
+++ b/python/tvm/topi/image/resize.py
@@ -653,11 +653,7 @@ def resize(
or 5-D with shape [batch, channel-major, in_height*scale, in_width*scale, channel-minor]
"""
method = method.lower()
- if method == "nearest_neighbor" and coordinate_transformation_mode != "asymmetric":
- raise ValueError(
- "Topi Resize does not support the combination of method %s "
- "and coordinate_transformation_mode %s" % (method, coordinate_transformation_mode)
- )
+
if layout == "NHWC":
in_n, in_h, in_w, in_c = data.shape
if output_shape is None:
diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py
index 9e59117..8dbd049 100644
--- a/tests/python/frontend/onnx/test_forward.py
+++ b/tests/python/frontend/onnx/test_forward.py
@@ -3356,15 +3356,27 @@ def test_resize():
# upsampling
verify([1, 16, 32, 32], [1, 16, 64, 64], [], "nearest", "asymmetric")
+ verify([1, 16, 32, 32], [1, 16, 64, 64], [], "linear", "asymmetric")
+ verify([1, 16, 32, 32], [1, 16, 64, 64], [], "nearest", "align_corners")
verify([1, 16, 32, 32], [1, 16, 64, 64], [], "linear", "align_corners")
+ verify([1, 16, 32, 32], [1, 16, 64, 64], [], "nearest", "half_pixel")
verify([1, 16, 32, 32], [1, 16, 64, 64], [], "linear", "half_pixel")
+
# downsampling
verify([1, 16, 32, 32], [1, 16, 16, 16], [], "nearest", "asymmetric")
+ verify([1, 16, 32, 32], [1, 16, 16, 16], [], "linear", "asymmetric")
+ verify([1, 16, 32, 32], [1, 16, 16, 16], [], "nearest", "align_corners")
verify([1, 16, 32, 32], [1, 16, 16, 16], [], "linear", "align_corners")
+ verify([1, 16, 32, 32], [1, 16, 16, 16], [], "nearest", "half_pixel")
verify([1, 16, 32, 32], [1, 16, 16, 16], [], "linear", "half_pixel")
+
# scales are specified instead of sizes
verify([1, 16, 32, 32], [], [1, 1, 2, 2], "nearest", "asymmetric")
+ verify([1, 16, 32, 32], [], [1, 1, 2, 2], "linear", "asymmetric")
+ verify([1, 16, 32, 32], [], [1, 1, 2, 2], "nearest", "align_corners")
+ verify([1, 16, 32, 32], [], [1, 1, 2, 2], "linear", "align_corners")
verify([1, 16, 32, 32], [], [1, 1, 0.5, 0.5], "linear", "half_pixel")
+ verify([1, 16, 32, 32], [], [1, 1, 0.5, 0.5], "nearest", "half_pixel")
def verify_opset_10(ishape, scales, mode):
nodes = [
diff --git a/tests/python/relay/test_op_level5.py b/tests/python/relay/test_op_level5.py
index 87f3ab8..929764b 100644
--- a/tests/python/relay/test_op_level5.py
+++ b/tests/python/relay/test_op_level5.py
@@ -67,13 +67,19 @@ def test_resize():
for kind in ["graph", "debug"]:
intrp = relay.create_executor(kind, ctx=ctx, target=target)
op_res = intrp.evaluate(func)(x_data)
- tvm.testing.assert_allclose(op_res.asnumpy(), ref_res, rtol=1e-4, atol=1e-5)
-
- for layout in ["NHWC", "NCHW"]:
- verify_resize((1, 4, 4, 4), 2, "bilinear", layout, "align_corners")
- verify_resize((2, 8, 17, 20), 3, "bilinear", layout, "half_pixel")
- verify_resize((2, 8, 17, 20), 3, "bilinear", layout, "asymmetric")
- verify_resize((3, 4, 5, 6), 5, "nearest_neighbor", layout, "asymmetric")
+ tvm.testing.assert_allclose(op_res.asnumpy(), ref_res, rtol=1e-3, atol=1e-4)
+
+ for method in ["nearest_neighbor", "bilinear"]:
+ for coord_trans in ["asymmetric", "half_pixel", "align_corners"]:
+ for layout in ["NHWC", "NCHW"]:
+ # TODO: Topi test does not have a function to produce numpy output for resize with
+ # nearest_neighbors and align_corners. Enable when topi test has this option
+ if coord_trans == "align_corners" and method == "nearest_neighbor":
+ continue
+ verify_resize((1, 4, 4, 4), 2, method, layout, coord_trans)
+ verify_resize((2, 8, 17, 20), 3, method, layout, coord_trans)
+ verify_resize((2, 8, 17, 20), 3, method, layout, coord_trans)
+ verify_resize((3, 4, 5, 6), 5, method, layout, coord_trans)
def test_resize3d_infer_type():
diff --git a/tests/python/topi/python/test_topi_image.py b/tests/python/topi/python/test_topi_image.py
index 518ee1f..c605df7 100644
--- a/tests/python/topi/python/test_topi_image.py
+++ b/tests/python/topi/python/test_topi_image.py
@@ -59,6 +59,9 @@ def verify_resize(
a_np, (out_height, out_width), layout, coord_trans
)
else:
+ # TODO: Nearest neighbor case doesn't do anything with coordinate transform mode, and also
+ # nearest_neighbors and align_corners combination in topi doesn't match the output of this
+ # function.
scale_h = out_height / in_height
scale_w = out_width / in_width
b_np = tvm.topi.testing.upsampling_python(a_np, (scale_h, scale_w), layout)
@@ -88,15 +91,14 @@ def test_resize():
verify_resize(4, 16, 32, 32, 50, 50, "NHWC")
# Scale NHWC + Align Corners
verify_resize(6, 32, 64, 64, 20, 20, "NHWC")
- # Nearest + Fractional
- verify_resize(4, 16, 32, 32, 50, 50, "NCHW", "asymmetric", method="nearest_neighbor")
- verify_resize(4, 16, 32, 32, 50, 50, "NHWC", "asymmetric", method="nearest_neighbor")
- # half_pixel
- verify_resize(4, 16, 16, 16, 32, 32, "NCHW", "half_pixel", method="bilinear")
- verify_resize(4, 16, 16, 16, 32, 32, "NHWC", "half_pixel", method="bilinear")
- # Bilinear + Fractional
- verify_resize(4, 16, 32, 32, 50, 50, "NCHW", "asymmetric", method="bilinear")
- verify_resize(4, 16, 32, 32, 50, 50, "NHWC", "asymmetric", method="bilinear")
+ for method in ["nearest_neighbor", "bilinear"]:
+ for coord_trans in ["asymmetric", "half_pixel", "align_corners"]:
+ for layout in ["NCHW", "NHWC"]:
+ # TODO: When topi test has an option for align corners and nearest neighbor that
+ # produces correct results, re-enable it.
+ if coord_trans == "align_corners" and method == "nearest_neighbor":
+ continue
+ verify_resize(4, 16, 32, 32, 50, 50, layout, coord_trans, method=method)
def verify_resize3d(