Skip to content

Commit

Permalink
Reorg tests and example for clarity / ease of use
Browse files Browse the repository at this point in the history
  • Loading branch information
pens committed Jan 23, 2018
1 parent ba370fb commit 98aceca
Show file tree
Hide file tree
Showing 47 changed files with 23 additions and 27 deletions.
23 changes: 8 additions & 15 deletions example/example.py → example.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import os
import shutil
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import eigenfish
import util

if __name__ == "__main__":
print("Loading training images")
fish_imgs = (["example/images/fish/%d.jpg" % i for i in range(0, 15)] +
["example/images/nofish/%d.jpg" % i for i in range(0, 15)])
fish_imgs = (["example_data/fish/%d.jpg" % i for i in range(0, 15)] +
["example_data/nofish/%d.jpg" % i for i in range(0, 15)])
fish_mat, shape = util.load_img_mat(fish_imgs)

print("Training model")
Expand All @@ -17,26 +13,23 @@
["no fish" for i in range(0, 15)]))

print("Loading test images")
test_imgs = (["example/images/fish/%d.jpg" % i for i in range(15, 20)] +
["example/images/nofish/%d.jpg" % i for i in range(15, 20)])
test_imgs = (["example_data/fish/%d.jpg" % i for i in range(15, 20)] +
["example_data/nofish/%d.jpg" % i for i in range(15, 20)])
test_mat = util.load_img_mat(test_imgs)[0]

print("Classifying test images")
labels = ef.classify(test_mat)
print("Labels:")
print(labels)
print("Labels: {}".format(labels))

print("Cross-validating test images")
pct = ef.cross_validate(test_mat, (["fish" for i in range(5)] +
["no fish" for i in range(5)]))
print("Percent correct:")
print(str(pct * 100) + "%")
print("Percent correct: {}%".format(pct * 100))

print("Saving trained model")
ef.save("model/example.ef")
ef.save("example.pkl")

print("Reloading saved model")
del ef
ef = eigenfish.Eigenfish(shape)
ef.load("model/example.ef")
shutil.rmtree("model/", True)
ef.load("example.pkl")
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
27 changes: 15 additions & 12 deletions test/test.py → test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import os
import shutil
import unittest
import util
from eigenfish import *
from process.process import *
from classify.classify import *
from util import *


class EmptyProcessor(Processor):
Expand Down Expand Up @@ -43,10 +43,10 @@ def setUp(self):
self.test_labels = (["zeroes" for i in range(3)] +
["ones" for i in range(3)])
self.ef = Eigenfish(self.shape)
os.mkdir("test/model/")
os.mkdir("test/")

def tearDown(self):
shutil.rmtree("test/model/", True)
shutil.rmtree("test/")

def test_train_classify(self):
self.ef.train(self.mat, self.labels)
Expand All @@ -63,15 +63,15 @@ def test_save_load(self):
self.ef.train(self.mat, self.labels)
res1 = self.ef.classify(self.test_mat)

self.ef.save('test/model/temp.pkl')
self.ef.save('test/temp.pkl')
self.ef = Eigenfish(self.shape)
self.ef.load('test/model/temp.pkl')
self.ef.load('test/temp.pkl')

res2 = self.ef.classify(self.test_mat)
for i in range(len(res1)):
self.assertEqual(res1[i], res2[i],
'Results not equal after save/load')
ef = Eigenfish(self.shape, 'test/model/temp.pkl')
ef = Eigenfish(self.shape, 'test/temp.pkl')
res3 = ef.classify(self.test_mat)
for i in range(len(res1)):
self.assertEqual(res1[i], res3[i],
Expand All @@ -84,8 +84,8 @@ def test_custom_modules(self):
self.assertEqual(["none"], res)

def test_load_img_mat(self):
filenames = ["test/data/%d.jpg" % i for i in range(4)]
img_mat, shape = util.load_img_mat(filenames)
filenames = ["test_data/%d.jpg" % i for i in range(4)]
img_mat, shape = load_img_mat(filenames)
self.assertEqual(img_mat.shape, (shape[0] * shape[1], 4))


Expand All @@ -100,10 +100,10 @@ def setUp(self):
self.test_labels = (["zeroes" for i in range(3)] +
["ones" for i in range(3)])
self.classifier = Classifier()
os.mkdir("test/model/")
os.mkdir("test/")

def tearDown(self):
shutil.rmtree("test/model", True)
shutil.rmtree("test/")

def test_train_classify(self):
self.classifier.train(self.mat, self.labels)
Expand All @@ -121,9 +121,9 @@ def test_save_load(self):
self.classifier.train(self.mat, self.labels)
res1 = self.classifier.classify(self.test_mat)

self.classifier.save('test/model/example.pkl')
self.classifier.save('test/temp.pkl')
self.classifier = Classifier()
self.classifier.load('test/model/example.pkl')
self.classifier.load('test/temp.pkl')

res2 = self.classifier.classify(self.test_mat)
for i in range(len(res1)):
Expand All @@ -147,3 +147,6 @@ def test_rpca(self):

def test_fft2_series(self):
fft = fft2_series(self.mat, self.shape)

if __name__ == "__main__":
unittest.main()
Empty file removed test/__init__.py
Empty file.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes

0 comments on commit 98aceca

Please sign in to comment.