def train_edge(conv_weight_decay=REGULARIZATION_STRENGTH): training_queue = ShuffleQueue(cleaneval_train) data_shape = [BATCH_SIZE, PATCH_SIZE - 1, 1, N_EDGE_FEATURES] labs_shape = [BATCH_SIZE, PATCH_SIZE - 1, 1, 1] train_features = tf.placeholder(tf.float32, shape=data_shape) train_labels = tf.placeholder(tf.int64, shape=labs_shape) logits = edge(train_features, is_training=True, conv_weight_decay=conv_weight_decay, dropout_keep_prob=DROPOUT_KEEP_PROB) l = loss(tf.reshape(logits, [-1, 4]), tf.reshape(train_labels, [-1])) train_op = tf.train.AdamOptimizer(LEARNING_RATE).minimize(l) test_features = tf.placeholder(tf.float32) tf.get_variable_scope().reuse_variables() test_logits = edge(test_features, is_training=False) saver = tf.train.Saver(tf.get_collection(EDGE_VARIABLES)) init_op = tf.global_variables_initializer() with tf.Session() as session: # Initialize session.run(init_op) def prediction(features): features = features[np.newaxis, :, np.newaxis, :] logits = session.run(test_logits, feed_dict={test_features: features}) return np.argmax(logits, axis=-1).flatten() BEST_VAL_SO_FAR = 0 for step in range(TRAIN_STEPS + 1): # Construct a bs-length numpy array _, edge_features, labels, edge_labels = get_batch(training_queue) # Run a training step loss_val, _ = session.run([l, train_op], feed_dict={ train_features: edge_features, train_labels: edge_labels }) if step % 100 == 0: accuracy_validation = evaluate_edge(cleaneval_validation, prediction) accuracy_train = evaluate_edge(cleaneval_train, prediction) if accuracy_validation > BEST_VAL_SO_FAR: best = True saver.save(session, os.path.join(CHECKPOINT_DIR, 'edge.ckpt')) BEST_VAL_SO_FAR = accuracy_validation else: best = False print("%10d: train=%.4f, val=%.4f %s" % (step, accuracy_train, accuracy_validation, '*' if best else '')) # saver.save(session, os.path.join(CHECKPOINT_DIR, 'edge.ckpt')) return accuracy_validation
def test_structured(lamb=EDGE_LAMBDA): from data import cleaneval_test, cleaneval_train, cleaneval_validation unary_features = tf.placeholder(tf.float32) edge_features = tf.placeholder(tf.float32) # hack to get the right shape weights _ = unary(tf.placeholder(tf.float32, shape=[1,PATCH_SIZE,1,N_FEATURES]), False) _ = edge(tf.placeholder(tf.float32, shape=[1,PATCH_SIZE,1,N_EDGE_FEATURES]), False) tf.get_variable_scope().reuse_variables() unary_logits = unary(unary_features, is_training=False) edge_logits = edge(edge_features, is_training=False) unary_saver = tf.train.Saver(tf.get_collection(UNARY_VARIABLES)) edge_saver = tf.train.Saver(tf.get_collection(EDGE_VARIABLES)) init_op = tf.global_variables_initializer() with tf.Session() as session: session.run(init_op) unary_saver.restore(session, os.path.join(CHECKPOINT_DIR, "unary.ckpt")) edge_saver.restore(session, os.path.join(CHECKPOINT_DIR, "edge.ckpt")) from time import time start = time() def prediction_structured(features, edge_feat): features = features[np.newaxis, :, np.newaxis, :] edge_feat = edge_feat[np.newaxis, :, np.newaxis, :] unary_lgts = session.run(unary_logits, feed_dict={unary_features: features}) edge_lgts = session.run(edge_logits, feed_dict={edge_features: edge_feat}) return viterbi(unary_lgts.reshape([-1,2]), edge_lgts.reshape([-1,4]), lam=lamb) def prediction_unary(features, _): features = features[np.newaxis, :, np.newaxis, :] logits = session.run(unary_logits, feed_dict={unary_features: features}) return np.argmax(logits, axis=-1).flatten() accuracy, precision, recall, f1 = evaluate_unary(cleaneval_test, prediction_structured) accuracy_u, precision_u, recall_u, f1_u = evaluate_unary(cleaneval_test, prediction_unary) end = time() print('duration', end-start) print('size', len(cleaneval_test)) print("Structured: Accuracy=%.5f, precision=%.5f, recall=%.5f, F1=%.5f" % (accuracy, precision, recall, f1)) print("Just unary: Accuracy=%.5f, precision=%.5f, recall=%.5f, F1=%.5f" % (accuracy_u, precision_u, recall_u, f1_u))
def classify(block_features_file, edge_features_file, labels_output_file, lamb=EDGE_LAMBDA): block_features = np.genfromtxt(block_features_file, delimiter=',') edge_features = np.genfromtxt(edge_features_file, delimiter=',') # Reshape block_features = block_features.T[np.newaxis, :, np.newaxis, :].astype(np.float32) edge_features = edge_features.T[np.newaxis, :, np.newaxis, :].astype(np.float32) unary_features = tf.constant(block_features) edge_features = tf.constant(edge_features) unary_logits = unary(unary_features, is_training=False) edge_logits = edge(edge_features, is_training=False) unary_saver = tf.train.Saver(tf.get_collection(UNARY_VARIABLES)) edge_saver = tf.train.Saver(tf.get_collection(EDGE_VARIABLES)) init_op = tf.global_variables_initializer() with tf.Session() as session: session.run(init_op) unary_saver.restore(session, os.path.join(CHECKPOINT_DIR, "unary.ckpt")) edge_saver.restore(session, os.path.join(CHECKPOINT_DIR, "edge.ckpt")) from time import time start = time() unary_lgts = session.run(unary_logits) edge_lgts = session.run(edge_logits) labels = viterbi(unary_lgts.reshape([-1, 2]), edge_lgts.reshape([-1, 4]), lam=lamb).astype(np.int32) duration = time() - start print("Done. Classification took %.2f seconds " % duration) with open(labels_output_file, 'w') as fp: fp.write(",".join('%d' % label for label in labels)) print('CSV labels written to %s' % labels_output_file)