def test_onnx_example_cdist_in_minkowski(self): x = numpy.array([1, 2, 1, 3, 2, 2, 2, 3]).astype(numpy.float32).reshape((4, 2)) x2 = numpy.array([[1, 2], [2, 2], [2.1, 2.1], [2, 2]]).astype(numpy.float32).reshape((4, 2)) for pp in [1, 2]: with self.subTest(pp=pp): cop = OnnxIdentity('input', op_version=get_opset_number_from_onnx()) cop2 = OnnxIdentity(onnx_cdist( cop, x2, dtype=numpy.float32, metric="minkowski", p=pp, op_version=get_opset_number_from_onnx()), output_names=['cdist'], op_version=get_opset_number_from_onnx()) model_def = cop2.to_onnx(inputs=[('input', FloatTensorType([None, None]))], outputs=[('cdist', FloatTensorType()) ]) try: sess = OnnxInference(model_def) except RuntimeError as e: raise AssertionError("Issue\n{}".format(model_def)) from e res = sess.run({'input': x})['cdist'] exp = scipy_cdist(x, x2, metric="minkowski", p=pp) self.assertEqualArray(exp, res, decimal=5) with self.subTest(pp=3): x = numpy.array([[6.1, 2.8, 4.7, 1.2], [5.7, 3.8, 1.7, 0.3], [7.7, 2.6, 6.9, 2.3], [6.0, 2.9, 4.5, 1.5], [6.8, 2.8, 4.8, 1.4], [5.4, 3.4, 1.5, 0.4], [5.6, 2.9, 3.6, 1.3], [6.9, 3.1, 5.1, 2.3]], dtype=numpy.float32) cop = OnnxAdd('input', 'input', op_version=get_opset_number_from_onnx()) cop2 = OnnxIdentity(onnx_cdist( cop, x, dtype=numpy.float32, metric="minkowski", p=3, op_version=get_opset_number_from_onnx()), output_names=['cdist'], op_version=get_opset_number_from_onnx()) model_def = cop2.to_onnx(inputs=[('input', FloatTensorType([None, None]))], outputs=[('cdist', FloatTensorType())]) sess = OnnxInference(model_def) res = sess.run({'input': x})['cdist'] exp = scipy_cdist(x * 2, x, metric="minkowski", p=3) self.assertEqualArray(exp, res, decimal=4)
def test_onnx_example_cdist_in(self): x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((3, 2)) x2 = np.array([1.1, 2.1, 4.01, 5.01, 5.001, 4.001, 0, 0]).astype(np.float32).reshape((4, 2)) cop = OnnxAdd('input', 'input') cop2 = OnnxIdentity(onnx_cdist(cop, x2, dtype=np.float32), output_names=['cdist']) model_def = cop2.to_onnx(inputs=[('input', FloatTensorType([None, None]))], outputs=[('cdist', FloatTensorType())]) sess = InferenceSession(model_def.SerializeToString()) res = sess.run(None, {'input': x}) exp = scipy_cdist(x * 2, x2, metric="sqeuclidean") assert_almost_equal(exp, res[0], decimal=5) x = np.array( [[6.1, 2.8, 4.7, 1.2], [5.7, 3.8, 1.7, 0.3], [7.7, 2.6, 6.9, 2.3], [6.0, 2.9, 4.5, 1.5], [6.8, 2.8, 4.8, 1.4], [5.4, 3.4, 1.5, 0.4], [5.6, 2.9, 3.6, 1.3], [6.9, 3.1, 5.1, 2.3]], dtype=np.float32) cop = OnnxAdd('input', 'input') cop2 = OnnxIdentity(onnx_cdist(cop, x, dtype=np.float32), output_names=['cdist']) model_def = cop2.to_onnx(inputs=[('input', FloatTensorType([None, None]))], outputs=[('cdist', FloatTensorType())]) sess = InferenceSession(model_def.SerializeToString()) res = sess.run(None, {'input': x}) exp = scipy_cdist(x * 2, x, metric="sqeuclidean") assert_almost_equal(exp, res[0], decimal=4)
def test_onnx_example_cdist_in(self): from skl2onnx.algebra.complex_functions import onnx_cdist x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape( (3, 2)) x2 = numpy.array([1.1, 2.1, 4.01, 5.01, 5.001, 4.001, 0, 0]).astype(numpy.float32).reshape((4, 2)) cop = OnnxAdd('input', 'input', op_version=get_opset_number_from_onnx()) cop2 = OnnxIdentity(onnx_cdist( cop, x2, dtype=numpy.float32, op_version=get_opset_number_from_onnx()), output_names=['cdist'], op_version=get_opset_number_from_onnx()) model_def = cop2.to_onnx(inputs=[('input', FloatTensorType([None, None]))], outputs=[('cdist', FloatTensorType(None, None))], target_opset=get_opset_number_from_onnx()) sess = OnnxInference(model_def) res = sess.run({'input': x}) exp = scipy_cdist(x * 2, x2, metric="sqeuclidean") self.assertEqualArray(exp, res['cdist'], decimal=5) x = numpy.array( [[6.1, 2.8, 4.7, 1.2], [5.7, 3.8, 1.7, 0.3], [7.7, 2.6, 6.9, 2.3], [6., 2.9, 4.5, 1.5], [6.8, 2.8, 4.8, 1.4], [5.4, 3.4, 1.5, 0.4], [5.6, 2.9, 3.6, 1.3], [6.9, 3.1, 5.1, 2.3]], dtype=numpy.float32) cop = OnnxAdd('input', 'input', op_version=get_opset_number_from_onnx()) cop2 = OnnxIdentity(onnx_cdist( cop, x, dtype=numpy.float32, op_version=get_opset_number_from_onnx()), output_names=['cdist'], op_version=get_opset_number_from_onnx()) model_def = cop2.to_onnx(inputs=[('input', FloatTensorType([None, None]))], outputs=[('cdist', FloatTensorType())], target_opset=get_opset_number_from_onnx()) sess = OnnxInference(model_def) res = sess.run({'input': x}) exp = scipy_cdist(x * 2, x, metric="sqeuclidean") self.assertEqualArray(exp, res['cdist'], decimal=4)
def test_onnx_example_cdist_in_custom_ops(self): x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((3, 2)) x2 = np.array([1.1, 2.1, 4.01, 5.01, 5.001, 4.001, 0, 0]).astype( np.float32).reshape((4, 2)) opv = _TARGET_OPSET_ cop = OnnxAdd( 'input', 'input', op_version=opv) cop2 = OnnxIdentity( OnnxCDist(cop, x2, op_version=opv), output_names=['cdist'], op_version=opv) model_def = cop2.to_onnx( inputs=[('input', FloatTensorType([None, None]))], outputs=[('cdist', FloatTensorType())]) try: sess = InferenceSession(model_def.SerializeToString()) except RuntimeError as e: if "CDist is not a registered" in str(e): return res = sess.run(None, {'input': x}) exp = scipy_cdist(x * 2, x2, metric="sqeuclidean") assert_almost_equal(exp, res[0], decimal=5) x = np.array([[6.1, 2.8, 4.7, 1.2], [5.7, 3.8, 1.7, 0.3], [7.7, 2.6, 6.9, 2.3], [6.0, 2.9, 4.5, 1.5], [6.8, 2.8, 4.8, 1.4], [5.4, 3.4, 1.5, 0.4], [5.6, 2.9, 3.6, 1.3], [6.9, 3.1, 5.1, 2.3]], dtype=np.float32) cop = OnnxAdd( 'input', 'input', op_version=opv) cop2 = OnnxIdentity( OnnxCDist(cop, x, op_version=opv), output_names=['cdist'], op_version=opv) model_def = cop2.to_onnx( inputs=[('input', FloatTensorType([None, None]))], outputs=[('cdist', FloatTensorType())]) sess = InferenceSession(model_def.SerializeToString()) res = sess.run(None, {'input': x}) exp = scipy_cdist(x * 2, x, metric="sqeuclidean") assert_almost_equal(exp, res[0], decimal=4)
def test_onnx_example_algebra(self): initial = np.array([0, 0]).astype(np.float32).reshape((2,)) x = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((3, 2)) opv = _TARGET_OPSET_ add_node = OnnxAdd( 'sum_in', 'next', output_names=['sum_out'], op_version=opv) id_node = OnnxIdentity( add_node, output_names=['scan_out'], op_version=opv) scan_body = id_node.to_onnx( {'sum_in': initial, 'next': initial}, outputs=[('sum_out', FloatTensorType()), ('scan_out', FloatTensorType())]) node = OnnxScan('initial', 'x', output_names=['y', 'z'], num_scan_inputs=1, body=scan_body.graph, op_version=opv) model_def = node.to_onnx( {'initial': initial, 'x': x}, outputs=[('y', FloatTensorType()), ('z', FloatTensorType())]) sess = InferenceSession(model_def.SerializeToString()) res = sess.run(None, {'initial': initial, 'x': x}) y = np.array([9, 12]).astype(np.float32).reshape((2,)) z = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((3, 2)) assert_almost_equal(y, res[0]) assert_almost_equal(z, res[1])
def test_onnx_example_cdist_bigger(self): from skl2onnx.algebra.complex_functions import onnx_cdist data = load_iris() X, y = data.data, data.target self.assertNotEmpty(y) X_train = X[::2] # y_train = y[::2] X_test = X[1::2] # y_test = y[1::2] onx = OnnxIdentity(onnx_cdist(OnnxIdentity('X', op_version=TARGET_OPSET), X_train.astype(numpy.float32), metric="euclidean", dtype=numpy.float32, op_version=TARGET_OPSET), output_names=['Y'], op_version=TARGET_OPSET) final = onx.to_onnx(inputs=[('X', FloatTensorType([None, None]))], outputs=[('Y', FloatTensorType())], target_opset=TARGET_OPSET) oinf = OnnxInference(final, runtime="python") res = oinf.run({'X': X_train.astype(numpy.float32)})['Y'] exp = scipy_cdist(X_train, X_train, metric="euclidean") self.assertEqualArray(exp, res, decimal=6) res = oinf.run({'X': X_test.astype(numpy.float32)})['Y'] exp = scipy_cdist(X_test, X_train, metric="euclidean") self.assertEqualArray(exp, res, decimal=6)
def test_pdist(self): from skl2onnx.algebra.complex_functions import onnx_squareform_pdist x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape( (3, 2)) cop = OnnxAdd('input', 'input', op_version=TARGET_OPSET) cdist = onnx_squareform_pdist(cop, dtype=numpy.float32, op_version=TARGET_OPSET) cop2 = OnnxIdentity(cdist, output_names=['cdist'], op_version=TARGET_OPSET) model_def = cop2.to_onnx({'input': FloatTensorType()}, outputs=[('cdist', FloatTensorType())], target_opset=TARGET_OPSET) sess = OnnxInference(model_def) res = sess.run({'input': x}) self.assertEqual(list(res.keys()), ['cdist']) exp = squareform(pdist(x * 2, metric="sqeuclidean")) self.assertEqualArray(exp, res['cdist']) x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape( (2, 3)) res = sess.run({'input': x}) self.assertEqual(list(res.keys()), ['cdist'])
def common_test_onnxt_runtime_unary(self, onnx_cl, np_fct): onx = onnx_cl('X', output_names=['Y']) X = numpy.array([[1, 2], [3, -4]], dtype=numpy.float64) model_def = onx.to_onnx({'X': X.astype(numpy.float32)}) # no inplace oinf = OnnxInference(model_def, inplace=False) got = oinf.run({'X': X}) self.assertEqual(list(sorted(got)), ['Y']) self.assertEqualArray(np_fct(X), got['Y'], decimal=6) # inplace oinf = OnnxInference(model_def, input_inplace=False, inplace=True) got = oinf.run({'X': X}) self.assertEqual(list(sorted(got)), ['Y']) self.assertEqualArray(np_fct(X), got['Y'], decimal=6) # inplace2 onx2 = OnnxIdentity(onnx_cl('X'), output_names=['Y']) model_def2 = onx2.to_onnx({'X': X.astype(numpy.float32)}) oinf = OnnxInference(model_def2, input_inplace=False, inplace=True) got = oinf.run({'X': X}) self.assertEqual(list(sorted(got)), ['Y']) self.assertEqualArray(np_fct(X), got['Y'], decimal=6) # input inplace expe = np_fct(X) oinf = OnnxInference(model_def, input_inplace=True, inplace=True) got = oinf.run({'X': X}) self.assertEqual(list(sorted(got)), ['Y']) self.assertEqualArray(expe, got['Y'], decimal=6)
def test_onnx_remove_identities2(self): from skl2onnx.algebra.complex_functions import onnx_squareform_pdist x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape( (3, 2)) cop = OnnxIdentity('input', op_version=get_opset_number_from_onnx()) cdist = onnx_squareform_pdist(cop, dtype=numpy.float32, op_version=get_opset_number_from_onnx()) cop2 = OnnxIdentity(cdist, output_names=['cdist'], op_version=get_opset_number_from_onnx()) model_def = cop2.to_onnx({'input': FloatTensorType()}, outputs=[('cdist', FloatTensorType())], target_opset=get_opset_number_from_onnx()) stats = onnx_statistics(model_def, optim=False) self.assertIn('subgraphs', stats) self.assertGreater(stats['subgraphs'], 1) self.assertGreater(stats['op_Identity'], 2) new_model = onnx_remove_node_identity(model_def) stats2 = onnx_statistics(new_model, optim=False) self.assertEqual(stats['subgraphs'], stats2['subgraphs']) self.assertLesser(stats2['op_Identity'], 2) oinf1 = OnnxInference(model_def) oinf2 = OnnxInference(new_model) y1 = oinf1.run({'input': x})['cdist'] y2 = oinf2.run({'input': x})['cdist'] self.assertEqualArray(y1, y2) self.assertLesser(stats2['op_Identity'], 1)
def squareform_pdist(X, **kwargs): opv = TARGET_OPSET diff = OnnxSub('next_in', 'next', output_names=['diff'], op_version=opv) id_next = OnnxIdentity('next_in', output_names=['next_out'], op_version=opv) norm = OnnxReduceSumSquare(diff, output_names=['norm'], axes=[1], op_version=opv) flat = OnnxSqueezeApi11(norm, output_names=['scan_out'], axes=[1], op_version=opv) scan_body = id_next.to_onnx( OrderedDict([('next_in', FloatTensorType()), ('next', FloatTensorType())]), outputs=[('next_out', FloatTensorType([None, None])), ('scan_out', FloatTensorType([None]))], other_outputs=[flat]) node = OnnxScan(X, X, output_names=['scan0_{idself}', 'scan1_{idself}'], num_scan_inputs=1, body=scan_body.graph, op_version=opv, **kwargs) return node[1]
def test_onnx_example_pdist_in(self): opv = _TARGET_OPSET_ x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((3, 2)) cop = OnnxAdd('input', 'input', op_version=opv) cop2 = OnnxIdentity(onnx_squareform_pdist(cop, dtype=np.float32, op_version=opv), output_names=['pdist'], op_version=opv) model_def = cop2.to_onnx(inputs=[('input', FloatTensorType([None, None]))], outputs=[('pdist', FloatTensorType())]) sess = InferenceSession(model_def.SerializeToString()) res = sess.run(None, {'input': x}) exp = squareform(pdist(x * 2, metric="sqeuclidean")) assert_almost_equal(exp, res[0]) x = np.array([1, 2, 4, 5]).astype(np.float32).reshape((2, 2)) sess = InferenceSession(model_def.SerializeToString()) res = sess.run(None, {'input': x}) exp = squareform(pdist(x * 2, metric="sqeuclidean")) assert_almost_equal(exp, res[0]) x = np.array([1, 2, 4, 5, 5, 6]).astype(np.float32).reshape((2, 3)) x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((2, 3)) sess = InferenceSession(model_def.SerializeToString()) res = sess.run(None, {'input': x}) exp = squareform(pdist(x * 2, metric="sqeuclidean")) assert_almost_equal(exp, res[0])
def test_onnx_example_cdist_in_euclidean(self): x2 = numpy.array([1.1, 2.1, 4.01, 5.01, 5.001, 4.001, 0, 0]).astype(numpy.float32).reshape((4, 2)) cop = OnnxAdd('input', 'input', op_version=get_opset_number_from_onnx()) cop2 = OnnxIdentity(onnx_cdist( cop, x2, dtype=numpy.float32, metric='euclidean', op_version=get_opset_number_from_onnx()), output_names=['cdist'], op_version=get_opset_number_from_onnx()) model_def = cop2.to_onnx(inputs=[('input', FloatTensorType([None, None]))], outputs=[('cdist', FloatTensorType())], target_opset=get_opset_number_from_onnx()) new_model = onnx_remove_node_identity(model_def) stats = onnx_statistics(model_def, optim=False) stats2 = onnx_statistics(new_model, optim=False) self.assertEqual(stats.get('op_Identity', 0), 3) self.assertEqual(stats2.get('op_Identity', 0), 1)
def test_onnx_rename_node_scan(self): def squareform_pdist(X, **kwargs): opv = TARGET_OPSET diff = OnnxSub('next_in', 'next', output_names=['diff'], op_version=opv) id_next = OnnxIdentity('next_in', output_names=['next_out'], op_version=opv) norm = OnnxReduceSumSquare(diff, output_names=['norm'], axes=[1], op_version=opv) flat = OnnxSqueezeApi11(norm, output_names=['scan_out'], axes=[1], op_version=opv) scan_body = id_next.to_onnx( OrderedDict([('next_in', FloatTensorType()), ('next', FloatTensorType())]), outputs=[('next_out', FloatTensorType([None, None])), ('scan_out', FloatTensorType([None]))], other_outputs=[flat]) node = OnnxScan(X, X, output_names=['scan0_{idself}', 'scan1_{idself}'], num_scan_inputs=1, body=scan_body.graph, op_version=opv, **kwargs) return node[1] rows = [] def flog(*s): rows.append(" ".join(map(str, s))) opv = TARGET_OPSET onnx_fct = OnnxIdentity(squareform_pdist('x'), output_names='Y', op_version=opv) model_def = onnx_fct.to_onnx(inputs=[('x', FloatTensorType())]) oinf1 = OnnxInference(model_def) new_model = onnx_rename_names(model_def, verbose=1, fLOG=flog, strategy='type') total = "\n".join(rows) self.assertNotIn('name: "Re_ReduceSumSquare"', str(new_model)) self.assertIn("'Re_ReduceSumSquare' -> 'n_24'", total) oinf2 = OnnxInference(new_model) x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape( (3, 2)) y1 = oinf1.run({'x': x}) y2 = oinf2.run({'x': x}) self.assertEqualArray(y1['Y'], y2['Y'])
def test_onnx_example_pdist(self): x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((3, 2)) opv = _TARGET_OPSET_ diff = OnnxSub('next_in', 'next', output_names=['diff'], op_version=opv) id_next = OnnxIdentity('next_in', output_names=['next_out'], op_version=opv) norm = OnnxReduceSumSquare(diff, output_names=['norm'], axes=[1], op_version=opv) flat = OnnxSqueezeApi11(norm, output_names=['scan_out'], axes=[1], op_version=opv) scan_body = id_next.to_onnx(OrderedDict([('next_in', x), ('next', FloatTensorType())]), outputs=[ ('next_out', FloatTensorType([3, 2])), ('scan_out', FloatTensorType([3])) ], other_outputs=[flat], target_opset=opv) sess = InferenceSession(scan_body.SerializeToString()) res = sess.run(None, {'next_in': x, 'next': x[:1]}) assert_almost_equal(x, res[0]) exp = np.array([0., 18., 20.], dtype=np.float32) assert_almost_equal(exp, res[1]) node = OnnxScan('x', 'x', output_names=['y', 'z'], num_scan_inputs=1, body=scan_body.graph, op_version=opv) model_def = node.to_onnx({'x': x}, outputs=[('y', FloatTensorType([3, 2])), ('z', FloatTensorType([3, 3]))]) try: onnx.checker.check_model(model_def) except ValidationError as e: if StrictVersion(onnx__version__) <= StrictVersion("1.5.0"): warnings.warn(e) else: raise e sess = InferenceSession(model_def.SerializeToString()) res = sess.run(None, {'x': x}) exp = squareform(pdist(x, metric="sqeuclidean")) assert_almost_equal(x, res[0]) assert_almost_equal(exp, res[1])
def generate_onnx_graph(opv): node = OnnxAdd(('X1', FloatTensorType()), np.array([0.1], dtype=np.float32), op_version=opv) out = OnnxLinearRegressor(node, coefficients=[0.3, 0.3, 0.4, 0.5, 0.6], intercepts=[-50.], op_version=1) last = OnnxIdentity(out, output_names=['Y'], op_version=opv) onx = last.to_onnx([('X1', FloatTensorType((None, 5)))], outputs=[('Y', FloatTensorType())], target_opset=opv) return onx, (node, out, last)
def test_add_log_loss(self): from onnxcustom.utils.orttraining_helper import add_loss_output ide = OnnxIdentity("X", op_version=opset, output_names=['Y']) onx = ide.to_onnx(inputs={'X': DoubleTensorType()}, outputs={'Y': DoubleTensorType()}, target_opset=opset) onx_loss = add_loss_output(onx, 'log', eps=1e-6) x1 = numpy.array([0, 0, 0.2, 0.5, 0.8, 1, 1]) X = numpy.vstack([1 - x1, x1]).T.astype(numpy.float64) y = numpy.array([0, 1, 0, 1, 1, 1, 0], dtype=numpy.int64) oinf = OnnxInference(onx_loss) output = oinf.run({'X': X, 'label': y.reshape((-1, 1))}) loss = output['loss'] skl_loss = log_loss(y, X[:, 1], eps=1e-6) self.assertLess(numpy.abs(skl_loss - loss[0, 0]), 1e-5)
def test_onnxt_pdist_dot(self): from skl2onnx.algebra.complex_functions import onnx_squareform_pdist # pylint: disable=E0401,E0611 x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape( (3, 2)) cop = OnnxAdd('input', 'input') cdist = onnx_squareform_pdist(cop, dtype=numpy.float32) cop2 = OnnxIdentity(cdist, output_names=['cdist']) model_def = cop2.to_onnx({'input': x}, outputs=[('cdist', FloatTensorType())]) oinf = OnnxInference(model_def, skip_run=True) dot = oinf.to_dot(recursive=True) self.assertIn("B_next_out", dot) self.assertIn("cluster", dot)
def generate_onnx_graph(opv): dtype = np.float32 if cls_type == FloatTensorType else np.float64 node = OnnxAdd(first_input, np.array([0.1], dtype=dtype), op_version=opv) lr = model() lr.fit(np.ones([10, 5]), np.arange(0, 10) % 3) out = OnnxSubEstimator(lr, node, op_version=1, options=options) if model == LogisticRegression: last = OnnxIdentity(out[1], output_names=['Y'], op_version=opv) else: last = OnnxIdentity(out, output_names=['Y'], op_version=opv) onx = last.to_onnx([('X1', cls_type((None, 5)))], outputs=[('Y', cls_type())], target_opset=opv) return onx
def test_onnx_stat_recursive(self): from skl2onnx.algebra.complex_functions import onnx_squareform_pdist cop = OnnxAdd( OnnxIdentity('input', op_version=__max_supported_opset__), 'input', op_version=__max_supported_opset__) cdist = onnx_squareform_pdist( cop, dtype=numpy.float32, op_version=__max_supported_opset__) cop2 = OnnxIdentity(cdist, output_names=['cdist'], op_version=__max_supported_opset__) model_def = cop2.to_onnx( {'input': FloatTensorType()}, outputs=[('cdist', FloatTensorType())], target_opset=__max_supported_opset__) stats = onnx_statistics(model_def) self.assertIn('subgraphs', stats) self.assertGreater(stats['subgraphs'], 1) self.assertGreater(stats['op_Identity'], 2)
def test_onnx_example_pdist(self): x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((3, 2)) diff = OnnxSub('next_in', 'next', output_names=['diff']) id_next = OnnxIdentity('next_in', output_names=['next_out']) norm = OnnxReduceSumSquare(diff, output_names=['norm'], axes=[1]) flat = OnnxSqueeze(norm, output_names=['scan_out'], axes=[1]) scan_body = id_next.to_onnx(OrderedDict([('next_in', x), ('next', FloatTensorType())]), outputs=[ ('next_out', FloatTensorType([3, 2])), ('scan_out', FloatTensorType([3])) ], other_outputs=[flat]) sess = InferenceSession(scan_body.SerializeToString()) res = sess.run(None, {'next_in': x, 'next': x[:1]}) assert_almost_equal(x, res[0]) exp = np.array([0., 18., 20.], dtype=np.float32) assert_almost_equal(exp, res[1]) node = OnnxScan('x', 'x', output_names=['y', 'z'], num_scan_inputs=1, body=scan_body.graph) model_def = node.to_onnx({'x': x}, outputs=[('y', FloatTensorType([3, 2])), ('z', FloatTensorType([3, 3]))]) try: onnx.checker.check_model(model_def) except ValidationError as e: if sys.platform.startswith("win"): # schema information in onnx is incomplete on Windows warnings.warn(e) else: raise e sess = InferenceSession(model_def.SerializeToString()) res = sess.run(None, {'x': x}) exp = squareform(pdist(x, metric="sqeuclidean")) assert_almost_equal(x, res[0]) assert_almost_equal(exp, res[1])
def test_onnx_subgraphs1(self): x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape( (3, 2)) cop = OnnxAdd(OnnxIdentity('input', op_version=TARGET_OPSET), 'input', op_version=TARGET_OPSET) cdist = onnx_squareform_pdist(cop, dtype=numpy.float32, op_version=TARGET_OPSET) cop2 = OnnxIdentity(cdist, output_names=['cdist'], op_version=TARGET_OPSET) model_def = cop2.to_onnx({'input': FloatTensorType([None, None])}, outputs=[('cdist', FloatTensorType([None, None]))], target_opset=TARGET_OPSET) sess = InferenceSession(model_def.SerializeToString()) res = sess.run(None, {'input': x}) self.assertEqual(len(res), 1)
def _onnx_copy(target_opset=None, dtype=numpy.float32): """ Returns the ONNX graph for function :math:`Y = X`. .. gdot:: :script: DOT-SECTION from mlprodict.onnxrt import OnnxInference from onnxcustom.utils.onnx_function import function_onnx_graph model_onnx = function_onnx_graph('copy') oinf = OnnxInference(model_onnx, inplace=False) print("DOT-SECTION", oinf.to_dot()) """ from skl2onnx.algebra.onnx_ops import OnnxIdentity res = OnnxIdentity('X', 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_onnx_example_cdist_in_euclidean(self): for metric in ['euclidean', 'minkowski']: for opv in [11, TARGET_OPSET]: with self.subTest(metric=metric, opv=opv): x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape((3, 2)) x2 = numpy.array( [1.1, 2.1, 4.01, 5.01, 5.001, 4.001, 0, 0]).astype(numpy.float32).reshape((4, 2)) cop = OnnxAdd('input', 'input', op_version=opv) if metric == "minkowski": cop2 = OnnxIdentity(onnx_cdist(cop, x2, dtype=numpy.float32, metric=metric, op_version=opv, p=2), output_names=['cdist'], op_version=opv) else: cop2 = OnnxIdentity(onnx_cdist(cop, x2, dtype=numpy.float32, metric=metric, op_version=opv), output_names=['cdist'], op_version=opv) model_def = cop2.to_onnx(inputs=[ ('input', FloatTensorType([None, None])) ], outputs=[('cdist', FloatTensorType())], target_opset=opv) sess = OnnxInference(model_def) res = sess.run({'input': x})['cdist'] exp = scipy_cdist(x * 2, x2, metric=metric) self.assertEqualArray(exp, res, decimal=5) if metric == "minkowski": continue x = numpy.array( [[6.1, 2.8, 4.7, 1.2], [5.7, 3.8, 1.7, 0.3], [7.7, 2.6, 6.9, 2.3], [6.0, 2.9, 4.5, 1.5], [6.8, 2.8, 4.8, 1.4], [5.4, 3.4, 1.5, 0.4], [5.6, 2.9, 3.6, 1.3], [6.9, 3.1, 5.1, 2.3]], dtype=numpy.float32) cop = OnnxAdd('input', 'input', op_version=opv) cop2 = OnnxIdentity(onnx_cdist(cop, x, dtype=numpy.float32, op_version=opv), output_names=['cdist'], op_version=opv) model_def = cop2.to_onnx(inputs=[ ('input', FloatTensorType([None, None])) ], outputs=[('cdist', FloatTensorType())], target_opset=opv) sess = OnnxInference(model_def) res = sess.run({'input': x})['cdist'] exp = scipy_cdist(x * 2, x, metric="sqeuclidean") self.assertEqualArray(exp, res, decimal=4)