Ejemplo n.º 1
0
    def test_pipeline_iris(self):
        iris = load_iris()
        X, y = iris.data, iris.target
        pipe = OnnxPipeline([('pca', PCA(n_components=2)),
                             ('no', StandardScaler()),
                             ('lr', LogisticRegression())],
                            enforce_float32=True,
                            op_version=TARGET_OPSET)
        pipe.fit(X, y)
        pipe.fit(X, y)
        self.assertTrue(hasattr(pipe, 'raw_steps_'))
        self.assertEqual(len(pipe.steps), 3)
        self.assertEqual(len(pipe.raw_steps_), 3)
        self.assertIsInstance(pipe.steps[0][1], OnnxTransformer)
        self.assertIsInstance(pipe.steps[1][1], OnnxTransformer)

        X = X.astype(numpy.float32)
        model_def = to_onnx(pipe,
                            X[:1],
                            target_opset=pipe.op_version,
                            options={id(pipe): {
                                         'zipmap': False
                                     }})
        sess = OnnxInference(model_def)
        res = sess.run({'X': X})
        self.assertEqualArray(res["label"], pipe.predict(X))
        self.assertEqualArray(res["probabilities"], pipe.predict_proba(X))
Ejemplo n.º 2
0
    def test_pipeline_pickable(self):
        _register_converters_mlinsights(True)
        iris = load_iris()
        X, y = iris.data, iris.target
        pipe = OnnxPipeline(
            [('gm', TransferTransformer(StandardScaler(), trainable=True)),
             ('lr', LogisticRegression())],
            enforce_float32=True,
            op_version=TARGET_OPSET)
        pipe.fit(X, y)
        pipe.fit(X, y)

        self.assertTrue(hasattr(pipe, 'raw_steps_'))
        self.assertEqual(len(pipe.steps), 2)
        self.assertEqual(len(pipe.raw_steps_), 2)
        self.assertIsInstance(pipe.steps[0][1], OnnxTransformer)

        X = X.astype(numpy.float32)
        model_def = to_onnx(pipe,
                            X[:1],
                            target_opset=pipe.op_version,
                            options={id(pipe): {
                                         'zipmap': False
                                     }})
        sess = OnnxInference(model_def)
        res = sess.run({'X': X})
        self.assertEqual(list(sorted(res)), ['label', 'probabilities'])
        self.assertEqualArray(res["label"], pipe.predict(X))
        self.assertEqualArray(res["probabilities"], pipe.predict_proba(X))
Ejemplo n.º 3
0
    def test_pipeline_pickable_options(self):
        _register_converters_mlinsights(True)
        iris = load_iris()
        X, y = iris.data, iris.target
        pipe = OnnxPipeline([('gm',
                              TransferTransformer(GaussianMixture(
                                  n_components=5, random_state=2),
                                                  trainable=True,
                                                  method='predict_proba')),
                             ('lr', LogisticRegression(random_state=2))],
                            enforce_float32=True,
                            op_version=TARGET_OPSET,
                            options={
                                'gm__score_samples': True,
                                'lr__zipmap': False
                            })
        pipe.fit(X, y)
        pipe.fit(X, y)

        self.assertTrue(hasattr(pipe, 'raw_steps_'))
        self.assertEqual(len(pipe.steps), 2)
        self.assertEqual(len(pipe.raw_steps_), 2)
        self.assertIsInstance(pipe.steps[0][1], OnnxTransformer)

        X = X.astype(numpy.float32)
        model_def = to_onnx(pipe,
                            X[:1],
                            target_opset=pipe.op_version,
                            options={id(pipe): {
                                         'zipmap': False
                                     }})
        sess = OnnxInference(model_def, runtime="python_compiled")
        self.assertIn("'probabilities': probabilities,", str(sess))
        sess = InferenceSession(model_def.SerializeToString())
        r = sess.run(None, {'X': X})
        self.assertEqual(len(r), 2)
        sess = OnnxInference(model_def)
        res = sess.run({'X': X})
        self.assertEqual(list(sorted(res)), ['label', 'probabilities'])
        self.assertEqualArray(res["probabilities"], pipe.predict_proba(X))
        self.assertEqualArray(res["label"], pipe.predict(X))
Ejemplo n.º 4
0
    def test_pipeline_iris_column_transformer_nocache(self):
        class MyMemory:
            def __init__(self):
                pass

            def cache(self, obj):
                return obj

        iris = load_iris()
        X, y = iris.data, iris.target
        pipe = OnnxPipeline(
            [('col',
              ColumnTransformer([('pca', PCA(n_components=2), [0, 1]),
                                 ('no', StandardScaler(), [2]),
                                 ('pass', 'passthrough', [3])])),
             ('lr', LogisticRegression())],
            enforce_float32=True,
            op_version=TARGET_OPSET,
            memory=MyMemory())
        pipe.fit(X, y)
        pipe.fit(X, y)
        self.assertTrue(hasattr(pipe, 'raw_steps_'))
        self.assertEqual(len(pipe.steps), 2)
        self.assertEqual(len(pipe.raw_steps_), 2)
        self.assertIsInstance(pipe.steps[0][1], OnnxTransformer)
        self.assertIsInstance(pipe.steps[1][1], LogisticRegression)

        X = X.astype(numpy.float32)
        model_def = to_onnx(pipe,
                            X[:1],
                            target_opset=pipe.op_version,
                            options={id(pipe): {
                                         'zipmap': False
                                     }})
        sess = OnnxInference(model_def)
        res = sess.run({'X': X})
        self.assertEqualArray(res["label"], pipe.predict(X))
        self.assertEqualArray(res["probabilities"],
                              pipe.predict_proba(X),
                              decimal=5)
# on ONNX outputs. That way, every step of the pipeline
# is trained based on ONNX output.
#
# * Trains the first step.
# * Converts the step into ONNX
# * Computes ONNX outputs.
# * Trains the second step on these outputs.
# * Converts the second step into ONNX.
# * Merges it with the first step.
# * Computes ONNX outputs of the merged two first steps.
# * ...
#
# It is implemented in
# class :epkg:`OnnxPipeline`.

model_onx = OnnxPipeline([('scaler', StandardScaler()),
                          ('dt', DecisionTreeRegressor(max_depth=max_depth))])

model_onx.fit(Xi_train, yi_train)

#############################################
# The conversion.

onx4 = to_onnx(model_onx, Xi_train[:1].astype(numpy.float32))

sess4 = InferenceSession(onx4.SerializeToString(),
                         providers=['CPUExecutionProvider'])

skl4 = model_onx.predict(X32)
ort4 = sess4.run(None, {'X': X32})[0]

print(diff(skl4, ort4))
Ejemplo n.º 6
0
 def test_pipeline_none_params(self):
     model_onx = OnnxPipeline([('scaler', StandardScaler()),
                               ('dt', DecisionTreeRegressor(max_depth=2))])
     self.assertNotEmpty(model_onx)