def test_onnx_rename_names_type(self): rows = [] def flog(*s): rows.append(" ".join(map(str, s))) dtype = numpy.float32 x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape( (3, 2)) cop = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=TARGET_OPSET) cop2 = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=TARGET_OPSET) cop3 = OnnxAdd('X', numpy.array([2], dtype=dtype), op_version=TARGET_OPSET, output_names=['inter']) cop4 = OnnxSub(OnnxMul(cop, cop3, op_version=TARGET_OPSET), cop2, output_names=['final'], op_version=TARGET_OPSET) model_def = cop4.to_onnx({'X': x}) oinf1 = OnnxInference(model_def) new_model = onnx_rename_names(model_def, verbose=1, fLOG=flog, strategy='type') total = "\n".join(rows) self.assertIn("'Ad_Addcst' -> 'i_05'", total) oinf2 = OnnxInference(new_model) y1 = oinf1.run({'X': x}) y2 = oinf2.run({'X': x}) self.assertEqualArray(y1['final'], y2['final'])
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_onnx_text_plot(self): idi = numpy.identity(2).astype(numpy.float32) opv = TARGET_OPSET A = OnnxAdd('X', idi, op_version=opv) B = OnnxSub(A, 'W', output_names=['Y'], op_version=opv) onx = B.to_onnx({ 'X': idi.astype(numpy.float32), 'W': idi.astype(numpy.float32) }) res = onnx_text_plot(onx) self.assertIn("Init", res)
def test_algebra_converter(self): coef = numpy.array([[1, 2], [3, 4]], dtype=numpy.float64) intercept = 1 X_test = numpy.array([[1, -2], [3, -4]], dtype=numpy.float64) onnx_fct = OnnxSub(OnnxMatMul('X', coef), numpy.array([intercept], dtype=numpy.float64), output_names=['Y']) onnx_model = onnx_fct.to_onnx({'X': X_test}, dtype=numpy.float64) sess = InferenceSession(onnx_model.SerializeToString()) ort_pred = sess.run(None, {'X': X_test})[0] assert_almost_equal(ort_pred, numpy.array([[-6., -7.], [-10., -11.]]))
def build_ort_where_add(op_version=12): node = OnnxSub( OnnxMul('x', 'cond', op_version=op_version), OnnxMul('y', OnnxSub('cond', numpy.array([1], dtype=numpy.float32), op_version=op_version), op_version=op_version), op_version=op_version, output_names=['z']) onx = node.to_onnx(inputs=[('cond', FloatTensorType()), ('x', FloatTensorType()), ('y', FloatTensorType())], target_opset=op_version) sess = InferenceSession(onx.SerializeToString()) return lambda cond, x, y: sess.run(None, {'cond': cond, 'x': x, 'y': y})
def test_onnx_remove_unused_outputs_new(self): dtype = numpy.float32 x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape( (3, 2)) cop = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=TARGET_OPSET) cop2 = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=TARGET_OPSET) cop3 = OnnxAdd('X', numpy.array([2], dtype=dtype), op_version=TARGET_OPSET, output_names=['inter']) cop4 = OnnxSub(OnnxMul(cop, cop3, op_version=TARGET_OPSET), cop2, output_names=['final'], op_version=TARGET_OPSET) model_def0 = cop4.to_onnx({'X': x}) model_def = select_model_inputs_outputs(model_def0, "inter", infer_shapes=True, remove_unused=False) stats = onnx_statistics(model_def, optim=True) c1 = model_def.SerializeToString() new_model = select_model_inputs_outputs(model_def0, "inter", infer_shapes=True) c2 = model_def.SerializeToString() self.assertEqual(c1, c2) stats2 = onnx_statistics(model_def, optim=True) stats3 = onnx_statistics(new_model, optim=False) self.assertEqual(stats['ninits'], 2) self.assertEqual(stats2['ninits'], 2) self.assertEqual(stats3['ninits'], 1) self.assertEqual(stats2['nnodes'], 1) self.assertEqual(stats3['nnodes'], 1) oinf1 = OnnxInference(model_def) y1 = oinf1.run({'X': x}) oinf2 = OnnxInference(new_model) y2 = oinf2.run({'X': x}) self.assertNotIn('final', y1) self.assertNotIn('final', y2) self.assertIn('inter', y1) self.assertIn('inter', y2) self.assertEqualArray(y1['inter'], y2['inter'])
def test_pipe_graph_display_text(self): idi = numpy.identity(2).astype(numpy.float32) opv = TARGET_OPSET A = OnnxAdd('X', idi, op_version=opv) B = OnnxSub(A, 'W', output_names=['Y'], op_version=opv) onx = B.to_onnx({ 'X': idi.astype(numpy.float32), 'W': idi.astype(numpy.float32) }) bigraph = onnx2bigraph(onx) graph = bigraph.display_structure() text = graph.to_text() for c in [ 'Input-1', 'Input-0', 'Output-0', 'W', 'W', 'I0', 'I1', 'inout', 'O0 I0', 'A S' ]: self.assertIn(c, text)
def test_onnx_remove_two_outputs(self): dtype = numpy.float32 x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape( (3, 2)) cop = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=get_opset_number_from_onnx()) cop2 = OnnxAdd('X', numpy.array([1], dtype=dtype), output_names=['keep'], op_version=get_opset_number_from_onnx()) cop3 = OnnxAdd('X', numpy.array([2], dtype=dtype), op_version=get_opset_number_from_onnx()) cop4 = OnnxSub(OnnxMul(cop, cop3, op_version=get_opset_number_from_onnx()), cop2, output_names=['final'], op_version=get_opset_number_from_onnx()) model_def = cop4.to_onnx({'X': x}, outputs=[('keep', FloatTensorType([None, 2])), ('final', FloatTensorType([None, 2]))]) c1 = model_def.SerializeToString() self.assertEqual(len(model_def.graph.output), 2) c2 = model_def.SerializeToString() self.assertEqual(c1, c2) stats = onnx_statistics(model_def, optim=True) new_model = onnx_remove_node_redundant(model_def, max_hash_size=10) stats2 = onnx_statistics(model_def, optim=True) stats3 = onnx_statistics(new_model, optim=False) self.assertEqual(stats['ninits'], 2) self.assertEqual(stats2['ninits'], 2) self.assertEqual(stats3['ninits'], 2) self.assertEqual(stats2['nnodes'], 6) self.assertEqual(stats3['nnodes'], 6) oinf1 = OnnxInference(model_def) y1 = oinf1.run({'X': x}) oinf2 = OnnxInference(new_model) y2 = oinf2.run({'X': x}) self.assertEqualArray(y1['final'], y2['final']) self.assertEqualArray(y1['keep'], y2['keep'])
def test_onnx_rename_names_exc(self): dtype = numpy.float32 x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape( (3, 2)) cop = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=TARGET_OPSET) cop2 = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=TARGET_OPSET) cop3 = OnnxAdd('X', numpy.array([2], dtype=dtype), op_version=TARGET_OPSET, output_names=['inter']) cop4 = OnnxSub(OnnxMul(cop, cop3, op_version=TARGET_OPSET), cop2, output_names=['final'], op_version=TARGET_OPSET) model_def = cop4.to_onnx({'X': x}) self.assertRaise(lambda: onnx_rename_names(model_def, strategy="none"), ValueError)
def _onnx_update_penalty_elastic_error(target_opset=None, dtype=numpy.float32, l1=1e-4, l2=1e-4): """ Returns the ONNX graph for function :math:`Y = f(W) = W - 2 \\beta W - \\alpha sign(W)` *l1* is :math:`\\beta` and *l2* is :math:`\\alpha`. .. gdot:: :script: DOT-SECTION from mlprodict.onnxrt import OnnxInference from onnxcustom.utils.onnx_function import function_onnx_graph model_onnx = function_onnx_graph( 'update_penalty_elastic_error') oinf = OnnxInference(model_onnx, inplace=False) print("DOT-SECTION", oinf.to_dot()) """ from skl2onnx.algebra.onnx_ops import (OnnxSub, OnnxMul, OnnxSign) res = OnnxSub(OnnxMul('X', numpy.array([1 - 2 * l2], dtype=dtype), op_version=target_opset), OnnxMul(OnnxSign('X', op_version=target_opset), numpy.array([l1], dtype=dtype), op_version=target_opset), op_version=target_opset, output_names=['Y']) var_type = dtype_to_var_type(dtype) varsx = [('X', var_type())] onx = res.to_onnx(varsx, outputs=[('Y', var_type())], target_opset=target_opset) return onx
def test_prepare_c_profiling(self): opset = TestOrt.opset dtype = numpy.float32 x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape( (3, 2)) cop = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=opset) cop2 = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=opset) cop3 = OnnxAdd('X', numpy.array([2], dtype=dtype), op_version=opset, output_names=['inter']) cop4 = OnnxSub(OnnxMul(cop, cop3, op_version=opset), cop2, output_names=['final'], op_version=13) model_def = cop4.to_onnx({'X': x}, target_opset=opset) temp = get_temp_folder(__file__, "temp_prepare_c_profiling") cmd = prepare_c_profiling(model_def, [x], dest=temp) self.assertStartsWith("onnx", cmd) self.assertExists(os.path.join(temp, "model.onnx")) self.assertExists(os.path.join(temp, "test_data_set_0", "input_0.pb")) self.assertExists(os.path.join(temp, "test_data_set_0", "output_0.pb"))
def test_onnx_remove_redundant(self): dtype = numpy.float32 x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape( (3, 2)) cop = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=TARGET_OPSET) cop2 = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=TARGET_OPSET) cop3 = OnnxAdd('X', numpy.array([2], dtype=dtype), op_version=TARGET_OPSET) cop4 = OnnxSub(OnnxMul(cop, cop3, op_version=TARGET_OPSET), cop2, output_names=['final'], op_version=TARGET_OPSET) model_def = cop4.to_onnx({'X': x}) stats = onnx_statistics(model_def, optim=True) c1 = model_def.SerializeToString() new_model = onnx_remove_node_redundant(model_def, max_hash_size=10) c2 = model_def.SerializeToString() self.assertEqual(c1, c2) stats2 = onnx_statistics(model_def, optim=True) stats3 = onnx_statistics(new_model, optim=False) self.assertEqual(stats['ninits'], 2) self.assertEqual(stats2['ninits'], 2) self.assertEqual(stats3['ninits'], 2) self.assertEqual(stats2['nnodes'], 6) self.assertEqual(stats3['nnodes'], 6) oinf1 = OnnxInference(model_def) y1 = oinf1.run({'X': x}) oinf2 = OnnxInference(new_model) y2 = oinf2.run({'X': x}) self.assertEqualArray(y1['final'], y2['final'])
def test_enumerate_model_node_outputs(self): dtype = numpy.float32 x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape( (3, 2)) cop = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=TARGET_OPSET) cop2 = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=TARGET_OPSET) cop3 = OnnxAdd('X', numpy.array([2], dtype=dtype), op_version=TARGET_OPSET, output_names=['inter']) cop4 = OnnxSub(OnnxMul(cop, cop3, op_version=TARGET_OPSET), cop2, output_names=['final'], op_version=TARGET_OPSET) model_def = cop4.to_onnx({'X': x}) nodes1 = list(enumerate_model_node_outputs(model_def)) nodes2 = list(enumerate_model_node_outputs(model_def, order=True)) self.assertEqual(list(sorted(nodes1)), list(sorted(nodes2))) expected = ['Ad_Addcst2', 'Ad_C0', 'inter', 'Ad_C02', 'Mu_C0', 'final'] self.assertEqual(nodes2, expected)