Esempio n. 1
0
    def test_predict_from_model_array(self):

        params = nn_params.copy()
        params.update({'nclasses': n_classes})
        optimizer = GenericSolver(**solver_param)
        datasets.dump_svmlight_file(xtr, ytr, tr_f)
        datasets.dump_svmlight_file(xte, yte, te_f)

        # Train model
        clf = MLP(**params)
        clf.fit(xtr_arr, validation_data=[xte_arr], solver=optimizer)
        y_pred_tr = clf.predict(xtr_arr)
        y_pred_te = clf.predict(xte_arr)

        # Load from tained model
        params = nn_params.copy()
        params.update({
            'finetuning': True,
            'template': None,
            'nclasses': n_classes
        })
        clf = MLP(sname=clf.sname,
                  repository=clf.model['repository'],
                  **params)

        assert np.array_equal(y_pred_tr, clf.predict(xtr_arr))
        assert np.array_equal(y_pred_te, clf.predict(xte_arr))
        os_utils._remove_files([tr_f, te_f])
# create and save train.svm and test.svm
tr_f = os.path.abspath('x_train.svm')
te_f = os.path.abspath('x_test.svm')
datasets.dump_svmlight_file(xtr, ytr, tr_f)
datasets.dump_svmlight_file(xte, yte, te_f)

# Define models and class weights
clf = MLP(**params)

train_data, test_data = SVMConnector(path=tr_f), SVMConnector(path=te_f)
logs = clf.fit(train_data,
               validation_data=[test_data],
               solver=solver,
               class_weights=class_weights,
               batch_size=128)

params.update({"resume": True})
clf = MLP(**params)
logs = clf.fit(train_data,
               validation_data=[test_data],
               solver=solver,
               class_weights=class_weights,
               batch_size=128)

yte_pred = clf.predict(test_data)
report = metrics.classification_report(yte, yte_pred)
print(report)

# remove saved svm files
os_utils._remove_files([tr_f, te_f])
Esempio n. 3
0
                          base_lr=0.1,
                          snapshot=100)
clf = MLP(sname=sname, repository=model_repo, **params)
clf.fit(xtr_svm, validation_data=[xte_svm, xtr_svm], solver=optimizer)
del clf

# load pre trained model
params = {
    'host': host,
    'port': port,
    'nclasses': nclasses,
    'finetuning': True,
    'template': None
}
clf = MLP(sname=sname, repository=model_repo, **params)
ytr_pred, yte_pred = clf.predict(xtr_svm), clf.predict(xte_svm)
report = metrics.classification_report(yte, yte_pred)
print(report)

# Predict using another file
params = {
    'host': host,
    'port': port,
    'nclasses': nclasses,
    'weights': os.path.join(model_repo, 'model_iter_400.caffemodel'),
    'template': None
}
clf = MLP(sname=sname, repository=model_repo, **params)
ytr_pred, yte_pred = clf.predict(xtr_svm), clf.predict(xte_svm)
report = metrics.classification_report(yte, yte_pred)
print(report)