def train(): g = tf.Graph() with g.as_default(): # load data get iterator data_loader = data_utils_mean.DataLoader(SEQUENCE_LENGTH, BATCH_SIZE, NUM_EPOCHS) iterator = data_loader.load_data(TRAIN_TFR_PATH, True) with tf.Session(graph=g) as sess: frameNo, image, label = iterator.get_next() # VGG FACE network VGGFace_network = vgg_face.VGGFace(SEQUENCE_LENGTH * BATCH_SIZE) image_batch = reshape_to_cnn(image) VGGFace_network.setup(image_batch, trainable=False) face_output = VGGFace_network.get_face_fc0() # RNN part rnn_in = reshape_to_rnn(face_output) prediction = models.get_prediction_highway(rnn_in, useHighway=False) prediction = tf.reshape(prediction, [BATCH_SIZE, SEQUENCE_LENGTH, 2]) label_batch = tf.reshape(label, [BATCH_SIZE, SEQUENCE_LENGTH, 2]) # compute losses using slim compute_loss(prediction, label_batch) total_loss = slim.losses.get_total_loss() optimizer = tf.train.AdamOptimizer(LEARNING_RATE) # restore VGG-FACE model at the beginning restore_names = VGGFace_network.get_restore_vars() variables_to_restore = tf.contrib.framework.get_variables_to_restore( include=restore_names) init_fn = slim.assign_from_checkpoint_fn(VGG_RESTORE_PATH, variables_to_restore, ignore_missing_vars=False) # summarize_gradients : Whether or not add summaries for each gradient. # variables_to_train: an optional list of variables to train. If None, it will default to all tf.trainable_variables(). train_op = slim.learning.create_train_op( total_loss, optimizer, summarize_gradients= True # Whether or not add summaries for each gradient. ) loggingTF.set_verbosity(1) # keep 10000 ckpts saver = tf.train.Saver(max_to_keep=10000) # including initialize local and global variables slim.learning.train( train_op, TRAIN_DIR, init_fn=init_fn, save_summaries_secs=60 * 15, # How often, in seconds, to save summaries. log_every_n_steps= 500, # The frequency, in terms of global steps, that the loss and global step are logged. save_interval_secs=60 * 15, # How often, in seconds, to save the model to `logdir`. saver=saver)
def evaluate(): g = tf.Graph() with g.as_default(): # load data get iterator data_loader = data_utils_mean.DataLoader(SEQUENCE_LENGTH, BATCH_SIZE, NUM_EPOCHS) iterator = data_loader.load_data(TEST_TFR_PATH, False) frameNo, image, label = iterator.get_next() # define model graph # VGG FACE network VGGFace_network = vgg_face.VGGFace(SEQUENCE_LENGTH * BATCH_SIZE) image_batch = tf.reshape(image, [-1, 96, 96, 3]) VGGFace_network.setup(image_batch, trainable=False) # image_batch is a tensor of shape (batch_size*seq_length,image_dim,image_dim,3) face_output = VGGFace_network.get_face_fc0() # RNN part rnn_in = reshape_to_rnn(face_output) prediction = models.get_prediction_highway(rnn_in, useHighway=False) prediction = tf.reshape(prediction, [BATCH_SIZE, SEQUENCE_LENGTH, 2]) label_batch = tf.reshape(label, [BATCH_SIZE, SEQUENCE_LENGTH, 2]) # Computing MSE and Concordance values, and adding them to summary names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({ 'eval/mse_valence': slim.metrics.streaming_mean_squared_error(prediction[:, :, 0], label_batch[:, :, 0]), 'eval/mse_arousal': slim.metrics.streaming_mean_squared_error(prediction[:, :, 1], label_batch[:, :, 1]), }) summary_ops = [] conc_total = 0 mse_total = 0 for i, name in enumerate(['valence', 'arousal']): with tf.name_scope(name) as scope: concordance_cc2, values, updates = metrics.concordance_cc2( tf.reshape(prediction[:, :, i], [-1]), tf.reshape(label_batch[:, :, i], [-1])) for n, v in updates.items(): names_to_updates[n + '/' + name] = v op = tf.summary.scalar('eval/concordance_' + name, concordance_cc2) op = tf.Print(op, [concordance_cc2], 'eval/concordance_' + name) summary_ops.append(op) mse_eval = 'eval/mse_' + name op = tf.summary.scalar(mse_eval, names_to_values[mse_eval]) op = tf.Print(op, [names_to_values[mse_eval]], mse_eval) summary_ops.append(op) mse_total += names_to_values[mse_eval] conc_total += concordance_cc2 conc_total = conc_total / 2 mse_total = mse_total / 2 op = tf.summary.scalar('eval/concordance_total', conc_total) op = tf.Print(op, [conc_total], 'eval/concordance_total') summary_ops.append(op) op = tf.summary.scalar('eval/mse_total', mse_total) op = tf.Print(op, [mse_total], 'eval/mse_total') summary_ops.append(op) num_batches = int(NUM_BATCHES) loggingTF.set_verbosity(1) if not os.path.exists(SUMMARY_PATH): os.makedirs(SUMMARY_PATH) # always check latest ckpt and wait for next. slim.evaluation.evaluate_once('', MODEL_PATH, SUMMARY_PATH, num_evals=num_batches, eval_op=list(names_to_updates.values()), summary_op=tf.summary.merge(summary_ops), )