示例#1
0
def test_unsupported_activation():
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        model = MLPClassifier(hidden_layer_sizes=(2, ), max_iter=10)
        model.fit([[1.0]], [True])
    with pytest.raises(Exception) as ex:
        model.activation = 'fake22'
        emlearn.convert(model)
    assert 'Unsupported activation' in str(ex.value)
    assert 'fake22' in str(ex.value)
示例#2
0
def test_sklearn_predict(modelparams, params):

    model = MLPClassifier(**modelparams, max_iter=10)

    for random in range(0, 3):
        # create dataset
        rng = numpy.random.RandomState(0)
        X, y = make_classification(n_features=params['features'],
                                   n_classes=params['classes'],
                                   n_redundant=0,
                                   n_informative=params['features'],
                                   random_state=rng,
                                   n_clusters_per_class=1,
                                   n_samples=50)
        X += 2 * rng.uniform(size=X.shape)
        X = StandardScaler().fit_transform(X)

        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2)
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            model.fit(X_train, y_train)

            cmodel = emlearn.convert(model)

            X_test = X_test[:3]
            cproba = cmodel.predict_proba(X_test)
            proba = model.predict_proba(X_test)
            cpred = cmodel.predict(X_test)
            pred = model.predict(X_test)

        assert_almost_equal(proba, cproba, decimal=6)
        assert_equal(pred, cpred)
示例#3
0
文件: test_trees.py 项目: zoq/emlearn
def test_trees_to_dot():
    X, Y = datasets.make_classification(n_classes=2,
                                        n_samples=10,
                                        random_state=1)
    model = RandomForestClassifier(n_estimators=3, max_depth=5, random_state=1)
    model.fit(X, Y)

    trees = emlearn.convert(model)
    dot = trees.to_dot(name='ffoo')
    with open('tmp/trees.dot', 'w') as f:
        f.write(dot)
示例#4
0
文件: test_trees.py 项目: zoq/emlearn
def test_trees_sklearn_predict(data, model, method):
    X, y = DATASETS[data]
    estimator = MODELS[model]

    estimator.fit(X, y)
    cmodel = emlearn.convert(estimator, method=method)

    pred_original = estimator.predict(X[:5])
    pred_c = cmodel.predict(X[:5])

    numpy.testing.assert_equal(pred_c, pred_original)
示例#5
0
def assert_equivalent(model, X_test, n_classes, method):
    cmodel = emlearn.convert(model, method=method)

    # TODO: support predict_proba, use that instead
    cpred = cmodel.predict(X_test)
    pred = model.predict(X_test)
    if n_classes == 2:
        pred = (pred[:, 0] > 0.5).astype(int)
    else:
        pred = numpy.argmax(pred, axis=1)

    assert_equal(pred, cpred)
示例#6
0
def test_bayes_equals_sklearn(data, model, method):
    X, y = DATASETS[data]
    estimator = MODELS[model]

    X = preprocessing.StandardScaler().fit_transform(X)
    estimator.fit(X, y)

    cmodel = emlearn.convert(estimator, method=method)
    
    pred_original = estimator.predict(X[:5])
    pred_c = cmodel.predict(X[:5])
    numpy.testing.assert_equal(pred_c, pred_original)
示例#7
0
def test_trees_sklearn_predict(data, model, method):
    X, y = DATASETS[data]
    estimator = MODELS[model]

    X = (X * 2**16).astype(int)  # currently only integers supported

    estimator.fit(X, y)
    cmodel = emlearn.convert(estimator, method=method)

    pred_original = estimator.predict(X[:5])
    pred_c = cmodel.predict(X[:5])

    numpy.testing.assert_equal(pred_c, pred_original)
示例#8
0
def test_gaussian_mixture_equals_sklearn(data, model, method):
    X, y = DATASETS[data]
    estimator = MODELS[model]

    X = preprocessing.StandardScaler().fit_transform(X)
    X = decomposition.PCA(3, random_state=random).fit_transform(X)
    estimator.fit(X, y)

    cmodel = emlearn.convert(estimator, method=method)

    dist = estimator.score_samples(X)
    cdist = cmodel.score_samples(X)
    numpy.testing.assert_allclose(cdist, dist, rtol=1e-5)

    pred_original = estimator.predict(X)
    pred_c = cmodel.predict(X)
    estimator.fit(X, y)

    numpy.testing.assert_equal(pred_c, pred_original)
示例#9
0
# 0.95+ with n_estimators=40, max_depth=20
# 0.90+ with n_estimators=10, max_depth=10
trees = 40
max_depth = 20
print('Training {} trees with max_depth {}'.format(trees, max_depth))
model = RandomForestClassifier(n_estimators=trees, max_depth=max_depth, random_state=rnd)
model.fit(Xtrain, ytrain)

# Predict
ypred = model.predict(Xtest)
print('Accuracy on validation set {:.2f}%'.format(metrics.accuracy_score(ypred, ytest)*100))

m = numpy.max(Xtrain), numpy.min(Xtrain)

filename = 'digits.h'
cmodel = emlearn.convert(model)
code = cmodel.save(file=filename)

print('Wrote C code to', filename)

assert len(port_list.comports()) > 0, "No serial ports available"

port = port_list.comports()[0].device # grab the first serial port
print('Classify on microcontroller via', port)
import serial
device = serial.Serial(port=port, baudrate=115200, timeout=0.1) 

repetitions = 10
Y_pred = []
times = []
for idx,row in enumerate(Xtest):
示例#10
0
def build_run_classifier(model, name):
    from sklearn.model_selection import train_test_split

    target_column = 'target'

    # Train model
    test, train = train_test_split(dataset, test_size=0.3, random_state=3)
    feature_columns = list(set(dataset.columns) - set([target_column]))

    model.fit(train[feature_columns], train[target_column])

    out_dir = os.path.join(here, 'classifiers')
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)

    model_filename = os.path.join(out_dir, f'{name}_model.h')
    cmodel = emlearn.convert(model)
    code = cmodel.save(file=model_filename, name='model')

    test_pred = cmodel.predict(test[feature_columns])

    # Generate a test dataet
    test_data = numpy.array(test[feature_columns]).flatten()
    test_res = numpy.array(test_pred).flatten()

    test_dataset = "\n".join([
        emlearn.cgen.array_declare(f"{name}_testset_data",
                                   dtype='float',
                                   values=test_data),
        emlearn.cgen.array_declare(f"{name}_testset_results",
                                   dtype='int',
                                   values=test_res),
        emlearn.cgen.constant_declare(f'{name}_testset_features',
                                      val=len(feature_columns)),
        emlearn.cgen.constant_declare(f'{name}_testset_samples',
                                      val=len(test)),
    ])

    test_code = test_dataset + \
    f'''
    #include "{model_filename}" // emlearn generated model

    #include <stdio.h> // printf

    int
    {name}_test() {{
        const int n_features = {name}_testset_features;
        const int n_testcases = {name}_testset_samples;

        int errors = 0;

        for (int i=0; i<n_testcases; i++) {{
            const float *features = {name}_testset_data + (i*n_features);
            const int expect_result = {name}_testset_results[i*1];

            const int32_t out = model_predict(features, n_features);

            if (out != expect_result) {{
                printf(\"test-fail sample=%d expect=%d got=%d \\n\", i, expect_result, out);
                errors += 1;
            }}

        }}
        return errors;
    }}

    int
    main(int argc, const char *argv[])
    {{
        const int errors = {name}_test();
        return errors;
    }}'''

    test_source_file = os.path.join(out_dir, f'test_{name}.c')
    with open(test_source_file, 'w') as f:
        f.write(test_code)

    print('Generated', test_source_file)

    include_dirs = [emlearn.includedir]
    test_executable = emlearn.common.compile_executable(
        test_source_file,
        out_dir,
        name='test_{name}',
        include_dirs=include_dirs)

    import subprocess
    errors = None
    try:
        subprocess.check_output([test_executable])
        errors = 0
    except subprocess.CalledProcessError as e:
        errors = e.returncode

    print(f"Tested {name}: {errors} errors")
示例#11
0
#!/usr/bin/env python3

import emlearn
import joblib

estimator = joblib.load("model")
cmodel = emlearn.convert(estimator)
cmodel.save(file='model.h')
示例#12
0
)

plt.subplots_adjust(left=0.02,
                    right=0.98,
                    bottom=0.001,
                    top=0.96,
                    wspace=0.05,
                    hspace=0.01)
for i_dataset, X in enumerate(datasets):
    for i_algorithm, (name, model) in enumerate(anomaly_algorithms):

        # Train model
        print(f"Trying {name}")
        try:
            model.fit(X)
        except FloatingPointError as e:
            print(e)
            continue

        # Convert to C
        cmodel = emlearn.convert(model, method='inline')

        # Visualize output
        ax = axs[i_dataset, i_algorithm]
        plot_results(ax, cmodel, X)

        if i_dataset == 0:
            ax.set_title(name, size=18)

plt.show()
示例#13
0
    def start_classi(self):
        path_test = self.ui.ad_test.text()
        path_train = self.ui.ad_train.text()
        path_val = self.ui.ad_val.text()

        #splite of the matices
        df0 = np.loadtxt(path_test, delimiter=",")
        df1 = np.loadtxt(path_train, delimiter=",")
        df2 = np.loadtxt(path_val, delimiter=",")
        # variables
        size0 = df0.shape
        input0 = size0[1] - 1
        size1 = df1.shape
        input1 = size1[1] - 1
        size2 = df2.shape
        input2 = size2[1] - 1

        # split input and output
        X0 = df1[:, 0:input0]
        Y0 = df1[:, input0]
        X1 = df1[:, 0:input1]
        Y1 = df1[:, input1]
        X2 = df2[:, 0:input2]
        Y2 = df2[:, input2]

        if self.ui.check_train.isChecked():
            file = QFileDialog.getOpenFileName(
                self,
                "Modell einlesen",
                "",
                "All Files (*);;Joblib File (*.joblib)",
            )
            model = load(file[0])
        else:

            if path_train == "" or path_val == "" or path_test == "":
                self.ui.msg_text.setText("path not complett")
            elif self.ui.check_svm.isChecked():

                #svm
                model = svm.SVC(kernel='linear',
                                decision_function_shape='ovr',
                                C=0.01,
                                gamma=0.001)
                model.fit(X1, Y1)

                if self.ui.che_mue.isChecked():
                    porter = port(model)
                    file_svm = QFileDialog.getSaveFileName(
                        self, "Save Objects", "",
                        "All Files (*);;Lib File (*.h)")

                    f = open(file_svm[0], 'w+')
                    f.write(porter)
                    f.close()

                # TODO: FILE save System SVM (done)

            elif self.ui.check_neighbor.isChecked():

                #Decision Tree
                model = DecisionTreeClassifier(random_state=0)
                model.fit(X1, Y1)

                if self.ui.che_mue.isChecked():
                    cmodel = emlearn.convert(model)
                    file_svm = QFileDialog.getSaveFileName(
                        self, "Save Objects", "",
                        "All Files (*);;Lib File (*.h)")
                    cmodel.save(file=file_svm[0])

            elif self.ui.check_neuro.isChecked():
                # creating model
                model = MLPClassifier(solver='lbfgs',
                                      alpha=1e-5,
                                      hidden_layer_sizes=(10, 2),
                                      random_state=1)
                model.fit(X1, Y1)

                if self.ui.che_mue.isChecked():
                    cmodel = emlearn.convert(model)
                    file_svm = QFileDialog.getSaveFileName(
                        self, "Save Objects", "",
                        "All Files (*);;Lib File (*.h)")
                    cmodel.save(file=file_svm[0])

            elif self.ui.check_bayes.isChecked():

                # bayes classifier
                model = GaussianNB()
                model.fit(X1, Y1)

                if self.ui.che_mue.isChecked():
                    cmodel = emlearn.convert(model)
                    file_svm = QFileDialog.getSaveFileName(
                        self, "Save Objects", "",
                        "All Files (*);;Lib File (*.h)")
                    cmodel.save(file=file_svm[0])

            else:
                self.ui.msg_text.setText("no choice of classifier")


#

        if self.ui.check_train_3.isChecked():
            #file creation
            path_classi, _ = QFileDialog.getSaveFileName(
                self, "QFileDialog.getSaveFileName()", "",
                "All Files (*);;joblib Files (*.joblib)")
            dump(model, path_classi)

        predicted = model.predict(X0)

        score = model.score(X2, Y2)