Exemple #1
0
def plot_curve():
    print('Loading results object...')
    res = io.load_object('rbf_svm_optimization_dense_norml1', ignore=True)

    print('Plotting...')
    colors = itertools.cycle([
        'blue', 'green', 'red', 'cyan', 'magenta', 'yellow', 'darkolivegreen',
        'darkviolet', 'black'
    ])
    fig = plt.figure(figsize=(40, 20), facecolor='white')
    # Compute subplot parameters
    num_subplots = len(res)
    num_rows = np.ceil(num_subplots / 2)
    # All subplots
    for ind, k in enumerate(sorted(res.keys())):
        results = res[k]
        x = results['param_C']
        y = results['param_gamma']
        z = results['mean_test_score']
        print('For codebook with visual words {} '.format(k))
        print('Accuracy {}'.format(max(z)))
        print('C {}'.format(x[np.argmax(z)]))
        print('gamma {}'.format(y[np.argmax(z)]))

        color = colors.next()
        ax = fig.add_subplot(num_rows, 2, ind + 1, projection='3d')
        ax.scatter(x, y, z, c=color, lw=2)
        ax.set_title('{} visual words'.format(k))
        ax.set_xlabel('C')
        ax.set_ylabel('gamma')
        ax.set_zlabel('Accuracy')
    plt.tight_layout()
    plt.show()
    plt.close()
Exemple #2
0
def train_poly_svm(X,
                   y,
                   C=1,
                   degree=3,
                   gamma='auto',
                   coef0=0.0,
                   standardize=True,
                   dim_reduction=None,
                   save_scaler=False,
                   save_pca=False,
                   model_name=None):
    # PCA for dimensionality reduction if necessary
    pca = None
    if dim_reduction is not None and dim_reduction > 0:
        pca = decomposition.PCA(n_components=dim_reduction)
        pca.fit(X)
        X = pca.transform(X)

    # Standardize the data before classification if necessary
    std_scaler = None
    if standardize:
        std_scaler = preprocessing.StandardScaler()
        std_scaler.fit(X)
        X_std = std_scaler.transform(X)
    else:
        X_std = X

    # Instance of SVM classifier
    clf = svm.SVC(kernel='poly',
                  C=C,
                  degree=degree,
                  gamma=gamma,
                  coef0=coef0,
                  probability=True)

    if model_name is not None:
        # Try to load a previously trained model
        try:
            clf = io.load_object(model_name)
        except (IOError, EOFError):
            clf.fit(X_std, y)
            # Store the model with the provided name
            io.save_object(clf, model_name)
    else:
        clf.fit(X_std, y)

    if save_scaler:
        io.save_object(std_scaler, save_scaler)

    if save_pca:
        io.save_object(pca, save_pca)

    return clf, std_scaler, pca
def plot_curve():
    print('Loading results object...')
    res = io.load_object('intersection_dense_none_svm_optimization',
                         ignore=True)

    print('Plotting...')
    colors = itertools.cycle([
        'blue', 'green', 'red', 'cyan', 'magenta', 'yellow', 'darkolivegreen',
        'darkviolet', 'black'
    ])
    plt.figure(figsize=(20, 10), dpi=200, facecolor='white')
    # Compute subplot parameters
    num_subplots = len(res)
    num_rows = np.ceil(num_subplots / 2)
    # All subplots
    for ind, k in enumerate(sorted(res.keys())):
        # Plot
        results = res[k]
        x = results['param_C']
        y = results['mean_test_score']
        e = results['std_test_score']
        sorted_indices = x.argsort()
        x_sorted = np.asarray(x[sorted_indices], dtype=np.float64)
        y_sorted = np.asarray(y[sorted_indices], dtype=np.float64)
        e_sorted = np.asarray(e[sorted_indices], dtype=np.float64)
        color = colors.next()
        ax = plt.subplot(num_rows, 2, ind + 1)
        ax.set_xscale("log")
        ax.errorbar(x_sorted,
                    y_sorted,
                    e_sorted,
                    linestyle='--',
                    lw=2,
                    marker='x',
                    color=color)
        ax.set_title('{} visual words'.format(k))
        ax.set_xlabel('C')
        ax.set_ylabel('Accuracy')

        # Print information
        print('CODEBOOK {} '.format(k))
        print('-------------')
        print('Mean accuracy: {}'.format(y.max()))
        print('Std accuracy: {}'.format(e[np.argmax(y)]))
        print('C: {}'.format(x[np.argmax(y)]))
        print()
    plt.tight_layout()
    plt.show()
    plt.close()
Exemple #4
0
def train_linear_svm(X,
                     y,
                     C=1,
                     standardize=True,
                     dim_reduction=23,
                     save_scaler=False,
                     save_pca=False,
                     model_name=None,
                     liblinear=False):
    # PCA for dimensionality reduction if necessary
    pca = None
    if dim_reduction is not None and dim_reduction > 0:
        pca = decomposition.PCA(n_components=dim_reduction)
        pca.fit(X)
        X = pca.transform(X)

    # Standardize the data before classification if necessary
    std_scaler = None
    if standardize:
        std_scaler = preprocessing.StandardScaler()
        std_scaler.fit(X)
        X_std = std_scaler.transform(X)
    else:
        X_std = X

    # Instance of SVM classifier
    clf = svm.LinearSVC(C=C, max_iter=5000,
                        tol=1e-4) if liblinear else svm.SVC(
                            kernel='linear', C=C, probability=True)

    if model_name is not None:
        # Try to load a previously trained model
        try:
            clf = io.load_object(model_name)
        except (IOError, EOFError):
            clf.fit(X_std, y)
            # Store the model with the provided name
            io.save_object(clf, model_name)
    else:
        clf.fit(X_std, y)

    if save_scaler:
        io.save_object(std_scaler, save_scaler)

    if save_pca:
        io.save_object(pca, save_pca)

    return clf, std_scaler, pca
Exemple #5
0
def plot_svm_param(filename, mode='2d', name='default'):
    results = io.load_object(filename)

    print(results)
    fig = plt.figure()
    fig.suptitle(name, fontsize=14, fontweight='bold')
    if mode == '3d':
        ax = fig.add_subplot(111, projection='3d')
        ax.scatter(results[0], results[1], results[2], c='r', marker='o')
        if name == 'poly':
            ax.set_xlabel('d')
            ax.set_ylabel('r')
            ax.set_zlabel('Accuracy')

        elif name == 'sigmoid':

            ax.set_xlabel('r')
            ax.set_ylabel('gamma')
            ax.set_zlabel('Accuracy')

        D = results[0]
        G = results[1]
        A = results[2]
        ind = np.argmax(results[2])
        print(name + ' 2D: Best parameters are: Degree ' + str(D[ind]) + ' Gamma ' + str(
            G[ind]) + ' with Accuracy ' + str(
            A[ind]))
    elif mode == '2d':
        ax = fig.add_subplot(111)
        ax.plot(results[0], results[2])
        ax.set_xlabel('Gamma')
        ax.set_ylabel('Accuracy')
        G = results[0]
        A = results[2]
        ind = np.argmax(results[2])
        print(name + ' : Best parameters are: Gamma ' + str(G[ind]) + ' with Accuracy ' + str(A[ind]))
    elif mode == 'cost':
        ax = fig.add_subplot(111)
        ax.set_xlabel('Cost')
        ax.set_ylabel('Accuracy')
        ax.plot(results[0], results[1])

        C = results[0]
        A = results[1]
        ind = np.argmax(results[1])
        print(name + '-cost : Best parameters are: C ' + str(C[ind]) + ' with Accuracy ' + str(A[ind]))

    plt.show()
Exemple #6
0
def create_gmm(D, codebook_name=None):
    from libraries.yael.yael import ynumpy

    k = settings.codebook_size
    if codebook_name is not None:
        # Try to load a previously trained codebook
        try:
            gmm = io.load_object(codebook_name)
        except (IOError, EOFError):
            gmm = ynumpy.gmm_learn(np.float32(D), k)
            # Store the model with the provided name
            io.save_object(gmm, codebook_name)
    else:
        gmm = ynumpy.gmm_learn(np.float32(D), k)

    return gmm
Exemple #7
0
def create_codebook(X, codebook_name=None, k_means_init='random'):
    k = settings.codebook_size
    batch_size = 20 * k if X.shape[0] > 20 * k else X.shape[0] / 10
    codebook = cluster.MiniBatchKMeans(n_clusters=k,
                                       verbose=False,
                                       batch_size=batch_size,
                                       compute_labels=False,
                                       reassignment_ratio=10**-4,
                                       init=k_means_init)

    if codebook_name is not None:
        # Try to load a previously trained codebook
        try:
            codebook = io.load_object(codebook_name)
        except (IOError, EOFError):
            codebook.fit(X)
            # Store the model with the provided name
            io.save_object(codebook, codebook_name)
    else:
        codebook.fit(X)

    return codebook
Exemple #8
0
def train_pyramid_svm(X,
                      y,
                      C=1,
                      standardize=True,
                      dim_reduction=None,
                      save_scaler=False,
                      save_pca=False,
                      model_name=None):

    # Standardize the data before classification if necessary
    std_scaler = None
    if standardize:
        std_scaler = preprocessing.StandardScaler()
        std_scaler.fit(X)
        X_std = std_scaler.transform(X)
    else:
        X_std = X

    clf = svm.SVC(kernel=kernels.pyramid_kernel, C=C, probability=True)

    if model_name is not None:
        # Instance of SVM classifier
        # Try to load a previously trained model
        try:
            clf = io.load_object(model_name)
        except (IOError, EOFError):
            clf.fit(X_std, y)
            # Store the model with the provided name
            io.save_object(clf, model_name)
    else:
        clf.fit(X_std, y)

    if save_scaler:
        io.save_object(std_scaler, save_scaler)

    return clf, std_scaler, None
Exemple #9
0
def train():
    start = time.time()

    # Read the training set
    train_images_filenames, train_labels = io.load_training_set()
    print('Loaded {} train images.'.format(len(train_images_filenames)))

    # Feature extraction with sift
    print('Obtaining sift features...')
    try:
        D, L, I = io.load_object('train_sift_descriptors', ignore=True), \
                  io.load_object('train_sift_labels', ignore=True), \
                  io.load_object('train_sift_indices', ignore=True)
    except IOError:
        D, L, I, _ = feature_extraction.parallel_sift(train_images_filenames,
                                                      train_labels,
                                                      num_samples_class=-1,
                                                      n_jobs=N_JOBS)
        io.save_object(D, 'train_sift_descriptors', ignore=True)
        io.save_object(L, 'train_sift_labels', ignore=True)
        io.save_object(I, 'train_sift_indices', ignore=True)
    print('Time spend: {:.2f} s'.format(time.time() - start))

    # Start hyperparameters optimization
    print('\nSTARTING HYPERPARAMETER OPTIMIZATION FOR RBF SVM')
    codebook_k_values = [2**i for i in range(7, 16)]
    params_distribution = {
        'C': np.logspace(-4, 1, 10**3),
        'gamma': np.logspace(-3, 1, 10**3)
    }
    n_iter = 100
    best_accuracy = 0
    best_params = {}
    cv_results = {}

    # Iterate codebook values
    for k in codebook_k_values:
        temp = time.time()
        print('Creating codebook with {} visual words'.format(k))
        D = D.astype(np.uint32)
        codebook = bovw.create_codebook(D,
                                        codebook_name='codebook_{}'.format(k))
        print('Time spend: {:.2f} s'.format(time.time() - temp))
        temp = time.time()

        print('Getting visual words from training set...')
        vis_words, labels = bovw.visual_words(D,
                                              L,
                                              I,
                                              codebook,
                                              normalization='l1')
        print('Time spend: {:.2f} s'.format(time.time() - temp))
        temp = time.time()

        print('Scaling features...')
        std_scaler = StandardScaler().fit(vis_words)
        vis_words = std_scaler.transform(vis_words)
        print('Time spend: {:.2f} s'.format(time.time() - temp))
        temp = time.time()

        print('Optimizing SVM hyperparameters...')
        svm = SVC(kernel='rbf')
        random_search = RandomizedSearchCV(svm,
                                           params_distribution,
                                           n_iter=n_iter,
                                           scoring='accuracy',
                                           n_jobs=N_JOBS,
                                           refit=False,
                                           verbose=1,
                                           cv=4)
        random_search.fit(vis_words, labels)
        print('Time spend: {:.2f} s'.format(time.time() - temp))

        # Convert MaskedArrays to ndarrays to avoid unpickling bugs
        results = random_search.cv_results_
        results['param_C'] = results['param_C'].data
        results['param_gamma'] = results['param_gamma'].data

        # Appending all parameter-scores combinations
        cv_results.update({k: results})
        io.save_object(cv_results, 'rbf_svm_optimization_norml1')

        # Obtaining the parameters which yielded the best accuracy
        if random_search.best_score_ > best_accuracy:
            best_accuracy = random_search.best_score_
            best_params = random_search.best_params_
            best_params.update({'k': k})

        print('-------------------------------\n')

    print('\nBEST PARAMS')
    print('k={}, C={} , gamma={} --> accuracy: {:.3f}'.format(
        best_params['k'], best_params['C'], best_params['gamma'],
        best_accuracy))

    print('Saving all cross-validation values...')
    io.save_object(cv_results, 'rbf_svm_optimization_norml1')
    print('Done')
Exemple #10
0
def plot_curve():
    io.log('Loading cross-validation values...')
    cv_values = io.load_object('intersection_svm_CNNfeatures', ignore=True)

    io.log('Loading best parameters...')
    best_params = io.load_object('best_params_intersection_svm_CNNfeatures1',
                                 ignore=True)

    io.log('Plotting...')
    colors = itertools.cycle([
        'blue', 'green', 'red', 'cyan', 'magenta', 'yellow', 'darkolivegreen',
        'darkviolet', 'black'
    ])

    # Subplot parameters
    plt.figure(facecolor='white')
    num_subplots = len(codebook_size)
    num_columns = 1
    num_rows = np.ceil(num_subplots / num_columns)

    # All subplots
    for ind, k in enumerate(codebook_size):
        # Search dictionary
        val = cv_values[(k)]
        results = val['cv_results']
        feature_time = val['feature_time']
        pca_time = val['pca_time']
        gmm_time = val['gmm_time']
        fisher_time = val['fisher_time']
        scaler_time = val['scaler_time']
        crossvalidation_time = val['crossvalidation_time']
        total_time = val['total_time']

        # Plot
        x = results['param_C']
        y = results['mean_test_score']
        e = results['std_test_score']
        sorted_indices = x.argsort()
        x_sorted = np.asarray(x[sorted_indices], dtype=np.float64)
        y_sorted = np.asarray(y[sorted_indices], dtype=np.float64)
        e_sorted = np.asarray(e[sorted_indices], dtype=np.float64)
        color = colors.next()
        ax = plt.subplot(num_rows, num_columns, ind + 1)
        ax.set_xscale("log")
        ax.set_ylim((0.7, 0.9))
        ax.errorbar(x_sorted,
                    y_sorted,
                    e_sorted,
                    linestyle='--',
                    lw=2,
                    marker='x',
                    color=color)
        ax.set_title('{} Gaussians in GMM'.format(k))
        ax.set_xlabel('C')
        ax.set_ylabel('Accuracy')

        # Print information
        io.log('CODEBOOK {} '.format(k))
        io.log('-------------')
        io.log('Mean accuracy: {}'.format(y.max()))
        io.log('Std accuracy: {}'.format(e[np.argmax(y)]))
        io.log('C: {}'.format(x[np.argmax(y)]))
        io.log()
        io.log('Timing')
        io.log('\tSIFT time: {:.2f} s'.format(feature_time))
        io.log('\tPCA time: {:.2f} s'.format(pca_time))
        io.log('\tGMM time: {:.2f} s'.format(gmm_time))
        io.log('\tFisher time: {:.2f} s'.format(fisher_time))
        io.log('\tScaler time: {:.2f} s'.format(scaler_time))
        io.log('\tCV time: {:.2f} s'.format(crossvalidation_time))
        io.log('\t_________________________')
        io.log('\tTOTAL TIME: {:.2f} s'.format(total_time))
        io.log()
    plt.tight_layout()
    plt.show()
    plt.close()
def train():
    best_accuracy = 0
    best_params = {}
    cv_results = {}
    """ SETTINGS """
    settings.n_jobs = 1

    # Read the training set
    train_images_filenames, train_labels = io.load_training_set()
    io.log('Loaded {} train images.'.format(len(train_images_filenames)))
    k = 64

    io.log('Obtaining dense CNN features...')
    start_feature = time.time()
    try:
        D, L, I = io.load_object('train_CNN_descriptors', ignore=True), \
                  io.load_object('train_CNN_labels', ignore=True), \
                  io.load_object('train_CNN_indices', ignore=True)
    except IOError:
        # load VGG model
        base_model = VGG16(weights='imagenet')
        # io.save_object(base_model, 'base_model', ignore=True)

        # visualize topology in an image
        plot(base_model,
             to_file='modelVGG16.png',
             show_shapes=True,
             show_layer_names=True)

        # crop the model up to a certain layer
        model = Model(input=base_model.input,
                      output=base_model.get_layer('block5_conv2').output)
        D, L, I = feature_extraction.parallel_CNN_features(
            train_images_filenames,
            train_labels,
            model,
            num_samples_class=-1,
            n_jobs=settings.n_jobs)
        io.save_object(D, 'train_CNN_descriptors', ignore=True)
        io.save_object(L, 'train_CNN_labels', ignore=True)
        io.save_object(I, 'train_CNN_indices', ignore=True)
    feature_time = time.time() - start_feature
    io.log('Elapsed time: {:.2f} s'.format(feature_time))

    for dim_red in pca_reduction:
        io.log('Applying PCA ... ')
        start_pca = time.time()
        settings.pca_reduction = D.shape[1] * dim_red
        pca, D_pca = feature_extraction.pca(D)
        pca_time = time.time() - start_pca
        io.log('Elapsed time: {:.2f} s'.format(pca_time))
        for k in codebook_size:
            io.log('Creating GMM model (k = {})'.format(k))
            start_gmm = time.time()
            settings.codebook_size = k
            gmm = bovw.create_gmm(
                D_pca,
                'gmm_{}_pca_{}_CNNfeature'.format(k, settings.pca_reduction))
            gmm_time = time.time() - start_gmm
            io.log('Elapsed time: {:.2f} s'.format(gmm_time))

            io.log('Getting Fisher vectors from training set...')
            start_fisher = time.time()
            fisher, labels = bovw.fisher_vectors(D_pca,
                                                 L,
                                                 I,
                                                 gmm,
                                                 normalization='l2')
            fisher_time = time.time() - start_fisher
            io.log('Elapsed time: {:.2f} s'.format(fisher_time))

            io.log('Scaling features...')
            start_scaler = time.time()
            std_scaler = StandardScaler().fit(fisher)
            vis_words = std_scaler.transform(fisher)
            scaler_time = time.time() - start_scaler
            io.log('Elapsed time: {:.2f} s'.format(scaler_time))

            io.log('Optimizing SVM hyperparameters...')
            start_crossvalidation = time.time()
            svm = SVC(kernel='precomputed')
            random_search = RandomizedSearchCV(svm,
                                               params_distribution,
                                               n_iter=n_iter,
                                               scoring='accuracy',
                                               n_jobs=settings.n_jobs,
                                               refit=False,
                                               cv=3,
                                               verbose=1)
            # Precompute Gram matrix
            gram = kernels.intersection_kernel(vis_words, vis_words)
            random_search.fit(gram, labels)
            crossvalidation_time = time.time() - start_crossvalidation
            io.log('Elapsed time: {:.2f} s'.format(crossvalidation_time))

            # Convert MaskedArrays to ndarrays to avoid unpickling bugs
            results = random_search.cv_results_
            results['param_C'] = results['param_C'].data

            # Appending all parameter-scores combinations
            cv_results.update({
                (k): {
                    'cv_results':
                    results,
                    'feature_time':
                    feature_time,
                    'pca_time':
                    pca_time,
                    'gmm_time':
                    gmm_time,
                    'fisher_time':
                    fisher_time,
                    'scaler_time':
                    scaler_time,
                    'crossvalidation_time':
                    crossvalidation_time,
                    'total_time':
                    feature_time + pca_time + gmm_time + fisher_time +
                    scaler_time + crossvalidation_time
                }
            })
            io.save_object(cv_results,
                           'intersection_svm_CNNfeatures',
                           ignore=True)

            # Obtaining the parameters which yielded the best accuracy
            if random_search.best_score_ > best_accuracy:
                best_accuracy = random_search.best_score_
                best_params = random_search.best_params_
                best_params.update({'k': k, 'pca': dim_red})

            io.log('-------------------------------\n')
    io.log('\nSaving best parameters...')
    io.save_object(best_params,
                   'best_params_intersection_svm_CNNfeatures',
                   ignore=True)
    best_params_file = os.path.abspath(
        './ignore/best_params_intersection_svm_CNNfeatures.pickle')
    io.log('Saved at {}'.format(best_params_file))

    io.log('\nSaving all cross-validation values...')
    io.save_object(cv_results, 'intersection_svm_CNNfeatures', ignore=True)
    cv_results_file = os.path.abspath(
        './ignore/intersection_svm_CNNfeatures.pickle')
    io.log('Saved at {}'.format(cv_results_file))

    io.log('\nBEST PARAMS')
    io.log('k={}, dim_red={}, C={} --> accuracy: {:.3f}'.format(
        best_params['k'], best_params['pca'], best_params['C'], best_accuracy))
Exemple #12
0
def train():
    best_accuracy = 0
    best_params = {}
    cv_results = {}
    """ SETTINGS """
    settings.n_jobs = 1

    # Read the training set
    train_images_filenames, train_labels = io.load_training_set()
    io.log('Loaded {} train images.'.format(len(train_images_filenames)))

    # Parameter sweep for dense SIFT
    for ds in dense_sampling_density:

        io.log('Obtaining dense features with sampling parameter {}...'.format(
            ds))
        start_sift = time.time()
        settings.dense_sampling_density = ds
        try:
            D, L, I = io.load_object('train_dense_descriptors_{}'.format(settings.dense_sampling_density), ignore=True), \
                      io.load_object('train_dense_labels_{}'.format(settings.dense_sampling_density), ignore=True), \
                      io.load_object('train_dense_indices_{}'.format(settings.dense_sampling_density), ignore=True)
        except IOError:
            D, L, I, _ = feature_extraction.parallel_dense(
                train_images_filenames,
                train_labels,
                num_samples_class=-1,
                n_jobs=settings.n_jobs)
            io.save_object(D,
                           'train_dense_descriptors_{}'.format(
                               settings.dense_sampling_density),
                           ignore=True)
            io.save_object(L,
                           'train_dense_labels_{}'.format(
                               settings.dense_sampling_density),
                           ignore=True)
            io.save_object(I,
                           'train_dense_indices_{}'.format(
                               settings.dense_sampling_density),
                           ignore=True)
        sift_time = time.time() - start_sift
        io.log('Elapsed time: {:.2f} s'.format(sift_time))

        # Parameter sweep for PCA
        for dim_red in pca_reduction:

            io.log('Applying PCA (dim = {})...'.format(dim_red))
            start_pca = time.time()
            settings.pca_reduction = dim_red
            pca, D_pca = feature_extraction.pca(D)
            pca_time = time.time() - start_pca
            io.log('Elapsed time: {:.2f} s'.format(pca_time))

            # Parameter sweep for codebook size
            for k in codebook_size:

                io.log('Creating GMM model (k = {})'.format(k))
                start_gmm = time.time()
                settings.codebook_size = k
                gmm = bovw.create_gmm(
                    D_pca, 'gmm_{}_dense_{}_pca_{}'.format(k, ds, dim_red))
                gmm_time = time.time() - start_gmm
                io.log('Elapsed time: {:.2f} s'.format(gmm_time))

                io.log('Getting Fisher vectors from training set...')
                start_fisher = time.time()
                fisher, labels = bovw.fisher_vectors(D_pca,
                                                     L,
                                                     I,
                                                     gmm,
                                                     normalization='l2')
                fisher_time = time.time() - start_fisher
                io.log('Elapsed time: {:.2f} s'.format(fisher_time))

                io.log('Scaling features...')
                start_scaler = time.time()
                std_scaler = StandardScaler().fit(fisher)
                vis_words = std_scaler.transform(fisher)
                scaler_time = time.time() - start_scaler
                io.log('Elapsed time: {:.2f} s'.format(scaler_time))

                io.log('Optimizing SVM hyperparameters...')
                start_crossvalidation = time.time()
                svm = SVC(kernel='precomputed')
                random_search = RandomizedSearchCV(svm,
                                                   params_distribution,
                                                   n_iter=n_iter,
                                                   scoring='accuracy',
                                                   n_jobs=settings.n_jobs,
                                                   refit=False,
                                                   cv=3,
                                                   verbose=1)
                # Precompute Gram matrix
                gram = kernels.intersection_kernel(vis_words, vis_words)
                random_search.fit(gram, labels)
                crossvalidation_time = time.time() - start_crossvalidation
                io.log('Elapsed time: {:.2f} s'.format(crossvalidation_time))

                # Convert MaskedArrays to ndarrays to avoid unpickling bugs
                results = random_search.cv_results_
                results['param_C'] = results['param_C'].data

                # Appending all parameter-scores combinations
                cv_results.update({
                    (k, dim_red, ds): {
                        'cv_results':
                        results,
                        'sift_time':
                        sift_time,
                        'pca_time':
                        pca_time,
                        'gmm_time':
                        gmm_time,
                        'fisher_time':
                        fisher_time,
                        'scaler_time':
                        scaler_time,
                        'crossvalidation_time':
                        crossvalidation_time,
                        'total_time':
                        sift_time + pca_time + gmm_time + fisher_time +
                        scaler_time + crossvalidation_time
                    }
                })
                io.save_object(
                    cv_results,
                    'intersection_svm_optimization_fisher_vectors_l2',
                    ignore=True)

                # Obtaining the parameters which yielded the best accuracy
                if random_search.best_score_ > best_accuracy:
                    best_accuracy = random_search.best_score_
                    best_params = random_search.best_params_
                    best_params.update({'k': k, 'pca': dim_red, 'ds': ds})

                io.log('-------------------------------\n')

    io.log('\nSaving best parameters...')
    io.save_object(
        best_params,
        'best_params_intersection_svm_optimization_fisher_vectors_l2',
        ignore=True)
    best_params_file = os.path.abspath(
        './ignore/best_params_intersection_svm_optimization_fisher_vectors_l2.pickle'
    )
    io.log('Saved at {}'.format(best_params_file))

    io.log('\nSaving all cross-validation values...')
    io.save_object(cv_results,
                   'intersection_svm_optimization_fisher_vectors_l2',
                   ignore=True)
    cv_results_file = os.path.abspath(
        './ignore/intersection_svm_optimization_fisher_vectors_l2.pickle')
    io.log('Saved at {}'.format(cv_results_file))

    io.log('\nBEST PARAMS')
    io.log('k={}, C={}, dim_red={}, dense_grid={} --> accuracy: {:.3f}'.format(
        best_params['k'], best_params['C'], best_params['pca'],
        best_params['ds'], best_accuracy))
Exemple #13
0
    predicted_class = lin_svm.classes_[np.argmax(prediction_prob)]
    return predicted_class == test_label, predicted_class, np.ravel(prediction_prob)


""" MAIN SCRIPT"""
if __name__ == '__main__':
    start = time.time()
    if calculate_results==1:
        # Read the training set
        train_images_filenames, train_labels = io.load_training_set()
        print('Loaded {} train images.'.format(len(train_images_filenames)))

        # Feature extraction with sift
        print('Obtaining dense sift features...')
        try:
            D, L, I = io.load_object('train_dense_descriptors', ignore=True), \
                      io.load_object('train_dense_labels', ignore=True), \
                      io.load_object('train_dense_indices', ignore=True)
        except IOError:
            D, L, I, _ = feature_extraction.parallel_dense(train_images_filenames, train_labels, num_samples_class=-1,
                                                        n_jobs=N_JOBS)
            io.save_object(D, 'train_dense_descriptors', ignore=True)
            io.save_object(L, 'train_dense_labels', ignore=True)
            io.save_object(I, 'train_dense_indices', ignore=True)

        print('Elapsed time: {:.2f} s'.format(time.time() - start))
        temp = time.time()

        print('Creating codebook with {} visual words'.format(K))
        codebook = bovw.create_codebook(D, codebook_name='dense_codebook')
        print('Elapsed time: {:.2f} s'.format(time.time() - temp))
    predicted_class = svm.classes_[np.argmax(prediction_prob)]
    return predicted_class == test_label, predicted_class, np.ravel(prediction_prob)


""" MAIN SCRIPT"""
if __name__ == '__main__':
    start = time.time()

    # Read the training set
    train_images_filenames, train_labels = io.load_training_set()
    print('Loaded {} train images.'.format(len(train_images_filenames)))

    # Feature extraction with sift
    print('Obtaining sift features...')
    try:
        D, L, I, Kp_pos = io.load_object('train_dense_descriptors', ignore=True), \
                  io.load_object('train_dense_labels', ignore=True), \
                  io.load_object('train_dense_indices', ignore=True), \
                  io.load_object('train_dense_keypoints', ignore=True)
    except IOError:
        print('error')
        D, L, I, Kp = feature_extraction.parallel_dense(train_images_filenames, train_labels, num_samples_class=-1,
                                                   n_jobs=N_JOBS)
        io.save_object(D, 'train_dense_descriptors', ignore=True)
        io.save_object(L, 'train_dense_labels', ignore=True)
        io.save_object(I, 'train_dense_indices', ignore=True)
        Kp_pos = np.array([Kp[i].pt for i in range(0, len(Kp))], dtype=np.float64)
        io.save_object(Kp_pos, 'train_dense_keypoints', ignore=True)

    print('Elapsed time: {:.2f} s'.format(time.time() - start))
    temp = time.time()
import mlcv.input_output as io
import argparse
import matplotlib.pyplot as plt

if __name__ == '__main__':
    arguments_parser = argparse.ArgumentParser()
    arguments_parser.add_argument('file')
    arguments_parser.add_argument('--dpi', type=int, default=50)
    arguments = arguments_parser.parse_args()
    filename = arguments.file
    dpi = arguments.dpi

    print(filename.upper())

    # Load file
    history = io.load_object(filename, ignore=True)

    # Plot
    plt.figure(dpi=dpi, facecolor='white')
    plt.plot(history['acc'])
    plt.plot(history['val_acc'])
    plt.title('Accuracy')
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.ylim((0, 1))
    plt.legend(['train', 'validation'], loc='lower right')
    plt.show()
    plt.close()

    plt.figure(dpi=dpi, facecolor='white')
    plt.plot(history['loss'])
Exemple #16
0
    """ SETTINGS """
    settings.n_jobs = 1
    settings.codebook_size = 32
    settings.dense_sampling_density = 16
    settings.pca_reduction = 64

    start = time.time()

    # Read the training set
    train_images_filenames, train_labels = io.load_training_set()
    print('Loaded {} train images.'.format(len(train_images_filenames)))

    # Feature extraction with sift
    print('Obtaining dense features...')
    try:
        D, L, I = io.load_object('train_dense_descriptors', ignore=True), \
                  io.load_object('train_dense_labels', ignore=True), \
                  io.load_object('train_dense_indices', ignore=True)
    except IOError:
        D, L, I, _ = feature_extraction.parallel_dense(train_images_filenames, train_labels,
                                                       num_samples_class=-1,
                                                       n_jobs=settings.n_jobs)
        io.save_object(D, 'train_dense_descriptors', ignore=True)
        io.save_object(L, 'train_dense_labels', ignore=True)
        io.save_object(I, 'train_dense_indices', ignore=True)
    print('Elapsed time: {:.2f} s'.format(time.time() - start))
    temp = time.time()

    print('Applying PCA...')
    pca, D = feature_extraction.pca(D)
    print('Elapsed time: {:.2f} s'.format(time.time() - temp))
Exemple #17
0
                                             probability=True)
    probabilities = np.sum(predictions, axis=0)
    predicted_class = svm.classes_[np.argmax(probabilities)]

    return predicted_class == test_label, predicted_class, test_label


if __name__ == '__main__':
    start = time.time()

    # Read the test set
    test_images_filenames, test_labels = io.load_test_set()
    print('Loaded {} test images.'.format(len(test_images_filenames)))

    # Load the trained model, scaler and PCA
    svm = io.load_object(SESSION1['model'])
    std_scaler = io.load_object(SESSION1['scaler'])
    pca = io.load_object(SESSION1['pca'])

    # Feature extraction with sift, prediction with SVM and aggregation to obtain final class
    print('Predicting test data...')
    result = joblib.Parallel(n_jobs=SESSION1['n_jobs'], backend='threading')(
        joblib.delayed(parallel_testing)(test_image, test_label, svm,
                                         std_scaler, pca)
        for test_image, test_label in zip(test_images_filenames, test_labels))

    correct_class = [i[0] for i in result]
    predicted = [i[1] for i in result]
    expected = [i[2] for i in result]

    num_correct = np.count_nonzero(correct_class)