예제 #1
0
import os

from ini_caltech101 import util

path = os.path.abspath(os.path.join('datasets', 'img-gen-resized', '101_ObjectCategories'))
stratify = True
seed = 42

# X_train contain only paths to images
(X_train, y_train) = util.load_paths_from_files(path, 'X_train.txt', 'y_train.txt', full_path=False)

(X_train, y_train) = util.shuffle_data(X_train, y_train, seed=seed)

nb_folds = 10

for cv_fold, ((X_cv_train, y_cv_train), (X_cv_test, y_cv_test)) in \
        enumerate(util.make_cv_split(X_train, y_train, nb_folds=nb_folds, stratify=stratify, seed=seed)):

    split_config = {'fold': cv_fold,
                    'nb_folds': nb_folds,
                    'stratify': stratify,
                    'seed': seed,
                    'train_samples': len(X_cv_train),
                    'test_samples': len(X_cv_test)}

    print("Save split for fold {}".format(cv_fold))
    util.save_cv_split_paths(path, X_cv_train, y_cv_train, X_cv_test, y_cv_test, cv_fold, split_config)

    print("Calculating mean and std...")
    X_mean, X_std = util.calc_stats(X_cv_train, base_path=path)
    print("Save stats")
예제 #2
0
        X_mean, X_std = util.load_cv_stats(base_path, cv_fold)
        normalize_data = (X_mean, X_std)

    nb_train_sample = X_train.shape[0]
    nb_val_sample = X_val.shape[0]
    nb_test_sample = X_test.shape[0]

    print('X_train shape:', X_train.shape)
    print(nb_train_sample, 'train samples')
    if X_val is not None:
        print(nb_val_sample, 'validation samples')
    print(nb_test_sample, 'test samples')

    # shuffle/permutation
    if shuffle_data:
        (X_train, y_train) = util.shuffle_data(X_train, y_train, seed=None)
        (X_val, y_val) = util.shuffle_data(X_val, y_val, seed=None)
        (X_test, y_test) = util.shuffle_data(X_test, y_test, seed=None)

    ##########################
    # MODEL BUILDING
    ##########################

    print("Building model...")

    if batch_normalization:
        weight_reg = 5e-4  # weight regularization value for l2
        dropout = False
        dropout_fc_layer = False
        lr = 0.005
        lr_decay = 5e-4
        X_mean, X_std = util.load_cv_stats(base_path, cv_fold)
        normalize_data = (X_mean, X_std)

    nb_train_sample = X_train.shape[0]
    nb_val_sample = X_val.shape[0]
    nb_test_sample = X_test.shape[0]

    print('X_train shape:', X_train.shape)
    print(nb_train_sample, 'train samples')
    if X_val is not None:
        print(nb_val_sample, 'validation samples')
    print(nb_test_sample, 'test samples')

    # shuffle/permutation
    if shuffle_data:
        (X_train, y_train) = util.shuffle_data(X_train, y_train, seed=None)
        (X_val, y_val) = util.shuffle_data(X_val, y_val, seed=None)
        (X_test, y_test) = util.shuffle_data(X_test, y_test, seed=None)

    ##########################
    # MODEL BUILDING
    ##########################

    print("Building model...")

    if batch_normalization:
        weight_reg = 5e-4 # weight regularization value for l2
        dropout = False
        dropout_fc_layer = False
        lr = 0.005
        lr_decay = 5e-4