def train_golden(RUN_TYPE, opts): print(opts.CLASSNAME, RUN_TYPE) # loading testing data # (remember, here we are testing on ALL golden... so it doesn't matter what the test fold is golden_1, golden_2 = data_io.load_splits(test_fold=0) train_files = golden_1 + golden_2 test_files = golden_2 if small: test_files = test_files[:3] train_files = train_files[:3] train_X, train_y = data_io.load_data( train_files, opts.SPEC_TYPE, opts.LEARN_LOG, opts.CLASSNAME, opts.A, opts.B) test_X, test_y = data_io.load_data( test_files, opts.SPEC_TYPE, opts.LEARN_LOG, opts.CLASSNAME, opts.A, opts.B) for idx in range(opts.ENSEMBLE_MEMBERS): logging_dir = data_io.base + 'predictions/%s/%d/%s/' % (RUN_TYPE, idx, opts.CLASSNAME) force_make_dir(logging_dir) # sys.stdout = ui.Logger(logging_dir + 'log.txt') opts.height = train_X[0].shape[0] with open(logging_dir + 'network_opts.yaml', 'w') as f: yaml.dump(opts, f, default_flow_style=False) train_and_test(train_X, test_X, train_y, test_y, test_files, logging_dir, opts)
def train_and_test(train_X, test_X, train_y, test_y, test_files, logging_dir, opts, TEST_FOLD=99, val_X=None, val_y=None): ''' Doesn't do any data loading - assumes the train and test data are passed in as parameters! ''' if val_X is None: val_X = test_X val_y = test_y # # creaging samplers and batch iterators train_sampler = SpecSampler(64, opts.HWW_X, opts.HWW_Y, opts.DO_AUGMENTATION, opts.LEARN_LOG, randomise=True, balanced=True) test_sampler = SpecSampler(64, opts.HWW_X, opts.HWW_Y, False, opts.LEARN_LOG, randomise=False, seed=10, balanced=True) height = train_X[0].shape[0] net = train_helpers.create_net(height, opts.HWW_X, opts.LEARN_LOG, opts.NUM_FILTERS, opts.WIGGLE_ROOM, opts.CONV_FILTER_WIDTH, opts.NUM_DENSE_UNITS, opts.DO_BATCH_NORM) y_in = T.ivector() x_in = net['input'].input_var # print lasagne.layers.get_output_shape_for(net['prob']) trn_output = lasagne.layers.get_output(net['prob'], deterministic=False) test_output = lasagne.layers.get_output(net['prob'], deterministic=True) params = lasagne.layers.get_all_params(net['prob'], trainable=True) _trn_loss = lasagne.objectives.categorical_crossentropy(trn_output, y_in).mean() _test_loss = lasagne.objectives.categorical_crossentropy( test_output, y_in).mean() _trn_acc = T.mean(T.eq(T.argmax(trn_output, axis=1), y_in)) _test_acc = T.mean(T.eq(T.argmax(test_output, axis=1), y_in)) updates = lasagne.updates.adam(_trn_loss, params, learning_rate=opts.LEARNING_RATE) print "Compiling...", train_fn = theano.function([x_in, y_in], [_trn_loss, _trn_acc], updates=updates) val_fn = theano.function([x_in, y_in], [_test_loss, _test_acc]) pred_fn = theano.function([x_in], test_output) print "DONE" for epoch in range(opts.MAX_EPOCHS): ###################### # TRAINING trn_losses = [] trn_accs = [] for xx, yy in tqdm(train_sampler(train_X, train_y)): trn_ls, trn_acc = train_fn(xx, yy) trn_losses.append(trn_ls) trn_accs.append(trn_acc) ###################### # VALIDATION val_losses = [] val_accs = [] for xx, yy in test_sampler(test_X, test_y): val_ls, val_acc = val_fn(xx, yy) val_losses.append(val_ls) val_accs.append(val_acc) print " %03d :: %02f - %02f - %02f - %02f" % ( epoch, np.mean(trn_losses), np.mean(trn_accs), np.mean(val_losses), np.mean(val_accs)) ####################### # TESTING if small: results_savedir = force_make_dir(logging_dir + 'results/') predictions_savedir = force_make_dir(logging_dir + 'per_file_predictions/') else: results_savedir = force_make_dir(logging_dir + 'results_SMALL_TEST/') predictions_savedir = force_make_dir( logging_dir + 'per_file_predictions_SMALL_TEST/') test_sampler = SpecSampler(64, opts.HWW_X, opts.HWW_Y, False, opts.LEARN_LOG, randomise=False, seed=10, balanced=False) for fname, spec, y in zip(test_files, test_X, test_y): probas = [] y_true = [] for Xb, yb in test_sampler([spec], [y]): probas.append(pred_fn(Xb)) y_true.append(yb) y_pred_prob = np.vstack(probas) y_true = np.hstack(y_true) y_pred = np.argmax(y_pred_prob, axis=1) with open(predictions_savedir + fname, 'w') as f: pickle.dump([y_true, y_pred_prob], f, -1) # save weights from network param_vals = lasagne.layers.get_all_param_values(net['prob']) with open(results_savedir + "weights_%d.pkl" % TEST_FOLD, 'w') as f: pickle.dump(param_vals, f, -1)
import lasagne import theano.tensor as T import theano sys.path.append('../lib') from train_helpers import SpecSampler import train_helpers import data_io train_name = 'ensemble_train_anthrop' classname = 'anthrop' base = yaml.load(open('../CONFIG.yaml'))['base_dir'] + '/predictions/' load_path = base + '/%s/%d/%s/results/weights_99.pkl' predictions_savedir = train_helpers.force_make_dir( base + '/%s/%s/per_file_predictions/' % (train_name, classname)) def predict(A, B, ENSEMBLE_MEMBERS, SPEC_TYPE, CLASSNAME, HWW_X, HWW_Y, DO_AUGMENTATION, LEARN_LOG, NUM_FILTERS, WIGGLE_ROOM, CONV_FILTER_WIDTH, NUM_DENSE_UNITS, DO_BATCH_NORM, MAX_EPOCHS, LEARNING_RATE): # Loading data golden_1, golden_2 = data_io.load_splits(test_fold=0) test_files = golden_1 + golden_2 test_X, test_y = data_io.load_data(test_files, SPEC_TYPE, LEARN_LOG, CLASSNAME, A, B) # # creaging samplers and batch iterators test_sampler = SpecSampler(64,
def train_and_test(train_X, test_X, train_y, test_y, test_files, logging_dir, opts, TEST_FOLD=99, val_X=None, val_y=None): ''' Doesn't do any data loading - assumes the train and test data are passed in as parameters! ''' if val_X is None: val_X = test_X val_y = test_y # # creaging samplers and batch iterators train_sampler = SpecSampler(64, opts.HWW_X, opts.HWW_Y, opts.DO_AUGMENTATION, opts.LEARN_LOG, randomise=True, balanced=True) test_sampler = SpecSampler(64, opts.HWW_X, opts.HWW_Y, False, opts.LEARN_LOG, randomise=False, seed=10, balanced=True) height = train_X[0].shape[0] net = create_net(height, opts.HWW_X, opts.LEARN_LOG, opts.NUM_FILTERS, opts.WIGGLE_ROOM, opts.CONV_FILTER_WIDTH, opts.NUM_DENSE_UNITS, opts.DO_BATCH_NORM) y_in = tf.placeholder(tf.int32, (None)) x_in = net['input'] print("todo - fix this up...") trn_output = net['fc8'] test_output = net['fc8'] _trn_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits( logits=trn_output, labels=y_in)) _test_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits( logits=test_output, labels=y_in)) print(y_in, trn_output, tf.argmax(trn_output, axis=1)) pred = tf.cast(tf.argmax(trn_output, axis=1), tf.int32) _trn_acc = tf.reduce_mean(tf.cast(tf.equal(y_in, pred), tf.float32)) pred = tf.cast(tf.argmax(test_output, axis=1), tf.int32) _test_acc = tf.reduce_mean(tf.cast(tf.equal(y_in, pred), tf.float32)) optimizer = tf.train.AdamOptimizer(learning_rate=opts.LEARNING_RATE, beta1=0.5, beta2=0.9) train_op = slim.learning.create_train_op(_trn_loss, optimizer) saver = tf.train.Saver(max_to_keep=5) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for epoch in range(opts.MAX_EPOCHS): ###################### # TRAINING trn_losses = [] trn_accs = [] for xx, yy in tqdm(train_sampler(train_X, train_y)): trn_ls, trn_acc, _ = sess.run( [_trn_loss, _trn_acc, train_op], feed_dict={x_in: xx, y_in: yy}) trn_losses.append(trn_ls) trn_accs.append(trn_acc) ###################### # VALIDATION val_losses = [] val_accs = [] for xx, yy in test_sampler(test_X, test_y): val_ls, val_acc = sess.run([_test_loss, _test_acc], feed_dict={x_in: xx, y_in: yy}) val_losses.append(val_ls) val_accs.append(val_acc) print(" %03d :: %02f - %02f - %02f - %02f" % (epoch, np.mean(trn_losses), np.mean(trn_accs), np.mean(val_losses), np.mean(val_accs))) ####################### # TESTING if small: results_savedir = force_make_dir(logging_dir + 'results_SMALL_TEST/') predictions_savedir = force_make_dir(logging_dir + 'per_file_predictions_SMALL_TEST/') else: results_savedir = force_make_dir(logging_dir + 'results/') predictions_savedir = force_make_dir(logging_dir + 'per_file_predictions/') test_sampler = SpecSampler(64, opts.HWW_X, opts.HWW_Y, False, opts.LEARN_LOG, randomise=False, seed=10, balanced=False) for fname, spec, y in zip(test_files, test_X, test_y): probas = [] y_true = [] for Xb, yb in test_sampler([spec], [y]): preds = sess.run(test_output, feed_dict={x_in: Xb}) probas.append(preds) y_true.append(yb) y_pred_prob = np.vstack(probas) y_true = np.hstack(y_true) y_pred = np.argmax(y_pred_prob, axis=1) print("Saving to {}".format(predictions_savedir)) with open(predictions_savedir + fname, 'wb') as f: pickle.dump([y_true, y_pred_prob], f, -1) # save weights from network saver.save(sess, results_savedir + "weights_%d.pkl" % TEST_FOLD, global_step=1)