You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by zh...@apache.org on 2021/05/13 07:15:27 UTC

[incubator-mxnet] branch v1.x updated: [v1.x] ONNX tweak Resize op (#20264)

This is an automated email from the ASF dual-hosted git repository.

zha0q1 pushed a commit to branch v1.x
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/v1.x by this push:
     new dec18b6  [v1.x] ONNX tweak Resize op (#20264)
dec18b6 is described below

commit dec18b67bcc1c625b65e9571eccf89dd3344ffb1
Author: Zhaoqi Zhu <zh...@gmail.com>
AuthorDate: Thu May 13 00:13:28 2021 -0700

    [v1.x] ONNX tweak Resize op (#20264)
    
    * fix
    
    * fix sanity
    
    * Update _op_translations_opset12.py
    
    * Update _op_translations_opset12.py
---
 .../_op_translations/_op_translations_opset12.py   | 29 ++++++++++++----------
 tests/python-pytest/onnx/test_operators.py         |  9 ++++---
 2 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/python/mxnet/onnx/mx2onnx/_op_translations/_op_translations_opset12.py b/python/mxnet/onnx/mx2onnx/_op_translations/_op_translations_opset12.py
index 0d9e21b..c1eadf4 100644
--- a/python/mxnet/onnx/mx2onnx/_op_translations/_op_translations_opset12.py
+++ b/python/mxnet/onnx/mx2onnx/_op_translations/_op_translations_opset12.py
@@ -207,7 +207,7 @@ def create_tensor(tensor_list, tensor_name, initializer, dtype='int64'):
         raw=False
     )
     initializer.append(tensor)
-    return tensor
+
 
 @mx_op.register("null")
 def convert_weights_and_inputs(node, **kwargs):
@@ -3332,6 +3332,8 @@ def convert_contrib_BilinearResize2D(node, **kwargs):
                                    not supported')
 
     create_tensor([], name+'_roi', kwargs['initializer'], dtype='float32')
+    create_tensor([], name+'_scales_empty', kwargs['initializer'],
+                  dtype='float32')
 
     nodes = []
     if scale_height == 0:
@@ -3341,21 +3343,21 @@ def convert_contrib_BilinearResize2D(node, **kwargs):
         nodes += [
             make_node('Shape', [input_nodes[0]], [name+'_shape']),
             make_node('Slice', [name+'_shape', name+'_0', name+'_2'], [name+'_shape_01']),
-            make_node('Concat', [name+'_shape_01', name+'_h_w'], [name+'_new_shape'], axis=0),
-            make_node('Cast', [name+'_shape'], [name+'_shape_f'], to=int(TensorProto.FLOAT)),
-            make_node('Cast', [name+'_new_shape'], [name+'_new_shape_f'],
-                      to=int(TensorProto.FLOAT)),
-            make_node('Div', [name+'_new_shape_f', name+'_shape_f'], [name+'_scales']),
-            make_node('Resize', [input_nodes[0], name+'_roi', name+'_scales'], [name],
-                      mode='linear', coordinate_transformation_mode='align_corners', name=name)
+            make_node('Concat', [name+'_shape_01', name+'_h_w'], [name+'_sizes'], axis=0),
         ]
     else:
         create_tensor([1, 1, scale_height, scale_width], name+'_scales', kwargs['initializer'],
                       dtype='float32')
         nodes += [
-            make_node('Resize', [input_nodes[0], name+'_roi', name+'_scales'], [name],
-                      mode='linear', coordinate_transformation_mode='align_corners', name=name)
+            make_node('Shape', [input_nodes[0]], [name+'_shape']),
+            make_node('Cast', [name+'_shape'], [name+'_shape_f'], to=int(TensorProto.FLOAT)),
+            make_node('Mul', [name+'_shape_f', name+'_scales'], [name+'_sizes_']),
+            make_node('Cast', [name+'_sizes_'], [name+'_sizes'], to=int(TensorProto.INT64)),
         ]
+    nodes += [
+        make_node('Resize', [input_nodes[0], name+'_roi', name+'_scales_empty', name+'_sizes'], [name],
+                  mode='linear', coordinate_transformation_mode='align_corners', name=name)
+    ]
 
     return nodes
 
@@ -3858,7 +3860,7 @@ def convert_contrib_box_decode(node, **kwargs):
 
 @mx_op.register("_contrib_AdaptiveAvgPooling2D")
 def convert_contrib_AdaptiveAvgPooling2D(node, **kwargs):
-    """Map MXNet's _contrib_BilinearResize2D operator attributes to onnx's operator.
+    """Map MXNet's _contrib_AdaptiveAvgPooling2D operator
     """
     from onnx.helper import make_node
     name, input_nodes, attrs = get_inputs(node, kwargs)
@@ -4484,7 +4486,7 @@ def convert_RNN(node, **kwargs):
     """Map MXNet's RNN operator attributes to onnx's operators
     and return the created node.
     """
-    from onnx.helper import make_node
+    from onnx.helper import make_node, make_tensor
     from onnx import TensorProto
 
     name, input_nodes, attrs = get_inputs(node, kwargs)
@@ -4522,7 +4524,8 @@ def convert_RNN(node, **kwargs):
     create_tensor([1], name+'_1', kwargs['initializer'])
     create_tensor([state_size], name+'_state_size', kwargs['initializer'])
     create_tensor([direction], name+'_direction', kwargs['initializer'])
-    tensor_1 = create_tensor([1], name+'_1_f', kwargs['initializer'], dtype)
+
+    tensor_1 = make_tensor(name+'_1_f', dtype, [1], [1])
 
     nodes = [
         make_node('Shape', [data], [name+'_data_shape']),
diff --git a/tests/python-pytest/onnx/test_operators.py b/tests/python-pytest/onnx/test_operators.py
index 533d7e9..35a43e0 100644
--- a/tests/python-pytest/onnx/test_operators.py
+++ b/tests/python-pytest/onnx/test_operators.py
@@ -482,6 +482,7 @@ def test_onnx_export_repeat(tmp_path, dtype, axis, repeats):
 
 
 @pytest.mark.parametrize('dtype', ['float16', 'float32', 'float64', 'int32', 'int64'])
+@pytest.mark.parametrize('shape', [(1, 3, 224, 224), (2, 2, 5, 8), (2, 4, 17, 23)])
 @pytest.mark.parametrize('params', [{'height': 7, 'width': 13},
                                     {'height': 10, 'width': 16},
                                     {'height': 3, 'width': 5},
@@ -489,12 +490,12 @@ def test_onnx_export_repeat(tmp_path, dtype, axis, repeats):
                                     {'scale_height': 3, 'scale_width': 2},
                                     {'scale_height': 1.7, 'scale_width': 2.3},
                                     {'scale_height': 0.5, 'scale_width': 0.6},
-                                    {'scale_height': 0.8, 'scale_width': 0.1},
+                                    {'scale_height': 0.8, 'scale_width': 0.13},
                                     {'scale_height': 2.5, 'scale_width': 0.5},
-                                    {'scale_height': 3, 'scale_width': 0.00001},
+                                    {'scale_height': 3, 'scale_width': 0.2},
                                     ])
-def test_onnx_export_contrib_BilinearResize2D(tmp_path, dtype, params):
-    x = mx.nd.arange(0, 160).reshape((2, 2, 5, 8))
+def test_onnx_export_contrib_BilinearResize2D(tmp_path, dtype, shape, params):
+    x = mx.random.uniform(0, 1, shape)
     M = def_model('contrib.BilinearResize2D', **params)
     op_export_test('contrib_BilinearResize2D', M, [x], tmp_path)