Esempio n. 1
0
 def test_change_onnx_domain(self):
     model = make_pipeline(StandardScaler())
     X = numpy.array([[0.1, 1.1], [0.2, 2.2], [0.4, 2.2], [0.2, 2.4]])
     model.fit(X)
     model_onnx = convert_sklearn(model, "pipe3",
                                  [("input", FloatTensorType([None, 2]))])
     model_onnx = change_onnx_domain(model_onnx,
                                     {'Scaler': ('ScalerNew', 'ML2')})
     self.assertIn('domain: "ML2"', str(model_onnx))
     self.assertIn('op_type: "ScalerNew"', str(model_onnx))
    def common_test_gpc(self, dtype=np.float32, n_classes=2):

        gp = GaussianProcessClassifier()
        gp, X = self.fit_classification_model(gp, n_classes=n_classes)

        # return_cov=False, return_std=False
        if dtype == np.float32:
            cls = FloatTensorType
        else:
            cls = DoubleTensorType
        model_onnx = to_onnx(gp,
                             initial_types=[('X', cls([None, None]))],
                             target_opset=TARGET_OPSET,
                             options={
                                 GaussianProcessClassifier: {
                                     'zipmap': False,
                                     'optim': 'cdist'
                                 }
                             })
        self.assertTrue(model_onnx is not None)

        try:
            sess = InferenceSession(model_onnx.SerializeToString())
        except OrtFail:
            if not hasattr(self, 'path'):
                return
            suffix = 'Double' if dtype == np.float64 else 'Float'
            # Operator Solve is missing
            model_onnx = change_onnx_domain(
                model_onnx, {'Solve': ('Solve%s' % suffix, 'ai.onnx.contrib')})
            so = SessionOptions()
            so.register_custom_ops_library(self.path)
            sess = InferenceSession(model_onnx.SerializeToString(), so)

            res = sess.run(None, {'X': X.astype(dtype)})
            assert_almost_equal(res[0].ravel(), gp.predict(X).ravel())
            assert_almost_equal(res[1], gp.predict_proba(X), decimal=3)
            return

        dt = 32 if dtype == np.float32 else 64
        dump_data_and_model(X.astype(dtype),
                            gp,
                            model_onnx,
                            verbose=False,
                            basename="SklearnGaussianProcessRBFT%d%d" %
                            (n_classes, dt))