def test_grid_search(self):
        iris = load_iris()
        X, y = iris.data, iris.target
        X_train, X_test, y_train, y_test = train_test_split(X, y)

        pca = PCA(n_components=2)
        pca.fit(X_train)
        onx = convert_sklearn(pca,
                              initial_types=[('input',
                                              FloatTensorType(
                                                  (1, X.shape[1])))])
        onx_bytes = onx.SerializeToString()
        tr = OnnxTransformer(onx_bytes)

        pipe = make_pipeline(tr, LogisticRegression(solver='liblinear'))

        param_grid = [{'logisticregression__penalty': ['l2', 'l1']}]

        clf = GridSearchCV(pipe, param_grid, cv=3)
        clf.fit(X_train, y_train)
        bp = clf.best_params_
        self.assertEqual(bp, {'logisticregression__penalty': 'l1'})

        tr2 = OnnxTransformer(onx_bytes)
        tr2.fit()
        assert_almost_equal(tr2.transform(X_test),
                            clf.best_estimator_.steps[0][1].transform(X_test))
        y_true, y_pred = y_test, clf.predict(X_test)
        cl = classification_report(y_true, y_pred)
        assert 'precision' in cl
        sc = clf.score(X_test, y_test)
        assert sc >= 0.80
Esempio n. 2
0
    def test_transform_dict(self):
        x = {'X': np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])}
        name = self.get_name("mul_1.pb")
        with open(name, "rb") as f:
            content = f.read()

        tr = OnnxTransformer(content)
        tr.fit()
        res = tr.transform(x)
        exp = np.array([[1., 4.], [9., 16.], [25., 36.]], dtype=np.float32)
        self.assertEqual(list(res.ravel()), list(exp.ravel()))
Esempio n. 3
0
    def test_transform_dataframe(self):
        x = pandas.DataFrame(data=[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
        x.columns = "X1 X2".split()
        name = self.get_name("mul_1.pb")
        with open(name, "rb") as f:
            content = f.read()

        tr = OnnxTransformer(content)
        tr.fit()
        try:
            tr.transform(x)
        except RuntimeError:
            pass
Esempio n. 4
0
initial_type = [('float_input', FloatTensorType([1, 4]))]
onx = convert_sklearn(clr, initial_types=initial_type)

with open("rf_iris.onnx", "wb") as f:
    f.write(onx.SerializeToString())

###################################
# Compute ONNX prediction similarly as scikit-learn transformer
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

with open("rf_iris.onnx", "rb") as f:
    content = f.read()

ot = OnnxTransformer(content, output_name="output_probability")
ot.fit(X_train, y_train)

print(ot.transform(X_test[:5].astype(np.float32)))

###################################
# .. index:: transfer learning, MobileNet, ImageNet
#
# Transfer Learning with MobileNet
# ++++++++++++++++++++++++++++++++
#
# Deep learning models started to win
# the `ImageNet <http://www.image-net.org/>`_
# competition in 2012 and most the winners
# are available on the web as pre-trained models.
# Transfer Learning is computed by wrapping
# a backend into a *scikit-learn*