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() # Construct and return the model image_batch = reshape_to_cnn(image) network_fn = nets_factory.get_network_fn( name='densenet121', num_classes=None, weight_decay=0.00004, data_format='NHWC', is_training=True ) face_output, _ = network_fn(image_batch) # print face_output.get_shape().as_list() # RNN part rnn_in = reshape_to_rnn(face_output) prediction = models.get_prediction_atten(rnn_in) 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 # when the network only contains DenseNet all variables can be restored, # so here we restore global variables with ignore_missing_vars = True variables_to_restore = tf.global_variables() init_fn = slim.assign_from_checkpoint_fn(DenseNet_RESTORE_PATH, variables_to_restore, ignore_missing_vars=True) # 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(). trainable_names = ['attention-layer', 'fcatten'] variables_to_train = tf.contrib.framework.get_variables_to_restore(include=trainable_names) print_var(variables_to_train) print 'global vars' print_var(tf.global_variables())
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 image_batch = tf.reshape(image, [-1, 96, 96, 3]) # Construct and return the model network_fn = nets_factory.get_network_fn(name='densenet161', num_classes=None, weight_decay=0.00004, data_format='NHWC', is_training=False) face_output, _ = network_fn(image_batch) # RNN part rnn_in = reshape_to_rnn(face_output) prediction = models.get_prediction_atten(rnn_in) 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) 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), )
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() # Construct and return the model image_batch = reshape_to_cnn(image) network_fn = nets_factory.get_network_fn(name='densenet121', num_classes=None, weight_decay=0.00004, data_format='NHWC', is_training=True) face_output, _ = network_fn(image_batch) # print face_output.get_shape().as_list() # RNN part rnn_in = reshape_to_rnn(face_output) prediction = models.get_prediction_atten(rnn_in, attn_length=30) 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 # when the network only contains DenseNet all variables can be restored, # so here we restore global variables with ignore_missing_vars = True variables_to_restore = tf.global_variables() init_fn = slim.assign_from_checkpoint_fn(DenseNet_RESTORE_PATH, variables_to_restore, ignore_missing_vars=True) # print_var(tf.global_variables()) # print_var(tf.trainable_variables()) # 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)