コード例 #1
0
    def test_predict(self):
        session = self.get_session()

        session.add(DatasetSample(image=pickle.dumps(np.array([0])), label=0))
        session.add(DatasetSample(image=pickle.dumps(np.array([1])), label=1))

        session.commit()

        t = ModelServingWrapper(LogisticRegression(), self.TESTBASE_PATH)
        t.fit()

        self.assertEqual(t.predict(np.array([1])), 1)
コード例 #2
0
    def test__get_training_dataset_shapes_mismatch(self):
        session = self.get_session()

        session.add(DatasetSample(image=pickle.dumps(np.array([0])), label=0))
        session.add(
            DatasetSample(image=pickle.dumps(np.array([0, 1])), label=0))
        session.commit()

        t = ModelServingWrapper(LinearRegression(), self.TESTBASE_PATH)

        with self.assertRaises(ValueError):
            images, labels, shape = t._get_training_dataset()
コード例 #3
0
    def test__fit(self):
        session = self.get_session()

        session.add(DatasetSample(image=pickle.dumps(np.array([0])), label=0))
        session.add(DatasetSample(image=pickle.dumps(np.array([1])), label=1))

        session.commit()

        t = ModelServingWrapper(LinearRegression(), self.TESTBASE_PATH)
        t._fit()

        self.assertAlmostEqual(t._model.coef_[0], 1)
        self.assertAlmostEqual(t._model.intercept_, 0)
コード例 #4
0
    def test__get_training_dataset_one_sample(self):
        session = self.get_session()

        test_vector = np.random.random(100)
        test_label = np.random.randint(0, 128)

        session.add(
            DatasetSample(image=pickle.dumps(test_vector), label=test_label))
        session.commit()

        t = ModelServingWrapper(LinearRegression(), self.TESTBASE_PATH)
        images, labels, shape = t._get_training_dataset()

        self.assertTrue(np.array_equal(images, np.vstack([test_vector])))
        self.assertTrue(np.array_equal(labels, np.array([test_label])))
        self.assertEqual(shape, test_vector.shape)
コード例 #5
0
    def test_predict_incorrect_shape(self):
        session = self.get_session()

        session.add(
            DatasetSample(image=pickle.dumps(np.array([0, 2])), label=1))
        session.add(
            DatasetSample(image=pickle.dumps(np.array([0, 1])), label=0))
        session.commit()

        t = ModelServingWrapper(LogisticRegression(), self.TESTBASE_PATH)
        t.fit()

        with self.assertRaises(ShapeMismatchError):
            t.predict(np.array([0]))
コード例 #6
0
    def test__get_training_dataset_empty(self):

        t = ModelServingWrapper(LinearRegression(), self.TESTBASE_PATH)

        with self.assertRaises(ValueError):
            t._get_training_dataset()
コード例 #7
0
    def test__init__with_fitted_model(self):
        fitted_model = LogisticRegression()
        fitted_model.fit(np.array([[0, 1, 2], [4, 5, 6]]), np.array([0, 1]))

        with self.assertRaises(ValueError):
            ModelServingWrapper(fitted_model, self.TESTBASE_PATH)
コード例 #8
0
    def test_predict_not_fitted(self):
        t = ModelServingWrapper(LogisticRegression(), self.TESTBASE_PATH)

        with self.assertRaises(ValueError):
            t.predict(np.array([0, 1, 2]))
コード例 #9
0
Example:

    learner.py mssql+pyodbc://user:[email protected]/mnist?driver=ODBC+Driver+17+for+SQL+Server models/model.pickle

"""

import argparse
import pickle

from sklearn.linear_model import LogisticRegression

from app.wrapper import ModelServingWrapper

parser = argparse.ArgumentParser(
    description='Fits the LogisticRegression model, using samples from given '
    'database and saves to file')

parser.add_argument('database_path',
                    type=str,
                    help='SQLAlchemy-compatible Database URL')
parser.add_argument('file_path', type=str, help='Path to file with model')

args = parser.parse_args()

model = ModelServingWrapper(LogisticRegression(), args.database_path)
accuracy = model.fit()

with open(args.file_path, 'wb') as handle:
    pickle.dump(model, handle)