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

[MXNET-886] ONNX export: HardSigmoid, Less, Greater, Equal #12812

Merged
merged 2 commits into from
Nov 29, 2018
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
42 changes: 42 additions & 0 deletions python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1545,3 +1545,45 @@ def convert_sum(node, **kwargs):
name=name
)
return [node]

@mx_op.register("hard_sigmoid")
def convert_hardsigmoid(node, **kwargs):
"""Map MXNet's hard_sigmoid operator attributes to onnx's HardSigmoid operator
and return the created node.
"""
name, input_nodes, attrs = get_inputs(node, kwargs)

vandanavk marked this conversation as resolved.
Show resolved Hide resolved
# Converting to float32
alpha = float(attrs.get("alpha", 0.2))
beta = float(attrs.get("beta", 0.5))

node = onnx.helper.make_node(
'HardSigmoid',
input_nodes,
[name],
alpha=alpha,
beta=beta,
name=name
)
return [node]

@mx_op.register("broadcast_lesser")
def convert_broadcast_lesser(node, **kwargs):
"""Map MXNet's broadcast_lesser operator attributes to onnx's Less operator
and return the created node.
"""
return create_basic_op_node('Less', node, kwargs)

@mx_op.register("broadcast_greater")
def convert_broadcast_greater(node, **kwargs):
"""Map MXNet's broadcast_greater operator attributes to onnx's Greater operator
and return the created node.
"""
return create_basic_op_node('Greater', node, kwargs)

@mx_op.register("broadcast_equal")
def convert_broadcast_equal(node, **kwargs):
"""Map MXNet's broadcast_equal operator attributes to onnx's Equal operator
and return the created node.
"""
return create_basic_op_node('Equal', node, kwargs)
26 changes: 26 additions & 0 deletions tests/python-pytest/onnx/export/mxnet_export_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,32 @@ def test_square():

npt.assert_almost_equal(result, numpy_op)

@with_seed()
def test_comparison_ops():
"""Test greater, lesser, equal"""
def test_ops(op_name, inputs, input_tensors, numpy_op):
outputs = [helper.make_tensor_value_info("output", TensorProto.FLOAT, shape=np.shape(inputs[0]))]
nodes = [helper.make_node(op_name, ["input"+str(i+1) for i in range(len(inputs))], ["output"])]
graph = helper.make_graph(nodes,
op_name + "_test",
input_tensors,
outputs)
model = helper.make_model(graph)
bkd_rep = backend.prepare(model)
output = bkd_rep.run(inputs)
npt.assert_almost_equal(output[0], numpy_op)
input_data = [np.random.rand(1, 3, 4, 5).astype("float32"),
np.random.rand(1, 5).astype("float32")]
input_tensor = []
for idx, ip in enumerate(input_data):
input_tensor.append(helper.make_tensor_value_info("input" + str(idx + 1),
TensorProto.FLOAT, shape=np.shape(ip)))
test_ops("Greater", input_data, input_tensor,
np.greater(input_data[0], input_data[1]).astype(np.float32))
test_ops("Less", input_data, input_tensor,
np.less(input_data[0], input_data[1]).astype(np.float32))
test_ops("Equal", input_data, input_tensor,
np.equal(input_data[0], input_data[1]).astype(np.float32))

def _assert_sym_equal(lhs, rhs):
assert lhs.list_inputs() == rhs.list_inputs() # input names must be identical
Expand Down
3 changes: 2 additions & 1 deletion tests/python-pytest/onnx/export/onnx_backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@
'test_operator_permute2',
'test_clip'
'test_cast',
'test_depthtospace'
'test_depthtospace',
'test_hardsigmoid'
]

BASIC_MODEL_TESTS = [
Expand Down
2 changes: 1 addition & 1 deletion tests/python-pytest/onnx/import/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
'test_sin',
'test_tan',
'test_shape',
'test_hardsigmoid_',
'test_hardsigmoid',
'test_averagepool_1d',
'test_averagepool_2d_pads_count_include_pad',
'test_averagepool_2d_precomputed_pads_count_include_pad',
Expand Down