Exemplo n.º 1
0
def main():
    # choose dataset
    #  dataset_str = 'line_drawings'
    dataset_str = 'skeletons'

    dataset = KFoldDataset(dataset_str)

    # standards
    img_size = (224, 224)
    color_mode = 'grayscale'
    input_shape = (224, 224, 1)
    #  color_mode = 'rgb'
    #  input_shape = (224, 224, 3)
    rescale = 1

    # load data as Numpy arrays
    X, Y = dataset.get_data(img_size, color_mode, rescale)

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

    # get labels from one hot encodings
    labels = [np.argmax(y) for y in Y]

    # train model on first fold
    train_idx, test_idx = next(kfold.split(X, labels))

    model = compile_mnist(input_shape, dataset.nb_classes)

    model = train_mnist(model, X[train_idx], Y[train_idx], X[test_idx],
                        Y[test_idx])
Exemplo n.º 2
0
def main():
    # CHOOSE DATASET HERE
    dataset_str = 'skeletons'

    dataset = KFoldDataset(dataset_str)

    train_and_test(dataset)
Exemplo n.º 3
0
def main():
  # CHOOSE 3 datasets
  #  dataset_strs = ['line_drawings', 'dR_symmetric', 'dR_asymmetric']
  #  dataset_strs = ['line_drawings', 'dR_weighted', 'd2R_weighted']
  #  dataset_strs = ['line_drawings', 'dR_weighted', 'dR_weighted']
  #  dataset_strs = ['line_drawings', 'line_drawings', 'dR_weighted']
  #  dataset_strs = ['line_drawings', 'd2R_weighted', 'd2R_weighted']
  #  dataset_strs = ['line_drawings', 'line_drawings', 'd2R_weighted']
  #  dataset_strs = ['line_drawings', 'd2R_weighted', 'dR_weighted']
  #  dataset_strs = ['dR_weighted', 'line_drawings', 'd2R_weighted']
  #  dataset_strs = ['dR_weighted', 'd2R_weighted', 'line_drawings']
  #  dataset_strs = ['d2R_weighted', 'line_drawings', 'dR_weighted']
  #  dataset_strs = ['d2R_weighted', 'dR_weighted', 'line_drawings']

  # controls
  #  dataset_strs = ['line_drawings', 'max_R', 'max_R']
  #  dataset_strs = ['line_drawings', 'min_R', 'min_R']
  #  dataset_strs = ['line_drawings', 'min_R', 'max_R']
  #  dataset_strs = ['line_drawings', 'dollar_weighted', 'dollar_weighted']

  #  dataset_strs = ['line_drawings', 'ribbon', 'separation']
  dataset_strs = ['line_drawings', 'taper', 'separation']


  datasets = [KFoldDataset(s) for s in dataset_strs]

  train_and_test(datasets)
Exemplo n.º 4
0
def main():
  # choose dataset
  #  dataset_str = 'rgb'
  dataset_str = 'line_drawings'

  dataset = KFoldDataset(dataset_str)

  train_and_visualize(dataset)
Exemplo n.º 5
0
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))
Exemplo n.º 6
0
def main():
    # CHOOSE DATASET
    # either a single dataset or a set of 3
    #  dataset_strs = ['rgb']
    dataset_strs = ['line_drawings']
    #  dataset_strs = ['line_drawings', 'dR_symmetric', 'dR_asymmetric']

    datasets = [KFoldDataset(s) for s in dataset_strs]

    train_and_test(datasets)
Exemplo n.º 7
0
def main():
  # CHOOSE DATASETS HERE
  # the 1st will be used for training,
  #  dataset_strs = ['line_drawings','dR_symmetric', 'dR_asymmetric']
  #  dataset_strs = ['ribbon']
  #  dataset_strs = ['separation']
  dataset_strs = ['taper']

  datasets = [KFoldDataset(s) for s in dataset_strs]

  train_and_test(datasets)
Exemplo n.º 8
0
def main():
    # CHOOSE DATASETS HERE
    # this should be a list of strings of the form
    # ['train_dataset', 'test_dataset_1', 'test_dataset_2', ...]
    # A model will be trained on the first dataset
    # (`train_dataset` here),
    # the trained model is then tested on each included `test_dataset`
    # the model will by default always be tested on the `train_dataset`
    dataset_strs = ['line_drawings']

    # convert from strings to KFoldDataset objects
    datasets = [KFoldDataset(s) for s in dataset_strs]

    train_and_test(datasets)
Exemplo n.º 9
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))