Esempio n. 1
0
def validate(in_path,
             in_labels_path,
             val_path,
             val_labels_path,
             c_exponent_range,
             gamma_exponent_range,
             image_size,
             cell_size,
             block_size,
             bin_count,
             extension=""):
    '''
    Performs cross validation for ranges of the classifier parameters.

    Arguments:
        in_path                 -- The path to the input images.
        labels_path             -- The path to the class labels.
        fold_count              -- The number of folds for the cross validation.
        c_exponent_range        -- The range of values for n where parameter C=10^n.
        gamma_exponent_range    -- The range of values for n where parameter gamma=10^n.
        image_size              -- The size to which to resize the images.
        cell_size               -- The cell size for HOG feature calculation.
        block_size              -- The block size for HOG feature calculation.
        bin_count               -- The number of bins for HOG feature calculation.
        extension               -- The file type extension of the images to load.
    '''

    _, in_images, in_labels = inp.load_images(in_path,
                                              in_labels_path,
                                              image_size,
                                              extension=extension)
    in_features = inp.compute_features(in_images, cell_size, block_size,
                                       bin_count)

    _, val_images, val_labels = inp.load_images(val_path,
                                                val_labels_path,
                                                image_size,
                                                extension=extension)
    val_features = inp.compute_features(val_images, cell_size, block_size,
                                        bin_count)

    best_parameters = (None, None)
    best_error = float('inf')

    for c_exponent in range(c_exponent_range[0], c_exponent_range[1] + 1):
        for gamma_exponent in range(gamma_exponent_range[0],
                                    gamma_exponent_range[1] + 1):
            c = 10**c_exponent
            gamma = 10**gamma_exponent

            error = validate_once(in_features, in_labels, val_features,
                                  val_labels, c, gamma)

            if error < best_error:
                best_error = error
                best_parameters = (c, gamma)

    print(best_parameters, best_error)
Esempio n. 2
0
def train(in_path, labels_path, model_path, image_size, c, gamma, cell_size, block_size, bin_count, extension = ""):
    '''
    Trains and writes to file a multi-class classifier.

    Arguments:
        in_path     -- The path to the directory containing the dataset's images.
        labels_path -- The path to the JSON file containing label information.
        model_path  -- The path to the file where to store the pickled model.
        image_size  -- The size to which to resize the images.
        c           -- The C parameter for the SVM classifier.
        gamma       -- The gamma parameter for the SVM classifier.
        cell_size   -- The cell size for HOG feature calculation.
        block_size  -- The block size for HOG feature calculation.
        bin_count   -- The number of bins for HOG feature calculation.
        extension   -- The file type extension of the images to load.
    '''

    _, images, labels = inp.load_images(in_path, labels_path, image_size, extension = extension)
    features = inp.compute_features(images, cell_size, block_size, bin_count)

    classifier = model.train(features, labels, c, gamma)

    print("Error: ", model.test(features, labels, classifier).error)

    classifier.hog_image_size = image_size
    classifier.hog_cell_size = cell_size
    classifier.hog_block_size = block_size
    classifier.hog_bin_count = bin_count

    with open(model_path, 'wb') as model_file: pickle.dump(classifier, model_file)
Esempio n. 3
0
def diff(in_path,
         labels_path,
         model_path1,
         model_path2,
         report_path,
         extension=""):
    '''
    Outputs the difference in error of two classifier models on a given dataset.

    Arguments:
        in_path     -- The path to the directory containing the dataset's images.
        labels_path -- The path to the JSON file containing label information.
        model_path1  -- The path to the file containing the first pickled model.
        model_path2  -- The path to the file containing the second pickled model.
        report_path -- The path to the file where to store the error report.
        extension   -- The file type extension of the images to load.
    '''

    with open(model_path1, 'rb') as model_file:
        classifier1 = pickle.load(model_file)
    with open(model_path2, 'rb') as model_file:
        classifier2 = pickle.load(model_file)

    _, images, labels = inp.load_images(in_path,
                                        labels_path,
                                        classifier1.hog_image_size,
                                        extension=extension)
    features = inp.compute_features(images, classifier1.hog_cell_size,
                                    classifier1.hog_block_size,
                                    classifier1.hog_bin_count)

    report1 = model.test(features, labels, classifier1)
    report2 = model.test(features, labels, classifier2)

    report_diff = report1 - report2

    print("Error: ", report_diff.error)
    print()
    print(
        report_diff.get_sparse_string(report_diff.confusion_matrix_relative,
                                      0.01))

    with open(report_path, 'w') as report_file:
        report_diff.dump(report_file)