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

Commit

Permalink
ONNX export: Comparison operators
Browse files Browse the repository at this point in the history
  • Loading branch information
vandanavk committed Nov 20, 2018
1 parent 7bc954c commit 9d440cf
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
21 changes: 21 additions & 0 deletions python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1565,3 +1565,24 @@ def convert_hardsigmoid(node, **kwargs):
name=name
)
return [node]

@mx_op.register("broadcast_lesser")
def convert_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_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_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)
30 changes: 30 additions & 0 deletions tests/python-pytest/onnx/export/mxnet_export_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,36 @@ 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))


if __name__ == '__main__':
test_models("bvlc_googlenet", (1, 3, 224, 224), (1, 1000))
test_models("bvlc_reference_caffenet", (1, 3, 224, 224), (1, 1000))
Expand Down

0 comments on commit 9d440cf

Please sign in to comment.