Esempio n. 1
0
def get_dataset(model, dataset_name):
    logger.debug("Loading dataset")
    flat = len(model._layers[0].batch_input_shape) == 2
    if dataset_name.upper() == "EMNIST":
        return Datasets.EMNIST(flatten=flat)
    else:
        return Datasets.MNIST(flatten=flat)
Esempio n. 2
0
def exp_confusion():
    print("exp_confusion")
    for model_f in sorted(glob.glob("mnist_models/*.h5")):
        print('-' * 80)
        print(model_f)
        model = keras.models.load_model(model_f)
        for dataset in [Datasets.MNIST(), Datasets.EMNIST()]:
            X = np.concatenate((dataset.get_range('test', 'x', 0),\
                    dataset.get_range('train', 'x', 0)), axis=0)
            Y = np.concatenate((dataset.get_range('test', 'y', 0),\
                    dataset.get_range('train', 'y', 0)), axis=0)
            print(model.evaluate(X, Y))
            Y_pred = map(np.argmax, model.predict(X))
            cm = confusion_matrix(map(np.argmax, Y), Y_pred)
            print(cm)
Esempio n. 3
0
def exp_dropout(model_path):
    """ https://github.com/yaringal/DropoutUncertaintyExps/blob/master/net/net.py """
    model = keras.models.load_model(model_path)
    dataset = Datasets.MNIST(flatten=False)
    data_x, indices = dataset.gets('test', 'x', range(20))
    data_y, _ = dataset.gets('test', 'y', indices)

    y_train = dataset.get_range('train', 'y', 0, None)
    mean_y_train, std_y_train = np.mean(y_train), np.std(y_train)

    #standard_pred = model.predict(data_x, batch_size=500, verbose=1)
    #standard_pred = standard_pred * std_y_train + mean_y_train
    #rmse_standard_pred = np.mean((data_y.squeeze() - standard_pred.squeeze())**2.)**0.5
    T = 99
    pred = np.array(
        [model.predict(data_x, batch_size=500, verbose=0) for _ in range(T)])
    #Yt_hat = Yt_hat * std_y_train + mean_y_train

    mean_pred = np.mean(pred, 0)
    std_pred = np.std(pred, 0)
    sum_std_pred = np.mean(std_pred, 1)
    print("mean_pred\n", mean_pred.shape, "\n", mean_pred, "\n")
    print("std_pred\n", std_pred.shape, "\n", std_pred, "\n")
    print("sum_std_pred\n", sum_std_pred.shape, "\n", sum_std_pred, "\n")
    # Mean squared error?
    mse = np.mean((data_y.squeeze() - mean_pred.squeeze())**2., 1)
    print("MSE\n", mse.shape, "\n", mse, "\n")
Esempio n. 4
0
def load_caltech101_30(tiny_problem=False):
    caltech = scio.loadmat(CALTECH101_30_DIR + '/caltech101-30.matlab')
    k_train, k_test = caltech['Ktrain'], caltech['Ktest']
    label_tr, label_te = caltech['tr_label'], caltech['te_label']
    file_tr, file_te = caltech['tr_files'], caltech['te_files']

    if tiny_problem:
        pattern_step = 5
        fraction_limit = 0.2
        k_train = k_train[:int(len(label_tr) * fraction_limit):pattern_step,
                          :int(len(label_tr) * fraction_limit):pattern_step]
        label_tr = label_tr[:int(len(label_tr) * fraction_limit):pattern_step]

    U, s, Vh = linalg.svd(k_train)
    S_sqrt = linalg.diagsvd(s ** 0.5, len(s), len(s))
    X = np.dot(U, S_sqrt)  # examples in rows

    train_x, val_x, test_x = X[0:len(X):3, :], X[1:len(X):3, :], X[2:len(X):3, :]
    label_tr_enc = to_one_hot_enc(np.array(label_tr) - 1)
    train_y, val_y, test_y = label_tr_enc[0:len(X):3, :], label_tr_enc[1:len(X):3, :], label_tr_enc[2:len(X):3, :]
    train_file, val_file, test_file = file_tr[0:len(X):3], file_tr[1:len(X):3], file_tr[2:len(X):3]

    test_dataset = Dataset(data=test_x, target=test_y, general_info_dict={'files': test_file})
    validation_dataset = Dataset(data=val_x, target=val_y, general_info_dict={'files': val_file})
    training_dataset = Dataset(data=train_x, target=train_y, general_info_dict={'files': train_file})

    return Datasets(train=training_dataset, validation=validation_dataset, test=test_dataset)
Esempio n. 5
0
    def _load_data(self):
        work_directory = '.cifar10_data'
        images_path = maybe_download('cifar-10-python.tar.gz', work_directory,
                                     CIFAR10_URL)

        with tarfile.open(images_path, 'r') as images_tar:
            train_data = [
                self._unpickle(
                    images_tar.extractfile(
                        'cifar-10-batches-py/data_batch_{}'.format(batch)))
                for batch in range(1, 6)
            ]

            train_data = {
                'x': np.concatenate([d['x'] for d in train_data], axis=0),
                'y': np.concatenate([d['y'] for d in train_data], axis=0),
            }

            test_data = self._unpickle(
                images_tar.extractfile('cifar-10-batches-py/test_batch'))

        train = DataWrapper(train_data['x'], train_data['y'])
        test = DataWrapper(test_data['x'], test_data['y'])
        validation = DataWrapper(np.asarray([]), np.asarray([]))

        return Datasets(train=train, test=test, validation=validation)
Esempio n. 6
0
def load_larochelle(directory):
    from tensorflow.contrib.learn.python.learn.datasets.base import Datasets
    return Datasets(
        train=load_mnist_binary_dataset(directory, 'train'),
        validation=load_mnist_binary_dataset(directory, 'valid'),
        test=load_mnist_binary_dataset(directory, 'test')
    )
Esempio n. 7
0
def load_mnist(one_hot=True, partitions=None, filters=None, maps=None):
    datasets = read_data_sets(MNIST_DIR, one_hot=one_hot)
    train = Dataset(datasets.train.images, datasets.train.labels)
    validation = Dataset(datasets.validation.images, datasets.validation.labels)
    test = Dataset(datasets.test.images, datasets.test.labels)
    res = [train, validation, test]
    if partitions:
        res = redivide_data(res, partition_proportions=partitions, filters=filters, maps=maps)
        res += [None] * (3 - len(res))
    return Datasets(train=res[0], validation=res[1], test=res[2])
Esempio n. 8
0
def exp_low_amax():
    model = keras.models.load_model('mnist_models/mnist_cnn0.h5')
    dataset = Datasets.EMNIST(flatten=False)
    X = np.concatenate((dataset.get_range(
        'test', 'x', 0), dataset.get_range('train', 'x', 0)),
                       axis=0)
    Y = np.concatenate((dataset.get_range(
        'test', 'y', 0), dataset.get_range('train', 'y', 0)),
                       axis=0)
    results = find_low_amax(model, X, Y)
Esempio n. 9
0
def read_data_sets(validation_size=5000, one_hot=True):
    cifar_filename = "datasets/" + "cifar-10-python.tar.gz"

    try:
        os.makedirs("datasets")
    except OSError:
        pass

    if not os.path.isfile(cifar_dir + batches[0]):
        # Download data
        print("Downloading ckplus dataset")
        urllib.urlretrieve(
            "http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz",
            cifar_filename)
        tar = tarfile.open(cifar_filename)
        tar.extractall(path="datasets")
        tar.close()
        os.remove(cifar_filename)

    # Process batches
    all_batch_images = []
    all_batch_labels = []
    for batch_name in batches:
        batch = np.load(cifar_dir + batch_name)
        batch_images = batch['data']
        all_batch_images.append(batch_images)
        batch_labels = batch['labels']
        all_batch_labels.extend(batch_labels)

    all_batch_images = np.vstack(all_batch_images).reshape(-1, 3, 32, 32)
    all_batch_images = all_batch_images.transpose([0, 2, 3, 1])
    all_batch_labels = np.array(all_batch_labels)

    train_images, validation_images, train_labels, validation_labels = train_test_split(
        all_batch_images,
        all_batch_labels,
        test_size=validation_size,
        random_state=0)

    test_batch = np.load(cifar_dir + "test_batch")
    test_images = test_batch['data'].reshape(-1, 3, 32, 32)
    test_images = test_images.transpose([0, 2, 3, 1])

    test_labels = np.array(test_batch['labels'])

    if one_hot:
        train_labels = dense_to_one_hot(train_labels, NUM_CLASSES)
        validation_labels = dense_to_one_hot(validation_labels, NUM_CLASSES)
        test_labels = dense_to_one_hot(test_labels, NUM_CLASSES)

    train = DataSet(train_images, train_labels, reshape=False)
    validation = DataSet(validation_images, validation_labels, reshape=False)
    test = DataSet(test_images, test_labels, reshape=False)

    return Datasets(train=train, validation=validation, test=test)
Esempio n. 10
0
def load_iris(partitions_proportions=None, classes=3):
    """Loads Iris dataset divided as training and test set (by default)"""
    training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
        filename=IRIS_TRAINING,
        target_dtype=np.int,
        features_dtype=np.float32)
    test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
        filename=IRIS_TEST,
        target_dtype=np.int,
        features_dtype=np.float32)

    tr_set = training_set.data
    tr_targets = to_one_hot_enc(training_set.target)

    tr_dst = Dataset(data=tr_set, target=tr_targets)

    tst_set = test_set.data
    tst_targets = to_one_hot_enc(test_set.target)
    tst_dst = Dataset(data=tst_set, target=tst_targets)

    if partitions_proportions:
        if classes == 2:
            # noinspection PyUnusedLocal
            def filter_class(x, y, info, i):
                return np.argmax(y) != 0  # discard first class

            filter_list = [filter_class]

            # noinspection PyUnusedLocal
            def project_map(x, y, info, i):
                return x, y[1:], info

        else:
            filter_list, project_map = (None, None)

        res = redivide_data([tr_dst, tst_dst], partitions_proportions, filters=filter_list, maps=project_map)
        res += [None] * (3 - len(res))
        return Datasets(train=res[0], validation=res[1], test=res[2])

    return Datasets(train=tr_dst, test=tst_dst, validation=None)
Esempio n. 11
0
    def _load_data(self):
        work_directory = '.svhn_data'
        train_path = maybe_download('train_32x32.mat', work_directory,
                                    'http://ufldl.stanford.edu/housenumbers/train_32x32.mat')

        test_path = maybe_download('test_32x32.mat', work_directory,
                                   'http://ufldl.stanford.edu/housenumbers/test_32x32.mat')

        train = DataWrapper(*self._transform(loadmat(train_path)))
        test = DataWrapper(*self._transform(loadmat(test_path)))
        validation = DataWrapper(np.asarray([]), np.asarray([]))

        return Datasets(train=train, test=test, validation=validation)
Esempio n. 12
0
def exp_coverage(dataset_name, model_path):
    logger.info("Loading model {}".format(model_path))
    model = keras.models.load_model(model_path)
    flat = len(model._layers[0].batch_input_shape) == 2
    logger.info("Loading dataset")
    dataset = None
    if dataset_name.upper() == "EMNIST":
        dataset = Datasets.EMNIST(flatten=flat)
    else:
        dataset = Datasets.MNIST(flatten=flat)

    f_outs = "csvs/neuron_cov.csv"
    logger.info("Initializing KerasCoverage")
    kcov = KerasCoverage(model, dataset)
    logger.info("Measuring coverage")
    kcov.run([dataset.get_range('test', 'x', 0, 10000)])
    kcov.measure_coverage('neuron')
    kcov.save_outs(filename=f_outs)
    print(len(kcov.coverage['neuron'].columns))
    logger.info("Calculating entropy from {}".format(f_outs))
    entropy = KerasEntropy(kcov.coverage['neuron'])
    entropy.measure()
def xform_seq3d_array(df, moving_window=64, steps_ahead_to_predict=5, train_test_ratio=4.0):
    '''Returns values and labels. The values are 3D[timestep][moving_window][columns]. The labels
       are [1,0] for close price is up the next N timestemp or [0,1] for close price is down
       the next N timestep, where N is steps_ahead_to_predict.'''
    def process_data(df):
        dfs = []
        df1 = df
        for i in range(moving_window):
            dfs += [df1]
            df1 = df1.shift()
        df_seq = pd.concat(dfs, axis=1)
        df_seq = df_seq.iloc[moving_window:-steps_ahead_to_predict]
        values = df_seq.values.reshape(-1, moving_window, len(df.columns))
        labels = []
        df_labels = df.iloc[moving_window:-steps_ahead_to_predict]
        for i,close_price in zip(df_labels.index, df_labels['close']):
            predict_close_price = df.loc[i+steps_ahead_to_predict, 'close']
            labels.append([1.0,0.0] if predict_close_price > close_price else [0.0,1.0])
        labels = np.array(labels)
        return values, labels

    # transform DataFrame to our liking
    df = df.drop(['time','time_close','ignore'], axis=1)
    values,labels = process_data(df)
    
    # normalize the data
    scaler = MinMaxScaler(feature_range=(0,1))
    for i in range(len(values)):
        values[i] = scaler.fit_transform(values[i])

    # shuffling the data
    perm = np.arange(labels.shape[0])
    np.random.shuffle(perm)
    values = values[perm]
    labels = labels[perm]

    # selecting 1/5 for testing, and 4/5 for training
    train_test_idx = int((1.0 / (train_test_ratio + 1.0)) * labels.shape[0])
    train_values = values[train_test_idx:,:,:]
    train_labels = labels[train_test_idx:]
    test_values = values[:train_test_idx,:,:]
    test_labels = labels[:train_test_idx]

    train = DataSet(train_values, train_labels)
    test = DataSet(test_values, test_labels)

    return Datasets(train=train, validation=None, test=test)
Esempio n. 14
0
def read_data_sets(train_dir="EMNIST_data",
                   train_images = 'emnist-byclass-train-images-idx3-ubyte.gz',
                   train_labels = 'emnist-byclass-train-labels-idx1-ubyte.gz',
                   test_images = 'emnist-byclass-test-images-idx3-ubyte.gz',
                   test_labels = 'emnist-byclass-test-labels-idx1-ubyte.gz',
                   num_classes=10,
                   one_hot=False,
                   dtype=dtypes.float32,
                   reshape=True,
                   validation_size=5):
  local_file = os.path.join(train_dir, train_images)
  with open(local_file, 'rb') as f:
    train_images = extract_images(f)

  local_file = os.path.join(train_dir, train_labels)
  with open(local_file, 'rb') as f:
    train_labels = extract_labels(f, one_hot=one_hot, num_classes=num_classes)

  local_file = os.path.join(train_dir, test_images)
  with open(local_file, 'rb') as f:
    test_images = extract_images(f)

  local_file = os.path.join(train_dir, test_labels)
  with open(local_file, 'rb') as f:
    test_labels = extract_labels(f, one_hot=one_hot, num_classes=num_classes)

  if not 0 <= validation_size <= len(train_images):
    raise ValueError(
        'Validation size should be between 0 and {}. Received: {}.'
        .format(len(train_images), validation_size))

  validation_images = train_images[:validation_size]
  validation_labels = train_labels[:validation_size]
  train_images = train_images[validation_size:]
  train_labels = train_labels[validation_size:]

  train = DataSet(train_images, train_labels, dtype=dtype, reshape=reshape)
  validation = DataSet(validation_images,
                       validation_labels,
                       dtype=dtype,
                       reshape=reshape)
  test = DataSet(test_images, test_labels, dtype=dtype, reshape=reshape)

  return Datasets(train=train, validation=validation, test=test)
Esempio n. 15
0
def load_cifar10(one_hot=True, partitions=None, filters=None, maps=None):
    path = CIFAR10_DIR + "/cifar-10.pickle"
    with open(path, "rb") as input_file:
        X, target_name, files = cpickle.load(input_file)
    dict_name_ID = {}
    i = 0
    list_of_targets = sorted(list(set(target_name)))
    for k in list_of_targets:
        dict_name_ID[k] = i
        i += 1
    dict_ID_name = {v: k for k, v in dict_name_ID.items()}
    Y = []
    for name_y in target_name:
        Y.append(dict_name_ID[name_y])
    if one_hot:
        Y = to_one_hot_enc(Y)
    dataset = Dataset(data=X, target=Y, general_info_dict={'dict_name_ID': dict_name_ID, 'dict_ID_name': dict_ID_name},
                      sample_info_dicts=[{'target_name': t, 'files': f} for t, f in zip(target_name, files)])
    if partitions:
        res = redivide_data([dataset], partitions, filters=filters, maps=maps, shuffle=True)
        res += [None] * (3 - len(res))
        return Datasets(train=res[0], validation=res[1], test=res[2])
    return dataset
Esempio n. 16
0
def load_cifar100(one_hot=True, partitions=None, filters=None, maps=None):
    path = CIFAR100_DIR + "/cifar-100.pickle"
    with open(path, "rb") as input_file:
        X, target_ID_fine, target_ID_coarse, fine_ID_corr, coarse_ID_corr, files = cpickle.load(input_file)

    target_ID_fine = target_ID_fine[:len(X)]
    target_ID_coarse = target_ID_coarse[:len(X)]

    fine_ID_corr = {v: k for v, k in zip(range(len(fine_ID_corr)), fine_ID_corr)}
    coarse_ID_corr = {v: k for v, k in zip(range(len(coarse_ID_corr)), coarse_ID_corr)}
    fine_label_corr = {v: k for k, v in fine_ID_corr.items()}
    coarse_label_corr = {v: k for k, v in coarse_ID_corr.items()}

    Y = []
    for name_y in target_ID_fine:
        Y.append(name_y)
    Y = np.array(Y)
    if one_hot:
        Y = to_one_hot_enc(Y)
    superY = []
    for name_y in target_ID_coarse:
        superY.append(name_y)
    superY = np.array(superY)
    if one_hot:
        superY = to_one_hot_enc(superY)

    print(len(X))
    print(len(Y))
    dataset = Dataset(data=X, target=Y,
                      general_info_dict={'dict_name_ID_fine': fine_label_corr, 'dict_name_ID_coarse': coarse_label_corr,
                                         'dict_ID_name_fine': fine_ID_corr, 'dict_ID_name_coarse': coarse_ID_corr},
                      sample_info_dicts=[{'Y_coarse': yc, 'files': f} for yc, f in zip(superY, files)])
    if partitions:
        res = redivide_data([dataset], partitions, filters=filters, maps=maps, shuffle=True)
        res += [None] * (3 - len(res))
        return Datasets(train=res[0], validation=res[1], test=res[2])
    return dataset
Esempio n. 17
0
    def _load_data(self):
        work_directory = '.faces_data'
        images_path = maybe_download('img_align_celeba.zip', work_directory,
                                     FACES_IMAGES_URL)
        labels_path = maybe_download('list_attr_celeba.txt', work_directory,
                                     FACES_LABELS_URL)

        # Load labels.
        image_count = 0
        attributes = []
        attributes_classes = ['Male', 'Young', 'Smiling', 'Attractive']
        label_map = {}
        with open(labels_path, 'r') as labels_file:
            for line_no, line in enumerate(labels_file):
                if line_no == 0:
                    # Parse example count.
                    image_count = int(line)
                    continue
                elif line_no == 1:
                    # Parse header.
                    attributes = line.split()
                    continue

                # Parse line and determine class label.
                line = line.split()
                if self.options.dataset_random_labels:
                    label = (line_no - 2) % self.class_count
                else:
                    label = 0
                    for index, attribute in enumerate(attributes_classes):
                        value = int(line[attributes.index(attribute) + 1])
                        if value == 1:
                            label += 2**index

                    if label > 9:
                        continue

                label_map[line[0]] = label

        # Load images.
        images = np.zeros(
            [image_count, self.width * self.height * self.channels],
            dtype=np.float32)
        labels = np.zeros([image_count], dtype=np.int8)
        with zipfile.ZipFile(images_path, 'r') as images_zip:
            image_infos = images_zip.infolist()
            index = 0
            progress = tqdm.tqdm(total=image_count, leave=False)
            for image_info in image_infos:
                if not image_info.filename.endswith('.jpg'):
                    continue

                label = label_map.get(os.path.basename(image_info.filename),
                                      None)
                if label is None:
                    continue

                with images_zip.open(image_info) as image_file:
                    image = imread(image_file).astype(np.float32)

                    # Resize image to target dimensions.
                    h, w = image.shape[:2]
                    image = imresize(
                        image, [int((float(h) / w) * self.width), self.width])
                    j = int(round((image.shape[0] - self.height) / 2.))
                    image = image[j:j + self.height, :, :]
                    image = image / 255.

                    images[index, :] = image.flatten()
                    labels[index] = label
                    index += 1
                    progress.update()

            image_count = index + 1
            images = images[:image_count]
            labels = labels[:image_count]
            progress.close()

        print('Image count:', index)
        print('Values: min={} max={} mean={}'.format(np.min(images),
                                                     np.max(images),
                                                     np.mean(images)))

        print('Class distribution:')
        for label, count in zip(*np.unique(labels, return_counts=True)):
            print('  {}: {}'.format(label, count))

        train = DataWrapper(images, labels)
        test = DataWrapper(images[:1000], labels[:1000])
        validation = DataWrapper(np.asarray([]), np.asarray([]))

        return Datasets(train=train, test=test, validation=validation)
Esempio n. 18
0
def main():

    ## ############## ##
    # ARGUMENT PARSING #
    ## ############## ##

    arguments = sys.argv

    print('len(arguments) = {}'.format(len(arguments)))
    print(arguments)

    if 7 <= len(arguments) <= 8:
        print(
            '-----------------------------------------------------------------------------'
        )
        print('{} started with {} arguments, they are interpreted as:'.format(
            arguments[0], len(arguments)))

        # 1: Datset
        DATASET = arguments[1]
        print('Dataset         : {}'.format(DATASET))

        # 2: config file path
        use_config_file = True
        config_file_path = arguments[2]
        print('Config path 	: {}'.format(config_file_path))

        # 3: weight initialization
        init_weigts_path = str(arguments[3])
        if init_weigts_path == 'None':
            initialization_mode = 'resume'
            print('Init mode 	: resume')
        else:
            initialization_mode = 'from_folder'
            model_weights_dir = init_weigts_path
            print('Init mode 	: {}'.format(initialization_mode))

        # 4: Log folder
        log_folder_name = arguments[4]
        print('Log folder  	: {}'.format(log_folder_name))

        # 5: run name
        custom_run_name = arguments[5]
        if str(custom_run_name) == 'None':
            custom_run_name = None
            print('run name 	: default')
        else:
            print('run name 	: {}'.format(custom_run_name))

        # 6: regularization factor
        regularization_factor = float(arguments[6])

        print(
            '-----------------------------------------------------------------------------'
        )

        visualize_model_walkthrough = True

        # 7: weights and log folder prefix (default is the nip-convnet root)
        if len(arguments) == 8:
            root_dir_path = arguments[7]

            if str(root_dir_path) == 'None':
                root_dir_path = None
            else:
                if not os.path.exists(root_dir_path):
                    print(
                        'Given root directory {} is not valid, please enter an existing folder.'
                        .format(root_dir_path))
                    root_dir_path = None

                else:
                    print(
                        'Custom root directory {} given, logs and weights will be saved in there.'
                        .format(root_dir_path))
        else:
            root_dir_path = None

    elif len(arguments) == 1:
        print('Using default settings from file')

        DATASET = "MNIST"

        # load architecture / training configurations from file
        use_config_file = False
        config_file_path = 'configs/cae_2l_sigmoid.ini'

        # important: these values need to be restored from file directly here, which means if a config file is specified, it needs to be loaded already here

        # restore weights from the last iteration (if the same training setup was used before)
        # restore_last_checkpoint = True
        initialization_mode = 'resume'  # resume - default - from_file
        model_weights_dir = 'test'

        # store model walkthrough (no CIFAR support yet)
        visualize_model_walkthrough = True

        log_folder_name = '07_CAE_MNIST_SIGMOID_debug'
        custom_run_name = None

        regularization_factor = 0.001

        root_dir_path = None

    else:
        print('Wrong number of arguments!')
        print(
            'Usage: {} dataset config_file_path pre-trained_weights_path log_folder run_name regularization_factor'
            .format(arguments[0]))
        print('dataset 					: (MNIST | MNIST_SMALL | CIFAR10 | CKPLUS)')
        print('config_file_path 		: relative path to config file to use')
        print(
            'init_weights_path 	 	: (None : resume training | path to old checkpoint to init from'
        )
        print(
            'log_folder 				: log folder name (used in logs/ and weights/ subdirectories)'
        )
        print(
            'run_name 				: (None : auto generated run name | custom run name')
        print(
            'regularization_factor	: (<= 0: do nothing | > 0: factor for L1 regularization of the hidden representation'
        )
        print(
            '-----------------------------------------------------------------------------'
        )

        sys.exit(1)

    ## ########### ##
    # INPUT HANDING #
    ## ########### ##

    if DATASET == "MNIST":
        # load mnist
        from tensorflow.examples.tutorials.mnist import input_data
        dataset = input_data.read_data_sets("MNIST_data/", one_hot=True)
        input_size = (28, 28)
        num_classes = 10
        nhwd_shape = False

    elif DATASET == "MNIST_SMALL":
        N = 1000

        # load mnist
        from tensorflow.contrib.learn.python.learn.datasets.mnist import DataSet
        from tensorflow.contrib.learn.python.learn.datasets.base import Datasets
        from tensorflow.examples.tutorials.mnist import input_data

        complete_dataset = input_data.read_data_sets("MNIST_data/",
                                                     one_hot=True)

        small_training_dataset = DataSet(complete_dataset.train._images[:N],
                                         complete_dataset.train._labels[:N],
                                         reshape=False)

        dataset = Datasets(train=small_training_dataset,
                           validation=complete_dataset.validation,
                           test=complete_dataset.test)

        input_size = (28, 28)
        num_classes = 10
        one_hot_labels = True
        nhwd_shape = False

    elif DATASET == "CKPLUS":
        import scripts.load_ckplus as load_ckplus
        dataset = load_ckplus.read_data_sets(split=False,
                                             one_hot=True,
                                             frames=100)
        input_size = (68, 65)
        num_classes = load_ckplus.NUM_CLASSES
        nhwd_shape = False

    elif DATASET == "CIFAR10":
        dataset = "cifar_10"  # signals the train_ae function that it needs to load the data via cifar10_input.py
        input_size = (24, 24, 3)
        num_classes = 1
        nhwd_shape = True

        maybe_download_and_extract()

    elif DATASET[:6] == "CIFAR_":
        # CIFAR_nk
        limit = int(DATASET.split('_')[1].split('k')[0])

        if limit > 0 and limit < 51:
            print("Using " + str(limit) + "k CIFAR training images")

            from tensorflow.contrib.learn.python.learn.datasets.mnist import DataSet
            from tensorflow.contrib.learn.python.learn.datasets.base import Datasets
            import scripts.load_cifar as load_cifar

            complete_dataset = load_cifar.read_data_sets(one_hot=True)

            small_training_dataset = DataSet(
                complete_dataset.train._images[:limit * 1000],
                complete_dataset.train._labels[:limit * 1000],
                dtype=dtypes.uint8,
                reshape=False)

            dataset = Datasets(train=small_training_dataset,
                               validation=complete_dataset.validation,
                               test=complete_dataset.test)

            one_hot_labels = True
            input_size = (32, 32, 3)
            num_classes = 10
            nhwd_shape = True
        else:
            raise Exception("CIFAR limit must be between 1k and 50k, is: " +
                            str(limit))

    else:
        print('ERROR: Dataset not available')

    if nhwd_shape == False:

        # input variables: x (images), y_ (labels), keep_prob (dropout rate)
        x = tf.placeholder(tf.float32, [None, input_size[0] * input_size[1]],
                           name='input_digits')
        # reshape the input to NHWD format
        x_image = tf.reshape(x, [-1, input_size[0], input_size[1], 1])

    else:

        x = tf.placeholder(tf.float32,
                           [None, input_size[0], input_size[1], input_size[2]],
                           name='input_images')
        x_image = x

    # directory containing the autoencoder file
    cae_dir = os.path.join('models', 'cae')

    ## #### ##
    # CONFIG #
    ## #### ##

    # TODO Sabbir: begin what needs to be in the config file -----------------------------

    config_loader = cfg.ConfigLoader()

    if not use_config_file:

        # -------------------------------------------------------
        # AUTOENCODER SPECIFICATIONS
        filter_dims = [(5, 5), (5, 5)]
        hidden_channels = [64, 64]
        pooling_type = 'max_pooling'
        strides = None  # other strides should not work yet
        activation_function = 'sigmoid'
        relu_leak = 0.2  # only for leaky relus

        error_function = 'mse'  # default is cross-entropy
        optimizer_type = 'gradient_descent'  # default is gradient descent

        output_reconstruction_activation = 'sigmoid'

        weight_init_mean = 0.001
        weight_init_stddev = 0.05
        initial_bias_value = 0.001

        batch_size = 128
        max_iterations = 100001
        chk_iterations = 500
        step_size = 0.1

        tie_conv_weights = True

        # store to config dict:
        config_dict = {}
        config_dict['filter_dims'] = filter_dims
        config_dict['hidden_channels'] = hidden_channels
        config_dict['pooling_type'] = pooling_type
        config_dict['strides'] = strides
        config_dict['activation_function'] = activation_function
        config_dict['relu_leak'] = relu_leak
        config_dict['error_function'] = error_function
        config_dict['optimizer_type'] = optimizer_type
        config_dict[
            'output_reconstruction_activation'] = output_reconstruction_activation
        config_dict['weight_init_mean'] = weight_init_mean
        config_dict['weight_init_stddev'] = weight_init_stddev
        config_dict['initial_bias_value'] = initial_bias_value
        config_dict['batch_size'] = batch_size
        config_dict['max_iterations'] = max_iterations
        config_dict['chk_iterations'] = chk_iterations
        config_dict['step_size'] = step_size
        config_dict['tie_conv_weights'] = int(tie_conv_weights)

        config_loader.configuration_dict = config_dict

    else:
        # load config from file
        print('Loading config from file {}'.format(config_file_path))
        config_loader.load_config_file(config_file_path, 'CAE')
        config_dict = config_loader.configuration_dict

        if config_dict is None:
            print('Loading not succesful')
            sys.exit()

        # init all config variables variables from the file
        filter_dims = config_dict['filter_dims']
        hidden_channels = config_dict['hidden_channels']
        pooling_type = config_dict['pooling_type']
        strides = config_dict['strides']
        activation_function = config_dict['activation_function']
        relu_leak = float(config_dict['relu_leak'])
        error_function = config_dict['error_function']
        optimizer_type = config_dict['optimizer_type']
        output_reconstruction_activation = config_dict[
            'output_reconstruction_activation']
        weight_init_mean = float(config_dict['weight_init_mean'])
        weight_init_stddev = float(config_dict['weight_init_stddev'])
        initial_bias_value = float(config_dict['initial_bias_value'])
        batch_size = int(config_dict['batch_size'])
        max_iterations = int(config_dict['max_iterations'])
        chk_iterations = int(config_dict['chk_iterations'])
        step_size = float(config_dict['step_size'])
        tie_conv_weights = bool(int(config_dict['tie_conv_weights']))

        print('Config succesfully loaded')

    # TODO Sabbir: end what needs to be in the config file -----------------------------

    weight_file_name = get_weight_file_name(filter_dims, hidden_channels,
                                            pooling_type, activation_function,
                                            tie_conv_weights, batch_size,
                                            step_size, weight_init_mean,
                                            weight_init_stddev,
                                            initial_bias_value)

    # log_folder_name = '02_CIFAR_2enc'
    # run_name = 'old_commit_style'
    # run_name 	= '{}'.format(weight_file_name)
    # run_name = '({}x{})|_{}_{}_{}|{}_{}'.format(activation_function, output_reconstruction_activation, weight_init_mean, weight_init_stddev, initial_bias_value)
    # run_name = '5x5_d2_smaller_init_{}_{}'.format(activation_function, output_reconstruction_activation)
    # run_name = '{}_{}({}|{})'.format(pooling_type, output_reconstruction_activation, '-'.join(map(str, hidden_channels)), step_size)
    # run_name = '{}_{}_{}_{}_{}({})'.format(DATASET, error_function, activation_function, output_reconstruction_activation,pooling_type, weight_init_mean)
    # run_name = 'relu_small_learning_rate_101_{}'.format(weight_file_name)
    # run_name = 'that_run_tho'
    # -------------------------------------------------------

    # construct names for logging

    architecture_str = 'a' + '_'.join(
        map(lambda x: str(x[0]) + str(x[1]), filter_dims)) + '-' + '_'.join(
            map(str, hidden_channels)
        ) + '-' + activation_function + '_' + pooling_type
    training_str = 'tr' + str(batch_size) + '_' + '_' + str(tie_conv_weights)

    if custom_run_name is None:
        run_name = architecture_str + training_str
    else:
        run_name = custom_run_name

    if root_dir_path is not None:
        log_folder_parent_dir = os.path.join(root_dir_path, 'logs')
    else:
        log_folder_parent_dir = 'logs'

    log_path = os.path.join(log_folder_parent_dir, log_folder_name, run_name)

    # folder to store the training weights in:
    if root_dir_path is not None:
        model_save_parent_dir = os.path.join(root_dir_path, 'weights')
    else:
        model_save_parent_dir = 'weights'

    # CHECK FOLDER STRUCTURE
    save_path = os.path.join(model_save_parent_dir, log_folder_name, run_name)
    check_dirs = [	\
					# weights directories

        model_save_parent_dir, \
        os.path.join(model_save_parent_dir, log_folder_name), \
        os.path.join(model_save_parent_dir, log_folder_name), \
        os.path.join(model_save_parent_dir, log_folder_name, run_name), \
        os.path.join(model_save_parent_dir, log_folder_name, run_name, 'best'), \
					# log directories

        log_folder_parent_dir, \
        os.path.join(log_folder_parent_dir, log_folder_name), \
        log_path \
        ]

    for directory in check_dirs:
        if not os.path.exists(directory):
            os.makedirs(directory)

    ## ###### ##
    # TRAINING #
    ## ###### ##

    # construct autoencoder (5x5 filters, 3 feature maps)
    autoencoder = CAE(
        x_image,
        filter_dims,
        hidden_channels,
        step_size,
        weight_init_stddev,
        weight_init_mean,
        initial_bias_value,
        strides,
        pooling_type,
        activation_function,
        tie_conv_weights,
        store_model_walkthrough=visualize_model_walkthrough,
        relu_leak=relu_leak,
        optimizer_type=optimizer_type,
        output_reconstruction_activation=output_reconstruction_activation,
        regularization_factor=regularization_factor)

    sess = tf.Session()
    sess.run(tf.global_variables_initializer())

    print("Begin autencoder training")

    writer = tf.summary.FileWriter(log_path, sess.graph)

    # store config file in the folder
    config_loader.store_config_file(os.path.join(log_path, 'config.ini'),
                                    'CAE')

    init_iteration = 0

    if initialization_mode == 'resume' or initialization_mode == 'from_folder':
        # initialize training with weights from a previous training

        cwd = os.getcwd()

        if initialization_mode == 'resume':
            chkpnt_file_path = save_path
        else:
            chkpnt_file_path = model_weights_dir

        print('Looking for checkpoint')

        saver = tf.train.Saver(autoencoder.all_variables_dict)
        latest_checkpoint = tf.train.latest_checkpoint(chkpnt_file_path)

        if latest_checkpoint is not None:

            print('Found checkpoint')

            init_iteration = int(latest_checkpoint.split('-')[-1]) + 1
            smallest_reconstruction_error = float(
                latest_checkpoint.split('-')[-2])

            print('iteration is: {}'.format(init_iteration))
            print('smallest reconstruction error so far was {}'.format(
                smallest_reconstruction_error))

            if initialization_mode == 'from_folder':
                print(
                    'retrieved weights from checkpoint, begin with new iteration 0'
                )
                init_iteration = 0

            saver.restore(sess, latest_checkpoint)

            train_ae(
                sess,
                writer,
                x,
                autoencoder,
                dataset,
                cae_dir,
                weight_file_name,
                error_function,
                batch_size,
                init_iteration,
                max_iterations,
                chk_iterations,
                save_prefix=save_path,
                minimal_reconstruction_error=smallest_reconstruction_error)

        else:
            print('No checkpoint was found, beginning with iteration 0')
            train_ae(sess,
                     writer,
                     x,
                     autoencoder,
                     dataset,
                     cae_dir,
                     weight_file_name,
                     error_function,
                     batch_size,
                     init_iteration,
                     max_iterations,
                     chk_iterations,
                     save_prefix=save_path)

    else:
        # always train a new autoencoder
        train_ae(sess,
                 writer,
                 x,
                 autoencoder,
                 dataset,
                 cae_dir,
                 weight_file_name,
                 error_function,
                 batch_size,
                 init_iteration,
                 max_iterations,
                 chk_iterations,
                 save_prefix=save_path)

    # print('Test the training:')

    # visualize_cae_filters(sess, autoencoder)
    if visualize_model_walkthrough:
        visualize_ae_representation(sess, x_image, autoencoder, dataset,
                                    log_folder_name, run_name, input_size, 5)

    # add logwriter for tensorboard
    writer.close()

    sess.close()
Esempio n. 19
0
def main():

    ## ############## ##
    # ARGUMENT PARSING #
    ## ############## ##
    arguments = sys.argv

    if 8 <= len(arguments) <= 9:
        print(
            '-----------------------------------------------------------------------------'
        )
        print('{} started with {} arguments, they are interpreted as:'.format(
            arguments[0], len(arguments)))

        # 1: Datset
        DATASET = arguments[1]
        print('Dataset         : {}'.format(DATASET))

        # 2: config file path
        use_config_file = True
        config_file_path = arguments[2]
        print('Config path 	: {}'.format(config_file_path))

        # 3: initialization options
        initialization_mode = str(arguments[3])

        # 4: path for pre-trained weights
        init_weights_path = str(arguments[4])

        if initialization_mode == 'pre_trained_encoding':
            pre_trained_conv_weights_directory = init_weights_path
            print('Init mode 	: pre-trained')
        elif initialization_mode == 'from_folder':
            model_weights_directory = init_weights_path
            print('Init mode 	: from_folder')
        elif initialization_mode == 'resume':
            print('Init mode 	: resume')
        else:
            print('Init mode 	: random initialization (default)')

        # 5: Log folder
        log_folder_name = arguments[5]
        print('Log folder  	: {}'.format(log_folder_name))

        # 6: run name
        custom_run_name = arguments[6]
        if str(custom_run_name) == 'None':
            custom_run_name = None
            print('run name 	: default')
        else:
            print('run name 	: {}'.format(custom_run_name))

        # 7: add a test set evaluation after the training process
        # 	 flag was formerly used to enable test set evaluation during training time but this should not e done
        test_evaluation = arguments[7]

        evaluate_using_test_set = False
        evaluation_set_size = 1024
        evaluation_batch_size = 512

        if str(test_evaluation) == 'true' or str(test_evaluation) == 'True':
            final_test_evaluation = True
        else:
            final_test_evaluation = False

        # 8: weights and log folder prefix (optional, default is the nip-convnet root)
        if len(arguments) == 9:
            root_dir_path = arguments[8]

            if str(root_dir_path) == 'None':
                root_dir_path = None
            else:
                if not os.path.exists(root_dir_path):
                    print(
                        'Given root directory {} is not valid, please enter an existing folder.'
                        .format(root_dir_path))
                    root_dir_path = None

                else:
                    print(
                        'Custom root directory {} given, logs and weights will be saved in there.'
                        .format(root_dir_path))
        else:
            root_dir_path = None

        print(
            '-----------------------------------------------------------------------------'
        )

    elif len(arguments) == 1:
        print('Using default settings from file')
        ## #################### ##
        # INITIALIZATION OPTIONS #
        ## #################### ##
        log_folder_name = '001_lr_decay'
        custom_run_name = 'test'  # 'sigmoid_pre-trained'
        DATASET = "MNIST_SMALL"

        root_dir_path = None

        # choose whether to use the real test set or the { 	validation set, (MNIST | CK+)
        # 													training   set, (CIFAR10)     }
        # the test set should only be used for the final evaluation of a models performance
        evaluate_using_test_set = False
        final_test_evaluation = True
        evaluation_set_size = 1024  # set negative value for the whole set or restrain set size
        evaluation_batch_size = 512  # batch size used for testing

        use_config_file = False

        initialization_mode = 'resume'
        # initialization_mode:
        # 'resume'						: 	resume training from latest checkpoint in weights/log_folder_name/run_name if possible, otherwise default
        # 'from_folder'					: 	load last checkpoint from folder given in
        # 'pre_trained_encoding'		:	load encoding weights from an auto-encoder
        # 'default'						: 	init weights at random
        # ------------------------------
        # from_folder:
        model_weights_directory = 'weights/1k_MNIST_CNN/sigmoid/best'
        # pre_trained_encoding
        pre_trained_conv_weights_directory = 'weights/07_CAE_MNIST_SIGMOID_debug/a55_55-64_64-sigmoid_max_poolingtr128__True'
        # use_config_file
        config_file_path = 'configs/simple_cnn_config.ini'

    else:
        print('Wrong number of arguments!')
        print(
            'Usage: {} dataset config_file_path init_mode pre-trained_weights_path log_folder run_name test_set_bool'
            .format(arguments[0]))
        print('dataset 					: (MNIST | MNIST_SMALL | CIFAR10 | CKPLUS)')
        print('config_file_path 		: relative path to config file to use')
        print(
            'init_mode 				: (resume | from_folder | pre_trained_encoding | default'
        )
        print(
            'pre-trained_weights_path : (None : resume training | relative path to pre-trained conv weights'
        )
        print(
            'log_folder 				: log folder name (used in logs/ and weights/ subdirectories)'
        )
        print(
            'run_name 				: (None : auto generated run name | custom run name')
        print(
            'test_set_bool 			: (true (Attention: str, bash-style lower case): use test set for validation | else: use training set to monitor progress'
        )

    ## #################### ##
    # DATASET INITIALIZATION #
    ## #################### ##
    if DATASET == "MNIST":
        # load mnist
        from tensorflow.examples.tutorials.mnist import input_data
        dataset = input_data.read_data_sets("MNIST_data/", one_hot=True)
        input_size = (28, 28)
        num_classes = 10
        one_hot_labels = True
        nhwd_shape = False

    elif DATASET == "MNIST_10k":

        N = 10000

        # load mnist
        from tensorflow.contrib.learn.python.learn.datasets.mnist import DataSet
        from tensorflow.contrib.learn.python.learn.datasets.base import Datasets
        from tensorflow.examples.tutorials.mnist import input_data

        complete_dataset = input_data.read_data_sets(
            "MNIST_data/",
            one_hot=True,
        )

        small_training_dataset = DataSet(complete_dataset.train._images[:N],
                                         complete_dataset.train._labels[:N],
                                         dtype=dtypes.uint8,
                                         reshape=False)

        dataset = Datasets(train=small_training_dataset,
                           validation=complete_dataset.validation,
                           test=complete_dataset.test)

        input_size = (28, 28)
        num_classes = 10
        one_hot_labels = True
        nhwd_shape = False

    elif DATASET == "MNIST_1k":

        N = 1000

        # load mnist
        from tensorflow.contrib.learn.python.learn.datasets.mnist import DataSet
        from tensorflow.contrib.learn.python.learn.datasets.base import Datasets
        from tensorflow.examples.tutorials.mnist import input_data

        complete_dataset = input_data.read_data_sets(
            "MNIST_data/",
            one_hot=True,
        )

        small_training_dataset = DataSet(complete_dataset.train._images[:N],
                                         complete_dataset.train._labels[:N],
                                         dtype=dtypes.uint8,
                                         reshape=False)

        dataset = Datasets(train=small_training_dataset,
                           validation=complete_dataset.validation,
                           test=complete_dataset.test)

        input_size = (28, 28)
        num_classes = 10
        one_hot_labels = True
        nhwd_shape = False

    elif DATASET == "CKPLUS":
        import scripts.load_ckplus as load_ckplus
        dataset = load_ckplus.read_data_sets(one_hot=True)
        input_size = (68, 65)
        num_classes = load_ckplus.NUM_CLASSES
        one_hot_labels = True
        nhwd_shape = False

    elif DATASET == "CIFAR10":
        dataset = "cifar_10"  # signals the train_cnn function that it needs to load the data via cifar_10_input.py
        one_hot_labels = False  # changes the error functions because this cifar-10 version doesn't use a one-hot encoding
        input_size = (24, 24, 3)
        num_classes = 10
        nhwd_shape = True

        maybe_download_and_extract()

    elif DATASET[:6] == "CIFAR_":
        # CIFAR_5k
        limit = int(DATASET.split('_')[1].split('k')[0])

        if limit > 0 and limit < 51:
            print("Using " + str(limit) + "k CIFAR training images")

            from tensorflow.contrib.learn.python.learn.datasets.mnist import DataSet
            from tensorflow.contrib.learn.python.learn.datasets.base import Datasets
            import scripts.load_cifar as load_cifar

            complete_dataset = load_cifar.read_data_sets(one_hot=True)

            small_training_dataset = DataSet(
                complete_dataset.train._images[:limit * 1000],
                complete_dataset.train._labels[:limit * 1000],
                dtype=dtypes.uint8,
                reshape=False)

            dataset = Datasets(train=small_training_dataset,
                               validation=complete_dataset.validation,
                               test=complete_dataset.test)

            one_hot_labels = True
            input_size = (32, 32, 3)
            num_classes = 10
            nhwd_shape = True
        else:
            raise Exception("CIFAR limit must be between 1k and 50k, is: " +
                            str(limit))

    if nhwd_shape == False:

        # input variables: x (images), y_ (labels), keep_prob (dropout rate)
        x = tf.placeholder(tf.float32, [None, input_size[0] * input_size[1]],
                           name='input_digits')
        # reshape the input to NHWD format
        x_image = tf.reshape(x, [-1, input_size[0], input_size[1], 1])

    else:

        x = tf.placeholder(tf.float32,
                           [None, input_size[0], input_size[1], input_size[2]],
                           name='input_images')
        x_image = x

    y_ = tf.placeholder(tf.int64, [None], name='target_labels')

    keep_prob = tf.placeholder(tf.float32)

    ## #### ##
    # CONFIG #
    ## #### ##

    config_loader = cfg.ConfigLoader()

    if not use_config_file:

        # ARCHITECTURE
        # feature extraction parameters
        filter_dims = [(5, 5), (5, 5)]
        hidden_channels = [32, 32]
        pooling_type = 'strided_conv'  # dont change, std::bac_alloc otherwise (TODO: understand why)
        strides = None  # other strides should not work yet
        activation_function = 'sigmoid'
        # fc-layer parameters:
        dense_depths = [384, 192]

        # TRAINING
        # training parameters:
        batch_size = 128
        max_iterations = 31
        chk_iterations = 1
        dropout_k_p = 0.5

        step_size = 0.1
        decay_steps = 2
        decay_rate = 0.1

        weight_decay_regularizer = 0.

        weight_init_stddev = 0.2
        weight_init_mean = 0.
        initial_bias_value = 0.

        # only optimize dense layers and leave convolutions as they are
        fine_tuning_only = False

        # store to config dict:
        config_dict = {}
        config_dict['filter_dims'] = filter_dims
        config_dict['hidden_channels'] = hidden_channels
        config_dict['pooling_type'] = pooling_type
        config_dict['strides'] = strides
        config_dict['activation_function'] = activation_function
        config_dict['dense_depths'] = dense_depths
        config_dict['batch_size'] = batch_size
        config_dict['max_iterations'] = max_iterations
        config_dict['chk_iterations'] = chk_iterations
        config_dict['dropout_k_p'] = dropout_k_p
        config_dict['fine_tuning_only'] = int(fine_tuning_only)
        config_dict['step_size'] = step_size
        config_dict['decay_steps'] = decay_steps
        config_dict['decay_rate'] = decay_rate
        config_dict['weight_init_stddev'] = weight_init_stddev
        config_dict['weight_init_mean'] = weight_init_mean
        config_dict['initial_bias_value'] = initial_bias_value
        config_dict['weight_decay_regularizer'] = weight_decay_regularizer

        config_loader.configuration_dict = config_dict

    else:
        # load config from file
        print('Loading config from file {}'.format(config_file_path))
        config_loader.load_config_file(config_file_path, 'CNN')
        config_dict = config_loader.configuration_dict

        if config_dict is None:
            print('Loading not succesful')
            sys.exit()

        # init all config variables variables from the file
        filter_dims = config_dict['filter_dims']
        hidden_channels = config_dict['hidden_channels']
        pooling_type = config_dict['pooling_type']
        strides = config_dict['strides']
        activation_function = config_dict['activation_function']
        dense_depths = config_dict['dense_depths']
        batch_size = int(config_dict['batch_size'])
        max_iterations = int(config_dict['max_iterations'])
        chk_iterations = int(config_dict['chk_iterations'])
        dropout_k_p = float(config_dict['dropout_k_p'])
        fine_tuning_only = bool(int(config_dict['fine_tuning_only']))
        step_size = float(config_dict['step_size'])
        decay_steps = int(config_dict['decay_steps'])
        decay_rate = float(config_dict['decay_rate'])
        weight_init_stddev = float(config_dict['weight_init_stddev'])
        weight_init_mean = float(config_dict['weight_init_mean'])
        initial_bias_value = float(config_dict['initial_bias_value'])
        weight_decay_regularizer = float(
            config_dict['weight_decay_regularizer'])

        print('Config succesfully loaded')

    # -------------------------------------------------------

    # construct names for logging

    architecture_str = '(a)' + '_'.join(
        map(lambda x: str(x[0]) + str(x[1]), filter_dims)) + '-' + '_'.join(
            map(str, hidden_channels)) + '-' + activation_function
    training_str = '(tr)' + str(batch_size) + '_' + '_' + str(dropout_k_p)

    if custom_run_name is None:
        run_name = architecture_str + '|' + training_str
    else:
        run_name = custom_run_name

    # LOG AND WEIGHTS FOLDER:

    if root_dir_path is not None:
        log_folder_parent_dir = os.path.join(root_dir_path, 'logs')
    else:
        log_folder_parent_dir = 'logs'
    log_path = os.path.join(log_folder_parent_dir, log_folder_name, run_name)

    # folder to store the training weights in:
    if root_dir_path is not None:
        model_save_parent_dir = os.path.join(root_dir_path, 'weights')
    else:
        model_save_parent_dir = 'weights'
    save_path = os.path.join(model_save_parent_dir, log_folder_name, run_name)
    check_dirs = [
        model_save_parent_dir,
        os.path.join(model_save_parent_dir, log_folder_name),
        os.path.join(model_save_parent_dir, log_folder_name),
        os.path.join(model_save_parent_dir, log_folder_name, run_name),
        os.path.join(model_save_parent_dir, log_folder_name, run_name, 'best')
    ]

    for directory in check_dirs:
        if not os.path.exists(directory):
            os.makedirs(directory)

    ## ###### ##
    # TRAINING #
    ## ###### ##

    init_iteration = 0

    cnn = CNN(x_image,
              y_,
              keep_prob,
              filter_dims,
              hidden_channels,
              dense_depths,
              pooling_type,
              activation_function,
              one_hot_labels=one_hot_labels,
              step_size=step_size,
              decay_steps=decay_steps,
              decay_rate=decay_rate,
              weight_init_stddev=weight_init_stddev,
              weight_init_mean=weight_init_mean,
              initial_bias_value=initial_bias_value,
              weight_decay_regularizer=weight_decay_regularizer)

    sess = tf.Session()
    sess.run(tf.global_variables_initializer())

    # add logwriter for tensorboard
    writer = tf.summary.FileWriter(log_path, sess.graph)

    # store config file in the folder
    config_loader.store_config_file(os.path.join(log_path, 'config.ini'),
                                    'CNN')

    initialization_finished = False

    if initialization_mode == 'resume' or initialization_mode == 'from_folder':
        # initialize training with weights from a previous training

        cwd = os.getcwd()

        if initialization_mode == 'from_folder':
            chkpnt_file_path = os.path.join(cwd, model_weights_directory)
        else:
            chkpnt_file_path = os.path.join(cwd, save_path)

        saver = tf.train.Saver(cnn.all_variables_dict)
        latest_checkpoint = tf.train.latest_checkpoint(chkpnt_file_path)

        print(latest_checkpoint)

        if latest_checkpoint is not None:

            print('Found checkpoint')

            init_iteration = int(latest_checkpoint.split('-')[-1]) + 1

            best_accuracy_so_far = float(latest_checkpoint.split('-')[-2])

            print('iteration is: {}'.format(init_iteration))
            print('accuracy is: {}'.format(best_accuracy_so_far))

            if initialization_mode == 'from_folder':
                print(
                    'retrieved weights from checkpoint, begin with new iteration 0'
                )
                init_iteration = 0

            saver.restore(sess, latest_checkpoint)

            train_cnn(sess,
                      cnn,
                      dataset,
                      x,
                      y_,
                      keep_prob,
                      dropout_k_p,
                      batch_size,
                      init_iteration,
                      max_iterations,
                      chk_iterations,
                      writer,
                      fine_tuning_only,
                      save_path,
                      best_accuracy_so_far,
                      num_test_images=evaluation_set_size,
                      test_batch_size=evaluation_batch_size,
                      evaluate_using_test_set=evaluate_using_test_set,
                      final_test_evaluation=final_test_evaluation)

            initialization_finished = True

        else:
            if initialization_mode == 'resume':
                print('No checkpoint was found, beginning with iteration 0')
            else:
                print('No checkpoint found, check the given folder!')
                sys.exit(1)

    elif initialization_mode == 'pre_trained_encoding':

        if pre_trained_conv_weights_directory is not None:

            print('Trying to load conv weights from file')

            cwd = os.getcwd()
            chkpnt_file_path = os.path.join(
                cwd, pre_trained_conv_weights_directory)

            print('Looking for checkpoint in {}'.format(chkpnt_file_path))

            saver = tf.train.Saver()
            latest_checkpoint = tf.train.latest_checkpoint(chkpnt_file_path)

            print('Latest checkpoint is: {}'.format(latest_checkpoint))

            if latest_checkpoint is not None:

                cnn.load_encoding_weights(sess, latest_checkpoint)

                print('Initialized the CNN with encoding weights found in {}'.
                      format(latest_checkpoint))

        else:
            print('no pre-trained weights file found ,check the given folder!')
            sys.exit(1)

    if not initialization_finished:
        # always train a new autoencoder
        train_cnn(sess,
                  cnn,
                  dataset,
                  x,
                  y_,
                  keep_prob,
                  dropout_k_p,
                  batch_size,
                  init_iteration,
                  max_iterations,
                  chk_iterations,
                  writer,
                  fine_tuning_only,
                  save_path,
                  num_test_images=evaluation_set_size,
                  test_batch_size=evaluation_batch_size,
                  evaluate_using_test_set=evaluate_using_test_set,
                  final_test_evaluation=final_test_evaluation)

    # TODO Sabbir: store the current config in a config file in the logs/log_folder_name/run_name folder

    writer.close()
    sess.close()
Esempio n. 20
0
def read_data_sets(split=True,
                   num_train_folders=90,
                   num_test_folders=24,
                   one_hot=True,
                   frames=3):

    all_folders = os.listdir(emotions_path)
    random.Random(0).shuffle(all_folders)

    if split:
        train_folders = all_folders[:num_train_folders]
        test_folders = all_folders[num_train_folders:num_train_folders +
                                   num_test_folders]
        validation_folders = all_folders[num_train_folders + num_test_folders:]

        train_df = pd.DataFrame(read_from_folders(train_folders, frames))
        validation_df = pd.DataFrame(
            read_from_folders(validation_folders, frames))
        test_df = pd.DataFrame(read_from_folders(test_folders, frames))
        print("{} CK+ TRAIN datapoints loaded".format(len(train_df)))
        print("{} CK+ VALIDATION datapoints loaded".format(len(validation_df)))
        print("{} CK+ TEST datapoints loaded".format(len(test_df)))
    else:
        train_df = pd.DataFrame(read_from_folders(all_folders, frames))
        validation_df = train_df.copy()
        test_df = train_df.copy()
        print("{} CK+ TRAIN datapoints loaded".format(len(train_df)))

    if one_hot:
        train_labels = dense_to_one_hot(train_df['emotion'].values,
                                        NUM_CLASSES)
        validation_labels = dense_to_one_hot(validation_df['emotion'].values,
                                             NUM_CLASSES)
        test_labels = dense_to_one_hot(test_df['emotion'].values, NUM_CLASSES)
    else:
        train_labels = train_df['emotion']
        validation_labels = validation_df['emotion']
        test_labels = test_df['emotion']
    del train_df['emotion']
    del validation_df['emotion']
    del test_df['emotion']

    train_idx = np.arange(len(train_labels))
    validation_idx = np.arange(len(validation_labels))
    test_idx = np.arange(len(test_labels))
    np.random.shuffle(train_idx)
    np.random.shuffle(validation_idx)
    np.random.shuffle(test_idx)

    train_images = train_df.as_matrix()
    validation_images = validation_df.as_matrix()
    test_images = test_df.as_matrix()

    train = DataSet(train_images[train_idx, :],
                    train_labels[train_idx],
                    reshape=False)
    validation = DataSet(validation_images[validation_idx, :],
                         validation_labels[validation_idx],
                         reshape=False)
    test = DataSet(test_images[test_idx, :],
                   test_labels[test_idx],
                   reshape=False)

    return Datasets(train=train, validation=validation, test=test)
Esempio n. 21
0
def load_XRMB(root=XRMB_DIR, half_window=2, max_speakers=100, only_independent=False, normalize_single_speaker=False):
    """
    Loads XRMB data.

    :param max_speakers:
    :param root: path for root directory.
    :param half_window: half window size for the data.
    :param only_independent:  if False returns speaker datasets that do not keep track of the speaker.
    :param normalize_single_speaker: if True normalizes each dataset independently
    :return:    A Datasets class containing speaker independent data for training, validation and test, or a list
                a triplet of lists of Dataset if speaker_dependent is True.
    """
    prefix = root + "/xrbm_spk_"

    set_types = ['train', 'val', 'test']

    def load_speaker(speaker_number, set_type):
        assert set_type in set_types
        files = (prefix + str(speaker_number).zfill(3) + "_%s%s.csv" % (set_type, data_type)
                 for data_type in ('audio', 'motor', 'sentences'))
        arrays = [pd.read_csv(fl, header=None).values for fl in files]
        return arrays[0], arrays[1], arrays[2] - 1  # sentence bounds are with MATLAB convetions

    def load_all_in(_range=range(1)):
        datasets = {n: [] for n in set_types}
        m, mo, sd, sto = None, None, None, None
        k = 0
        for set_type in set_types:
            for k in _range:
                try:
                    general_info_dict = {'speaker': k, 'original set': set_type}
                    data, targets, sentence_bounds = load_speaker(k, set_type)
                    if normalize_single_speaker and k != 0:  # with k = 0 use mean and sd from training set
                        data, m_sd, sd_sd = np_normalize_data(data, return_mean_and_sd=True)
                        targets, mo_sd, sto_sd = np_normalize_data(targets, return_mean_and_sd=True)
                        general_info_dict['normalizing stats'] = (m_sd, sd_sd, mo_sd, sto_sd)
                    else:
                        data, m, sd = np_normalize_data(data, m, sd, return_mean_and_sd=True)
                        targets, mo, sto = np_normalize_data(targets, mo, sto, return_mean_and_sd=True)
                        general_info_dict['normalizing stats'] = (m, sd, mo, sto)

                    data = WindowedData(data, sentence_bounds, window=half_window, process_all=True)
                    datasets[set_type].append(Dataset(data, targets,
                                                      sample_info_dicts={'speaker': k} if k != 0 else None,
                                                      general_info_dict=general_info_dict))

                except OSError or FileNotFoundError:
                    k -= 1
                    break
            print('loaded %d speakers for %s' % (k, set_type))

        return datasets

    if not only_independent:
        res = load_all_in(range(0, max_speakers))

        for _set_type in set_types:  # sample-wise speaker info to the general datasets
            res[_set_type][0].sample_info_dicts = np.concatenate([
                np.array([{'speaker': k + 1}] * ds.num_examples) for k, ds in enumerate(res[_set_type][1:])
            ])

        return Datasets(train=res['train'], validation=res['val'], test=res['test'])
    else:
        res = load_all_in()
        return Datasets(train=res['train'][0], validation=res['val'][0], test=res['test'][0])
Esempio n. 22
0
def load_timit(folder=TIMIT_DIR, only_primary=False, filters=None, maps=None, small=False, context=None,
               fake=False, process_all=False):

    def load_timit_sentence_bound():
        def sentence_bound_reader(name):
            bnd = pd.read_csv(folder + '/timit_%sSentenceBound.csv' % name, header=None).values
            return bnd - 1

        return [sentence_bound_reader(n) for n in ['train', 'val', 'test']]

    folder = folder or TIMIT_DIR
    if isinstance(process_all, bool):
        process_all = [process_all] * 3

    if fake:
        def generate_dataset(secondary=False):
            target = np.random.randn(2000, 183)
            if secondary:
                target = np.hstack([target, np.random.randn(2000, 300)])
            return np.random.randn(2000, 123), target

        training_data, training_target = generate_dataset(not only_primary)
        validation_data, validation_target = generate_dataset()
        test_data, test_target = generate_dataset()
        training_info_dict = None
    else:
        split_number = '00' if small else ''
        training_target = pd.read_csv(folder + '/timit_trainTargets%s.csv' % split_number, header=None).values
        training_data = pd.read_csv(folder + '/timit-preproc_traindata_norm_noctx%s.csv' %
                                    split_number, header=None).values
        training_info_dict = {'dim_primary_target': _dim(training_target[0])}
        print('loaded primary training data')
        if not only_primary:
            training_secondary_target = pd.read_csv(folder + '/timit_trainTargetsPE%s.csv'
                                                    % split_number, header=None).values
            training_target = np.hstack([training_target, training_secondary_target])
            training_info_dict['dim_secondary_target'] = _dim(training_secondary_target[0])
            print('loaded secondary task targets')

        validation_data = pd.read_csv(folder + '/timit-preproc_valdata_norm_noctx%s.csv'
                                      % split_number, header=None).values
        validation_target = pd.read_csv(folder + '/timit_valTargets%s.csv' % split_number, header=None).values
        print('loaded validation data')

        test_data = pd.read_csv(folder + '/timit-preproc_testdata_norm_noctx.csv', header=None).values
        test_target = pd.read_csv(folder + '/timit_testTargets.csv', header=None).values
        print('loaded test data')

    if context:
        sbs = load_timit_sentence_bound()
        training_data, validation_data, test_data = (WindowedData(d, s, context, process_all=pa) for d, s, pa
                                                     in zip([training_data, validation_data, test_data],
                                                            sbs, process_all))

    test_dataset = Dataset(data=test_data, target=test_target)
    validation_dataset = Dataset(data=validation_data, target=validation_target)
    training_dataset = Dataset(data=training_data, target=training_target, general_info_dict=training_info_dict)

    res = Datasets(train=training_dataset, validation=validation_dataset, test=test_dataset)

    return res