def test_onnx_if_algebra_indirect_unnamed_clear_input(self): opv = TARGET_OPSET x1 = np.array([[0, 3], [7, 0]], dtype=np.float32) x2 = np.array([[1, 0], [2, 0]], dtype=np.float32) node_xy = OnnxMul('x1', 'x2', op_version=opv) node_then = OnnxAdd( 'x1', 'xy', output_names=['absxythen'], op_version=opv) then_body = node_then.to_onnx( {'x1': x1, 'xy': x2}, target_opset=opv, outputs=[('absxythen', FloatTensorType())]) node_else = OnnxSub( 'x1', 'x2', output_names=['absxyelse'], op_version=opv) else_body = node_else.to_onnx( {'x1': x1, 'x2': x2}, target_opset=opv, outputs=[('absxyelse', FloatTensorType())]) cond = OnnxGreater( OnnxReduceSum('x1', op_version=opv), OnnxReduceSum('x2', op_version=opv), op_version=opv) ifnode = OnnxIf(cond, then_branch=then_body.graph, else_branch=else_body.graph, op_version=opv, output_names=['y'], global_context={'xy': node_xy}, clear_subgraph_inputs=True) model_def = ifnode.to_onnx( {'x1': x1, 'x2': x2}, target_opset=opv, outputs=[('y', FloatTensorType())]) sess = InferenceSession(model_def.SerializeToString()) res = sess.run(None, {'x1': x1, 'x2': x2}) assert_almost_equal(x1 + x1 * x2, res[0])
def test_if2(self): opv = TARGET_OPSET x1 = numpy.array([[0, 3], [7, 0]], dtype=numpy.float32) x2 = numpy.array([[1, 0], [2, 0]], dtype=numpy.float32) node = OnnxAdd( 'x1', 'x2', output_names=['absxythen'], op_version=opv) then_body = node.to_onnx( {'x1': x1, 'x2': x2}, target_opset=opv, outputs=[('absxythen', FloatTensorType())]) node = OnnxSub( 'x1', 'x2', output_names=['absxyelse'], op_version=opv) else_body = node.to_onnx( {'x1': x1, 'x2': x2}, target_opset=opv, outputs=[('absxyelse', FloatTensorType())]) del else_body.graph.input[:] del then_body.graph.input[:] cond = OnnxGreater( OnnxReduceSum('x1', op_version=opv), OnnxReduceSum('x2', op_version=opv), op_version=opv) ifnode = OnnxIf(cond, then_branch=then_body.graph, else_branch=else_body.graph, op_version=opv, output_names=['y']) model_def = ifnode.to_onnx( {'x1': x1, 'x2': x2}, target_opset=opv, outputs=[('y', FloatTensorType())]) oinf = OnnxInference(model_def) dot = oinf.to_dot() self.assertIn("Gr_Greater -> Gr_C0;", dot)
def test_onnx_if_algebra_direct(self): opv = TARGET_OPSET x1 = np.array([[0, 3], [7, 0]], dtype=np.float32) x2 = np.array([[1, 0], [2, 0]], dtype=np.float32) node = OnnxAdd( 'x1', 'x2', output_names=['absxythen'], op_version=opv) then_body = node.to_onnx( {'x1': x1, 'x2': x2}, target_opset=opv, outputs=[('absxythen', FloatTensorType())]) node = OnnxSub( 'x1', 'x2', output_names=['absxyelse'], op_version=opv) else_body = node.to_onnx( {'x1': x1, 'x2': x2}, target_opset=opv, outputs=[('absxyelse', FloatTensorType())]) del else_body.graph.input[:] del then_body.graph.input[:] cond = OnnxGreater( OnnxReduceSum('x1', op_version=opv), OnnxReduceSum('x2', op_version=opv), op_version=opv) ifnode = OnnxIf(cond, then_branch=then_body.graph, else_branch=else_body.graph, op_version=opv, output_names=['y']) model_def = ifnode.to_onnx( {'x1': x1, 'x2': x2}, target_opset=opv, outputs=[('y', FloatTensorType())]) sess = InferenceSession(model_def.SerializeToString()) res = sess.run(None, {'x1': x1, 'x2': x2}) assert_almost_equal(x1 + x2, res[0])
def validator_classifier_converter(scope, operator, container): input0 = operator.inputs[0] # first input in ONNX graph outputs = operator.outputs # outputs in ONNX graph op = operator.raw_operator # scikit-learn model (mmust be fitted) opv = container.target_opset # The model calls another one. The class `OnnxSubEstimator` # calls the converter for this operator. model = op.estimator_ onnx_op = OnnxSubEstimator(model, input0, op_version=opv, options={'zipmap': False}) rmax = OnnxReduceMax(onnx_op[1], axes=[1], keepdims=0, op_version=opv) great = OnnxGreater(rmax, np.array([op.threshold], dtype=np.float32), op_version=opv) valid = OnnxCast(great, to=onnx_proto.TensorProto.INT64, op_version=opv) r1 = OnnxIdentity(onnx_op[0], output_names=[outputs[0].full_name], op_version=opv) r2 = OnnxIdentity(onnx_op[1], output_names=[outputs[1].full_name], op_version=opv) r3 = OnnxIdentity(valid, output_names=[outputs[2].full_name], op_version=opv) r1.add_to(scope, container) r2.add_to(scope, container) r3.add_to(scope, container)
def build_leaky_relu_decomposed_greater(alpha=0.5, target_opset=15): signo = OnnxGreater('X', numpy.array([0], dtype=numpy.float32), op_version=target_opset) sign = OnnxCast(signo, to=TensorProto.FLOAT, op_version=target_opset) fact = OnnxAdd(OnnxMul(sign, numpy.array([1 - alpha], dtype=numpy.float32), op_version=target_opset), numpy.array([alpha], dtype=numpy.float32), op_version=target_opset) x = OnnxMul('X', fact, op_version=target_opset, output_names=['Y']) return x.to_onnx({'X': FloatTensorType()}, outputs={'Y': FloatTensorType()}, target_opset=target_opset)
def test_onnx_simple_text_plot_if(self): opv = TARGET_OPSET x1 = numpy.array([[0, 3], [7, 0]], dtype=numpy.float32) x2 = numpy.array([[1, 0], [2, 0]], dtype=numpy.float32) node = OnnxAdd('x1', 'x2', output_names=['absxythen'], op_version=opv) then_body = node.to_onnx({ 'x1': x1, 'x2': x2 }, target_opset=opv, outputs=[('absxythen', FloatTensorType())]) node = OnnxSub('x1', 'x2', output_names=['absxyelse'], op_version=opv) else_body = node.to_onnx({ 'x1': x1, 'x2': x2 }, target_opset=opv, outputs=[('absxyelse', FloatTensorType())]) del else_body.graph.input[:] del then_body.graph.input[:] cond = OnnxGreater(OnnxReduceSum('x1', op_version=opv), OnnxReduceSum('x2', op_version=opv), op_version=opv) ifnode = OnnxIf(cond, then_branch=then_body.graph, else_branch=else_body.graph, op_version=opv, output_names=['y']) model_def = ifnode.to_onnx({ 'x1': x1, 'x2': x2 }, target_opset=opv, outputs=[('y', FloatTensorType())]) text = onnx_simple_text_plot(model_def) expected = textwrap.dedent(""" input: """).strip(" \n") self.assertIn(expected, text) self.assertIn("If(Gr_C0) -> y", text) oinf = OnnxInference(model_def) text2 = oinf.to_text(kind="seq") self.assertEqual(text, text2)
def test_if(self): tensor_type = FloatTensorType op_version = TARGET_OPSET bthen = OnnxConstant( value_floats=numpy.array([0], dtype=numpy.float32), op_version=op_version, output_names=['res_then']) bthen.set_onnx_name_prefix('then') belse = OnnxConstant( value_floats=numpy.array([1], dtype=numpy.float32), op_version=op_version, output_names=['res_else']) belse.set_onnx_name_prefix('else') bthen_body = bthen.to_onnx( OrderedDict(), outputs=[('res_then', tensor_type())], target_opset=op_version) belse_body = belse.to_onnx( OrderedDict(), outputs=[('res_else', tensor_type())], target_opset=op_version) onx = OnnxIf(OnnxGreater('X', numpy.array([0], dtype=numpy.float32), op_version=op_version), output_names=['Z'], then_branch=bthen_body.graph, else_branch=belse_body.graph, op_version=op_version) x = numpy.array([1, 2], dtype=numpy.float32) y = numpy.array([1, 3], dtype=numpy.float32) model_def = onx.to_onnx({'X': x.astype(numpy.float32), 'Y': y.astype(numpy.float32)}, target_opset=TARGET_OPSET) got = OnnxInference(model_def).run({'X': x, 'Y': y}) self.assertEqualArray(numpy.array([0.], dtype=numpy.float32), got['Z']) x = numpy.array([-1, -2], dtype=numpy.float32) y = numpy.array([-1, -3], dtype=numpy.float32) model_def = onx.to_onnx({'X': x.astype(numpy.float32), 'Y': y.astype(numpy.float32)}, target_opset=TARGET_OPSET) got = OnnxInference(model_def).run({'X': x, 'Y': y}) self.assertEqualArray(numpy.array([1.], dtype=numpy.float32), got['Z'])
def test_onnx_if_to_dot(self): opv = 15 x1 = numpy.array([[0, 3], [7, 0]], dtype=numpy.float32) x2 = numpy.array([[1, 0], [2, 0]], dtype=numpy.float32) node = OnnxAdd('x1', 'x2', output_names=['absxythen'], op_version=opv) then_body = node.to_onnx({ 'x1': x1, 'x2': x2 }, target_opset=opv, outputs=[('absxythen', FloatTensorType())]) node = OnnxSub('x1', 'x2', output_names=['absxyelse'], op_version=opv) else_body = node.to_onnx({ 'x1': x1, 'x2': x2 }, target_opset=opv, outputs=[('absxyelse', FloatTensorType())]) del else_body.graph.input[:] del then_body.graph.input[:] cond = OnnxGreater(OnnxReduceSum('x1', op_version=opv), OnnxReduceSum('x2', op_version=opv), op_version=opv) ifnode = OnnxIf(cond, then_branch=then_body.graph, else_branch=else_body.graph, op_version=opv, output_names=['y']) model_def = ifnode.to_onnx({ 'x1': x1, 'x2': x2 }, target_opset=opv, outputs=[('y', FloatTensorType())]) dot = OnnxInference(model_def, skip_run=True).to_dot(recursive=True) self.assertIn("subgraph cluster_If", dot)