Commit 51d98fd3 authored by Taddeus Kroes's avatar Taddeus Kroes

Debugged performance test script.

parent b5f8ca8a
...@@ -4,8 +4,14 @@ from svmutil import svm_train, svm_problem, svm_parameter, svm_predict, \ ...@@ -4,8 +4,14 @@ from svmutil import svm_train, svm_problem, svm_parameter, svm_predict, \
class Classifier: class Classifier:
def __init__(self, c=None, gamma=None, filename=None, neighbours=3, \ def __init__(self, c=None, gamma=None, filename=None, neighbours=3, \
verbose=0): verbose=0):
self.neighbours = neighbours
self.verbose = verbose
if filename: if filename:
# If a filename is given, load a model from the given filename # If a filename is given, load a model from the given filename
if verbose:
print 'Loading classifier from "%s"...' % filename
self.model = svm_load_model(filename) self.model = svm_load_model(filename)
elif c == None or gamma == None: elif c == None or gamma == None:
raise Exception('Please specify both C and gamma.') raise Exception('Please specify both C and gamma.')
...@@ -16,11 +22,11 @@ class Classifier: ...@@ -16,11 +22,11 @@ class Classifier:
self.param.gamma = gamma # Parameter for radial kernel self.param.gamma = gamma # Parameter for radial kernel
self.model = None self.model = None
self.neighbours = neighbours
self.verbose = verbose
def save(self, filename): def save(self, filename):
"""Save the SVM model in the given filename.""" """Save the SVM model in the given filename."""
if self.verbose:
print 'Saving classifier in "%s"...' % filename
svm_save_model(filename, self.model) svm_save_model(filename, self.model)
def train(self, learning_set): def train(self, learning_set):
......
...@@ -5,14 +5,15 @@ from data import exists, DATA_FOLDER ...@@ -5,14 +5,15 @@ from data import exists, DATA_FOLDER
def load_classifier(neighbours, blur_scale, c=None, gamma=None, verbose=0): def load_classifier(neighbours, blur_scale, c=None, gamma=None, verbose=0):
classifier_file = DATA_FOLDER + 'classifier_%s_%s.dat' \ classifier_file = 'classifier_%s_%s.dat' \
% (blur_scale, neighbours) % (blur_scale, neighbours)
classifier_path = DATA_FOLDER + classifier_file
if exists(classifier_file): if exists(classifier_file):
if verbose: if verbose:
print 'Loading classifier...' print 'Loading classifier...'
classifier = Classifier(filename=classifier_file, \ classifier = Classifier(filename=classifier_path, \
neighbours=neighbours, verbose=verbose) neighbours=neighbours, verbose=verbose)
elif c != None and gamma != None: elif c != None and gamma != None:
if verbose: if verbose:
...@@ -23,6 +24,7 @@ def load_classifier(neighbours, blur_scale, c=None, gamma=None, verbose=0): ...@@ -23,6 +24,7 @@ def load_classifier(neighbours, blur_scale, c=None, gamma=None, verbose=0):
learning_set = load_learning_set(neighbours, blur_scale, \ learning_set = load_learning_set(neighbours, blur_scale, \
verbose=verbose) verbose=verbose)
classifier.train(learning_set) classifier.train(learning_set)
classifier.save(classifier_path)
else: else:
raise Exception('No soft margin and gamma specified.') raise Exception('No soft margin and gamma specified.')
......
...@@ -22,7 +22,7 @@ chars = [] ...@@ -22,7 +22,7 @@ chars = []
i = 0 i = 0
br = False br = False
for value in sorted(listdir()): for value in sorted(listdir(IMAGES_FOLDER)):
for image in sorted(listdir(IMAGES_FOLDER + value)): for image in sorted(listdir(IMAGES_FOLDER + value)):
f = IMAGES_FOLDER + value + '/' + image f = IMAGES_FOLDER + value + '/' + image
image = GrayscaleImage(f) image = GrayscaleImage(f)
...@@ -37,15 +37,20 @@ for value in sorted(listdir()): ...@@ -37,15 +37,20 @@ for value in sorted(listdir()):
if br: if br:
break break
# Load classifier # Load classifier (run create_classifier.py first)
classifier = load_classifier(neighbours, blur_scale, verbose=1) classifier = load_classifier(neighbours, blur_scale, verbose=1)
# Measure the time it takes to recognize <count> characters # Measure the time it takes to recognize <count> characters
start = time() start = time()
for char in chars: for char in chars:
# Normalize the character image
char.image = NormalizedCharacterImage(image, blur=blur_scale, height=42) char.image = NormalizedCharacterImage(image, blur=blur_scale, height=42)
# Create the image's feature vector
char.get_single_cell_feature_vector(neighbours) char.get_single_cell_feature_vector(neighbours)
# Feed the feature vector to the classifier
classifier.classify(char) classifier.classify(char)
elapsed = time() - start elapsed = time() - start
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment