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)
Beispiel #2
0
    def test_model_bernoulli_nb_bc_onnxruntime1(self):
        model, X = self.fit_classification_model(BernoulliNB(), 2)
        model_onnx = convert_sklearn(
            model,
            "?", [("input", FloatTensorType([None, X.shape[1]]))],
            target_opset=TARGET_OPSET)
        exp1 = model.predict(X)
        exp = model.predict_proba(X)

        model_onnx.ir_version = get_ir_version(TARGET_OPSET)
        oinf = _capture_output(
            lambda: OnnxInference(model_onnx, runtime='onnxruntime1'), 'c')[0]
        got = oinf.run({'input': X})
        self.assertEqualArray(exp1, got['output_label'])
        got2 = DataFrame(got['output_probability']).values
        self.assertEqualArray(exp, got2, decimal=4)
Beispiel #3
0
    def test_model_bernoulli_nb_bc_onnxruntime2(self):
        model, X = self.fit_classification_model(BernoulliNB(), 2)
        model_onnx = convert_sklearn(
            model, "?", [("input", FloatTensorType([None, X.shape[1]]))],
            options={id(model): {'zipmap': False}},
            target_opset=get_opset_number_from_onnx())
        exp1 = model.predict(X)
        exp = model.predict_proba(X)

        model_onnx.ir_version = get_ir_version_from_onnx()
        oinf = _capture_output(
            lambda: OnnxInference(model_onnx, runtime='onnxruntime2'),
            'c')[0]
        got = oinf.run({'input': X})
        self.assertEqualArray(exp1, got['label'])
        got2 = DataFrame(got['probabilities']).values
        self.assertEqualArray(exp, got2, decimal=4)
    def onnx_test_knn_single_classreg(self,
                                      dtype,
                                      n_targets=1,
                                      debug=False,
                                      add_noise=False,
                                      runtime='python',
                                      target_opset=None,
                                      optim=None,
                                      kind='reg',
                                      level=1,
                                      largest0=True,
                                      metric_params=None,
                                      **kwargs):
        iris = load_iris()
        X, y = iris.data, iris.target
        if add_noise:
            X += numpy.random.randn(X.shape[0], X.shape[1]) * 10
        if kind == 'reg':
            y = y.astype(dtype)
        elif kind == 'bin':
            y = (y % 2).astype(numpy.int64)
        elif kind == 'mcl':
            y = y.astype(numpy.int64)
        else:
            raise AssertionError("unknown '{}'".format(kind))

        if n_targets != 1:
            yn = numpy.empty((y.shape[0], n_targets), dtype=dtype)
            for i in range(n_targets):
                yn[:, i] = y + i
            y = yn
        X_train, X_test, y_train, _ = train_test_split(X, y, random_state=11)
        X_test = X_test.astype(dtype)
        if kind in ('bin', 'mcl'):
            clr = KNeighborsClassifier(metric_params=metric_params, **kwargs)
        elif kind == 'reg':
            clr = KNeighborsRegressor(metric_params=metric_params, **kwargs)
        else:
            raise NotImplementedError(kind)
        clr.fit(X_train, y_train)

        if optim is None:
            options = None
        else:
            options = {clr.__class__: {'optim': 'cdist'}}
        if not largest0:
            if options is None:
                options = {}
            if clr.__class__ not in options:
                options[clr.__class__] = {}
            options[clr.__class__].update({'largest0': False})

        if target_opset is None:
            opsets = list(
                sorted(set([9, 10, 11, 12, 13, 14, 15,
                            TARGET_OPSET])))  # opset=13, 14, ...
        else:
            opsets = [target_opset]
        for ops in opsets:
            if ops is None:
                raise AssertionError("Cannot happen: {}.".format(opsets))
            with self.subTest(target_opset=ops):
                try:
                    model_def = to_onnx(clr,
                                        X_train.astype(dtype),
                                        rewrite_ops=True,
                                        target_opset=ops,
                                        options=options)
                except NameError as e:
                    if "Option 'largest0' not in" in str(e):
                        continue
                if 'onnxruntime' in runtime:
                    model_def.ir_version = get_ir_version(ops)
                try:
                    if runtime == 'onnxruntime2':
                        oinf = _capture_output(
                            lambda: OnnxInference(model_def, runtime=runtime),  # pylint: disable=W0640
                            'c')[0]
                    else:
                        oinf = OnnxInference(model_def, runtime=runtime)
                except (RuntimeError, TypeError, OrtInvalidArgument) as e:
                    if "No Op registered for Identity with domain_version of 12" in str(
                            e):
                        continue
                    if debug:
                        raise AssertionError(
                            "Unable to create a model for target_opset={}\n----\n{}\n----"
                            .format(ops,
                                    str(model_def)[:100])) from e
                    if "Unknown model file format version." in str(e):
                        continue
                    raise AssertionError(
                        "Unable to create model for opset={} and runtime='{}'\n{}"
                        "".format(ops, runtime,
                                  str(model_def)[:100])) from e

                if debug:
                    y = oinf.run({'X': X_test}, verbose=level, fLOG=print)
                else:
                    y = oinf.run({'X': X_test})

                lexp = clr.predict(X_test)
                if kind == 'reg':
                    self.assertEqual(list(sorted(y)), ['variable'])
                    if dtype == numpy.float32:
                        self.assertEqualArray(lexp,
                                              y['variable'],
                                              decimal=5,
                                              squeeze=True)
                    else:
                        self.assertEqualArray(lexp,
                                              y['variable'],
                                              squeeze=True)
                else:
                    self.assertEqual(list(sorted(y)),
                                     ['output_label', 'output_probability'])
                    self.assertEqualArray(lexp, y['output_label'])
                    lprob = clr.predict_proba(X_test)
                    self.assertEqualArray(lprob,
                                          DataFrame(
                                              y['output_probability']).values,
                                          decimal=5)
    def test_kernel_ker2_def_ort1(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 = OnnxInference(model_onnx.SerializeToString(),
                             runtime="onnxruntime1")

        rows = []

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

        res = _capture_output(
            lambda: sess.run({'X': Xtest_.astype(numpy.float32)},
                             intermediate=True, verbose=1, fLOG=myprint),
            'c')[0]
        self.assertGreater(len(rows), 2)
        m1 = res['Y']
        self.assertNotEmpty(m1)
        self.assertGreater(len(res), 2)
        # m2 = ker(Xtest_)
        # self.assertEqualArray(m1, m2, decimal=5)

        cpu = OnnxInference(model_onnx.SerializeToString())
        sbs = side_by_side_by_values(
            [cpu, sess], inputs={'X': Xtest_.astype(numpy.float32)})
        self.assertGreater(len(sbs), 2)
        self.assertIsInstance(sbs, list)
        self.assertIsInstance(sbs[0], dict)
        self.assertIn('step', sbs[0])
        self.assertIn('step', sbs[1])
        self.assertIn('metric', sbs[0])
        self.assertIn('metric', sbs[1])
        self.assertIn('cmp', sbs[0])
        self.assertIn('cmp', sbs[1])

        sess3 = _capture_output(
            lambda: OnnxInference(model_onnx.SerializeToString(),
                                  runtime="onnxruntime2"), 'c')[0]
        try:
            sbs = side_by_side_by_values(
                [cpu, sess, sess3], inputs={'X': Xtest_.astype(numpy.float32)})
        except RuntimeError as e:
            if "Got invalid dimensions for input" in str(e):
                # probable bug somewhere
                return
            raise e
        self.assertNotEmpty(sbs)

        inputs = {'X': Xtest_.astype(numpy.float32)}
        sbs = side_by_side_by_values(
            [(cpu, inputs), (sess, inputs), (sess3, inputs)])
        self.assertNotEmpty(sbs)