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=get_opset_number_from_onnx()),
                                      X_train.astype(numpy.float32),
                                      metric="euclidean",
                                      dtype=numpy.float32,
                                      op_version=get_opset_number_from_onnx()),
                           output_names=['Y'],
                           op_version=get_opset_number_from_onnx())
        final = onx.to_onnx(inputs=[('X', FloatTensorType([None, None]))],
                            outputs=[('Y', FloatTensorType())],
                            target_opset=get_opset_number_from_onnx())

        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_onnxrt_string_normalizer_stopwords(self):
        corpus = numpy.array([
            'This is the first document.',
            'This document is the second document.',
            'And this is the third one.', 'Is this the first document?'
        ])

        op = OnnxStringNormalizer('text',
                                  op_version=get_opset_number_from_onnx(),
                                  output_names=['out'],
                                  stopwords=['this'])
        onx = op.to_onnx(inputs=[('text', StringTensorType())])
        oinf = OnnxInference(onx)
        res = oinf.run({'text': corpus})
        self.assertEqual(list(res['out']),
                         list(_.replace("this ", "") for _ in corpus))

        op = OnnxStringNormalizer('text',
                                  op_version=get_opset_number_from_onnx(),
                                  output_names=['out'],
                                  stopwords=['this'],
                                  case_change_action='LOWER',
                                  is_case_sensitive=0)
        onx = op.to_onnx(inputs=[('text', StringTensorType())])
        oinf = OnnxInference(onx)
        res = oinf.run({'text': corpus})
        self.assertEqual(list(res['out']),
                         list(_.lower().replace("this ", "") for _ in corpus))
Example #3
0
    def test_pipeline_add(self):
        iris = load_iris()
        X, y = iris.data, iris.target
        pca = PCA(n_components=2)
        pca.fit(X)

        add = OnnxAdd('X', numpy.full((1, X.shape[1]), 1, dtype=numpy.float32),
                      output_names=['Yadd'],
                      op_version=get_opset_number_from_onnx())
        onx = add.to_onnx(inputs=[('X', FloatTensorType((None, X.shape[1])))],
                          outputs=[('Yadd', FloatTensorType((None, X.shape[1])))])

        tr = OnnxTransformer(onx)
        tr.fit()

        pipe = make_pipeline(tr, LogisticRegression())
        pipe.fit(X, y)
        pred = pipe.predict(X)
        self.assertEqual(pred.shape, (150, ))
        model_onnx = to_onnx(pipe, X.astype(numpy.float32),
                             target_opset=get_opset_number_from_onnx())

        oinf = OnnxInference(model_onnx)
        y1 = pipe.predict(X)
        y2 = oinf.run({'X': X.astype(numpy.float32)})
        self.assertEqual(list(y2), ['output_label', 'output_probability'])
        self.assertEqualArray(y1, y2['output_label'])
        y1 = pipe.predict_proba(X)
        probas = DataFrame(list(y2['output_probability'])).values
        self.assertEqualArray(y1, probas, decimal=5)
    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=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())

        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 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 test_kernel_ker2_def_ort(self):
     ker = Sum(
         CK(0.1, (1e-3, 1e3)) * RBF(length_scale=10,
                                    length_scale_bounds=(1e-3, 1e3)),
         CK(0.1, (1e-3, 1e3)) * RBF(length_scale=1,
                                    length_scale_bounds=(1e-3, 1e3)))
     onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=numpy.float32,
                          op_version=get_opset_number_from_onnx())
     model_onnx = onx.to_onnx(
         inputs=[('X', FloatTensorType([None, None]))],
         outputs=[('Y', FloatTensorType([None, None]))],
         target_opset=get_opset_number_from_onnx())
     model_onnx.ir_version = get_ir_version_from_onnx()
     sess = _capture_output(
         lambda: OnnxInference(model_onnx.SerializeToString(),
                               runtime="onnxruntime2"), 'c')[0]
     try:
         res = sess.run({'X': Xtest_.astype(numpy.float32)})
     except RuntimeError as e:
         if "Got invalid dimensions for input" in str(e):
             # probable bug somewhere
             return
         raise e
     m1 = res['Y']
     m2 = ker(Xtest_)
     self.assertEqualArray(m1, m2, decimal=5)
Example #7
0
    def test_onnxt_idi(self):
        idi = numpy.identity(2)
        onx = OnnxAdd('X', idi, output_names=['Y'],
                      op_version=get_opset_number_from_onnx())
        model_def = onx.to_onnx({'X': idi.astype(numpy.float32)},
                                target_opset=get_opset_number_from_onnx())

        oinf = OnnxInference(model_def)
        res = str(oinf)
        self.assertIn('op_type: "Add"', res)

        sb = model_def.SerializeToString()
        oinf = OnnxInference(sb)
        res = str(oinf)
        self.assertIn('op_type: "Add"', res)

        sb = BytesIO(model_def.SerializeToString())
        oinf = OnnxInference(sb)
        res = str(oinf)
        self.assertIn('op_type: "Add"', res)

        temp = get_temp_folder(__file__, "temp_onnxrt_idi")
        name = os.path.join(temp, "m.onnx")
        with open(name, "wb") as f:
            f.write(model_def.SerializeToString())

        oinf = OnnxInference(name)
        res = str(oinf)
        self.assertIn('op_type: "Add"', res)
    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_export_sklearn_kernel_rational_quadratic(self):
        def kernel_rational_quadratic_none(X,
                                           length_scale=1.0,
                                           alpha=2.0,
                                           op_version=None):
            dists = squareform_pdist(X,
                                     metric='sqeuclidean',
                                     op_version=op_version)
            cst = py_pow(length_scale, 2, op_version=op_version)
            cst = py_mul(cst, alpha, 2, op_version=op_version)
            t_cst = py_make_float_array(cst)
            tmp = dists / t_cst
            t_one = py_make_float_array(1)
            base = tmp + t_one
            t_alpha = py_make_float_array(py_opp(alpha, op_version=op_version))
            K = numpy.power(base, t_alpha)
            return K

        x = numpy.array([[1, 2], [3, 4], [5, 6]], dtype=float)
        kernel = RationalQuadratic(length_scale=1.0, alpha=2.0)
        exp = kernel(x, None)
        got = kernel_rational_quadratic_none(
            x,
            length_scale=1.0,
            alpha=2.0,
            op_version=get_opset_number_from_onnx())
        self.assertEqualArray(exp, got)

        fct = translate_fct2onnx(kernel_rational_quadratic_none,
                                 cpl=True,
                                 output_names=['Z'],
                                 dtype=numpy.float32)

        r = fct('X',
                dtype=numpy.float32,
                op_version=get_opset_number_from_onnx())
        self.assertIsInstance(r, OnnxIdentity)
        inputs = {'X': x.astype(numpy.float32)}
        try:
            onnx_g = r.to_onnx(inputs)
        except RuntimeError as e:
            if "Opset number 12 is higher than targeted opset 11" in str(e):
                return
            raise e

        oinf = OnnxInference(onnx_g)
        res = oinf.run(inputs)
        self.assertEqualArray(exp, res['Z'])

        exp = kernel(x.T, None)
        got = kernel_rational_quadratic_none(x.T)
        self.assertEqualArray(exp, got)
        inputs = {'X': x.T.astype(numpy.float32)}
        res = oinf.run(inputs)
        self.assertEqualArray(exp, res['Z'])
Example #10
0
 def test_onnxt_json(self):
     idi = numpy.identity(2)
     idi2 = numpy.identity(2) * 2
     onx = OnnxAdd(
         OnnxAdd('X', idi, op_version=get_opset_number_from_onnx()),
         idi2, output_names=['Y'],
         op_version=get_opset_number_from_onnx())
     model_def = onx.to_onnx({'X': idi.astype(numpy.float32)},
                             target_opset=get_opset_number_from_onnx())
     oinf = OnnxInference(model_def)
     js = oinf.to_json()
     self.assertIn('"initializers": {', js)
 def test_kernel_ker12_def(self):
     ker = (Sum(CK(0.1, (1e-3, 1e3)), CK(0.1, (1e-3, 1e3)) *
                RBF(length_scale=1, length_scale_bounds=(1e-3, 1e3))))
     onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=numpy.float32,
                          op_version=get_opset_number_from_onnx())
     model_onnx = onx.to_onnx(
         inputs=[('X', FloatTensorType([None, None]))],
         outputs=[('Y', FloatTensorType([None, None]))],
         target_opset=get_opset_number_from_onnx())
     sess = OnnxInference(model_onnx.SerializeToString())
     res = sess.run({'X': Xtest_.astype(numpy.float32)})
     m1 = res['Y']
     m2 = ker(Xtest_)
     self.assertEqualArray(m1, m2)
Example #12
0
 def test_onnxt_run(self):
     idi = numpy.identity(2, dtype=numpy.float32)
     idi2 = (numpy.identity(2) * 2).astype(numpy.float32)
     onx = OnnxAdd(
         OnnxAdd('X', idi, op_version=get_opset_number_from_onnx()),
         idi2, output_names=['Y'],
         op_version=get_opset_number_from_onnx())
     model_def = onx.to_onnx({'X': idi.astype(numpy.float32)},
                             target_opset=get_opset_number_from_onnx())
     oinf = OnnxInference(model_def)
     X = numpy.array([[1, 1], [3, 3]])
     y = oinf.run({'X': X.astype(numpy.float32)})
     exp = numpy.array([[4, 1], [3, 6]], dtype=numpy.float32)
     self.assertEqual(list(y), ['Y'])
     self.assertEqualArray(y['Y'], exp)
Example #13
0
 def test_constant_of_shape(self):
     x = numpy.array([1, 2, 4, 5, 5, 4]).astype(
         numpy.float32).reshape((3, 2))
     tensor_value = make_tensor(
         "value", TensorProto.FLOAT, (1,), [-5])  # pylint: disable=E1101
     cop2 = OnnxConstantOfShape(
         OnnxShape('input', op_version=get_opset_number_from_onnx()),
         value=tensor_value,
         output_names=['mat'],
         op_version=get_opset_number_from_onnx())
     model_def = cop2.to_onnx({'input': x},
                              outputs=[('mat', FloatTensorType())],
                              target_opset=get_opset_number_from_onnx())
     oinf = OnnxInference(model_def, skip_run=True)
     dot = oinf.to_dot()
     self.assertIn('ConstantOfShape', dot)
Example #14
0
    def test_onnxview(self):

        idi = numpy.identity(2)
        onx = OnnxAdd('X',
                      idi,
                      output_names=['Y'],
                      op_version=get_opset_number_from_onnx())
        model_def = onx.to_onnx({'X': idi.astype(numpy.float32)})

        mg = OnnxNotebook()
        mg.add_context({"model": model_def})
        cmd = "--help"
        res, out, _ = self.capture(lambda: mg.onnxview(cmd))
        self.assertEmpty(res)
        self.assertIn("notebook", out)

        mg = OnnxNotebook()
        mg.add_context({"model": model_def})
        cmd = "model"
        res = mg.onnxview(cmd)
        self.assertNotEmpty(res)
        self.assertIn('RenderJsDot', str(res))

        mg = OnnxNotebook()
        mg.add_context({"model": model_def})
        cmd = "-r 1 model"
        res = mg.onnxview(cmd)
        self.assertNotEmpty(res)
        self.assertIn('RenderJsDot', str(res))
Example #15
0
    def test_onnxt_lrc_iris_run_node_time(self):
        iris = load_iris()
        X, y = iris.data, iris.target
        X_train, X_test, y_train, _ = train_test_split(X, y, random_state=11)
        clr = LogisticRegression(solver="liblinear")
        clr.fit(X_train, y_train)

        model_def = to_onnx(clr, X_train.astype(numpy.float32),
                            target_opset=get_opset_number_from_onnx())
        oinf = OnnxInference(model_def)
        _, mt = oinf.run({'X': X_test}, node_time=True)
        self.assertIsInstance(mt, list)
        self.assertGreater(len(mt), 1)
        self.assertIsInstance(mt[0], dict)

        rows = []

        def myprint(*args):
            rows.append(' '.join(map(str, args)))

        _, mt = oinf.run({'X': X_test}, node_time=True,
                         verbose=1, fLOG=myprint)
        self.assertIsInstance(mt, list)
        self.assertGreater(len(mt), 1)
        self.assertIsInstance(mt[0], dict)
    def test_onnxrt_tfidf_vectorizer_empty(self):
        inputi = numpy.array([[1, 1, 3, 3, 3, 7, 8, 6, 7, 5, 6,
                               8]]).astype(numpy.int64)
        output = numpy.array([[1., 1., 1.]]).astype(numpy.float32)

        ngram_counts = numpy.array([0, 0]).astype(numpy.int64)
        ngram_indexes = numpy.array([0, 1, 2]).astype(numpy.int64)
        pool_int64s = numpy.array([  # unigrams
            5, 6, 7, 8, 6, 7
        ]).astype(numpy.int64)  # bigrams

        op = OnnxTfIdfVectorizer('tokens',
                                 op_version=get_opset_number_from_onnx(),
                                 mode='TF',
                                 min_gram_length=2,
                                 max_gram_length=2,
                                 max_skip_count=0,
                                 ngram_counts=ngram_counts,
                                 ngram_indexes=ngram_indexes,
                                 pool_int64s=pool_int64s,
                                 output_names=['out'])
        onx = op.to_onnx(inputs=[('tokens', Int64TensorType())],
                         outputs=[('out', FloatTensorType())])
        oinf = OnnxInference(onx)
        res = oinf.run({'tokens': inputi})
        self.assertEqual(output.tolist(), res['out'].tolist())
    def test_export_sklearn_kernel_dot_product_default(self):
        def kernel_call_ynone(X, sigma_0=2.):
            t_sigma_0 = py_make_float_array(py_pow(sigma_0, 2))
            K = X @ numpy.transpose(X, axes=[1, 0]) + t_sigma_0
            return K

        x = numpy.array([[1, 2], [3, 4], [5, 6]], dtype=float)
        kernel = DotProduct(sigma_0=2.)
        exp = kernel(x, None)
        got = kernel_call_ynone(x, sigma_0=2.)
        self.assertEqualArray(exp, got)

        fct = translate_fct2onnx(kernel_call_ynone,
                                 cpl=True,
                                 output_names=['Z'])

        r = fct('X', op_version=get_opset_number_from_onnx())
        self.assertIsInstance(r, OnnxIdentity)
        inputs = {'X': x.astype(numpy.float32)}
        onnx_g = r.to_onnx(inputs)
        oinf = OnnxInference(onnx_g)
        res = oinf.run(inputs)
        self.assertEqualArray(exp, res['Z'])

        exp = kernel(x.T, None)
        got = kernel_call_ynone(x.T)
        self.assertEqualArray(exp, got)
        inputs = {'X': x.T.astype(numpy.float32)}
        res = oinf.run(inputs)
        self.assertEqualArray(exp, res['Z'])
Example #18
0
    def test_onnxt_reduce_size(self):
        idi = numpy.identity(2)
        onx = OnnxAdd('X', idi, output_names=['Y'],
                      op_version=get_opset_number_from_onnx())
        model_def = onx.to_onnx({'X': idi.astype(numpy.float32)})

        oinf = OnnxInference(model_def, runtime="python_compiled")
        res = oinf.run({'X': idi.astype(numpy.float32)})
        self.assertEqual(idi * 2, res['Y'])

        oinf.reduce_size(False)
        res = oinf.run({'X': idi.astype(numpy.float32)})
        self.assertEqual(idi * 2, res['Y'])
        st = BytesIO()
        try:
            pickle.dump(oinf, st)
        except AttributeError:
            # missing obj
            pass

        oinf = OnnxInference(model_def, runtime="python_compiled")
        res = oinf.run({'X': idi.astype(numpy.float32)})
        self.assertEqual(idi * 2, res['Y'])

        oinf.reduce_size(True)
        res = oinf.run({'X': idi.astype(numpy.float32)})
        self.assertEqual(idi * 2, res['Y'])
        st = BytesIO()
        pickle.dump(oinf, st)
        val = st.getvalue()
        oinf2 = pickle.load(BytesIO(val))
        self.assertNotEmpty(oinf2)
    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'])
Example #20
0
    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',
                      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': x}, outputs=[('cdist', FloatTensorType())],
            target_opset=get_opset_number_from_onnx())

        oinf = OnnxInference(model_def, skip_run=True)
        dot = oinf.to_dot(recursive=True)
        self.assertIn("B_next_out", dot)
        self.assertIn("cluster", dot)
Example #21
0
    def test_onnxt_pickle_check(self):
        idi = numpy.identity(2)
        onx = OnnxAdd('X', idi, output_names=['Y'],
                      op_version=get_opset_number_from_onnx())
        model_def = onx.to_onnx({'X': idi.astype(numpy.float32)},
                                target_opset=get_opset_number_from_onnx())
        oinf = OnnxInference(model_def)
        shape = oinf.shape_inference()
        self.assertNotEmpty(shape)
        if not sys.platform.startswith('win'):
            # Crashes (onnx crashes).
            try:
                oinf.check_model()
            except ValidationError as e:
                warnings.warn("Why? " + str(e))  # pylint: disable=E1101

        pkl = pickle.dumps(oinf)
        obj = pickle.loads(pkl)
        self.assertEqual(str(oinf), str(obj))
    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)
Example #23
0
 def test_onnxt_dot(self):
     idi = numpy.identity(2)
     idi2 = numpy.identity(2) * 2
     onx = OnnxAdd(
         OnnxAdd('X', idi, op_version=get_opset_number_from_onnx()),
         idi2, output_names=['Y'],
         op_version=get_opset_number_from_onnx())
     model_def = onx.to_onnx({'X': idi.astype(numpy.float32)},
                             target_opset=get_opset_number_from_onnx())
     oinf = OnnxInference(model_def)
     dot = oinf.to_dot()
     self.assertIn('Add [', dot)
     self.assertIn('Add1 [', dot)
     self.assertIn('Add\\n(Ad_Add)', dot)
     self.assertIn('Add\\n(Ad_Add1)', dot)
     self.assertIn('X -> Ad_Add;', dot)
     self.assertIn('Ad_Addcst1 -> Ad_Add1;', dot)
     self.assertIn('Ad_Addcst -> Ad_Add;', dot)
     self.assertIn('Ad_Add1 -> Y;', dot)
Example #24
0
 def test_onnxt_graph(self):
     idi = numpy.identity(2)
     idi2 = numpy.identity(2) * 2
     onx = OnnxAdd(
         OnnxAdd('X', idi, op_version=get_opset_number_from_onnx()),
         idi2, output_names=['Y'],
         op_version=get_opset_number_from_onnx())
     model_def = onx.to_onnx({'X': idi.astype(numpy.float32)},
                             target_opset=get_opset_number_from_onnx())
     oinf = OnnxInference(model_def)
     js = oinf.to_sequence()
     self.assertIn('inits', js)
     self.assertIn('inputs', js)
     self.assertIn('outputs', js)
     self.assertIn('intermediate', js)
     self.assertIn('nodes', js)
     self.assertIn('sequence', js)
     self.assertEqual(len(js['sequence']), 2)
     self.assertEqual(len(js['intermediate']), 2)
    def test_onnxrt_string_normalizer(self):
        corpus = numpy.array([
            'This is the first document.',
            'This document is the second document.',
            'And this is the third one.', 'Is this the first document?'
        ])

        op = OnnxStringNormalizer('text',
                                  op_version=get_opset_number_from_onnx(),
                                  output_names=['out'])
        onx = op.to_onnx(inputs=[('text', StringTensorType())])
        oinf = OnnxInference(onx)
        res = oinf.run({'text': corpus})
        self.assertEqual(list(res['out']), list(corpus))

        res = oinf.run({'text': corpus.reshape((2, 2))})
        self.assertEqual(res['out'].tolist(), corpus.reshape((2, 2)).tolist())

        op = OnnxStringNormalizer('text',
                                  op_version=get_opset_number_from_onnx(),
                                  output_names=['out'],
                                  case_change_action='LOWER')
        onx = op.to_onnx(inputs=[('text', StringTensorType())])
        oinf = OnnxInference(onx)
        res = oinf.run({'text': corpus})
        self.assertEqual(list(res['out']), list(_.lower() for _ in corpus))

        op = OnnxStringNormalizer('text',
                                  op_version=get_opset_number_from_onnx(),
                                  output_names=['out'],
                                  case_change_action='UPPER')
        onx = op.to_onnx(inputs=[('text', StringTensorType())])
        oinf = OnnxInference(onx)
        res = oinf.run({'text': corpus})
        self.assertEqual(list(res['out']), list(_.upper() for _ in corpus))

        op = OnnxStringNormalizer('text',
                                  op_version=get_opset_number_from_onnx(),
                                  output_names=['out'],
                                  case_change_action='UPPER2')
        onx = op.to_onnx(inputs=[('text', StringTensorType())])
        oinf = OnnxInference(onx)
        self.assertRaise(lambda: oinf.run({'text': corpus}), RuntimeError)
Example #26
0
 def test_onnxt_lreg(self):
     pars = dict(coefficients=numpy.array([1., 2.]), intercepts=numpy.array([1.]),
                 post_transform='NONE')
     onx = OnnxLinearRegressor('X', output_names=['Y'], **pars)
     model_def = onx.to_onnx({'X': pars['coefficients'].astype(numpy.float32)},
                             outputs=[('Y', FloatTensorType([1]))],
                             target_opset=get_opset_number_from_onnx())
     oinf = OnnxInference(model_def)
     dot = oinf.to_dot()
     self.assertIn('coefficients=[1. 2.]', dot)
     self.assertIn('LinearRegressor', dot)
Example #27
0
    def test_onnx_stat_recursive(self):
        from skl2onnx.algebra.complex_functions import onnx_squareform_pdist
        cop = OnnxAdd(OnnxIdentity('input',
                                   op_version=get_opset_number_from_onnx()),
                      '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)
        self.assertIn('subgraphs', stats)
        self.assertGreater(stats['subgraphs'], 1)
        self.assertGreater(stats['op_Identity'], 2)
Example #28
0
    def test_onnxt_knn_iris_dot(self):
        iris = load_iris()
        X, y = iris.data, iris.target
        X_train, __, y_train, _ = train_test_split(X, y, random_state=11)
        clr = KNeighborsClassifier()
        clr.fit(X_train, y_train)

        model_def = to_onnx(clr, X_train.astype(numpy.float32),
                            target_opset=get_opset_number_from_onnx())
        oinf = OnnxInference(model_def, skip_run=True)
        dot = oinf.to_dot()
        self.assertNotIn("class_labels_0 -> ;", dot)
    def test_onnx_remove_redundant_subgraphs_full(self):
        from skl2onnx.algebra.complex_functions import onnx_squareform_pdist
        cop = OnnxAdd(OnnxIdentity('input',
                                   op_version=get_opset_number_from_onnx()),
                      'input',
                      op_version=get_opset_number_from_onnx())
        cdist = onnx_squareform_pdist(cop,
                                      dtype=numpy.float32,
                                      op_version=get_opset_number_from_onnx())
        cdist2 = onnx_squareform_pdist(cop,
                                       dtype=numpy.float32,
                                       op_version=get_opset_number_from_onnx())
        cop2 = OnnxAdd(cdist,
                       cdist2,
                       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)
        new_model = onnx_optimisations(model_def)
        stats2 = onnx_statistics(new_model, optim=False)
        self.assertLess(stats2['size'], stats['size'])
        self.assertLess(stats2['nnodes'], stats['nnodes'])
        self.assertLess(stats2['op_Identity'], stats['op_Identity'])
 def test_onnxrt_python_count_vectorizer(self):
     corpus = numpy.array([
         'This is the first document.',
         'This document is the second document.',
         'And this is the third one.', 'Is this the first document?'
     ])
     vect = CountVectorizer()
     vect.fit(corpus)
     exp = vect.transform(corpus)
     onx = to_onnx(vect, corpus, target_opset=get_opset_number_from_onnx())
     oinf = OnnxInference(onx)
     got = oinf.run({'X': corpus})
     self.assertEqualArray(exp.todense(), got['variable'])