Esempio n. 1
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()))
    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. 3
0
    def test_multiple_transform(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()

        res = list(OnnxTransformer.enumerate_create(content))
        assert len(res) > 0
        for k, tr in res:
            tr.fit()
            try:
                tr.transform(x)
            except RuntimeError:
                pass
Esempio n. 4
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
    def test_pipeline(self):
        iris = load_iris()
        X, y = iris.data, iris.target
        pca = PCA(n_components=2)
        pca.fit(X)

        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())
        pipe.fit(X, y)
        pred = pipe.predict(X)
        self.assertEqual(pred.shape, (150, ))
        skl_pred = pca.transform(X)
        skl_onx = pipe.steps[0][1].transform(X)
        assert_almost_equal(skl_pred, skl_onx, decimal=5)
Esempio n. 6
0
 def test_pipeline_iris(self):
     iris = load_iris()
     X, y = iris.data, iris.target
     pipe = make_pipeline(PCA(n_components=2), LogisticRegression())
     pipe.fit(X, y)
     onx = convert_sklearn(pipe,
                           initial_types=[('input',
                                           FloatTensorType(
                                               (1, X.shape[1])))])
     onx_bytes = onx.SerializeToString()
     res = list(OnnxTransformer.enumerate_create(onx_bytes))
     outputs = []
     shapes = []
     for k, tr in res:
         outputs.append(k)
         tr.fit()
         y = tr.transform(X)
         self.assertEqual(y.shape[0], X.shape[0])
         shapes.append(y.shape)
     self.assertEqual(len(set(outputs)), len(outputs))
     shapes = set(shapes)
     self.assertEqual(shapes, {(150, 3), (150, 4), (150, 2), (150, )})
    def test_grid_search_onnx(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_bytes2 = onx.SerializeToString()

        pca = PCA(n_components=3)
        pca.fit(X_train)
        onx = convert_sklearn(pca,
                              initial_types=[('input',
                                              FloatTensorType(
                                                  (1, X.shape[1])))])
        onx_bytes3 = onx.SerializeToString()

        pipe = make_pipeline(OnnxTransformer(onx_bytes2), LogisticRegression())

        param_grid = [{
            'onnxtransformer__onnx_bytes': [onx_bytes2, onx_bytes3]
        }]

        clf = GridSearchCV(pipe, param_grid, cv=3)
        clf.fit(X_train, y_train)
        bp = clf.best_params_
        assert "onnxtransformer__onnx_bytes" in bp

        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
	(grabbed, frame) = vs.read()

	if not grabbed:
		break

	#frame = cv2.resize(frame, (256,256), interpolation = cv2.INTER_LINEAR)
	frame = imutils.resize(frame, width= 256)

	#constructing a blob from our image
	blob = cv2.dnn.blobFromImage(cv2.resize(
	    frame, (256, 256)), 1/255.0, (256, 256), 0, swapRB=True, crop=False)

	#Open the model and run forward pass with the given blob
	with open(model_file, "rb") as f:
	    model_bytes = f.read()
	ot = OnnxTransformer(model_bytes)
	start = time.time()
	pred = ot.fit_transform(blob)
	end = time.time()

	# Infer the total number of classes, height and width
	(numClasses, height, width) = pred.shape[1:4]

	# Argmax is utilized to find the class label with largest probability for every pixel in the image
	classMap = np.argmax(pred[0], axis=0)

	# classes are mapped to their respective colours
	mask = COLORS[classMap]

	# resizing the mask and class map to match its dimensions with the input image
	mask = cv2.resize(
Esempio n. 9
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
Esempio n. 10
0
]

onx_bytes = []

for model in dec_models:
    model.fit(X_train)
    onx = convert_sklearn(model,
                          initial_types=[('X', FloatTensorType(
                              (1, X.shape[1])))])
    onx_bytes.append(onx.SerializeToString())

##############################
# Pipeline with OnnxTransformer
# +++++++++++++++++++++++++++++++

pipe = make_pipeline(OnnxTransformer(onx_bytes[0]),
                     LogisticRegression(multi_class='ovr'))

################################
# Grid Search
# +++++++++++
#
# The serialized models are now used as a parameter
# in the grid search.

param_grid = [{
    'onnxtransformer__onnx_bytes': onx_bytes,
    'logisticregression__penalty': ['l2', 'l1'],
    'logisticregression__solver': ['liblinear', 'saga']
}]
fig, ax = plt.subplots(figsize=(40, 20))
ax.imshow(image)
ax.axis('off')

###########################
# Visualize intermediate outputs
# ++++++++++++++++++++++++++++++

from skonnxrt.sklapi import OnnxTransformer  # noqa

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

input = corpus[2]
print("with input:", [input])
for step in OnnxTransformer.enumerate_create(content):
    print("-> node '{}'".format(step[0]))
    step[1].fit()
    print(step[1].transform(input))

#################################
# **Versions used for this example**

import numpy, sklearn  # noqa
print("numpy:", numpy.__version__)
print("scikit-learn:", sklearn.__version__)
import onnx, onnxruntime, skl2onnx, skonnxrt  # noqa
print("onnx: ", onnx.__version__)
print("onnxruntime: ", onnxruntime.__version__)
print("scikit-onnxruntime: ", skonnxrt.__version__)
print("skl2onnx: ", skl2onnx.__version__)