def test_experimental(self): recognizer = Recognizer() recognizer.load_data([ 'BASE', 'FLOWER', 'JAIL', 'SNOW', 'STRIPE_1', 'STRIPE_2', 'STRIPE_3', 'TRI', ]) recognizer.model = [ RecognizerFlatten(), RecognizerSplit(), RecognizerPCA(), RecognizerLDA(), RecognizerSVM() ] recognizer.model[1].n_components = 80 recognizer.model[2].n_components = 10 recognizer.fit() recognizer.model[1].n_components = 75 recognizer.model[2].n_components = 30 self.assertGreater(recognizer.score(), 0.3)
def test_experimental(self): recognizer = Recognizer() recognizer.load_data([ # 'BLACK', 'BLUE', 'BROWN', 'GREEN', # 'PINK', 'WHITE', 'YELLOW', # 'CONE', 'MAPSCROLL', 'STONE', # 'NA', 'BASE', 'FLOWER', 'JAIL', 'SNOW', 'STRIPE_1', 'STRIPE_2', 'STRIPE_3', 'TRI', ]) recognizer.model = [RecognizerFlatten(), RecognizerSplit(), RecognizerPCA(), RecognizerLDA(), RecognizerSVM()] result = [] # for n_pca in [10, 50, 80, 100, 150, 200, 300, 500, 700]: for n_pca in [150]: # for n_lda in [1, 3, 5, 7, 10, 15, 30, 50, 70, 100, 130, 200, 250, 300]: for n_lda in [15]: recognizer.model[1].n_components = n_pca recognizer.model[2].n_components = n_lda n = 10 score = [] for i in range(n): recognizer.fit() score.append(recognizer.score()) avg = float(sum(score)) / n import sys print {'n_pca': n_pca, 'n_lda': n_lda, 'score': avg} result.append({'n_pca': n_pca, 'n_lda': n_lda, 'score': avg}) import sys print result recognizer.model[1].n_components = 75 recognizer.model[2].n_components = 30 self.assertGreater(recognizer.score(), 0.5)
def test_fit_predict_with_LDA_SVM(self): self.subject.load_data(['BLACK', 'BLUE']) self.subject.model = [ RecognizerFlatten(), RecognizerLDA(), RecognizerSVM() ] self.subject.fit() for X, y in zip(self.subject.data.X, self.subject.data.y): self.assertEqual(self.subject.predict(X), y)
def test_fit_predict_with_Split_PCA_SVM(self): self.subject.load_data(['BLACK', 'BLUE']) self.subject.model = [ RecognizerFlatten(), RecognizerSplit(), RecognizerPCA(), RecognizerSVM() ] self.subject.fit() self.subject.score()
def test_dump_load(self): model_filename = 'tmp/test_model.pkl' self.subject.load_data(['BLACK', 'BLUE']) self.subject.model = [ RecognizerFlatten(), RecognizerPCA(), RecognizerSVM() ] self.subject.fit() self.subject.dump(model_filename) self.subject = Recognizer() self.subject.load(model_filename) data = Data() data.load_data(['BLACK', 'BLUE']) for X, y in zip(data.X, data.y): self.assertEqual(self.subject.predict(X), y)
def test_basic_usage(self): model_filename = 'tmp/basic_usage.pkl' recognizer = Recognizer() recognizer.load_data([ 'BLACK', 'BLUE', 'BROWN', 'GREEN', 'PINK', 'WHITE', 'YELLOW', 'CONE', 'MAPSCROLL', 'STONE', 'NA', ]) recognizer.model = [ RecognizerFlatten(), RecognizerSplit(), RecognizerPCA(), RecognizerSVM() ] recognizer.fit() self.assertGreater(recognizer.score(), 0.9) recognizer.clear() recognizer.dump(model_filename) recognizer = Recognizer() recognizer.load(model_filename) data = Data() data.load_data([ 'BLACK', 'BLUE', 'BROWN', 'GREEN', 'PINK', 'WHITE', 'YELLOW', 'CONE', 'MAPSCROLL', 'STONE', 'NA', ]) self.assertGreater(recognizer.score(data), 0.97)
class RecognizerFlattenTest(unittest.TestCase): def setUp(self): self.original_base = ImageReader.base ImageReader.base = 'tests/fixtures/' + ImageReader.base self.subject = RecognizerFlatten() def tearDown(self): ImageReader.base = self.original_base def test_fit_predict(self): self.subject.load_data(['BLACK', 'BLUE']) self.subject.fit() self.assertEqual( self.subject.predict(self.subject.data.X).shape[1], 3 * 50 * 50) for X, y in zip(self.subject.data.X, self.subject.data.y): self.assertEqual(self.subject.predict(X).shape, (3 * 50 * 50, ))
def setUp(self): self.original_base = ImageReader.base ImageReader.base = 'tests/fixtures/' + ImageReader.base self.subject = RecognizerFlatten()
def test_fit_predict(self): self.subject.load_data(['BLACK', 'BLUE']) self.subject.model = [RecognizerFlatten(), RecognizerLDA()] self.subject.fit() for X, y in zip(self.subject.data.X, self.subject.data.y): self.assertTrue(self.subject.predict(X) is not None)