Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[v1.x] ONNX tweak Resize op #20264

Merged
merged 4 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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']),
Expand Down
9 changes: 5 additions & 4 deletions tests/python-pytest/onnx/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,19 +482,20 @@ 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},
{'height': 2, 'width': 4},
{'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)

Expand Down