def gen_pca(pca_d=None): if pca_d is None: questions = [ inquirer.Text('pca_d', message='Number of principal components', validate=lambda _, x: 1 <= int(x) <= int(784)) ] pca_d = int(inquirer.prompt(questions)['pca_d']) print('Loading original data...') _, load_data = choose_dataset('emnist') train_data, valid_data, test_data = load_data('tf/') print(pca_d) pca = PCA(n_components=pca_d) # Fit to validation data only print('Fitting to validation data...') pca.fit(valid_data.images) train_images = pca.transform(train_data.images) valid_images = pca.transform(valid_data.images) test_images = pca.transform(test_data.images) print('Writing PCA transformed data to disk...') save_pickled_files([ 'tf/emnist/train_pca.pkl', 'tf/emnist/validation_pca.pkl', 'tf/emnist/test_pca.pkl' ], [ make_dataset(images, labels) for images, labels in zip([train_images, valid_images, test_images], [train_data.labels, valid_data.labels, test_data.labels]) ])
def main(argv): # Load parameters and data for the chosen dataset. model, INPUT_SIZE, example_shape, load_data \ = choose_dataset(FLAGS.dataset) train_data, _, _ = load_data() with tf.Graph().as_default(): # Build all the ops print("building computation graph...") x = tf.placeholder(tf.float32, example_shape(FLAGS.batch_size)) y = tf.placeholder(tf.float32, shape=(FLAGS.batch_size)) hypothesis = tf.squeeze(model.inference(x)) loss_op = model.loss(hypothesis, y) # pred_op = model.predictions(logits) # Go train_model(model, x, y, loss_op, train_data.images, train_data.labels)
def main(argv): model, IMAGE_SIZE, example_shape, load_data \ = choose_dataset(FLAGS.dataset) train_data, validation_data, test_data = load_data() images = test_data.images if FLAGS.set == 0 else \ validation_data.images if FLAGS.set == 1 else \ train_data.images labels = test_data.labels if FLAGS.set == 0 else \ validation_data.labels if FLAGS.set == 1 else \ train_data.labels with tf.Graph().as_default(): print("building computation graph...") x, y = init_placeholders(FLAGS.batch_size, example_shape(FLAGS.batch_size)) logits = model.inference(x) # pred_op = model.predictions(logits) sess = init_session() model.load_weights(sess, FLAGS.model_dir) # evaluate(sess, x, y, pred_op, images, labels, FLAGS.batch_size) compute_logits(sess, x, y, logits, images, FLAGS.batch_size)
def train_or_eval(do_train=True, weights_type_bits=None, hidden_sizes=None, use_pca=None, use_old_pca=None, pca_d=None): weights_type, bits = ask_weight_info() if weights_type_bits is None \ else weights_type_bits hidden_sizes = ask_layer_info() if hidden_sizes is None else hidden_sizes use_pca = ask_use_pca() if use_pca is None else use_pca # TODO: need to ask for the number of hidden layers and their # sizes. When compiling / evaluating, we should be able to get # that information by looking at the weights, but that might mess # with our current method of determining whether the weights are # quantized or not. An easy fix is probably to actually check for # shift/scale values (maybe by looking at the shape of where they # should be) instead of only looking at the length of the weights # tuple. if use_pca: # Check if any PCA data exists. if Path('tf/emnist/test_pca.pkl').is_file(): data = load_pickled('tf/emnist/test_pca.pkl') if use_old_pca: print('Using existing PCA data.') else: if use_old_pca is None: questions = [ inquirer.Confirm( 'old_pca', message= 'Use existing PCA data (%d principal components)?' % data.images.shape[1]) ] if inquirer.prompt(questions)['old_pca']: print('Using existing PCA data.') else: gen_pca(pca_d) else: if use_old_pca: print('Using existing PCA data.') else: gen_pca(pca_d) else: if use_old_pca: print('train_or_eval error: use_old_pca is set but there \ is no existing PCA data') exit(-1) else: gen_pca(pca_d) # Write PCA flag to disk so we can easily check later whether PCA # is being used or not (so we use the appropriate number of # batches during evaluation). save_pickled('pca.pkl', use_pca) # Choose between float and quantized models. model = float_model if weights_type == 'float' else quantized_model # Load parameters and data for the chosen dataset. _, load_data \ = choose_dataset('emnist' + ('_pca' if use_pca else '')) print('Loading data...') train_data, validation_data, test_data = load_data('tf/') # Choose which set to use (train, test, etc.). # We pass 3 for combined train+validation if PCA isn't being used, # otherwise we pass 2 to use only training data. images, labels = choose_images_labels(train_data, validation_data, test_data, 2 if use_pca else 3) example_shape = images.shape[1:] input_size = reduce(mul, example_shape, 1) with tf.Graph().as_default(): # Build all of the ops. print("Initializing...") # Choose between a few different values for learning rate and # stuff depending on the options chosen by the user. learning_rate, max_steps, decay_step, decay_factor = choose_hypers( weights_type, bits) batch_size = 100 x, y, weights, logits, loss_op, pred_op, train_op = build_ops( 100, bits, learning_rate, decay_step, decay_factor, model, input_size, hidden_sizes) # Create session and initialize variables. sess = init_session() # Go. if do_train: print('Training model...') seq = train_model(sess, model, x, y, train_op, loss_op, pred_op, weights, images, labels, batch_size, max_steps, 'tf/models/default/', bits, 1.0, log=False) i = 0 for _, acc in seq: i += 1 render_progress_bar(i, max_steps, (colored('Accuracy', 'yellow') + ': %0.02f%%') \ % (acc * 100.0)) clear_bottom_bar() print('Done training model.') print('Selecting weights with best accuracy (%.02f%%).' % (acc * 100.0)) print(str(acc)) else: print('Evaluating model...') model.load_weights(sess, 'tf/models/default/', num_bits=bits, pca_d=pca_d) acc = evaluate(sess, x, y, pred_op, images, labels, 100, log=False) print('acc: %.02f' % acc)
def main(argv): # Choose between float and quantized models. model = choose_model() if FLAGS.pca == 1 and FLAGS.dataset != 'emnist': print('Error: PCA only supported for EMNIST') exit(-1) # Load parameters and data for the chosen dataset. save_images, load_data = choose_dataset(FLAGS.dataset + ('' if FLAGS.pca == 0 else '_pca')) print('loading data...') train_data, validation_data, test_data = load_data('') # Choose which set to use (train, test, etc.). images, labels = choose_images_labels(train_data, validation_data, test_data, FLAGS.set) # Compute flattened input dimensionality by folding mul over the # shape dimensions. example_shape = images.shape[1:] input_size = reduce(mul, example_shape, 1) hidden_sizes = map(int, FLAGS.layer_sizes.split()) with tf.Graph().as_default(): # Build all of the ops. print("building computation graph...") x, y, weights, logits, loss_op, pred_op, train_op = build_ops( FLAGS.batch_size, FLAGS.bits, FLAGS.learning_rate, FLAGS.decay_step, FLAGS.decay_factor, model, input_size, hidden_sizes) # Create session and initialize variables. sess = init_session() # Go. i = 0 if FLAGS.action.lower() == 'train': seq = train_model(sess, model, x, y, train_op, loss_op, pred_op, weights, images, labels, FLAGS.batch_size, FLAGS.max_steps, FLAGS.model_dir, FLAGS.bits, FLAGS.stop, log=False) for _, acc in seq: i += 1 if i % 1000 == 0: print('acc: %.02f' % acc) elif FLAGS.action.lower() == 'eval': model.load_weights(sess, FLAGS.model_dir, input_size, hidden_sizes, num_bits=FLAGS.bits) acc = evaluate(sess, x, y, pred_op, images, labels, FLAGS.batch_size, log=False) print('acc: %.02f' % acc) else: print('unknown action: ' + str(FLAGS.action)) exit(-1)