Beispiel #1
0
def weight_decay():

    with open('./experiment_configs/10_20_config_search.yaml',
              'r+') as f_search:
        config = yaml.load(f_search)

        config['use_growl'] = True
        config['growl_params'] = [[1e-20, 1e-20], [3e-1, 3e-3]]
        config['use_group_lasso'] = False
        config['use_wd'] = True
        config['log_dir'] = log_dir
        lr = 1e-3
        config['learning_rate'] = lr
        # for baseline
        config['num_epochs'] = 300
        config['retraining'] = True

        wd = [0.01] * 5
        for i, wd_i in enumerate(wd):
            config['wd'] = wd[i]
            config['res_dir'] = config[
                'log_dir'] + '/weight_decay_{}_p_{}_idx_{}'.format(
                    wd_i, config['preference'], i)
            config['plot_dir'] = config['res_dir'] + '/plot_res'
            config['summary_dir'] = config['res_dir'] + '/summary_res'
            train_mnist(config)
Beispiel #2
0
def grlasso_l2():

    with open('./experiment_configs/10_20_config_search.yaml',
              'r+') as f_search:
        config = yaml.load(f_search)

        config['use_growl'] = False
        config['use_group_lasso'] = True
        config['use_wd'] = True

        lr = 1e-3
        config['learning_rate'] = lr

        config['log_dir'] = log_dir

        lambda1 = [80] * 5
        lambda_wd = [0.001]
        preference = [0.6]

        for p in preference:
            config['preference'] = p
            for wd in lambda_wd:
                for idx1, lambda1_i in enumerate(lambda1):
                    config['growl_params'] = [[lambda1_i, 0]] * NUM_LAYER
                    config['wd'] = wd
                    config['res_dir'] = config[
                        'log_dir'] + '/grEN_l1_{}_l_wd_{}_p_{}_idx_{}'.format(
                            lambda1_i, wd, config['preference'], idx1)
                    config['plot_dir'] = config['res_dir'] + '/plot_res'
                    config['summary_dir'] = config['res_dir'] + '/summary_res'
                    train_mnist(config)
Beispiel #3
0
def PLD():

    with open('./experiment_configs/10_20_config_search.yaml',
              'r+') as f_search:
        config = yaml.load(f_search)

        config['use_growl'] = True
        config['use_group_lasso'] = False
        config['use_wd'] = False
        config['reg_params_type'] = 'PLD'

        PLD_transition = 0.5
        preference = 0.6
        lr = 1e-3
        config['learning_rate'] = lr
        config['log_dir'] = log_dir

        lambda1 = 87

        lambda1 = [12] * 5
        lambda2 = [0.215]
        lambda_wd = [0]

        for idx1, lambda1_i in enumerate(lambda1):
            for lambda2_i in lambda2:
                for wd in lambda_wd:
                    config['preference'] = preference
                    config['wd'] = wd
                    config['growl_params'] = [[lambda1_i, lambda2_i]
                                              ] * NUM_LAYER
                    config['PLD_transition'] = PLD_transition
                    config['res_dir'] = config[
                        'log_dir'] + '/PLDwd_lr_{}_l1_{}_l2_{}_PLDt_{}_p_{}_wd_{}_idx_{}'.format(
                            lr, lambda1_i, lambda2_i, PLD_transition,
                            config['preference'], wd, idx1)
                    config['plot_dir'] = config['res_dir'] + '/plot_res'
                    config['summary_dir'] = config['res_dir'] + '/summary_res'
                    train_mnist(config)
Beispiel #4
0
def group_lasso():

    with open('./experiment_configs/10_20_config_search.yaml',
              'r+') as f_search:
        config = yaml.load(f_search)

        config['use_growl'] = False
        config['use_group_lasso'] = True
        config['use_wd'] = False
        lr = 1e-3
        config['learning_rate'] = lr
        config['log_dir'] = log_dir

        lambda1 = [60] * 5

        for idx1, lambda1_i in enumerate(lambda1):
            config['growl_params'] = [[lambda1_i, 0]] * NUM_LAYER
            config['res_dir'] = config[
                'log_dir'] + '/group_lasso_lr_{}_l1_p_{}_{}_idx1_{}'.format(
                    lr, lambda1_i, config['preference'], idx1)
            config['plot_dir'] = config['res_dir'] + '/plot_res'
            config['summary_dir'] = config['res_dir'] + '/summary_res'
            train_mnist(config)
Beispiel #5
0
def baseline():

    with open('./experiment_configs/10_20_config_search.yaml',
              'r+') as f_search:
        config = yaml.load(f_search)

        config['use_growl'] = False
        config['growl_params'] = [[1e-20, 1e-20], [3e-1, 3e-3]]
        config['use_group_lasso'] = False
        config['use_wd'] = False
        config['log_dir'] = log_dir
        lr = 1e-3
        config['learning_rate'] = lr
        # for baseline
        config['num_epochs'] = 400
        config['retraining'] = False

        for i in range(5):
            config['res_dir'] = config['log_dir'] + '/baseline_idx_{}'.format(
                i)
            config['plot_dir'] = config['res_dir'] + '/plot_res'
            config['summary_dir'] = config['res_dir'] + '/summary_res'
            train_mnist(config)
def train_and_test():
    # load intact line drawings
    intact_dataset = KFoldDataset('line_drawings')
    X_intact, Y_intact, class_indices = intact_dataset.get_data(
        IMG_SIZE, 'grayscale')

    # load dR weighted grayscale drawings
    #  weighted_dataset = KFoldDataset('toronto_dR_weighted')
    #  X_weighted,Y_weighted,class_indices = weighted_dataset.get_data(IMG_SIZE, 'grayscale')
    symmetric_dataset = KFoldDataset('dR_symmetric')
    X_sym, Y_sym, class_indices = symmetric_dataset.get_data(
        IMG_SIZE, 'grayscale')
    asymmetric_dataset = KFoldDataset('dR_asymmetric')
    X_asym, Y_asym, class_indices = asymmetric_dataset.get_data(
        IMG_SIZE, 'grayscale')

    # init 5-fold cross validation
    kfold = StratifiedKFold(n_splits=5, shuffle=True)

    # combine datasets into single 2-channel set
    X = np.ndarray(shape=(X_intact.shape[0:3] + (2, )))

    # put each dataset in respective channels
    X[:, :, :, 0] = X_intact.squeeze()
    #  X[:,:,:,1] = X_weighted.squeeze()
    #  X[:,:,:,1] = X_sym.squeeze()
    X[:, :, :, 1] = X_asym.squeeze()

    # convert ground-truth one-hot encodings to class labels
    labels = [np.argmax(y) for y in Y_intact]

    two_channel_scores = [None] * 5
    nb_classes = 6
    i = 0
    for train_idx, test_idx in kfold.split(X, labels):
        print('fold ' + str(i + 1) + ' of 5')

        input_shape = X.shape[1:]
        model = compile_mnist(input_shape, nb_classes)
        model = train_mnist(model, X[train_idx], Y_intact[train_idx],
                            X[test_idx], Y_intact[test_idx])
        score = model.evaluate(X[test_idx], Y_intact[test_idx])
        two_channel_scores[i] = score[1]

        K.clear_session()
        i += 1

    print(two_channel_scores)
    print(np.mean(two_channel_scores))
Beispiel #7
0
def train_and_test():
  # load rgb images
  rgb_dataset = KFoldDataset('toronto_rgb')
  X_rgb,Y_rgb,class_indices = rgb_dataset.get_data(IMG_SIZE, 'rgb')

  # load intact line drawings
  intact_dataset = KFoldDataset('toronto_line_drawings')
  X_intact,Y_intact,class_indices = intact_dataset.get_data(IMG_SIZE, COLOR_MODE)

  # load symmetric splits
  sym_dataset = KFoldDataset('toronto_dR_symmetric')
  X_sym,Y_sym,class_indices = sym_dataset.get_data(IMG_SIZE, COLOR_MODE)
  # load asymmetric splits
  asym_dataset = KFoldDataset('toronto_dR_asymmetric')
  X_asym,Y_asym,class_indices = asym_dataset.get_data(IMG_SIZE, COLOR_MODE)

  # init 5-fold cross validation
  kfold = StratifiedKFold(n_splits=5, shuffle=True)

  # combine datasets into single 3-channel set
  # new array to hold combined images
  X = np.ndarray(shape=(X_intact.shape[0:3] + (3,)))

  # put each dataset in respective channels
  X[:,:,:,0] = X_intact.squeeze()
  X[:,:,:,1] = X_sym.squeeze()
  X[:,:,:,2] = X_asym.squeeze()

  # convert ground-truth one-hot encodings to class labels
  labels = [np.argmax(y) for y in Y_intact]

  # train for each fold
  rgb_scores = [None] * 5
  intact_scores = [None] * 5
  three_channel_scores = [None] * 5
  nb_classes = 6
  i=0
  for train_idx, test_idx in kfold.split(X, labels):
    print('fold ' + str(i+1) + ' of 5')

    # train on rgb images ==================
    input_shape = X_rgb.shape[1:]
    model = compile_mnist(input_shape, nb_classes)
    model = train_mnist(model,
      X_rgb[train_idx], Y_rgb[train_idx],
      X_rgb[test_idx], Y_rgb[test_idx]
    )
    score = model.evaluate(X_rgb[test_idx], Y_rgb[test_idx])
    rgb_scores[i] = score[1]

    # Train on just intact images =====
    input_shape = X_intact.shape[1:]
    model = compile_mnist(input_shape, nb_classes)
    model = train_mnist(model,
      X_intact[train_idx], Y_intact[train_idx],
      X_intact[test_idx], Y_intact[test_idx]
    )
    score = model.evaluate(X_intact[test_idx], Y_intact[test_idx])
    intact_scores[i] = score[1]

    # Train on 3-channel combo images ======
    # load model
    input_shape = X.shape[1:]
    model = compile_mnist(input_shape, nb_classes)

    # train model
    model = train_mnist(model,
      X[train_idx], Y_intact[train_idx],
      X[test_idx], Y_intact[test_idx]
    )

    # get final evaluation
    score = model.evaluate(X[test_idx], Y_intact[test_idx])
    # append only accuracy
    three_channel_scores[i] = score[1]

    # clear tensorflow memory
    K.clear_session()

    i+=1

  # print final score
  print(rgb_scores)
  print(np.mean(rgb_scores))
  print(intact_scores)
  print(np.mean(intact_scores))
  print(three_channel_scores)
  print(np.mean(three_channel_scores))