コード例 #1
0
def train():

    if len(sys.argv) != 2:
        print(
            "Invalid number of params! Usage: python train_vgg.py config_path")

    config_path = sys.argv[1]

    config = load_config(config_path)

    root_log_dir = config['root_log_dir']

    if not os.path.exists(root_log_dir):
        os.makedirs(root_log_dir, exist_ok=True)

    experiment_name = '_'.join(
        [config['experiment_name'],
         get_experiment_id()])

    experiment_dir = os.path.join(root_log_dir, experiment_name)

    os.mkdir(experiment_dir)

    (xtr, ytr_bit, ytr_deg), (xval, yval_bit,
                              yval_deg), (xte, yte_bit,
                                          yte_deg) = load_dataset(config)

    loss_te = pick_loss(config)

    image_height, image_width, n_channels = xtr.shape[1], xtr.shape[
        2], xtr.shape[3]

    predict_kappa = config['predict_kappa']
    fixed_kappa_value = config['fixed_kappa_value']

    n_trials = config['n_trials']
    best_trial_id = 0

    if not config['random_hyp_search']:

        batch_sizes = config['batch_sizes']
        learning_rates = config['learning_rates']
        params_grid = np.asarray(
            list(itertools.product(learning_rates, batch_sizes)) * n_trials)
        learning_rates = params_grid[:, 0]
        batch_sizes = params_grid[:, 1].astype('int')
        lr_decays = np.ones(n_trials) * 0.0
        epsilons = np.ones(n_trials) * 1.0e-7
        conv_dropouts = np.ones(n_trials) * config['conv_dropout']
        fc_dropouts = np.ones(n_trials) * config['fc_dropout']

    else:
        learning_rates = ht.sample_exp_float(n_trials,
                                             base=10,
                                             min_factor=-7,
                                             max_factor=0)
        batch_sizes = ht.sample_exp_int(n_trials,
                                        base=2,
                                        min_factor=1,
                                        max_factor=10)
        lr_decays = np.ones(n_trials) * 0.0
        epsilons = np.ones(n_trials) * 1.0e-7
        conv_dropouts = np.ones(n_trials) * config['conv_dropout']
        fc_dropouts = np.ones(n_trials) * config['fc_dropout']
        # lr_decays = ht.sample_exp_float(n_trials, base=10, min_factor=-3, max_factor=0)
        # epsilons = ht.sample_exp_float(n_trials, base=10, min_factor=-9, max_factor=0)
        # conv_dropouts = np.random.rand(n_trials)
        # fc_dropouts = np.random.rand(n_trials)

    results = dict()
    res_cols = [
        'trial_id', 'batch_size', 'learning_rate', 'lr_decay', 'epsilon',
        'conv_dropout', 'fc_dropout', 'tr_maad_mean', 'tr_maad_sem',
        'tr_likelihood', 'tr_likelihood_sem', 'val_maad_mean', 'val_maad_sem',
        'val_likelihood', 'val_likelihood_sem', 'te_maad_mean', 'te_maad_sem',
        'te_likelihood', 'te_likelihood_sem'
    ]

    results_df = pd.DataFrame(columns=res_cols)
    results_csv_path = os.path.join(experiment_dir, 'results.csv')
    results_yml_path = os.path.join(experiment_dir, 'results.yml')

    for tid in range(0, n_trials):

        learning_rate = learning_rates[tid]
        batch_size = batch_sizes[tid]
        lr_decay = lr_decays[tid]
        epsilon = epsilons[tid]
        fc_dropout = fc_dropouts[tid]
        conv_dropout = conv_dropouts[tid]

        print("TRIAL %d // %d" % (tid, n_trials))
        print("batch_size: %d" % batch_size)
        print("learning_rate: %f" % learning_rate)
        print("weight decay: %f" % lr_decay)
        print("epsilons: %f" % epsilon)
        print("conv dropout value: %f" % conv_dropout)
        print("fc dropout value: %f" % fc_dropout)

        trial_dir = os.path.join(experiment_dir, str(tid))
        os.mkdir(trial_dir)
        print("logs could be found at %s" % trial_dir)

        vgg_model = vgg.BiternionVGG(image_height=image_height,
                                     image_width=image_width,
                                     n_channels=n_channels,
                                     predict_kappa=predict_kappa,
                                     fixed_kappa_value=fixed_kappa_value,
                                     fc_dropout_val=fc_dropout,
                                     conv_dropout_val=conv_dropout)

        # optimizer = keras.optimizers.Adadelta(lr=1.0,
        #                                       epsilon=1.0e-7,
        #                                       decay=lr_decay)

        optimizer = keras.optimizers.Adam(lr=learning_rate,
                                          epsilon=epsilon,
                                          decay=lr_decay)

        vgg_model.model.compile(loss=loss_te, optimizer=optimizer)

        tensorboard_callback = keras.callbacks.TensorBoard(log_dir=trial_dir,
                                                           write_images=True)

        train_csv_log = os.path.join(trial_dir, 'train.csv')
        csv_callback = keras.callbacks.CSVLogger(train_csv_log,
                                                 separator=',',
                                                 append=False)

        best_model_weights_file = os.path.join(
            trial_dir, 'vgg_bit_' + config['loss'] + '_town.best.weights.h5')

        # model_ckpt_callback = keras.callbacks.ModelCheckpoint(best_model_weights_file,
        #                                                       monitor='val_loss',
        #                                                       mode='min',
        #                                                       save_best_only=True,
        #                                                       save_weights_only=True,
        #                                                       period=1,
        #                                                       verbose=1)

        val_loss_log_path = os.path.join(trial_dir, 'val_loss.csv')

        model_ckpt_callback = ModelCheckpointEveryNBatch(
            best_model_weights_file,
            val_loss_log_path,
            xval,
            yval_bit,
            verbose=1,
            save_best_only=True,
            period=config['val_check_period'])

        vgg_model.model.save_weights(best_model_weights_file)

        vgg_model.model.fit(x=xtr,
                            y=ytr_bit,
                            batch_size=batch_size,
                            epochs=config['n_epochs'],
                            verbose=1,
                            validation_data=(xval, yval_bit),
                            callbacks=[
                                tensorboard_callback, csv_callback,
                                model_ckpt_callback
                            ])

        best_model = vgg.BiternionVGG(image_height=image_height,
                                      image_width=image_width,
                                      n_channels=n_channels,
                                      predict_kappa=predict_kappa,
                                      fixed_kappa_value=fixed_kappa_value,
                                      fc_dropout_val=fc_dropout,
                                      conv_dropout_val=conv_dropout)

        best_model.model.load_weights(best_model_weights_file)

        trial_results = dict()
        trial_results['tid'] = tid
        trial_results['learning_rate'] = float(learning_rate)
        trial_results['batch_size'] = float(batch_size)
        trial_results['lr_decay'] = float(lr_decay)
        trial_results['epsilon'] = float(epsilon)
        trial_results['conv_dropout'] = float(conv_dropout)
        trial_results['fc_dropout'] = float(fc_dropout)
        trial_results['ckpt_path'] = best_model_weights_file
        trial_results['train'] = best_model.evaluate(xtr, ytr_deg, 'train')
        trial_results['validation'] = best_model.evaluate(
            xval, yval_deg, 'validation')
        trial_results['test'] = best_model.evaluate(xte, yte_deg, 'test')
        results[tid] = trial_results

        results_np = results_to_np(trial_results)

        trial_res_df = pd.DataFrame(results_np, columns=res_cols)
        results_df = results_df.append(trial_res_df)
        results_df.to_csv(results_csv_path, sep=';')
        save_results_yml(results, results_yml_path)

        if tid > 0:
            if config['loss'] == 'vm_likelihood':
                if trial_results['validation']['log_likelihood_mean'] > \
                        results[best_trial_id]['validation']['log_likelihood_mean']:
                    best_trial_id = tid
                    print(
                        "Better log likelihood achieved, current best trial: %d"
                        % best_trial_id)
            else:
                if trial_results['validation']['maad_loss'] < \
                        results[best_trial_id]['validation']['maad_loss']:
                    best_trial_id = tid
                    print("Better MAAD achieved, current best trial: %d" %
                          best_trial_id)

    print("loading best model..")
    best_ckpt_path = results[best_trial_id]['ckpt_path']
    overall_best_ckpt_path = os.path.join(
        experiment_dir, 'vgg.full_model.overall_best.weights.hdf5')
    shutil.copy(best_ckpt_path, overall_best_ckpt_path)

    best_model = vgg.BiternionVGG(
        image_height=image_height,
        image_width=image_width,
        n_channels=n_channels,
        predict_kappa=predict_kappa,
        fixed_kappa_value=fixed_kappa_value,
        fc_dropout_val=fc_dropouts[best_trial_id],
        conv_dropout_val=conv_dropouts[best_trial_id])

    best_model.model.load_weights(overall_best_ckpt_path)

    print("finetuning kappa values..")
    best_kappa = fixed_kappa_value
    if not predict_kappa:
        best_kappa = finetune_kappa(xval, yval_bit, best_model)
        print("best kappa: %f" % best_kappa)

    best_model = vgg.BiternionVGG(
        image_height=image_height,
        image_width=image_width,
        n_channels=n_channels,
        predict_kappa=predict_kappa,
        fixed_kappa_value=best_kappa,
        fc_dropout_val=fc_dropouts[best_trial_id],
        conv_dropout_val=conv_dropouts[best_trial_id])

    best_model.model.load_weights(overall_best_ckpt_path)

    best_results = dict()
    best_results['learning_rate'] = results[best_trial_id]['learning_rate']
    best_results['batch_size'] = results[best_trial_id]['batch_size']

    print("evaluating best model..")
    best_results['train'] = best_model.evaluate(xtr, ytr_deg, 'train')
    best_results['validation'] = best_model.evaluate(xval, yval_deg,
                                                     'validation')
    best_results['test'] = best_model.evaluate(xte, yte_deg, 'test')
    results['best'] = best_results

    save_results_yml(results, results_yml_path)
    results_df.to_csv(results_csv_path, sep=';')

    return
コード例 #2
0
def train():

    config_path = sys.argv[1]

    with open(config_path, 'r') as f:
        config = yaml.load(f)
    root_log_dir = config['root_log_dir']
    data_path = config['data_path']

    experiment_name = '_'.join(
        [config['experiment_name'],
         get_experiment_id()])

    if not os.path.exists(root_log_dir):
        os.mkdir(root_log_dir)
    experiment_dir = os.path.join(root_log_dir, experiment_name)
    os.mkdir(experiment_dir)
    shutil.copy(config_path, experiment_dir)

    xtr, ytr_deg, xval, yval_deg, xte, yte_deg = load_towncentre(
        'data/TownCentre.pkl.gz', canonical_split=True, verbose=1)

    image_height, image_width = xtr.shape[1], xtr.shape[2]
    ytr_bit = deg2bit(ytr_deg)
    yval_bit = deg2bit(yval_deg)
    yte_bit = deg2bit(yte_deg)

    net_output = config['net_output']

    if net_output == 'biternion':
        ytr = ytr_bit
        yval = yval_bit
    elif net_output == 'degrees':
        ytr = ytr_deg
        yval = yval_deg
    else:
        raise ValueError("net_output should be 'biternion' or 'degrees'")

    predict_kappa = config['predict_kappa']
    fixed_kappa_value = config['fixed_kappa_value']

    if config['loss'] == 'cosine':
        print("using cosine loss..")
        loss_te = cosine_loss_tf
    elif config['loss'] == 'von_mises':
        print("using von-mises loss..")
        loss_te = von_mises_loss_tf
    elif config['loss'] == 'mad':
        print("using mad loss..")
        loss_te = mad_loss_tf
    elif config['loss'] == 'vm_likelihood':
        print("using likelihood loss..")
        if predict_kappa:
            loss_te = von_mises_neg_log_likelihood_keras
        else:

            def _von_mises_neg_log_likelihood_keras_fixed(y_true, y_pred):
                mu_pred = y_pred[:, 0:2]
                kappa_pred = tf.ones([tf.shape(y_pred[:, 2:])[0], 1
                                      ]) * fixed_kappa_value
                return -K.mean(
                    von_mises_log_likelihood_tf(y_true, mu_pred, kappa_pred,
                                                net_output))

            loss_te = _von_mises_neg_log_likelihood_keras_fixed
    else:
        raise ValueError(
            "loss should be 'mad','cosine','von_mises' or 'vm_likelihood'")

    optimizer = get_optimizer(config['optimizer_params'])

    best_trial_id = 0
    n_trials = 1
    results = dict()

    for tid in range(0, n_trials):

        print("TRIAL %d" % tid)
        trial_dir = os.path.join(experiment_dir, str(tid))
        os.mkdir(trial_dir)
        print("logs could be found at %s" % trial_dir)

        vgg_model = vgg.BiternionVGG(image_height=image_height,
                                     image_width=image_width,
                                     n_channels=3,
                                     predict_kappa=predict_kappa,
                                     fixed_kappa_value=fixed_kappa_value)

        vgg_model.model.compile(loss=loss_te, optimizer=optimizer)

        tensorboard_callback = keras.callbacks.TensorBoard(log_dir=trial_dir)

        train_csv_log = os.path.join(trial_dir, 'train.csv')
        csv_callback = keras.callbacks.CSVLogger(train_csv_log,
                                                 separator=',',
                                                 append=False)

        best_model_weights_file = os.path.join(
            trial_dir, 'vgg_bit_' + config['loss'] + '_town.best.weights.h5')

        model_ckpt_callback = keras.callbacks.ModelCheckpoint(
            best_model_weights_file,
            monitor='val_loss',
            mode='min',
            save_best_only=True,
            save_weights_only=True,
            period=1,
            verbose=1)

        vgg_model.model.fit(x=xtr,
                            y=ytr,
                            batch_size=config['batch_size'],
                            epochs=config['n_epochs'],
                            verbose=1,
                            validation_data=(xval, yval),
                            callbacks=[
                                tensorboard_callback, csv_callback,
                                model_ckpt_callback
                            ])

        # final_model_ckpt_file = os.path.join(trial_dir, 'vgg_bit_' + config['loss'] + '_town.final.weigths.h5')
        # vgg_model.model.save_weights(final_model_ckpt_file)

        best_model = vgg.BiternionVGG(image_height=image_height,
                                      image_width=image_width,
                                      n_channels=3,
                                      predict_kappa=predict_kappa,
                                      fixed_kappa_value=fixed_kappa_value)

        best_model.model.load_weights(best_model_weights_file)

        trial_results = dict()
        trial_results['ckpt_path'] = best_model_weights_file
        trial_results['train'] = best_model.evaluate(xtr, ytr_deg, 'train')
        trial_results['validation'] = best_model.evaluate(
            xval, yval_deg, 'validation')
        trial_results['test'] = best_model.evaluate(xte, yte_deg, 'test')
        results[tid] = trial_results

        if tid > 0:
            if trial_results['validation']['log_likelihood_mean'] > \
                    results[best_trial_id]['validation']['log_likelihood_mean']:
                best_trial_id = tid
                print(
                    "Better validation loss achieved, current best trial: %d" %
                    best_trial_id)

    print("evaluating model..")
    best_ckpt_path = results[best_trial_id]['ckpt_path']
    overall_best_ckpt_path = os.path.join(
        experiment_dir, 'vgg.full_model.overall_best.weights.hdf5')
    shutil.copy(best_ckpt_path, overall_best_ckpt_path)

    best_model = vgg.BiternionVGG(image_height=image_height,
                                  image_width=image_width,
                                  n_channels=3,
                                  predict_kappa=predict_kappa,
                                  fixed_kappa_value=fixed_kappa_value)

    best_model.model.load_weights(overall_best_ckpt_path)

    best_results = dict()
    best_results['train'] = best_model.evaluate(xtr, ytr_deg, 'train')
    best_results['validation'] = best_model.evaluate(xval, yval_deg,
                                                     'validation')
    best_results['test'] = best_model.evaluate(xte, yte_deg, 'test')
    results['best'] = best_results

    results_yml_file = os.path.join(experiment_dir, 'results.yml')
    with open(results_yml_file, 'w') as results_yml_file:
        yaml.dump(results, results_yml_file, default_flow_style=False)

    return
コード例 #3
0
def main():

    n_u = 8
    exp_id = get_experiment_id()
    root_log_dir = 'logs/cvae/'

    experiment_dir = os.path.join(root_log_dir, exp_id)
    os.mkdir(experiment_dir)

    xtr, ytr_deg, xval, yval_deg, xte, yte_deg = load_towncentre(
        'data/TownCentre.pkl.gz', canonical_split=True, verbose=1)

    # xtr, ytr_deg = aug_data(xtr, ytr_deg)
    # xval, yval_deg = aug_data(xval, yval_deg)
    # xte, yte_deg = aug_data(xval, yval_deg)

    ytr_bit = deg2bit(ytr_deg)
    yval_bit = deg2bit(yval_deg)
    yte_bit = deg2bit(yte_deg)

    image_height, image_width, n_channels = xtr.shape[1:]
    phi_shape = yte_bit.shape[1]

    best_trial_id = 0
    n_trials = 10
    results = dict()

    overall_best_ckpt_path = os.path.join(
        experiment_dir, 'cvae.full_model.overall_best.weights.hdf5')

    for tid in range(0, n_trials):

        n_epochs = 100
        batch_size = 10

        print("TRIAL %d" % tid)
        trial_dir = os.path.join(experiment_dir, str(tid))
        os.mkdir(trial_dir)

        cvae_best_ckpt_path = os.path.join(
            trial_dir, 'cvae.full_model.trial_%d.best.weights.hdf5' % tid)

        tensorboard_callback = keras.callbacks.TensorBoard(log_dir=trial_dir)

        train_csv_log = os.path.join(trial_dir, 'train.csv')
        csv_callback = keras.callbacks.CSVLogger(train_csv_log,
                                                 separator=',',
                                                 append=False)

        model_ckpt_callback = keras.callbacks.ModelCheckpoint(
            cvae_best_ckpt_path,
            monitor='val_loss',
            mode='min',
            save_best_only=True,
            save_weights_only=True,
            period=1,
            verbose=1)

        cvae_model = CVAE(image_height=image_height,
                          image_width=image_width,
                          n_channels=n_channels,
                          n_hidden_units=n_u,
                          kl_weight=0.7)

        cvae_bestloglike_ckpt_path = os.path.join(
            trial_dir,
            'cvae.full_model.trial_%d.best_likelihood.weights.hdf5' % tid)
        eval_callback = EvalCVAEModel(xval, yval_deg, 'validation', cvae_model,
                                      cvae_bestloglike_ckpt_path)

        cvae_model.full_model.fit([xtr, ytr_bit], [ytr_bit],
                                  batch_size=batch_size,
                                  epochs=n_epochs,
                                  validation_data=([xval, yval_bit], yval_bit),
                                  callbacks=[
                                      tensorboard_callback, csv_callback,
                                      model_ckpt_callback, eval_callback
                                  ])

        cvae_model.evaluate(xtr, ytr_deg, 'train')
        cvae_model.evaluate(xval, yval_deg, 'validation')
        cvae_model.evaluate(xte, yte_deg, 'test')

        best_model = CVAE(image_height=image_height,
                          image_width=image_width,
                          n_channels=n_channels,
                          n_hidden_units=n_u)

        best_model.full_model.load_weights(cvae_bestloglike_ckpt_path)

        trial_results = dict()
        trial_results['ckpt_path'] = cvae_best_ckpt_path
        trial_results['train'] = best_model.evaluate(xtr, ytr_deg, 'train')
        trial_results['validation'] = best_model.evaluate(
            xval, yval_deg, 'validation')
        trial_results['test'] = best_model.evaluate(xte, yte_deg, 'test')
        results[tid] = trial_results

        if tid > 0:
            if trial_results['validation']['importance_log_likelihood'] > \
                    results[best_trial_id]['validation']['importance_log_likelihood']:
                best_trial_id = tid
                print(
                    "Better validation log-likelihood achieved, current best trial: %d"
                    % best_trial_id)
                shutil.copy(results[best_trial_id]['ckpt_path'],
                            overall_best_ckpt_path)

    print("Loading best model (trial_id = %d)" % best_trial_id)

    best_ckpt_path = results[best_trial_id]['ckpt_path']
    shutil.copy(best_ckpt_path, overall_best_ckpt_path)

    best_model = CVAE(image_height=image_height,
                      image_width=image_width,
                      n_channels=n_channels,
                      n_hidden_units=n_u)
    best_model.full_model.load_weights(overall_best_ckpt_path)

    best_results = dict()
    best_results['train'] = best_model.evaluate(xtr, ytr_deg, 'train')
    best_results['validation'] = best_model.evaluate(xval, yval_deg,
                                                     'validation')
    best_results['test'] = best_model.evaluate(xte, yte_deg, 'test')

    results['best'] = best_results

    results_yml_file = os.path.join(experiment_dir, 'results.yml')
    with open(results_yml_file, 'w') as results_yml_file:
        yaml.dump(results, results_yml_file, default_flow_style=False)
コード例 #4
0
def train():

    config_path = sys.argv[1]

    with open(config_path, 'r') as f:
        config = yaml.load(f)
    root_log_dir = config['root_log_dir']
    data_path = config['data_path']

    experiment_name = '_'.join(
        [config['experiment_name'],
         get_experiment_id()])

    if not os.path.exists(root_log_dir):
        os.mkdir(root_log_dir)
    experiment_dir = os.path.join(root_log_dir, experiment_name)
    os.mkdir(experiment_dir)
    shutil.copy(config_path, experiment_dir)

    (xtr, ptr_rad, ttr_rad, rtr_rad, names_tr), \
    (xval, pval_rad, tval_rad, rval_rad, names_val), \
    (xte, pte_rad, tte_rad, rte_rad, names_te) = load_idiap('data//IDIAP.pkl')

    image_height, image_width = xtr.shape[1], xtr.shape[2]

    net_output = config['net_output']
    if net_output == 'pan':
        ytr_deg = np.rad2deg(ptr_rad)
        yval_deg = np.rad2deg(pval_rad)
        yte_deg = np.rad2deg(pte_rad)
    elif net_output == 'tilt':
        ytr_deg = np.rad2deg(ttr_rad)
        yval_deg = np.rad2deg(tval_rad)
        yte_deg = np.rad2deg(tte_rad)
    elif net_output == 'roll':
        ytr_deg = np.rad2deg(rtr_rad)
        yval_deg = np.rad2deg(rval_rad)
        yte_deg = np.rad2deg(rte_rad)
    else:
        raise ValueError("net_output should be 'pan', 'tilt' or 'roll'")

    if config['loss'] == 'mad':
        print("using mad loss..")
        loss_te = mad_loss_tf
    else:
        raise ValueError("loss could be only 'mad' by now")

    optimizer = get_optimizer(config['optimizer_params'])

    best_trial_id = 0
    n_trials = 5
    results = dict()

    for tid in range(0, n_trials):

        print("TRIAL %d" % tid)
        trial_dir = os.path.join(experiment_dir, str(tid))
        os.mkdir(trial_dir)
        print("logs could be found at %s" % trial_dir)

        vgg_model = vgg.DegreeVGG(image_width=image_width,
                                  image_height=image_height,
                                  n_outputs=1,
                                  n_channels=3)

        vgg_model.model.compile(loss=loss_te, optimizer=optimizer)

        tensorboard_callback = keras.callbacks.TensorBoard(log_dir=trial_dir)

        train_csv_log = os.path.join(trial_dir, 'train.csv')
        csv_callback = keras.callbacks.CSVLogger(train_csv_log,
                                                 separator=',',
                                                 append=False)

        best_model_weights_file = os.path.join(
            trial_dir, 'vgg_bit_' + config['loss'] + '_town.best.weights.h5')

        model_ckpt_callback = keras.callbacks.ModelCheckpoint(
            best_model_weights_file,
            monitor='val_loss',
            mode='min',
            save_best_only=True,
            save_weights_only=True,
            period=1,
            verbose=1)

        vgg_model.model.save_weights(best_model_weights_file)

        vgg_model.model.fit(x=xtr,
                            y=ytr_deg,
                            batch_size=config['batch_size'],
                            epochs=config['n_epochs'],
                            verbose=1,
                            validation_data=(xval, yval_deg),
                            callbacks=[csv_callback, model_ckpt_callback])

        best_model = vgg.DegreeVGG(image_width=image_width,
                                   image_height=image_height,
                                   n_outputs=1,
                                   n_channels=3)

        best_model.model.load_weights(best_model_weights_file)

        trial_results = dict()
        trial_results['ckpt_path'] = best_model_weights_file

        trial_results['train'] = best_model.evaluate(xtr, ytr_deg, 'train')
        trial_results['validation'] = best_model.evaluate(
            xval, yval_deg, 'validation')
        trial_results['test'] = best_model.evaluate(xte, yte_deg, 'test')
        results[tid] = trial_results

        if tid > 0:
            if trial_results['validation']['maad_loss'] > \
                    results[best_trial_id]['validation']['maad_loss']:
                best_trial_id = tid
                print(
                    "Better validation loss achieved, current best trial: %d" %
                    best_trial_id)

    print("evaluating model..")
    best_ckpt_path = results[best_trial_id]['ckpt_path']
    overall_best_ckpt_path = os.path.join(
        experiment_dir, 'vgg.full_model.overall_best.weights.hdf5')
    shutil.copy(best_ckpt_path, overall_best_ckpt_path)

    best_model = vgg.DegreeVGG(image_width=image_width,
                               image_height=image_height,
                               n_outputs=1,
                               n_channels=3)

    best_model.model.load_weights(overall_best_ckpt_path)

    best_results = dict()
    best_results['train'] = best_model.evaluate(xtr, ytr_deg, 'train')
    best_results['validation'] = best_model.evaluate(xval, yval_deg,
                                                     'validation')
    best_results['test'] = best_model.evaluate(xte, yte_deg, 'test')
    results['best'] = best_results

    results_yml_file = os.path.join(experiment_dir, 'results.yml')
    with open(results_yml_file, 'w') as results_yml_file:
        yaml.dump(results, results_yml_file, default_flow_style=False)

    return
コード例 #5
0
def train():

    config_path = sys.argv[1]

    with open(config_path, 'r') as f:
        config = yaml.load(f)
    root_log_dir = config['root_log_dir']
    data_path = config['data_path']

    experiment_name = '_'.join(
        [config['experiment_name'],
         get_experiment_id()])

    if not os.path.exists(root_log_dir):
        os.mkdir(root_log_dir)
    experiment_dir = os.path.join(root_log_dir, experiment_name)
    os.mkdir(experiment_dir)
    shutil.copy(config_path, experiment_dir)

    (xtr, ptr_rad, ttr_rad, rtr_rad, names_tr), \
    (xval, pval_rad, tval_rad, rval_rad, names_val), \
    (xte, pte_rad, tte_rad, rte_rad, names_te) = load_idiap('data//IDIAP.pkl')

    image_height, image_width = xtr.shape[1], xtr.shape[2]

    net_output = config['net_output']

    if net_output == 'pan':
        ytr = rad2bit(ptr_rad)
        yval = rad2bit(pval_rad)
        yte = rad2bit(pte_rad)
        ytr_deg = np.rad2deg(ptr_rad)
        yval_deg = np.rad2deg(pval_rad)
        yte_deg = np.rad2deg(pte_rad)
    elif net_output == 'tilt':
        ytr = rad2bit(ttr_rad)
        yval = rad2bit(tval_rad)
        yte = rad2bit(tte_rad)
        ytr_deg = np.rad2deg(ttr_rad)
        yval_deg = np.rad2deg(tval_rad)
        yte_deg = np.rad2deg(tte_rad)
    elif net_output == 'roll':
        ytr = rad2bit(rtr_rad)
        yval = rad2bit(rval_rad)
        yte = rad2bit(rte_rad)
        ytr_deg = np.rad2deg(rtr_rad)
        yval_deg = np.rad2deg(rval_rad)
        yte_deg = np.rad2deg(rte_rad)
    else:
        raise ValueError("net_output should be 'pan', 'tilt' or 'roll'")

    net_output = config['net_output']

    predict_kappa = config['predict_kappa']
    fixed_kappa_value = config['fixed_kappa_value']

    if config['loss'] == 'cosine':
        print("using cosine loss..")
        loss_te = cosine_loss_tf
    elif config['loss'] == 'von_mises':
        print("using von-mises loss..")
        loss_te = von_mises_loss_tf
    elif config['loss'] == 'mad':
        print("using mad loss..")
        loss_te = mad_loss_tf
    elif config['loss'] == 'vm_likelihood':
        print("using likelihood loss..")
        if predict_kappa:
            loss_te = von_mises_neg_log_likelihood_keras
        else:

            def _von_mises_neg_log_likelihood_keras_fixed(y_true, y_pred):
                mu_pred = y_pred[:, 0:2]
                kappa_pred = tf.ones([tf.shape(y_pred[:, 2:])[0], 1
                                      ]) * fixed_kappa_value
                return -K.mean(
                    von_mises_log_likelihood_tf(y_true, mu_pred, kappa_pred))

            loss_te = _von_mises_neg_log_likelihood_keras_fixed
    else:
        raise ValueError(
            "loss should be 'mad','cosine','von_mises' or 'vm_likelihood'")

    best_trial_id = 0
    n_trials = config['n_trials']
    batch_size = config['batch_size']
    learning_rate = config['optimizer_params']['learning_rate']

    results = dict()

    for tid in range(0, n_trials):

        print("TRIAL %d // %d" % (tid, n_trials))
        print("batch_size: %d" % batch_size)
        print("learning_rate: %f" % learning_rate)

        trial_dir = os.path.join(experiment_dir, str(tid))
        os.mkdir(trial_dir)
        print("logs could be found at %s" % trial_dir)

        vgg_model = vgg.BiternionVGG(image_height=image_height,
                                     image_width=image_width,
                                     n_channels=3,
                                     predict_kappa=predict_kappa,
                                     fixed_kappa_value=fixed_kappa_value)

        optimizer = keras.optimizers.Adam(epsilon=1.0e-07,
                                          lr=learning_rate,
                                          decay=0.0)

        vgg_model.model.compile(loss=loss_te, optimizer=optimizer)

        tensorboard_callback = keras.callbacks.TensorBoard(log_dir=trial_dir)

        train_csv_log = os.path.join(trial_dir, 'train.csv')
        csv_callback = keras.callbacks.CSVLogger(train_csv_log,
                                                 separator=',',
                                                 append=False)

        best_model_weights_file = os.path.join(
            trial_dir, 'vgg_bit_' + config['loss'] + '_town.best.weights.h5')

        model_ckpt_callback = keras.callbacks.ModelCheckpoint(
            best_model_weights_file,
            monitor='val_loss',
            mode='min',
            save_best_only=True,
            save_weights_only=True,
            period=1,
            verbose=1)

        vgg_model.model.save_weights(best_model_weights_file)

        vgg_model.model.fit(x=xtr,
                            y=ytr,
                            batch_size=batch_size,
                            epochs=config['n_epochs'],
                            verbose=1,
                            validation_data=(xval, yval),
                            callbacks=[
                                tensorboard_callback, csv_callback,
                                model_ckpt_callback
                            ])

        best_model = vgg.BiternionVGG(image_height=image_height,
                                      image_width=image_width,
                                      n_channels=3,
                                      predict_kappa=predict_kappa,
                                      fixed_kappa_value=fixed_kappa_value)

        best_model.model.load_weights(best_model_weights_file)

        trial_results = dict()
        trial_results['learning_rate'] = float(learning_rate)
        trial_results['batch_size'] = float(batch_size)
        trial_results['ckpt_path'] = best_model_weights_file
        trial_results['train'] = best_model.evaluate(xtr, ytr_deg, 'train')
        trial_results['validation'] = best_model.evaluate(
            xval, yval_deg, 'validation')
        trial_results['test'] = best_model.evaluate(xte, yte_deg, 'test')
        results[tid] = trial_results

        if tid > 0:
            if trial_results['validation']['log_likelihood_mean'] > \
                    results[best_trial_id]['validation']['log_likelihood_mean']:
                best_trial_id = tid
                print(
                    "Better validation loss achieved, current best trial: %d" %
                    best_trial_id)

    print("loading best model..")
    best_ckpt_path = results[best_trial_id]['ckpt_path']
    overall_best_ckpt_path = os.path.join(
        experiment_dir, 'vgg.full_model.overall_best.weights.hdf5')
    shutil.copy(best_ckpt_path, overall_best_ckpt_path)

    print("finetuning kappa values..")
    best_model = vgg.BiternionVGG(image_height=image_height,
                                  image_width=image_width,
                                  n_channels=3,
                                  predict_kappa=predict_kappa,
                                  fixed_kappa_value=fixed_kappa_value)

    best_model.model.load_weights(overall_best_ckpt_path)
    best_kappa = fixed_kappa_value

    if not predict_kappa:
        best_kappa = finetune_kappa(xval, yval, best_model)
        print("best kappa: %f" % best_kappa)

    best_model = vgg.BiternionVGG(image_height=image_height,
                                  image_width=image_width,
                                  n_channels=3,
                                  predict_kappa=predict_kappa,
                                  fixed_kappa_value=best_kappa)

    best_model.model.load_weights(overall_best_ckpt_path)

    best_results = dict()
    best_results['learning_rate'] = results[best_trial_id]['learning_rate']
    best_results['batch_size'] = results[best_trial_id]['batch_size']

    print("evaluating best model..")
    best_results['train'] = best_model.evaluate(xtr, ytr_deg, 'train')
    best_results['validation'] = best_model.evaluate(xval, yval_deg,
                                                     'validation')
    best_results['test'] = best_model.evaluate(xte, yte_deg, 'test')
    results['best'] = best_results

    results_yml_file = os.path.join(experiment_dir, 'results.yml')
    with open(results_yml_file, 'w') as results_yml_file:
        yaml.dump(results, results_yml_file, default_flow_style=False)

    return
コード例 #6
0
def main():

    if len(sys.argv) != 2:
        print(
            "Invalid number of params! Usage: python train_cvae.py config_path"
        )

    config_path = sys.argv[1]

    config = load_config(config_path)

    root_log_dir = config['root_log_dir']

    model_type = config['model_type']

    if not os.path.exists(root_log_dir):
        os.makedirs(root_log_dir, exist_ok=True)

    experiment_name = '_'.join(
        [config['experiment_name'],
         get_experiment_id()])

    experiment_dir = os.path.join(root_log_dir, experiment_name)

    os.mkdir(experiment_dir)

    (xtr, ytr_bit,
     ytr_deg), (xval, yval_bit,
                yval_deg), (xte, yte_bit,
                            yte_deg) = load_dataset(config['dataset'],
                                                    config['data_path'],
                                                    config['net_output'])
    eval_data = [xtr, ytr_deg, xval, yval_deg, xte, yte_deg]

    image_height, image_width, n_channels = xtr.shape[1], xtr.shape[
        2], xtr.shape[3]

    n_trials = config['n_trials']
    best_trial_id = 0

    results = dict()

    if config['random_hyp_search']:
        hyp_params = generate_hyper_params(n_trials, model_type)
    else:
        hyp_params = load_hyper_params(config)

    checkpoints = dict()
    results_csv = os.path.join(experiment_dir, 'results.csv')
    global_results_csv = os.path.join(root_log_dir, 'global_results.csv')

    for tid in range(0, n_trials):

        trial_dir = os.path.join(experiment_dir, str(tid))
        os.mkdir(trial_dir)
        print("logs could be found at %s" % trial_dir)

        print_hyp_params(hyp_params, tid)

        trial_hyp_params = get_trial_hyp_params(hyp_params, tid)

        trial_best_ckpt_path = os.path.join(trial_dir,
                                            'model.best.weights.hdf5')
        trial_hyp_params['ckpt_path'] = trial_best_ckpt_path
        trial_hyp_params['hyp_yaml_path'] = os.path.join(
            trial_dir, 'model.best.params.yml')

        keras_callbacks = define_callbacks(config=config,
                                           trial_dir=trial_dir,
                                           ckpt_path=trial_best_ckpt_path,
                                           val_data=[xval, yval_bit])

        model = define_model(model_type=model_type,
                             model_hyp_params=trial_hyp_params,
                             config=config,
                             image_height=image_height,
                             image_width=image_width)

        model.save_weights(trial_best_ckpt_path)

        model.fit([xtr, ytr_bit], [xval, yval_bit],
                  batch_size=trial_hyp_params['batch_size'],
                  n_epochs=config['n_epochs'],
                  callbacks=keras_callbacks)

        if model_type == 'bivgg':
            if not model.predict_kappa:
                trial_hyp_params['best_kappa'] = model.fixed_kappa_value

        save_dict(trial_hyp_params, trial_hyp_params['hyp_yaml_path'])

        model.load_weights(trial_best_ckpt_path)

        print("\n evaluating model # %d" % tid)
        trial_results = evaluate_model(model, eval_data)

        results[tid] = trial_results
        checkpoints[tid] = trial_best_ckpt_path

        trial_df = results_to_df(trial_results, trial_hyp_params)

        if tid == 0:
            res_df = trial_df
        else:
            res_df = res_df.append(trial_df).reset_index(drop=True)

        res_df.to_csv(results_csv, sep=';')
        save_global_results(trial_df, global_results_csv)

        if tid > 0:
            if model_type == 'cvae':

                if trial_results['validation']['elbo'] > results[
                        best_trial_id]['validation']['elbo']:
                    best_trial_id = tid
                    print(
                        "Better validation loss achieved, current best trial: %d"
                        % best_trial_id)

            else:

                if trial_results['validation']['log_likelihood_mean'] > \
                        results[best_trial_id]['validation']['log_likelihood_mean']:
                    best_trial_id = tid
                    print(
                        "Better validation loss achieved, current best trial: %d"
                        % best_trial_id)

    print("Loading best model (trial_id = %d)" % best_trial_id)

    best_ckpt_path = checkpoints[best_trial_id]
    overall_best_ckpt_path = os.path.join(experiment_dir,
                                          'model.overall_best.weights.hdf5')
    shutil.copy(best_ckpt_path, overall_best_ckpt_path)
    best_hyp_params = get_trial_hyp_params(hyp_params, best_trial_id)

    best_model = define_model(model_type=model_type,
                              model_hyp_params=best_hyp_params,
                              config=config,
                              image_height=image_height,
                              image_width=image_width)

    best_model.load_weights(overall_best_ckpt_path)

    print("Evaluating best model..")
    _ = evaluate_model(best_model, eval_data)

    return
コード例 #7
0
def main():

    if len(sys.argv) != 2:
        print(
            "Invalid number of params! Usage: python train_vgg.py config_path")

    config_path = sys.argv[1]

    config = load_config(config_path)

    root_log_dir = config['root_log_dir']

    if not os.path.exists(root_log_dir):
        os.makedirs(root_log_dir, exist_ok=True)

    experiment_name = '_'.join(
        [config['experiment_name'],
         get_experiment_id()])

    experiment_dir = os.path.join(root_log_dir, experiment_name)

    os.mkdir(experiment_dir)

    (xtr, ytr_bit, ytr_deg), (xval, yval_bit,
                              yval_deg), (xte, yte_bit,
                                          yte_deg) = load_dataset(config)

    image_height, image_width, n_channels = xtr.shape[1], xtr.shape[
        2], xtr.shape[3]

    n_trials = config['n_trials']
    best_trial_id = 0

    results = dict()

    image_height, image_width, n_channels = xtr.shape[1:]

    best_trial_id = 0

    results = dict()

    n_epochs = config['n_epochs']
    n_trials = config['n_trials']
    batch_sizes = config['batch_sizes']
    learning_rates = config['learning_rates']
    n_components_lst = config['n_components']
    params_grid = list(
        itertools.product(learning_rates, batch_sizes,
                          n_components_lst)) * n_trials

    res_cols = [
        'trial_id', 'batch_size', 'learning_rate', 'n_components', 'val_maad',
        'val_likelihood', 'test_maad', 'test_likelihood'
    ]

    results_df = pd.DataFrame(columns=res_cols)
    results_csv = os.path.join(experiment_dir, 'results.csv')

    for tid, params in enumerate(params_grid):

        learning_rate = params[0]
        batch_size = params[1]
        n_components = params[2]

        print("TRIAL %d // %d" % (tid, len(params_grid)))
        print("batch_size: %d" % batch_size)
        print("learning_rate: %f" % learning_rate)
        print("n_components: %f" % n_components)

        trial_dir = os.path.join(experiment_dir, str(tid))
        os.mkdir(trial_dir)

        vmmix_best_ckpt_path = os.path.join(
            trial_dir, 'vmmix.full_model.trial_%d.best.weights.hdf5' % tid)

        tensorboard_callback = keras.callbacks.TensorBoard(log_dir=trial_dir)

        train_csv_log = os.path.join(trial_dir, 'train.csv')
        csv_callback = keras.callbacks.CSVLogger(train_csv_log,
                                                 separator=',',
                                                 append=False)

        model_ckpt_callback = keras.callbacks.ModelCheckpoint(
            vmmix_best_ckpt_path,
            monitor='val_loss',
            mode='min',
            save_best_only=True,
            save_weights_only=True,
            period=1,
            verbose=1)

        vggmix_model = BiternionVGGMixture(image_height=image_height,
                                           image_width=image_width,
                                           n_channels=n_channels,
                                           n_components=n_components,
                                           learning_rate=learning_rate)

        vggmix_model.model.save_weights(vmmix_best_ckpt_path)

        vggmix_model.model.fit(xtr,
                               ytr_bit,
                               batch_size=batch_size,
                               epochs=n_epochs,
                               validation_data=(xval, yval_bit),
                               callbacks=[
                                   tensorboard_callback, csv_callback,
                                   model_ckpt_callback
                               ])

        best_model = BiternionVGGMixture(image_height=image_height,
                                         image_width=image_width,
                                         n_channels=n_channels,
                                         n_components=n_components)

        best_model.model.load_weights(vmmix_best_ckpt_path)

        trial_results = dict()
        trial_results['ckpt_path'] = vmmix_best_ckpt_path
        trial_results['train'] = best_model.evaluate(xtr, ytr_deg, 'train')
        trial_results['validation'] = best_model.evaluate(
            xval, yval_deg, 'validation')
        trial_results['test'] = best_model.evaluate(xte, yte_deg, 'test')
        results[tid] = trial_results

        results_np = np.asarray([
            tid, batch_size, learning_rate, n_components,
            trial_results['validation']['maad_loss'],
            trial_results['validation']['log_likelihood_mean'],
            trial_results['test']['maad_loss'],
            trial_results['test']['log_likelihood_mean']
        ]).reshape([1, 8])

        trial_res_df = pd.DataFrame(results_np, columns=res_cols)
        results_df = results_df.append(trial_res_df)
        results_df.to_csv(results_csv)

        if tid > 0:
            if trial_results['validation']['log_likelihood_mean'] > \
                    results[best_trial_id]['validation']['log_likelihood_mean']:
                best_trial_id = tid
                print(
                    "Better validation loss achieved, current best trial: %d" %
                    best_trial_id)

    print("Loading best model (trial_id = %d)" % best_trial_id)

    best_ckpt_path = results[best_trial_id]['ckpt_path']
    overall_best_ckpt_path = os.path.join(
        experiment_dir, 'vmmix.full_model.overall_best.weights.hdf5')
    shutil.copy(best_ckpt_path, overall_best_ckpt_path)

    best_model_n_components = params_grid[best_trial_id][2]

    best_model = BiternionVGGMixture(image_height=image_height,
                                     image_width=image_width,
                                     n_channels=n_channels,
                                     n_components=best_model_n_components)
    best_model.model.load_weights(overall_best_ckpt_path)

    best_results = dict()
    best_results['train'] = best_model.evaluate(xtr, ytr_deg, 'train')
    best_results['validation'] = best_model.evaluate(xval, yval_deg,
                                                     'validation')
    best_results['test'] = best_model.evaluate(xte, yte_deg, 'test')

    results['best'] = best_results

    results_yml_file = os.path.join(experiment_dir, 'results.yml')
    with open(results_yml_file, 'w') as results_yml_file:
        yaml.dump(results, results_yml_file, default_flow_style=False)

    results_df.to_csv(results_csv)
コード例 #8
0
def main():

    exp_id = get_experiment_id()

    root_log_dir = 'logs/vmmix/'
    if not os.path.exists(root_log_dir):
        os.mkdir(root_log_dir)

    experiment_dir = os.path.join(root_log_dir, exp_id)
    os.mkdir(experiment_dir)

    xtr, ytr_deg, xval, yval_deg, xte, yte_deg = load_towncentre(
        'data/TownCentre.pkl.gz', canonical_split=True, verbose=1)

    # xtr, ytr_deg = aug_data(xtr, ytr_deg)
    # xval, yval_deg = aug_data(xval, yval_deg)
    # xte, yte_deg = aug_data(xval, yval_deg)

    ytr_bit = deg2bit(ytr_deg)
    yval_bit = deg2bit(yval_deg)
    yte_bit = deg2bit(yte_deg)

    image_height, image_width, n_channels = xtr.shape[1:]
    phi_shape = yte_bit.shape[1]

    best_trial_id = 0
    n_trials = 5
    results = dict()

    n_epochs = 100
    batch_size = 32
    n_components = 5

    for tid in range(0, n_trials):

        print("TRIAL %d" % tid)
        trial_dir = os.path.join(experiment_dir, str(tid))
        os.mkdir(trial_dir)

        vmmix_best_ckpt_path = os.path.join(
            trial_dir, 'vmmix.full_model.trial_%d.best.weights.hdf5' % tid)

        tensorboard_callback = keras.callbacks.TensorBoard(log_dir=trial_dir)

        train_csv_log = os.path.join(trial_dir, 'train.csv')
        csv_callback = keras.callbacks.CSVLogger(train_csv_log,
                                                 separator=',',
                                                 append=False)

        model_ckpt_callback = keras.callbacks.ModelCheckpoint(
            vmmix_best_ckpt_path,
            monitor='val_loss',
            mode='min',
            save_best_only=True,
            save_weights_only=True,
            period=1,
            verbose=1)

        vggmix_model = BiternionVGGMixture(image_height=image_height,
                                           image_width=image_width,
                                           n_channels=n_channels,
                                           n_components=n_components)

        vggmix_model.model.fit(xtr,
                               ytr_bit,
                               batch_size=batch_size,
                               epochs=n_epochs,
                               validation_data=(xval, yval_bit),
                               callbacks=[
                                   tensorboard_callback, csv_callback,
                                   model_ckpt_callback
                               ])

        best_model = BiternionVGGMixture(image_height=image_height,
                                         image_width=image_width,
                                         n_channels=n_channels,
                                         n_components=n_components)

        best_model.model.load_weights(vmmix_best_ckpt_path)

        trial_results = dict()
        trial_results['ckpt_path'] = vmmix_best_ckpt_path
        trial_results['train'] = best_model.evaluate(xtr, ytr_deg, 'train')
        trial_results['validation'] = best_model.evaluate(
            xval, yval_deg, 'validation')
        trial_results['test'] = best_model.evaluate(xte, yte_deg, 'test')
        results[tid] = trial_results

        if tid > 0:
            if trial_results['validation']['log_likelihood_mean'] > \
                    results[best_trial_id]['validation']['log_likelihood_mean']:
                best_trial_id = tid
                print(
                    "Better validation loss achieved, current best trial: %d" %
                    best_trial_id)

    print("Loading best model (trial_id = %d)" % best_trial_id)

    best_ckpt_path = results[best_trial_id]['ckpt_path']
    overall_best_ckpt_path = os.path.join(
        experiment_dir, 'vmmix.full_model.overall_best.weights.hdf5')
    shutil.copy(best_ckpt_path, overall_best_ckpt_path)

    best_model = BiternionVGGMixture(image_height=image_height,
                                     image_width=image_width,
                                     n_channels=n_channels,
                                     n_components=n_components)
    best_model.model.load_weights(overall_best_ckpt_path)

    best_results = dict()
    best_results['train'] = best_model.evaluate(xtr, ytr_deg, 'train')
    best_results['validation'] = best_model.evaluate(xval, yval_deg,
                                                     'validation')
    best_results['test'] = best_model.evaluate(xte, yte_deg, 'test')

    results['best'] = best_results

    results_yml_file = os.path.join(experiment_dir, 'results.yml')
    with open(results_yml_file, 'w') as results_yml_file:
        yaml.dump(results, results_yml_file, default_flow_style=False)