Esempio n. 1
0
import create_segments
import sys
import logging

### Own modules
from general.hog import doHog
from general.pca import runPCA
from general.create_sets import create_sets
from train.svm import runSVM
from train.knn import runKNN

segmentDir = 'tmp/segments/'
featureDir = 'tmp/features/'
trainDir = featureDir + 'train/'
testDir = featureDir + 'test/'

for k in range(19):
    k += 1
    avgAcc = 0.0
    for i in range(10):
        print "hog"
        doHog(segmentDir, featureDir, "xeryus")
        print "create sets"
        create_sets(featureDir)
        print "knn"
        kNN, accuracy = runKNN(trainDir, testDir, k, "hog")

        avgAcc += accuracy
    print "Average accuracy for k =", k, ":\t", avgAcc/10
    avgAcc = 0
Esempio n. 2
0
    createSegments = False
    if len(sys.argv) == 2:
        createSegments = True

    ### Directories
    segmentDir = 'tmp/segments/'
    featureDir = 'tmp/features/'
    trainDir = featureDir + 'train/'

    ### First segment the training images
    if createSegments:
        logging.info("Creating segments of the images")
        create_segments.create_seg()

    ### Run hog over the segmented images
    ### Third argument for type of hog ("xeryus" or "other"), default = "xeryus"
    logging.info("Doing the HOG")
    doHog(segmentDir, featureDir)

    ### Create random train and test sets from the hog features
    # logging.info("Creating train and test set")
    create_sets(featureDir)

    ### Receive trained kNN (it also tests it on characters)
    logging.info("Running kNN")
    k = 10
    kNN = runKNN(trainDir, k)
    with open('tmp/knn.pickle', 'w') as f:
        pickle.dump(kNN, f)
if __name__ == '__main__':
    logging.config.fileConfig('logging.conf')
    assert IMG_DIR.exists(), "You must first create segmented images"

    block_sizes = ((1, 1), (2, 2), (3, 3), (4, 4))
    cell_sizes  = ((4, 4), (6, 6), (8, 8), (12, 12), (16, 16))

    results = {}

    start_time = time.time()
    for cell_size in cell_sizes:
        for block_size in block_sizes:
            svm_time = time.time()

            logging.info("Block size: %s Cell size: %s", block_size, cell_size)
            logging.debug("Creating HOG features")
            do_hog(block_size=block_size, cell_size=cell_size,
                    char_size=(88, 88), window_size=(96, 96))

            logging.debug("Creating test and training sets")
            create_sets(FEATURE_DIR)

            logging.debug("Training SVM")
            SVM, accuracy = runSVM(FEATURE_DIR + 'train/',
                FEATURE_DIR + 'test/')
            logging.info("SVM training time: %f s", time.time() - svm_time)
            logging.info("SVM accuracy: %f", accuracy)

    logging.info("Total execution time: %f s", time.time() - start_time)
Esempio n. 4
0
from train.svm import runSVM
import create_segments

if __name__ == '__main__':
    if len(sys.argv) >= 2 and sys.argv[1] != "--create_segments":
        print("Usage: python %s [--create_segments]" % sys.argv[0])

    need_create_segments = len(sys.argv) == 2

    segmentDir = Path('tmp/segments/')
    featureDir = Path('tmp/features/')
    trainDir = featureDir + 'train/'
    testDir  = featureDir + 'test/'

    if need_create_segments:
        logging.info("Create segments of the images")
        create_segments.create_seg()

    logging.info("Creating HOG features")
    doHog(segmentDir, featureDir)

    logging.info("Creating train and test set")
    create_sets(featureDir, 0.7)

    logging.info("Running SVM")
    SVM, accuracy = runSVM(trainDir, testDir)
    logging.info("SVM accuracy: %f" % accuracy)
    with open('tmp/svm.pickle', 'w') as f:
        pickle.dump(SVM, f)
    logging.info("Done")