Example #1
0
def run(classifier, train_size, test_size, short_report=False):
    print(classifier)
    print("training set size: ", train_size)
    print("test set size:     ", test_size)
    runner = cf.ClassifierRunner(classifier)
    runner.train(mnist.training_images(train_size), mnist.training_labels(train_size))
    runner.run(mnist.test_images(test_size), mnist.test_labels(test_size))
    print(runner.get_report_str(short=short_report))
# Size that digit images are normalised to
NORMALISED_SIZE = (20, 20)


class KnnNormSizeDeslant(knn.KnnDigitClassifier):

    def train(self, images, labels):
        super(KnnNormSizeDeslant, self).train(
            self.preprocess_all(images), labels)

    def classify(self, image):
        return super(KnnNormSizeDeslant, self).classify(self.preprocess(image))

    def preprocess(self, image):
        desl = imgproc.deslant(image)
        return imgproc.crop_to_outer_contour(desl, NORMALISED_SIZE)

    def preprocess_all(self, images):
        for image in images:
            yield self.preprocess(image)


if __name__ == '__main__':
    NUM_TRAINS = 100
    NUM_TESTS = 100
    runner = cs.ClassifierRunner(KnnNormSizeDeslant())
    runner.train(mnist.training_images(NUM_TRAINS), mnist.training_labels(NUM_TRAINS))
    runner.run(mnist.test_images(NUM_TESTS), mnist.test_labels(NUM_TESTS))
    print(runner.get_report_str())
Example #3
0
def run(classifier, train_size, test_size):
    print(classifier)
    runner = cf.ClassifierRunner(classifier)
    runner.train(mnist.training_images(train_size), mnist.training_labels(train_size))
    runner.run(mnist.test_images(test_size), mnist.test_labels(test_size))