def tower_loss_acc(scope, images, labels): """Calculate the total loss and accuracy on a single tower running the model. Args: scope: unique prefix string identifying the tower, e.g. 'tower_0' images: input images with shape [batch_size, sequence_length, height, width, channel] labels: label ground truth [batch_size] Returns: Tensor of shape [] containing the total loss for a batch of data """ # Build the inference Graph with tf.variable_scope("c3d_var") as c3d_scope: try: logits = c3d_model.inference_c3d(images, 0.5) except ValueError: c3d_scope.reuse_variables() logits = c3d_model.inference_c3d(images, 0.5) # Build the portion of the Graph calculating the losses. Note that we will # assemble the total_loss using a custom function below. _ = c3d_model.loss(logits, labels) # Assemble all of the losses for the current tower only. losses = tf.get_collection('losses', scope) # Calculate the total loss for the current tower total_loss = tf.add_n(losses, name='total_loss') # Attach a scalar summary to all individual losses and the total loss; do the # same for the averaged version of the losses. for l in losses + [total_loss]: # Remove 'tower_[0-9]/' from the name in case this is a multi-GPU training # session. This helps the clarity of presentation on tensorboard. loss_name = re.sub('%s_[0-9]*/' % c3d_model.TOWER_NAME, '', l.op.name) tf.summary.scalar(loss_name, l) # Compute the moving average of all individual losses and the total loss. loss_averages = tf.train.ExponentialMovingAverage(0.99, name='loss') loss_averages_op = loss_averages.apply(losses + [total_loss]) with tf.control_dependencies([loss_averages_op]): loss = tf.identity(total_loss) # Calculate the accuracy correct_pred = tf.equal(tf.argmax(logits, 1), labels) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) # add the accuracy to summary tf.summary.scalar('accuracy', accuracy) return total_loss, accuracy
def evaluate(): with tf.Graph().as_default() as g: # Get the image and the labels placeholder images_placeholder, labels_placeholder = placeholder_inputs( FLAGS.batch_size) # Build the Graph that computes the logits predictions from the inference # model. with tf.variable_scope('c3d_var'): logits = c3d_model.inference_c3d(images_placeholder, batch_size=FLAGS.batch_size) top_k_op = tf.nn.in_top_k(logits, labels_placeholder, 1) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( c3d_model.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) while True: eval_once(saver, top_k_op, images_placeholder, labels_placeholder) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def train(): """Train the SVM classifier """ with tf.Graph().as_default() as g: # Get the image and the labels placeholder images_placeholder = placeholder_inputs(FLAGS.batch_size) # Build the Graph that computes the lc6 feature with tf.variable_scope('c3d_var'): # Extract the video feature according to the pretrained C3D model. features_op = c3d_model.inference_c3d(images_placeholder, batch_size=FLAGS.batch_size, features=True) # Apply the L2 normalization function to all features features_op = tf.nn.l2_normalize(features_op, 1) # Restore the moving average version of the learned variables for evaluation. variable_averages = tf.train.ExponentialMovingAverage( c3d_model.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) print('Start processing the Data') # Get the features and the labels from the training dataset features, labels = get_data(saver, features_op, images_placeholder) print('Done processing the Data, Start Training SVM') # train the svm # train_svm_classifer(features, labels) # train the linear svm train_linear_svm_classifier(features, labels)
def get_logits(images_placeholder, labels_placeholder,batch_size,gpu_num = 1): with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.04, 0.00), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.00), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.00), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.00), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.00), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.04, 0.001), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.04, 0.002), 'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.04, 0.005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.04, 0.0), 'bc2': _variable_with_weight_decay('bc2', [128], 0.04, 0.0), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.04, 0.0), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.04, 0.0), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.04, 0.0), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.04, 0.0), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.04, 0.0), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.04, 0.0), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.04, 0.0), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.04, 0.0), 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.04, 0.0), } logits = [] for gpu_index in range(0, gpu_num): with tf.device('/gpu:%d' % gpu_index): logit = c3d_model.inference_c3d(images_placeholder[gpu_index * batch_size:(gpu_index + 1) * batch_size,:,:,:,:], 1, batch_size, weights, biases) logits.append(logit) logits = tf.concat(logits,0) return logits
def tower_loss_accuracy(scope, video_clip, labels, dropout_ratio): """Calculate the total loss on a single tower. Args: scope: unique prefix string identifying the tower, e.g. 'tower_0' video_clip: Images. 4D tensor of shape [batch_size, height, width, 3]. labels: Labels. 1D tensor of shape [batch_size]. dropout_ratio: Returns: Tensor of shape [] containing the total loss and accuracy for a batch of data """ # Build inference Graph. logits = c3d_model.inference_c3d(video_clip, dropout_ratio) # labels = tf.cast(tf.reduce_sum(labels, 1),tf.int32) # calculate the loss and accuracy of a batch. loss, accuracy = batch_loss_accu(logits, labels) # tf.summary.scalar('loss_inference', loss) # Assemble all of the losses for the current tower only. losses = tf.get_collection('losses', scope) # Calculate the total loss for the current tower. total_loss = tf.add_n(losses, name='total_loss') tf.summary.scalar('tower_loss', total_loss) return total_loss, accuracy
def run_test(): config = Config() test_lines = list(open(config.test_list, 'r')) train_lines = list(open(config.train_list, 'r')) num_test_videos = len(test_lines) print("Number of test videos={}".format(num_test_videos)) # Get the sets of images and labels for training, validation, and images_placeholder, labels_placeholder = placeholder_inputs() logit = c3d_model.inference_c3d(images_placeholder, 0.6, config.weight_initial) norm_score = tf.nn.softmax(logit) accuracy = tower_acc(logit, labels_placeholder) saver = tf.train.Saver() sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) init = tf.global_variables_initializer() sess.run(init) # Create a saver for writing training checkpoints. #saver.restore(sess, config.model_filename) next_start_pos = 0 all_steps = int((num_test_videos - 1) / config.batch_size + 1) res_acc = 0 for step in xrange(all_steps): start_time = time.time() test_images, test_labels, next_start_pos, _ = \ input_data.read_clip_and_label( test_lines, batch_size=config.batch_size, start_pos=next_start_pos ) acc = sess.run(accuracy, feed_dict={ images_placeholder: test_images, labels_placeholder: test_labels }) print(acc) res_acc = res_acc + acc print(res_acc / all_steps) print("done")
def run_training(): # Get the sets of images and labels for training, validation, and # Tell TensorFlow that the model will be built into the default Graph. # Create model directory if not os.path.exists(model_save_dir): os.makedirs(model_save_dir) use_pretrained_model = False model_filename = "./sports1m_finetuning_ucf101.model" with tf.Graph().as_default(): global_step = tf.get_variable( 'global_step', [], initializer=tf.constant_initializer(0), trainable=False ) images_placeholder, labels_placeholder = placeholder_inputs( FLAGS.batch_size * gpu_num ) tower_grads1 = [] tower_grads2 = [] logits = [] opt_stable = tf.train.AdamOptimizer(LEARNING_RATE_STABLE) opt_finetuning = tf.train.AdamOptimizer(LEARNING_RATE_FINETUNE) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.0005), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.0005), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.0005), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.0005), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.0005), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.0005), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.0005), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.0005), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.0005), #'wd1': _variable_with_weight_decay('wd1', [16384, 4096], 0.0005), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.0005), 'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.0005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.000), 'bc2': _variable_with_weight_decay('bc2', [128], 0.000), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.000), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.000), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.000), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.000), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.000), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.000), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.000), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.000), 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.000), } for gpu_index in range(0, gpu_num): with tf.device('/gpu:%d' % gpu_index): varlist2 = [ weights['out'],biases['out'] ] varlist1 = list( set(list(weights.values()) + list(biases.values())) - set(varlist2) ) logit = c3d_model.inference_c3d( images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size,:,:,:,:], 0.5, FLAGS.batch_size, weights, biases, use_pretrained_model ) loss_name_scope = ('gpud_%d_loss' % gpu_index) loss = tower_loss( loss_name_scope, logit, labels_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size] ) grads1 = opt_stable.compute_gradients(loss, varlist1) grads2 = opt_finetuning.compute_gradients(loss, varlist2) tower_grads1.append(grads1) tower_grads2.append(grads2) logits.append(logit) logits = tf.concat(logits,0) accuracy = tower_acc(logits, labels_placeholder) tf.summary.scalar('accuracy', accuracy) grads1 = average_gradients(tower_grads1) grads2 = average_gradients(tower_grads2) apply_gradient_op1 = opt_stable.apply_gradients(grads1) apply_gradient_op2 = opt_finetuning.apply_gradients(grads2, global_step=global_step) variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY) variables_averages_op = variable_averages.apply(tf.trainable_variables()) train_op = tf.group(apply_gradient_op1, apply_gradient_op2, variables_averages_op) null_op = tf.no_op() # Create a saver for writing training checkpoints. saver = tf.train.Saver(list(weights.values()) + list(biases.values())) init = tf.global_variables_initializer() # Create a session for running Ops on the Graph. sess = tf.Session( config=tf.ConfigProto(allow_soft_placement=True) ) sess.run(init) if os.path.isfile(model_filename) and use_pretrained_model: saver.restore(sess, model_filename) # Create summary writter merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter('./visual_logs/train', sess.graph) test_writer = tf.summary.FileWriter('./visual_logs/test', sess.graph) for step in xrange(FLAGS.max_steps): start_time = time.time() train_images, train_labels, _, _, _, valid_len = input_data.read_clip_and_label( # filename='list/trainlist01.txt', filename='../../ucf101_all_frames/train-test-splits/trainlist01-hyperion.txt', batch_size=FLAGS.batch_size * gpu_num, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP, crop_size=c3d_model.CROP_SIZE, shuffle=True, flip_with_probability=0.5, pad_short_clips=PAD_SHORT_CLIPS ) sess.run(train_op, feed_dict={ images_placeholder: train_images, labels_placeholder: train_labels }) duration = time.time() - start_time print('Step %d: %.3f sec, valid_len = %s' % (step, duration, valid_len)) # Save a checkpoint and evaluate the model periodically. if (step) % 10 == 0 or (step + 1) == FLAGS.max_steps: saver.save(sess, os.path.join(model_save_dir, 'c3d_ucf_model'), global_step=step) print('Training Data Eval:') summary, acc = sess.run( [merged, accuracy], feed_dict={images_placeholder: train_images, labels_placeholder: train_labels }) print ("accuracy: " + "{:.5f}".format(acc)) train_writer.add_summary(summary, step) print('Validation Data Eval:') val_images, val_labels, _, _, _, _ = input_data.read_clip_and_label( #filename='list/testlist01.txt', filename='../../ucf101_all_frames/train-test-splits/testlist01-hyperion.txt', batch_size=FLAGS.batch_size * gpu_num, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP, crop_size=c3d_model.CROP_SIZE, shuffle=True, pad_short_clips=PAD_SHORT_CLIPS ) summary, acc = sess.run( [merged, accuracy], feed_dict={ images_placeholder: val_images, labels_placeholder: val_labels }) print ("accuracy: " + "{:.5f}".format(acc)) test_writer.add_summary(summary, step) print("done")
def run(): global_start_time = time.time() config = Config() # Create model directory if not os.path.exists(config.model_save_dir): os.makedirs(config.model_save_dir) model_filename = "prosthesis_model" with tf.Graph().as_default(): global_step = tf.get_variable( 'global_step', [], initializer=tf.constant_initializer(0), trainable=False) images_placeholder, labels_placeholder = placeholder_inputs(config) tower_grads1 = [] tower_grads2 = [] logits = [] opt_stable = tf.train.AdamOptimizer(config.stable_learning_rate) opt_finetuning = tf.train.AdamOptimizer(config.finetune_learning_rate) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 1, 64], 0.0005), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.0005), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.0005), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.0005), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.0005), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.0005), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.0005), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.0005), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.0005), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.0005), 'out': _variable_with_weight_decay('wout', [4096, config.num_classes], 0.0005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.000), 'bc2': _variable_with_weight_decay('bc2', [128], 0.000), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.000), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.000), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.000), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.000), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.000), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.000), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.000), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.000), 'out': _variable_with_weight_decay('bout', [config.num_classes], 0.000), } varlist2 = [ weights['out'],biases['out'] ] varlist1 = list( set(weights.values() + biases.values()) - set(varlist2) ) logit = c3d_model.inference_c3d( images_placeholder[ :config.batch_size, : , : , : , : ], config.drop_rate, config.batch_size, weights, biases) loss_name_scope = ('gpud_0_loss') loss = tower_loss( loss_name_scope, logit, labels_placeholder[ :config.batch_size]) grads1 = opt_stable.compute_gradients(loss, varlist1) grads2 = opt_finetuning.compute_gradients(loss, varlist2) tower_grads1.append(grads1) tower_grads2.append(grads2) logits.append(logit) logits = tf.concat(logits, 0) accuracy = tower_acc(logits, labels_placeholder) correct, total = get_correct_total(logits, labels_placeholder) grads1 = average_gradients(tower_grads1) grads2 = average_gradients(tower_grads2) apply_gradient_op1 = opt_stable.apply_gradients(grads1) apply_gradient_op2 = opt_finetuning.apply_gradients(grads2, global_step=global_step) variable_averages = tf.train.ExponentialMovingAverage(config.moving_average_decay) variables_averages_op = variable_averages.apply(tf.trainable_variables()) train_op = tf.group(apply_gradient_op1, apply_gradient_op2, variables_averages_op) # Create a saver for writing training checkpoints. saver = tf.train.Saver(weights.values() + biases.values()) init = tf.global_variables_initializer() # Create a session for running Ops on the Graph. sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) sess.run(init) best_valid_acc = 0.0 if config.is_train: train_data = MyData( path='.', set_indices=config.train_trials, config=config, data_type="training") valid_data = MyData( path='.', set_indices=config.valid_trials, config=config, data_type="validation") if config.is_train: for epoch in range(config.num_epochs): start_time = time.time() train_acc, train_loss = run_epoch( sess, images_placeholder, labels_placeholder, train_data, correct, total, loss, train_op) valid_acc, valid_loss = run_epoch( sess, images_placeholder, labels_placeholder, valid_data, correct, total, loss) duration = time.time() - start_time print('Epoch %d train loss: %.3f, acc: %.5f. Valid loss: %.3f, acc: %.5f, duration: %d:%02d:%02d' % \ ((epoch + 1, train_loss, train_acc, valid_loss, valid_acc) + format_time(duration))) if valid_acc > best_valid_acc: save_path = saver.save( sess, os.path.join(config.model_save_dir, config.model_filename)) print('Valid accuracy improved. Model saved in file: %s' % save_path) best_valid_acc = valid_acc saver.restore(sess, os.path.join(config.model_save_dir, config.model_filename)) print("Model restored") best_valid_acc, best_valid_loss = run_epoch( sess, images_placeholder, labels_placeholder, valid_data, correct, total, loss) total_duration = time.time() - global_start_time print("Done. Best validation loss: %.3f, accuracy: %.5f. Total duration: %d:%02d:%02d" % ((best_valid_loss, best_valid_acc) + format_time(total_duration)))
def run_test(): tf.reset_default_graph() init_path = 'D:/autism/3DCNN-master/c3d_ucf_model-2520' for i in os.listdir(init_path): model_name = init_path + '/' + 'c3d_ucf_model-2520' print(model_name) test_list_file = 'D:/autism/3DCNN-master/C3D-tensorflow/list/test_list.list' num_test_videos = len(list(open(test_list_file, 'r'))) print("Number of test videos={}".format(num_test_videos)) # Get the sets of images and labels for training, validation, and images_placeholder, labels_placeholder = placeholder_inputs( FLAGS.batch_size * gpu_num) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.04, 0.00), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.00), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.00), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.00), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.00), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.04, 0.001), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.04, 0.002), 'out': _variable_with_weight_decay('wout', [4096, 8], 0.04, 0.005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.04, 0.0), 'bc2': _variable_with_weight_decay('bc2', [128], 0.04, 0.0), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.04, 0.0), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.04, 0.0), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.04, 0.0), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.04, 0.0), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.04, 0.0), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.04, 0.0), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.04, 0.0), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.04, 0.0), 'out': _variable_with_weight_decay('bout', [8], 0.04, 0.0), } logits = [] for gpu_index in range(0, gpu_num): with tf.device('/gpu:%d' % gpu_index): logit = c3d_model.inference_c3d( images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size, :, :, :, :], 0.6, FLAGS.batch_size, weights, biases) logits.append(logit) logits = tf.concat(logits, 0) norm_score = tf.nn.softmax(logits) saver = tf.train.Saver() sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) init = tf.global_variables_initializer() sess.run(init) # Create a saver for writing training checkpoints. saver.restore(sess, model_name) # And then after everything is built, start the training loop. bufsize = 0 write_file = open("D:/autism/3DCNN-master/predict_" + str(i) + ".txt", "w+") next_start_pos = -1 all_steps = int((num_test_videos - 1) / (FLAGS.batch_size * gpu_num) + 1) for step in xrange(all_steps): start_time = time.time() test_images, test_labels, next_start_pos, _, valid_len = \ input_data.read_clip_and_label( test_list_file, FLAGS.batch_size * gpu_num, start_pos=next_start_pos ) #print(test_images[0],test_labels[0]) predict_score = norm_score.eval( session=sess, feed_dict={images_placeholder: test_images}) for i in range(0, valid_len): true_label = test_labels[i], top1_predicted_label = np.argmax(predict_score[i]) # Write results: true label, class prob for true label, predicted label, class prob for predicted label write_file.write('{}, {}, {}, {}\n'.format( true_label[0], predict_score[i][true_label], top1_predicted_label, predict_score[i][top1_predicted_label])) write_file.close() print("done for " + str(i))
def run_training(): # Get the sets of images and labels for training, validation, and # Tell TensorFlow that the model will be built into the default Graph. # Create model directory if not os.path.exists(FLAGS.model_save_dir): os.makedirs(FLAGS.model_save_dir) use_pretrained_model = FLAGS.use_pretrained_model model_filename = "./sports1m_finetuning_ucf101.model" with tf.Graph().as_default(): global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False) images_placeholder, labels_placeholder = placeholder_inputs( FLAGS.batch_size * FLAGS.gpu_num) tower_grads1 = [] tower_grads2 = [] logits = [] opt_stable = tf.train.AdamOptimizer(FLAGS.learning_rate) opt_finetuning = tf.train.AdamOptimizer(FLAGS.learning_rate_fine) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 1, 64], 0.0005), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.0005), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.0005), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.0005), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.0005), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.0005), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.0005), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.0005), #'wc6a': _variable_with_weight_decay('wc6a', [3, 3, 3, 512, 256], 0.0005), #'wc6b': _variable_with_weight_decay('wc6b', [3, 3, 3, 256, 128], 0.0005), #'wc7a': _variable_with_weight_decay('wc7a', [3, 3, 3, 128, 64], 0.0005), #'wc7b': _variable_with_weight_decay('wc7b', [3, 3, 3, 64, 32], 0.0005), #'wc8a': _variable_with_weight_decay('wc8a', [3, 3, 3, 32, 16], 0.0005), #'wc8b': _variable_with_weight_decay('wc8b', [3, 3, 3, 16, 8], 0.0005), #'wd1': _variable_with_weight_decay('wd1', [128, 64], 0.0005), #'out': _variable_with_weight_decay('wout', [128, c3d_model.NUM_CLASSES], 0.0005) 'wd1': _variable_with_weight_decay('wd1', [32768, 4096], 0.0005), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.0005), 'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.0005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.000), 'bc2': _variable_with_weight_decay('bc2', [128], 0.000), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.000), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.000), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.000), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.000), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.000), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.000), #'bc6a': _variable_with_weight_decay('bc6a', [256], 0.000), #'bc6b': _variable_with_weight_decay('bc6b', [128], 0.000), #'bc7a': _variable_with_weight_decay('bc7a', [64], 0.000), #'bc7b': _variable_with_weight_decay('bc7b', [32], 0.000), #'bc8a': _variable_with_weight_decay('bc8a', [16], 0.000), #'bc8b': _variable_with_weight_decay('bc8b', [8], 0.000), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.000), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.000), 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.000), } for gpu_index in range(0, FLAGS.gpu_num): with tf.device('/gpu:%d' % gpu_index): varlist2 = [weights['out'], biases['out']] varlist1 = list( set(list(weights.values()) + list(biases.values())) - set(varlist2)) logit = c3d_model.inference_c3d( images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size, :, :, :, :], 0.5, FLAGS.batch_size, weights, biases) loss_name_scope = ('gpud_%d_loss' % gpu_index) loss = tower_loss( loss_name_scope, logit, labels_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size]) grads1 = opt_stable.compute_gradients(loss, varlist1) grads2 = opt_finetuning.compute_gradients(loss, varlist2) tower_grads1.append(grads1) tower_grads2.append(grads2) logits.append(logit) #grad = opt_stable.minimize(loss) logits = tf.concat(logits, 0) accuracy = tower_acc(logits, labels_placeholder) tf_precision, tf_recall, f1_score = f1score(labels_placeholder, logits) tf.summary.scalar('accuracy', accuracy) tf.summary.scalar('f1_score', f1_score) tf.summary.scalar('tf_precision', tf_precision) tf.summary.scalar('tf_recall', tf_recall) grads1 = average_gradients(tower_grads1) grads2 = average_gradients(tower_grads2) apply_gradient_op1 = opt_stable.apply_gradients(grads1) apply_gradient_op2 = opt_finetuning.apply_gradients( grads2, global_step=global_step) variable_averages = tf.train.ExponentialMovingAverage( FLAGS.MOVING_AVERAGE_DECAY) variables_averages_op = variable_averages.apply( tf.trainable_variables()) train_op = tf.group(apply_gradient_op1, apply_gradient_op2, variables_averages_op) #train_op = tf.group(grad, variables_averages_op) null_op = tf.no_op() # Create a saver for writing training checkpoints. saver = tf.train.Saver(list(weights.values()) + list(biases.values())) init = tf.global_variables_initializer() # Create a session for running Ops on the Graph. sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) sess.run(init) if os.path.isfile(model_filename) and use_pretrained_model: print( "==========================load pre train model=================================" ) saver.restore(sess, model_filename) # Create summary writter merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter( os.path.join(FLAGS.model_save_dir, 'logs/train'), sess.graph) test_writer = tf.summary.FileWriter( os.path.join(FLAGS.model_save_dir, 'logs/test'), sess.graph) for step in range(FLAGS.max_steps): start_time = time.time() train_images, train_labels, _, _, _ = input_process_data.read_clip_and_label( FLAGS.dataset_dir, filename=FLAGS.train_list, batch_size=FLAGS.batch_size * FLAGS.gpu_num, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP, crop_size=c3d_model.CROP_SIZE, shuffle=True) _, loss_ = sess.run( [train_op, loss], feed_dict={ images_placeholder: train_images, labels_placeholder: train_labels, }) duration = time.time() - start_time #if (step) % 10 ==0: print(('Step %d: %.3f sec' % (step, duration)), "loss is:", np.mean(loss_)) # Save a checkpoint and evaluate the model periodically. if (step) % 10 == 0 or (step + 1) == FLAGS.max_steps: saver.save(sess, os.path.join(FLAGS.model_save_dir, 'c3d_model'), global_step=step) print('Training Data Eval:') summary, acc, p, r, f1 = sess.run( [merged, accuracy, tf_precision, tf_recall, f1_score], feed_dict={ images_placeholder: train_images, labels_placeholder: train_labels }) print(("accuracy: " + "{:.5f}".format(acc))) print(("f1_score: {:.5f}".format(f1))) train_writer.add_summary(summary, step) print('Validation Data Eval:') val_images, val_labels, _, _, _ = input_process_data.read_clip_and_label( FLAGS.dataset_dir, filename=FLAGS.test_list, batch_size=FLAGS.batch_size * FLAGS.gpu_num, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP, crop_size=c3d_model.CROP_SIZE, shuffle=True) summary, acc, p, r, f1 = sess.run( [merged, accuracy, tf_precision, tf_recall, f1_score], feed_dict={ images_placeholder: val_images, labels_placeholder: val_labels }) print(("accuracy: " + "{:.5f}".format(acc))) print(("f1_score:{:.5f}".format(f1))) test_writer.add_summary(summary, step) print("done")
def run_training(): # Create model directory if not os.path.exists(model_save_dir): os.makedirs(model_save_dir) use_pretrained_model = True model_filename = "/home/ankur/data/new_disk/c3d_ucf101_finetune_whole_iter_20000_TF.model" with tf.Graph().as_default(): global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False) images_placeholder, labels_placeholder = placeholder_inputs( FLAGS.batch_size * gpu_num) tower_grads1 = [] tower_grads2 = [] logits = [] opt_stable = tf.train.AdamOptimizer(1e-4) opt_finetuning = tf.train.AdamOptimizer(1e-3) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.0005), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.0005), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.0005), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.0005), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.0005), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.0005), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.0005), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.0005), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.0005), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.0005), 'out': _variable_with_weight_decay('wout', [4096, 8], 0.0005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.000), 'bc2': _variable_with_weight_decay('bc2', [128], 0.000), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.000), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.000), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.000), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.000), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.000), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.000), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.000), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.000), 'out': _variable_with_weight_decay('bout', [8], 0.000), } for gpu_index in range(0, gpu_num): with tf.device('/gpu:%d' % gpu_index): varlist2 = [weights['out'], biases['out']] varlist1 = list((set(weights.values()) | set(biases.values())) - set(varlist2)) logit = c3d_model.inference_c3d( images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size, :, :, :, :], 0.5, FLAGS.batch_size, weights, biases) loss_name_scope = ('gpud_%d_loss' % gpu_index) loss = tower_loss( loss_name_scope, logit, labels_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size]) grads1 = opt_stable.compute_gradients(loss, varlist1) grads2 = opt_finetuning.compute_gradients(loss, varlist2) tower_grads1.append(grads1) tower_grads2.append(grads2) logits.append(logit) logits = tf.concat(logits, 0) accuracy = tower_acc(logits, labels_placeholder) tf.summary.scalar('accuracy', accuracy) grads1 = average_gradients(tower_grads1) grads2 = average_gradients(tower_grads2) apply_gradient_op1 = opt_stable.apply_gradients(grads1) apply_gradient_op2 = opt_finetuning.apply_gradients( grads2, global_step=global_step) variable_averages = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY) variables_averages_op = variable_averages.apply( tf.trainable_variables()) train_op = tf.group(apply_gradient_op1, apply_gradient_op2, variables_averages_op) null_op = tf.no_op() # Create a saver for writing training checkpoints. restore_saver = tf.train.Saver( list(weights.values())[:-1] + list(biases.values())[:-1]) saver = tf.train.Saver(list(weights.values()) + list(biases.values()), max_to_keep=100) init = tf.global_variables_initializer() # Create a session for running Ops on the Graph. sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) sess.run(init) if os.path.isfile(model_filename) and use_pretrained_model: restore_saver.restore(sess, model_filename) # Create summary writter merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter( '/home/ankur/data/new_disk/3DCnn-output/visual_logs/train', sess.graph) test_writer = tf.summary.FileWriter( '/home/ankur/data/new_disk/3DCnn-output/visual_logs/test', sess.graph) for step in xrange(FLAGS.max_steps): start_time = time.time() train_images, train_labels, _, _, _ = input_data.read_clip_and_label( filename= '/home/ankur/data/new_disk/C3D-tensorflow/list/train_list.list', batch_size=FLAGS.batch_size * gpu_num, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP, crop_size=c3d_model.CROP_SIZE, shuffle=True) sess.run(train_op, feed_dict={ images_placeholder: train_images, labels_placeholder: train_labels }) duration = time.time() - start_time print('Step %d: %.3f sec' % (step, duration)) # Save a checkpoint and evaluate the model periodically. if (step) % 10 == 0 or (step + 1) == FLAGS.max_steps: saver.save(sess, os.path.join(model_save_dir, 'c3d_ucf_model'), global_step=step) print('Training Data Eval:') summary, acc = sess.run( [merged, accuracy], feed_dict={ images_placeholder: train_images, labels_placeholder: train_labels }) print("accuracy: " + "{:.5f}".format(acc)) train_writer.add_summary(summary, step) print('Validation Data Eval:') val_images, val_labels, _, _, _ = input_data.read_clip_and_label( filename= '/home/ankur/data/new_disk/C3D-tensorflow/list/test_list.list', batch_size=FLAGS.batch_size * gpu_num, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP, crop_size=c3d_model.CROP_SIZE, shuffle=True) summary, acc = sess.run([merged, accuracy], feed_dict={ images_placeholder: val_images, labels_placeholder: val_labels }) print("accuracy: " + "{:.5f}".format(acc)) test_writer.add_summary(summary, step) print("done")
'bc1': _variable_with_weight_decay('bc1', [64], 0.000), 'bc2': _variable_with_weight_decay('bc2', [128], 0.000), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.000), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.000), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.000), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.000), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.000), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.000), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.000), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.000), 'out': _variable_with_weight_decay('bout', [n_classes], 0.000), } outputs = c3d_model.inference_c3d( inputs_placeholder, 0.5, weights, biases) loss = calc_reward(outputs) train_op = tf.train.GradientDescentOptimizer(1e-3).minimize(loss) accuracy = tower_acc(outputs, labels_placeholder) tf.summary.scalar('accuracy', accuracy) merged = tf.summary.merge_all() with tf.Session() as sess: saver = tf.train.Saver() init = tf.global_variables_initializer()
def run_test(): # get the list of classes classes = get_class_list(CLASS_INDEX_FILE) model_name = "/home/jordanc/datasets/UCF-101/model_ckpts/c3d_ucf_model-4999" test_list_file = 'train-test-splits/testlist01.txt' num_test_videos = len(list(open(test_list_file, 'r'))) print("Number of test videos={}".format(num_test_videos)) # Get the sets of images and labels for training, validation, and images_placeholder, labels_placeholder = placeholder_inputs( FLAGS.batch_size * gpu_num) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.04, 0.00), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.00), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.00), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.00), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.00), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.04, 0.001), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.04, 0.002), 'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.04, 0.005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.04, 0.0), 'bc2': _variable_with_weight_decay('bc2', [128], 0.04, 0.0), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.04, 0.0), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.04, 0.0), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.04, 0.0), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.04, 0.0), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.04, 0.0), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.04, 0.0), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.04, 0.0), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.04, 0.0), 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.04, 0.0), } logits = [] activations = [] for gpu_index in range(0, gpu_num): with tf.device('/gpu:%d' % gpu_index): logit, activation = c3d_model.inference_c3d( images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size, :, :, :, :], 0.6, FLAGS.batch_size, weights, biases) logits.append(logit) activations.append(activation) logits = tf.concat(logits, 0) activations = tf.concat(activations, 0) norm_score = tf.nn.softmax(logits) saver = tf.train.Saver() sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) init = tf.global_variables_initializer() sess.run(init) # Create a saver for writing training checkpoints. saver.restore(sess, model_name) # And then after everything is built, start the training loop. bufsize = 0 write_file = open("predict_ret.txt", "w+", bufsize) next_start_pos = 0 all_steps = int((num_test_videos - 1) / (FLAGS.batch_size * gpu_num) + 1) for step in xrange(all_steps): # Fill a feed dictionary with the actual set of images and labels # for this particular training step. start_time = time.time() sample_output = c3d_model.read_clip_and_label( directory='/home/jordanc/datasets/UCF-101/UCF-101/', filename=test_list_file, batch_size=FLAGS.batch_size * gpu_num, start_pos=next_start_pos) test_images = sample_output[0] test_labels = sample_output[1] next_start_pos = sample_output[2] valid_len = sample_output[4] sample_names = sample_output[5] predict_score, activations_out = sess.run( [norm_score, activations], feed_dict={images_placeholder: test_images}) for i in range(0, valid_len): true_label = test_labels[i], top1_predicted_label = np.argmax(predict_score[i]) # Write results: true label, class prob for true label, predicted label, class prob for predicted label write_file.write('{}, {}, {}, {}\n'.format( true_label[0], predict_score[i][true_label], top1_predicted_label, predict_score[i][top1_predicted_label])) write_file.close() print("done")
def __init__(self, pre, options): self._options = options global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False) images_placeholder, labels_placeholder = self.placeholder_inputs( pre, options) self._inputs = images_placeholder self._targets = labels_placeholder tower_grads1 = [] tower_grads2 = [] logits = [] opt1 = tf.train.AdamOptimizer(1e-4) opt2 = tf.train.AdamOptimizer(2e-4) for gpu_index in range(0, options.gpus): with tf.device('/gpu:%d' % gpu_index): with tf.name_scope('%s_%d' % ('gestabase', gpu_index)) as scope: with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': self._variable_with_weight_decay( 'wc1', [3, 3, 3, pre.num_channels, 64], 0.0005), 'wc2': self._variable_with_weight_decay( 'wc2', [3, 3, 3, 64, 128], 0.0005), 'wc3a': self._variable_with_weight_decay( 'wc3a', [3, 3, 3, 128, 256], 0.0005), 'wc3b': self._variable_with_weight_decay( 'wc3b', [3, 3, 3, 256, 256], 0.0005), 'wc4a': self._variable_with_weight_decay( 'wc4a', [3, 3, 3, 256, 512], 0.0005), 'wc4b': self._variable_with_weight_decay( 'wc4b', [3, 3, 3, 512, 512], 0.0005), 'wc5a': self._variable_with_weight_decay( 'wc5a', [3, 3, 3, 512, 512], 0.0005), 'wc5b': self._variable_with_weight_decay( 'wc5b', [3, 3, 3, 512, 512], 0.0005), # changed 'wd1': self._variable_with_weight_decay( 'wd1', [8192, 4096], 0.0005), 'wd2': self._variable_with_weight_decay( 'wd2', [4096, 4096], 0.0005), 'out': self._variable_with_weight_decay( 'wout', [4096, pre.num_labels], 0.0005) } biases = { 'bc1': self._variable_with_weight_decay( 'bc1', [64], 0.000), 'bc3a': self._variable_with_weight_decay( 'bc3a', [256], 0.000), 'bc2': self._variable_with_weight_decay( 'bc2', [128], 0.000), 'bc3b': self._variable_with_weight_decay( 'bc3b', [256], 0.000), 'bc4a': self._variable_with_weight_decay( 'bc4a', [512], 0.000), 'bc4b': self._variable_with_weight_decay( 'bc4b', [512], 0.000), 'bc5a': self._variable_with_weight_decay( 'bc5a', [512], 0.000), 'bc5b': self._variable_with_weight_decay( 'bc5b', [512], 0.000), 'bd1': self._variable_with_weight_decay( 'bd1', [4096], 0.000), 'bd2': self._variable_with_weight_decay( 'bd2', [4096], 0.000), 'out': self._variable_with_weight_decay( 'bout', [pre.num_labels], 0.000), } varlist1 = weights.values() varlist2 = biases.values() logit = c3d_model.inference_c3d( images_placeholder[gpu_index * options.batch_size:(gpu_index + 1) * options.batch_size, :, :, :, :], options.dropout, options.batch_size, weights, biases) loss = self.tower_loss(scope, logit, labels_placeholder) grads1 = opt1.compute_gradients(loss, varlist1) grads2 = opt2.compute_gradients(loss, varlist2) tower_grads1.append(grads1) tower_grads2.append(grads2) logits.append(logit) tf.get_variable_scope().reuse_variables() logits = tf.concat(0, logits) self._norm_score = tf.nn.softmax(logits) grads1 = self.average_gradients(tower_grads1) grads2 = self.average_gradients(tower_grads2) apply_gradient_op1 = opt1.apply_gradients(grads1) apply_gradient_op2 = opt2.apply_gradients(grads2, global_step=global_step) variable_averages = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY) variables_averages_op = variable_averages.apply( tf.trainable_variables()) train_op = tf.group(apply_gradient_op1, apply_gradient_op2, variables_averages_op) null_op = tf.no_op() self._summary = tf.summary.merge_all() self._train_op = train_op self._loss = loss
def run_test(): # gets the path to the model file to be tested. model_name = "./C3D-tensorflow/sports1m_finetuning_ucf101.model" # get the list file that holds the test set information test_list_file = 'test.list' # printing the amount of test data. num_test_videos = len(list(open(test_list_file, 'r'))) print("Number of test videos={}".format(num_test_videos)) # initializing a batch sample of the input model corresponding to the tensor images_placeholder, labels_placeholder = placeholder_inputs(FLAGS.batch_size * gpu_num) with tf.variable_scope('var_name') as var_scope: # based on the definition of C3D network structure # Learning Spatiotemporal Features with 3D Convolutional Networks # https://arxiv.org/pdf/1412.0767.pdf weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.04, 0.00), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.00), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.00), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.00), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.00), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.04, 0.001), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.04, 0.002), 'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.04, 0.005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.04, 0.0), 'bc2': _variable_with_weight_decay('bc2', [128], 0.04, 0.0), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.04, 0.0), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.04, 0.0), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.04, 0.0), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.04, 0.0), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.04, 0.0), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.04, 0.0), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.04, 0.0), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.04, 0.0), 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.04, 0.0) } logits = [] # traverse the GPU, distribute a batch data equally to each GPU for testing, # and add the results of network output to the list. for gpu_index in range(0, gpu_num): with tf.device('/gpu:%d' % gpu_index): logit = c3d_model.inference_c3d(images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size, :, :, :, :], 0.6, FLAGS.batch_size, weights, biases) logits.append(logit) # calculating the softmax score of the network output. logits = tf.concat(logits, 0) norm_score = tf.nn.softmax(logits) # creating a Saver object that holds all the variables. saver = tf.train.Saver() # creating a session that runs all the operations in the calculation diagram # and automatically selecting the device to run on. sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) init = tf.global_variables_initializer() # initializing all variables. sess.run(init) # loading the model to test. saver.restore(sess, model_name) # open predict_ret.txt. # https://stackoverflow.com/questions/45263064/how-can-i-fix-this-valueerror-cant-have-unbuffered-text-i-o-in-python-3/45263101 # bufsize = 0 # write_file = open("predict_ret.txt", "w+", bufsize) write_file = open("predict_ret.txt", "w+") next_start_pos = 0 # counting test times all_steps = int((num_test_videos - 1) / (FLAGS.batch_size * gpu_num) + 1) # get a batch of test samples in order for testing each time, # and get the probability and prediction category # of each batch test sample in each category. # writing the information for each test sample to predict_ret.txt. # the format of the information written to predict_ret.txt is # [ground truth][class probability for true label][predicted label][class probability for predicted label] for step in xrange(all_steps): # fill a feed dictionary with the actual set of images and labels # for this particular training step. test_images, test_labels, next_start_pos, _, valid_len = \ input_data.read_clip_and_label( test_list_file, FLAGS.batch_size * gpu_num, start_pos=next_start_pos ) predict_score = norm_score.eval( session=sess, feed_dict={images_placeholder: test_images} ) for i in range(0, valid_len): true_label = test_labels[i], top1_predicted_label = np.argmax(predict_score[i]) # write results to predict_ret.txt write_file.write('{}, {}, {}, {}\n'.format( # ground truth true_label[0], # class probability for true label predict_score[i][true_label], # predicted label top1_predicted_label, # class probability for predicted label predict_score[i][top1_predicted_label]) ) write_file.close() print("done")
def run_test(): with tf.variable_scope('var_name') as var_scope: images_placeholder, labels_placeholder = placeholder_inputs(BATCH_SIZE) weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], wd=0.0005), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], wd=0.0005), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], wd=0.0005), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], wd=0.0005), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], wd=0.0005), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], wd=0.0005), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], wd=0.0005), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], wd=0.0005), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], wd=0.0005), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], wd=0.0005), # 'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], wd=0.0005), 'in': _variable_with_weight_decay('in', [4096, c3d_model.NUM_HIDDEN_UNIT], wd=0.0005), 'out': _variable_with_weight_decay('out', [c3d_model.NUM_HIDDEN_UNIT, c3d_model.NUM_CLASSES], wd=0.0005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.000), 'bc2': _variable_with_weight_decay('bc2', [128], 0.000), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.000), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.000), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.000), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.000), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.000), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.000), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.000), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.000), # 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.000), 'in': _variable_with_weight_decay('in', [c3d_model.NUM_HIDDEN_UNIT], 0.000), 'out': _variable_with_weight_decay('out', [c3d_model.NUM_CLASSES], 0.000) } dense1 = c3d_model.inference_c3d(images_placeholder, 1.0, BATCH_SIZE, weights, biases) logit = c3d_model.RNN(dense1, batch_size=BATCH_SIZE, weights=weights, biases=biases) prediction = tf.nn.softmax(logit) accuracy = tower_acc(prediction, labels_placeholder) init = tf.global_variables_initializer() config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) saver = tf.train.Saver() sess.run(init) saver.restore(sess, "my_net/save_net.ckpt") next_start_pos = 0 acc_mean = 0.0 acc_clip = [] # print("weights:", sess.run(weights)) # print("weights:", sess.run(biases)) clip_start_pos = 0 for step in xrange(MAX_STEPS): # Fill a feed dictionary with the actual set of images and labels # for this particular training step. start_time = time.time() test_images, test_labels, _,_, _ = input_data.read_clip_and_label( filename='dataset/test_data/', batch_size=BATCH_SIZE, start_pos=next_start_pos, clip_start_pos= clip_start_pos, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP, crop_size=c3d_model.CROP_SIZE, shuffle=True ) if((step+1) % 2 == 0): clip_start_pos += 16 next_start_pos = 0 else: next_start_pos += 10 print("test step: " + str(step+1)) acc = sess.run(accuracy, feed_dict={images_placeholder: test_images, labels_placeholder: test_labels }) print("accuracy: " + "{:.5f}".format(acc)) acc_mean += acc if((step+1) % 2 == 0): global acc_mean acc_mean = acc_mean/2 acc_clip.append(acc_mean) acc_mean = 0.0 return acc_clip
def run_training(): # Get the sets of images and labels for training, validation, and # Tell TensorFlow that the model will be built into the default Graph. # Create model directory if not os.path.exists(model_save_dir): os.makedirs(model_save_dir) if not os.path.exists(ckpt_saved): use_pretrained_model = True model_filename = "model/sports1m_finetuning_ucf101.model" else: use_pretrained_model = False model_filename = "./checkpoint/" with tf.name_scope('c3d'): # with tf.Graph().as_default(): global_step = tf.get_variable( 'global_step', [], initializer=tf.constant_initializer(0), trainable=False ) images_placeholder, labels_placeholder = placeholder_inputs( FLAGS.batch_size * gpu_num ) tower_grads1 = [] tower_grads2 = [] logits = [] opt_stable = tf.train.AdamOptimizer(1e-4) opt_finetuning = tf.train.AdamOptimizer(1e-3) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.0005), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.0005), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.0005), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.0005), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.0005), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.0005), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.0005), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.0005), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.0005), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.0005), 'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.0005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.000), 'bc2': _variable_with_weight_decay('bc2', [128], 0.000), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.000), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.000), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.000), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.000), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.000), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.000), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.000), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.000), 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.000), } print ('num_classes: ', c3d_model.NUM_CLASSES) for gpu_index in range(0, gpu_num): with tf.device('/gpu:%d' % gpu_index): varlist2 = [ weights['out'],biases['out'] ] # varlist1 = list( set(weights.values() + biases.values()) - set(varlist2) ) varlist1 = list((set(weights.values()) | set(biases.values())) - set(varlist2)) logit = c3d_model.inference_c3d( images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size,:,:,:,:], 0.5, FLAGS.batch_size, weights, biases ) loss_name_scope = ('gpud_%d_loss' % gpu_index) loss = tower_loss( loss_name_scope, logit, labels_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size] ) grads1 = opt_stable.compute_gradients(loss, varlist1) grads2 = opt_finetuning.compute_gradients(loss, varlist2) tower_grads1.append(grads1) tower_grads2.append(grads2) logits.append(logit) logits = tf.concat(logits,0) accuracy = tower_acc(logits, labels_placeholder) tf.summary.scalar('accuracy', accuracy) grads1 = average_gradients(tower_grads1) grads2 = average_gradients(tower_grads2) apply_gradient_op1 = opt_stable.apply_gradients(grads1) apply_gradient_op2 = opt_finetuning.apply_gradients(grads2, global_step=global_step) variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY) variables_averages_op = variable_averages.apply(tf.trainable_variables()) train_op = tf.group(apply_gradient_op1, apply_gradient_op2, variables_averages_op) null_op = tf.no_op() # Restore all the layers excluding the last one exclude_variables = ['var_name/wout', 'var_name/bout'] restore_variables = [v.name for v in tf.trainable_variables(scope='var_name')] # all_variables = tf.contrib.framework.get_variables_to_restore(exclude=exclude_variables + restore_variables) # Initialization operation from scratch for the new output layer fout_variables = tf.contrib.framework.get_variables_by_suffix('out') fc8_init = tf.variables_initializer(fout_variables) # # Create a saver for writing training checkpoints. saver_variables = tf.trainable_variables(scope='var_name') saver = tf.train.Saver(saver_variables) # Create a session for running Ops on the Graph. sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) variables_to_restore = tf.contrib.framework.get_variables_to_restore(include=restore_variables, exclude=exclude_variables) if use_pretrained_model: # saver.restore(sess, model_filename) init_fn = tf.contrib.framework.assign_from_checkpoint_fn(model_filename, variables_to_restore) step = 0 else: # saver.restore(sess, tf.train.latest_checkpoint(new_model_filename)) ckpt = tf.train.get_checkpoint_state(model_filename) # Extract from checkpoint filename step = int(os.path.basename(ckpt.model_checkpoint_path).split('-')[1])+1 init_fn = tf.contrib.framework.assign_from_checkpoint_fn(model_filename+"c3d_ucf_model-"+str(step-1), variables_to_restore) print("step number: ", step) # Initialize all variables sess.run(tf.global_variables_initializer()) # Load the pretrained weights init_fn(sess) # Initialize the weights sess.run(fc8_init) # Create summary writter merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter('./visual_logs/train', sess.graph) test_writer = tf.summary.FileWriter('./visual_logs/test', sess.graph) while step < FLAGS.max_steps: start_time = time.time() Batch_size=FLAGS.batch_size * gpu_num train_images, train_labels = read_tfRecords.extract_tfRecords( Batch_size = Batch_size, phase = 'train', sess = sess ) print('batch size: ', Batch_size) sess.run(train_op, feed_dict={ images_placeholder: train_images, labels_placeholder: train_labels }) duration = time.time() - start_time print('Step %d: %.3f sec' % (step, duration)) step = step+1 # Save a checkpoint and evaluate the model periodically. if (step-1) % 10 == 0 or (step) == FLAGS.max_steps: saver.save(sess, os.path.join(model_save_dir, 'c3d_ucf_model'), global_step=step-1) print('Training Data Eval:') summary, acc = sess.run( [merged, accuracy], feed_dict={images_placeholder: train_images, labels_placeholder: train_labels }) print ("accuracy: " + "{:.5f}".format(acc)) train_writer.add_summary(summary, step) print('Validation Data Eval:') # training = False val_images, val_labels = read_tfRecords.extract_tfRecords( Batch_size = Batch_size, phase = 'test', sess = sess ) summary, acc = sess.run( [merged, accuracy], feed_dict={images_placeholder: val_images, labels_placeholder: val_labels }) print ("accuracy: " + "{:.5f}".format(acc)) test_writer.add_summary(summary, step) print("done")
def run_test(): model_name = FLAGS.checkpoint test_list_file = FLAGS.TEST_LIST_PATH num_test_videos = len(pd.read_csv(test_list_file)) print(("Number of test videos={}".format(num_test_videos))) # Get the sets of images and labels for training, validation, and images_placeholder, labels_placeholder = placeholder_inputs(FLAGS.batch_size * FLAGS.gpu_num) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 1, 64], 0.04, 0.00), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.00), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.00), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.00), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.00), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc6a': _variable_with_weight_decay('wc6a', [3, 3, 3, 512, 256], 0.0005, 0.00), 'wc6b': _variable_with_weight_decay('wc6b', [3, 3, 3, 256, 128], 0.0005, 0.00), 'wc7a': _variable_with_weight_decay('wc7a', [3, 3, 3, 128, 64], 0.0005, 0.00), 'wc7b': _variable_with_weight_decay('wc7b', [3, 3, 3, 64, 32], 0.0005, 0.00), 'wc8a': _variable_with_weight_decay('wc8a', [3, 3, 3, 32, 16], 0.0005, 0.00), 'wc8b': _variable_with_weight_decay('wc8b', [3, 3, 3, 16, 8], 0.0005, 0.00), 'wd1': _variable_with_weight_decay('wd1', [128, 64], 0.0005,0.00), 'out': _variable_with_weight_decay('wout', [128, c3d_model.NUM_CLASSES], 0.0005, 0.00) #'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.04, 0.001), #'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.04, 0.002), #'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.04, 0.005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.04, 0.0), 'bc2': _variable_with_weight_decay('bc2', [128], 0.04, 0.0), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.04, 0.0), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.04, 0.0), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.04, 0.0), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.04, 0.0), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.04, 0.0), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.04, 0.0), 'bc6a': _variable_with_weight_decay('bc6a', [256], 0.04, 0.000), 'bc6b': _variable_with_weight_decay('bc6b', [128], 0.04, 0.000), 'bc7a': _variable_with_weight_decay('bc7a', [64], 0.04, 0.000), 'bc7b': _variable_with_weight_decay('bc7b', [32], 0.04, 0.000), 'bc8a': _variable_with_weight_decay('bc8a', [16], 0.04, 0.000), 'bc8b': _variable_with_weight_decay('bc8b', [8], 0.04, 0.000), #'bd1': _variable_with_weight_decay('bd1', [4096], 0.04, 0.0), #'bd2': _variable_with_weight_decay('bd2', [4096], 0.04, 0.0), 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.04, 0.0), } logits = [] for gpu_index in range(0, FLAGS.gpu_num): with tf.device('/gpu:%d' % gpu_index): logit = c3d_model.inference_c3d(images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size,:,:,:,:], 0.6, FLAGS.batch_size, weights, biases) logits.append(logit) logits = tf.concat(logits,0) norm_score = tf.nn.softmax(logits) saver = tf.train.Saver() sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) init = tf.global_variables_initializer() sess.run(init) # Create a saver for writing training checkpoints. saver.restore(sess, model_name) # And then after everything is built, start the training loop. bufsize = 4 #write_file = open("./output/work.txt", "w+", bufsize) next_start_pos = 0 all_steps = int((num_test_videos - 1) / (FLAGS.batch_size * FLAGS.gpu_num) + 1) loss=[] accuracy_epoch = 0 accuracy_out = 0 start_time = time.time() idlist_ = [] retlist_ = [] df = pd.read_csv(test_list_file) for step in range(all_steps): test_images, test_labels, next_start_pos, dirnames_, valid_len = \ input_data.read_clip_and_label( FLAGS.dataset_dir, test_list_file, FLAGS.batch_size * FLAGS.gpu_num, start_pos = next_start_pos, crop_size = c3d_model.CROP_SIZE, shuffle = False, ) print("=====", step) predict_score = norm_score.eval( session = sess, feed_dict = {images_placeholder: test_images} ) for i in range(0, valid_len): true_label = test_labels[i] top1_predicted_label = np.argmax(predict_score[i]) # Write results: true label, class prob for true label, predicted label, class prob for predicted label if true_label == top1_predicted_label: accuracy_out += 1 #loss.append(true_label[0]-top1_predicted_label) idlist_.append(dirnames_[0].split('/')[-1]) retlist_.append(top1_predicted_label) #print(true_label, predict_score, top1_predicted_label) #write_file.write('{}, {}, {}, {}\n'.format( # true_label[0], # predict_score[i][true_label], # top1_predicted_label, # predict_score[i][top1_predicted_label])) #accuracy_epoch += accuracy_out #write_file.close() end_time = time.time() csvfile = FLAGS.csv_output df=pd.DataFrame({'id':idlist_,'ret':retlist_}) df.to_csv(csvfile, index=False, sep=',') print("done")
def build_c3d_model(): """ build c3d model :return: norm_score: sess: """ #model_name = "pretrained_model/c3d_ucf101_finetune_whole_iter_20000_TF.model.mdlp" #model_name = "pretrained_model/conv3d_deepnetA_sport1m_iter_1900000_TF.model" model_name = "pretrained_model/sports1m_finetuning_ucf101.model" # Get the sets of images and labels for training, validation, and with tf.compat.v1.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.04, 0.00), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.00), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.00), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.00), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.00), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.04, 0.001), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.04, 0.002), 'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.04, 0.005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.04, 0.0), 'bc2': _variable_with_weight_decay('bc2', [128], 0.04, 0.0), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.04, 0.0), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.04, 0.0), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.04, 0.0), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.04, 0.0), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.04, 0.0), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.04, 0.0), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.04, 0.0), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.04, 0.0), 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.04, 0.0), } logits = [] for gpu_index in range(0, gpu_num): with tf.device('/gpu:%d' % gpu_index): logit = c3d_model.inference_c3d( images_placeholder[0 * FLAGS.batch_size:(0 + 1) * FLAGS.batch_size, :, :, :, :], 0.6, FLAGS.batch_size, weights, biases) logits.append(logit) logits = tf.concat(logits, 0) norm_score = tf.nn.softmax(logits) saver = tf.compat.v1.train.Saver() sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto( allow_soft_placement=True)) init = tf.compat.v1.global_variables_initializer() sess.run(init) # Create a saver for writing training checkpoints. saver.restore(sess, model_name) return norm_score, sess
def run_training(): if not os.path.exists(model_save_dir): os.makedirs(model_save_dir) use_pretrained_model = True model_filename = "./sports1m_finetuning_ucf101.model" with tf.Graph().as_default(): global_step = tf.get_variable( 'global_step', [], initializer=tf.constant_initializer(0), trainable=False ) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.005), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.005), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.005), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.005), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.005), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.005), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.005), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.005), #'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.005), #'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.005), #'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.000), 'bc2': _variable_with_weight_decay('bc2', [128], 0.000), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.000), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.000), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.000), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.000), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.000), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.000), #'bd1': _variable_with_weight_decay('bd1', [4096], 0.000), #'bd2': _variable_with_weight_decay('bd2', [4096], 0.000), #'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.000), } fcn_weights = { 'wconv6': _variable_with_weight_decay('conv6', [1, 4, 4, 512, 512], 0.005), 'wconv7': _variable_with_weight_decay('conv7', [1, 7, 7, 512, 512], 0.005), 'wup6': _variable_with_weight_decay('up6', [2, 1, 1, 4096, 512], 0.005), 'wup7': _variable_with_weight_decay('up7', [2, 1, 1, 4096, 4096], 0.005), 'wup8': _variable_with_weight_decay('up8', [2, 1, 1, fcn_model.NUM_CLASSES, 4096], 0.005), } fcn_biases = { 'bconv6': _variable_with_weight_decay('bconv6', [512], 0.000), 'bconv7': _variable_with_weight_decay('bconv7', [512], 0.000), 'bup6': _variable_with_weight_decay('bup6', [4096], 0.000), 'bup7': _variable_with_weight_decay('bup7', [4096], 0.000), 'bup8': _variable_with_weight_decay('bup8', [fcn_model.NUM_CLASSES], 0.000), } with tf.name_scope('inputs'): images_placeholder, labels_placeholder, keep_pro = placeholder_inputs( FLAGS.batch_size ) varlist1 = list( set(fcn_weights.values() + fcn_biases.values()) ) varlist2 = list( set(weights.values() + biases.values()) ) feature_map = c3d_model.inference_c3d( images_placeholder, keep_pro, FLAGS.batch_size, weights, biases ) logit=fcn_model.inference_pool54( feature_map, keep_pro, FLAGS.batch_size, fcn_weights, fcn_biases ) loss = fcn_model_loss( logit, labels_placeholder, FLAGS.batch_size ) SGD_cdc = tf.train.GradientDescentOptimizer(1e-4).minimize(loss, var_list = varlist1) SGD_c3d = tf.train.GradientDescentOptimizer(1e-5).minimize(loss, var_list = varlist2) accuracy = tower_acc(logit, labels_placeholder, FLAGS.batch_size) tf.summary.scalar('accuracy', accuracy) variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY) variables_averages_op = variable_averages.apply(tf.trainable_variables()) train_op = tf.group(SGD_cdc, SGD_c3d, variables_averages_op) null_op = tf.no_op() # Create a saver for writing training checkpoints. saver = tf.train.Saver(weights.values() + biases.values()) new_saver = tf.train.Saver(weights.values() + biases.values()+ fcn_weights.values() + fcn_biases.values()) init = tf.global_variables_initializer() # Create a session for running Ops on the Graph. sess = tf.Session( config=tf.ConfigProto(allow_soft_placement=True) ) sess.run(init) merged = tf.summary.merge_all() if os.path.isfile(model_filename) and use_pretrained_model: print 'loading pretrained_model....' saver.restore(sess, model_filename) print 'complete!' # Create summary writter train_writer = tf.summary.FileWriter('./visual_logs/SGD_pool54_visual_logs/train', sess.graph) test_writer = tf.summary.FileWriter('./visual_logs/SGD_pool54_visual_logs/test', sess.graph) video_list = [] position = -1 for step in xrange(FLAGS.max_steps+1): start_time = time.time() train_images, train_labels, _, _, video_list, position = input_train_data.read_clip_and_label( filename='annotation/train.list', batch_size=FLAGS.batch_size, start_pos=position, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP, crop_size=c3d_model.CROP_SIZE, video_list=video_list ) sess.run(train_op, feed_dict={ images_placeholder: train_images, labels_placeholder: train_labels, keep_pro: 0.5 }) duration = time.time() - start_time print('Batchnum %d: %.3f sec' % (step, duration)) if (step) %2 == 0 or (step + 1) == FLAGS.max_steps: print('Step %d/%d: %.3f sec' % (step, FLAGS.max_steps, duration)) print('Training Data Eval:') summary,loss_train,acc = sess.run( [merged, loss, accuracy], feed_dict={ images_placeholder: train_images, labels_placeholder: train_labels, keep_pro: 1 }) print 'loss: %f' % np.mean(loss_train) print ("accuracy: " + "{:.5f}".format(acc)) train_writer.add_summary(summary, step) if (step) %10 == 0 or (step + 1) == FLAGS.max_steps: print('Validation Data Eval:') val_images, val_labels, _, _, _, _ = input_train_data.read_clip_and_label( filename='annotation/test.list', batch_size=FLAGS.batch_size, start_pos=-1, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP, crop_size=c3d_model.CROP_SIZE, video_list=[] ) summary,loss_val, acc = sess.run( [merged, loss, accuracy], feed_dict={ images_placeholder: val_images, labels_placeholder: val_labels, keep_pro: 1 }) print 'loss: %f' % np.mean(loss_val) print ("accuracy: " + "{:.5f}".format(acc)) test_writer.add_summary(summary, step) # Save the model checkpoint periodically. if step > 1 and step % 200 == 0: checkpoint_path = os.path.join('./models/SGD_pool54', 'model.ckpt') new_saver.save(sess, checkpoint_path, global_step=global_step) print("done")
def run_test(ds_dir, mean_file, model_name, test_list_file, batch_size): tf.reset_default_graph() try: FLAGS = flags.FLAGS FLAGS.batch_size = batch_size except: flags.DEFINE_integer('batch_size', batch_size, 'Batch size.') FLAGS = flags.FLAGS #model_name = "./models-5sec/c3d_ucf_model-4999" #model_name = "./models.5sec/c3d_ucf_model-75450" #model_name = "./models-1sec/c3d_ucf_model-4999" #model_name = "./models.5sec.summarized.1sec/c3d_ucf_model-4999" #model_name = "./models-multi-5sec-5sec_sum_1/c3d_ucf_model-4999" #model_name = "./models-multi-5-5sum1/c3d_ucf_model-9999" num_test_videos = len(list(open(test_list_file, 'r'))) print("Number of test videos={}".format(num_test_videos)) # max_bt_sz = -1;min # # for factor in range(1, 31): # if num_test_videos%factor==0: # max_bt_sz=factor # if max_bt_sz == 1: # print("no good batchsize available, setting to 25") # max_bt_sz = 20 # FLAGS.batch_size = max_bt_sz # print("batch size:", FLAGS.batch_size) # Get the sets of images and labels for testing images_placeholder, labels_placeholder = placeholder_inputs( FLAGS.batch_size * gpu_num) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.04, 0.00), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.00), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.00), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.00), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.00), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.04, 0.001), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.04, 0.002), 'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.04, 0.005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.04, 0.0), 'bc2': _variable_with_weight_decay('bc2', [128], 0.04, 0.0), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.04, 0.0), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.04, 0.0), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.04, 0.0), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.04, 0.0), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.04, 0.0), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.04, 0.0), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.04, 0.0), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.04, 0.0), 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.04, 0.0), } logits = [] for gpu_index in range(0, gpu_num): with tf.device('/gpu:%d' % gpu_index): logit = c3d_model.inference_c3d( images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size, :, :, :, :], 0, FLAGS.batch_size, weights, biases) logits.append(logit) logits = tf.concat(logits, 0) norm_score = tf.nn.softmax(logits) saver = tf.train.Saver() sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) init = tf.global_variables_initializer() sess.run(init) # Restoring a saved model. if not model_name.__contains__(".meta"): saver = tf.train.import_meta_graph(model_name + '.meta') else: # saver = tf.train.import_meta_graph(model_name) var_list = [v for v in tf.trainable_variables()] saver = tf.train.Saver(weights.values() + biases.values()) saver.restore(sess, model_name) # And then after everything is built, start the testing loop. bufsize = 0 write_file = open("predict_ret.txt", "w+", bufsize) next_start_pos = 0 all_steps = int((num_test_videos - 1) / (FLAGS.batch_size * gpu_num) + 1) print("num_test_videos, batch_size, gpu_num,all steps", num_test_videos, FLAGS.batch_size, gpu_num, all_steps) total_testing_duration = 0 for step in range(all_steps): # Fill a feed dictionary with the actual set of images and labels # for this particular testing step. start_time = time.time() # try: test_images, test_labels, next_start_pos, _, valid_len = \ input_data.read_clip_and_label( ds_dir, mean_file, test_list_file, FLAGS.batch_size * gpu_num, start_pos=next_start_pos, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP ) # except: # print("exception occured loading at step:", step) # try: predict_score = norm_score.eval( session=sess, feed_dict={images_placeholder: test_images}) # except: # print("exception occured prediction at step:", step) duration = time.time() - start_time print('Step %d: %.3f sec' % (step, duration), 'next start index:', next_start_pos) total_testing_duration += duration # try: for i in range(0, valid_len): true_label = test_labels[i], top1_predicted_label = np.argmax(predict_score[i]) # Write results: true label, class prob for true label, predicted label, class prob for predicted label write_file.write('{}, {}, {}, {}\n'.format( true_label[0], predict_score[i][true_label], top1_predicted_label, predict_score[i][top1_predicted_label])) # except: # print ("exception occured saving predictions at step:", step) # break # test only 1 batch print('Prediction time taken =', total_testing_duration) import datetime now = datetime.datetime.now() with open('stats.txt', 'a') as f: f.write(now.strftime("%Y-%m-%d %H:%M\n")) f.write(" testing time:" + str(total_testing_duration) + "\n") write_file.close() print("done")
def run_test(): test_list_file = './test.list' num_test_videos = len(list(open(test_list_file,'r'))) print("Number of test videos={}".format(num_test_videos)) # Get the sets of images and labels for training, validation, and images_placeholder, labels_placeholder = placeholder_inputs(FLAGS.batch_size * gpu_num) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.04, 0.00), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.00), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.00), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.00), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.00), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.04, 0.001), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.04, 0.002), 'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.04, 0.005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.04, 0.0), 'bc2': _variable_with_weight_decay('bc2', [128], 0.04, 0.0), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.04, 0.0), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.04, 0.0), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.04, 0.0), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.04, 0.0), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.04, 0.0), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.04, 0.0), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.04, 0.0), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.04, 0.0), 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.04, 0.0), } logits = [] for gpu_index in range(0, gpu_num): with tf.device('/gpu:%d' % gpu_index): logit = c3d_model.inference_c3d(images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size,:,:,:,:], 0.6, FLAGS.batch_size, weights, biases) logits.append(logit) logits = tf.concat(logits,0) norm_score = tf.nn.softmax(logits) saver = tf.train.Saver() sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) init = tf.global_variables_initializer() sess.run(init) # Create a saver for writing training checkpoints. model_file = tf.train.latest_checkpoint(model_save_dir) saver.restore(sess, model_file) write_file = open("key_frames.list", "w+") next_start_pos = 0 right_count = 0 random_right_count = 0 random_frame_index = 0 random_test_loop_count = 100 top1_predicted_label = 0 for step in xrange(num_test_videos): start_time = time.time() test_images, test_labels, next_start_pos, read_dirnames, frame_start_index = \ input_data.read_vedio_clips_and_label( test_list_file, start_pos=step ) predict_score = norm_score.eval( session=sess, feed_dict={images_placeholder: test_images} ) valid_len = len(test_images) key_frame_index = 0 true_label = test_labels[key_frame_index] max_accuracy = predict_score[key_frame_index][true_label] #print("read_dirnames: " + read_dirnames[0]) #print("true_label: %d" % true_label) for i in range(0, valid_len): if (max_accuracy < predict_score[i][true_label]): key_frame_index = i max_accuracy = predict_score[i][true_label] #统计选择关键帧情况的正确数据 top1_predicted_label = np.argmax(predict_score[key_frame_index]) if (top1_predicted_label == true_label): right_count = right_count + 1 #统计随机选择一帧情况的正确数据 for i in xrange(0, random_test_loop_count): random_frame_index = random.randrange(0,valid_len) top1_predicted_label = np.argmax(predict_score[random_frame_index]) if (top1_predicted_label == true_label): random_right_count = random_right_count + 1 # Write results: dircrector name, true label, frame start index, key frame index, max accuracy write_file.write('{} {} {} {} {}\n'.format( read_dirnames[key_frame_index], true_label, frame_start_index, key_frame_index, predict_score[key_frame_index][true_label])) accuracy = right_count / num_test_videos random_accuracy = random_right_count / (random_test_loop_count * num_test_videos) write_file.write("model file: " + model_file + "\n") write_file.write("total count: %d\n" % num_test_videos) write_file.write("right count: %d\n" % right_count) write_file.write("random right count: %d\n" % random_right_count) write_file.write("random test loop count: %d\n" % random_test_loop_count) write_file.write("key frame case accuracy: " + "{:.5f}\n".format(accuracy)) write_file.write("random frame case accuracy: " + "{:.5f}\n".format(random_accuracy)) print("model file: " + model_file) print("total count: %d" % num_test_videos) print("right count: %d" % right_count) print("random right count: %d" % random_right_count) print("random test loop count: %d" % random_test_loop_count) print("accuracy: " + "{:.5f}".format(accuracy)) print("random accuracy: " + "{:.5f}".format(random_accuracy)) write_file.close() print("done")
def run_test(): model_name = __folder_params.home+"../model_weights/sports1m_finetuning_ucf101.model" # https://www.dropbox.com/sh/8wcjrcadx4r31ux/AAAkz3dQ706pPO8ZavrztRCca?dl=0&preview=sports1m_finetuning_ucf101.model test_list_files = [v for v in utils.get_files_iterator('video')] num_test_videos = len(test_list_files) print("Number of test videos={}".format(num_test_videos)) # Get the sets of images and labels for training, validation, and images_placeholder, labels_placeholder = placeholder_inputs(FLAGS.batch_size * gpu_num) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.04, 0.00), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.00), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.00), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.00), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.00), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.04, 0.001), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.04, 0.002), 'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.04, 0.005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.04, 0.0), 'bc2': _variable_with_weight_decay('bc2', [128], 0.04, 0.0), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.04, 0.0), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.04, 0.0), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.04, 0.0), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.04, 0.0), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.04, 0.0), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.04, 0.0), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.04, 0.0), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.04, 0.0), 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.04, 0.0), } logits = [] for gpu_index in range(0, gpu_num): with tf.device('/gpu:%d' % gpu_index): logit = c3d_model.inference_c3d(images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size,:,:,:,:], 0.6, FLAGS.batch_size, weights, biases) logits.append(logit) logits = tf.concat(logits,0) norm_score = tf.nn.softmax(logits) saver = tf.train.Saver() sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) init = tf.global_variables_initializer() sess.run(init) # Create a saver for writing training checkpoints. saver.restore(sess, model_name) # And then after everything is built, start the training loop. bufsize = 0 next_start_pos = 0 all_steps = int((num_test_videos - 1) / (FLAGS.batch_size * gpu_num) + 1) for step in xrange(all_steps): # Fill a feed dictionary with the actual set of images and labels # for this particular training step. start_time = time.time() test_images, next_start_pos, dirnames, valid_len = \ input_data.read_clip_and_label( test_list_files, FLAGS.batch_size * gpu_num, start_pos=next_start_pos ) predict_score = norm_score.eval( session=sess, feed_dict={images_placeholder: test_images} ) final_paths= [utils.adress_file(video_path, "C3D") for video_path in dirnames] for i in range(0, valid_len): top1_predicted_label = predict_score[i] # Write results: true label, class prob for true label, predicted label, class prob for predicted label with open(final_paths[i], 'w+') as write_file: for v in top1_predicted_label: write_file.write("%.8f " % (v)) print("done")
def evaluate(in_x): with tf.Graph().as_default() as g: # Get the image and the labels placeholder images_placeholder, labels_placeholder = placeholder_inputs(2) # Build the Graph that computes the logits predictions from the inference # model. with tf.variable_scope('c3d_var'): logits = c3d_model.inference_c3d(images_placeholder) #top_k_op = tf.nn.in_top_k(logits, labels_placeholder, 1) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( c3d_model.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) config = tf.ConfigProto() config.intra_op_parallelism_threads = 4 config.inter_op_parallelism_threads = 4 config.graph_options.optimizer_options.opt_level = -1 with tf.Session(config=config) as sess: ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: # Restores from checkpoint saver.restore(sess, ckpt.model_checkpoint_path) # Assuming model_checkpoint_path looks something like: # /my-favorite-path/cifar10_train/model.ckpt-0, # extract global_step from it. global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] else: print('No checkpoint file found') return # Start the queue runners. coord = tf.train.Coordinator() try: threads = [] for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS): threads.extend(qr.create_threads(sess, coord=coord, daemon=True, start=True)) #in_x = data_input(d) #cv2.destroyAllWindows() #l=sess.run([logits],feed_dict={images_placeholder:in_x}) #l=np.array(logits) #l = np.reshape(l, (np.product(l.shape),)) print(logits.shape) #l = np.reshape(logits, (2, 7)) #p=sess.run(tf.nn.softmax(l)) p=tf.nn.softmax(logits) pred=sess.run(tf.argmax(p,1),feed_dict={images_placeholder:in_x}) print(pred) except Exception as e: coord.request_stop(e) coord.request_stop() coord.join(threads, stop_grace_period_secs=10)
def run_test(): model_name = "./sports1m_finetuning_ucf101.model" test_list_file = './list/test1.list' num_test_videos = len(list(open(test_list_file, 'r'))) print("Number of test videos={}".format(num_test_videos)) # Get the sets of images and labels for training, validation, and images_placeholder, labels_placeholder = placeholder_inputs( FLAGS.batch_size * gpu_num) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.0005), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.0005), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.0005), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.0005), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.0005), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.0005), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.0005), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.0005), 'deconv1': _variable_with_weight_decay('deconv1', [1, 4, 4, 1, 512], 0.0005), 'deconv2': _variable_with_weight_decay('deconv2', [1, 3, 3, 1, 1], 0.0005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.04, 0.0), 'bc2': _variable_with_weight_decay('bc2', [128], 0.04, 0.0), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.04, 0.0), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.04, 0.0), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.04, 0.0), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.04, 0.0), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.04, 0.0), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.04, 0.0) } logits = [] for gpu_index in range(0, gpu_num): with tf.device('/gpu:%d' % gpu_index): logit = c3d_model.inference_c3d( images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size, :, :, :, :], 0.6, FLAGS.batch_size, weights, biases) logits.append(logit) logits = tf.concat(0, logits) sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)) init = tf.initialize_all_variables() sess.run(init) next_start_pos = 0 all_steps = int((num_test_videos - 1) / (FLAGS.batch_size * gpu_num) + 1) predict_images = None for step in xrange(all_steps): # Fill a feed dictionary with the actual set of images and labels # for this particular training step. test_images, test_labels, next_start_pos, _, valid_len = \ input_data.read_clip_and_label( test_list_file, FLAGS.batch_size * gpu_num, start_pos=next_start_pos ) predict_data = sess.run(logits, feed_dict={images_placeholder: test_images}) if predict_images is None: predict_images = predict_data else: predict_images = np.concatenate((predict_images, predict_data), axis=0) print("done") return predict_images
def construct_net(): images_placeholder, labels_placeholder = placeholder_inputs(batch_size) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.04, 0.00), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.00), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.00), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.00), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.00), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.04, 0.001), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.04, 0.002), 'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.04, 0.005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.04, 0.0), 'bc2': _variable_with_weight_decay('bc2', [128], 0.04, 0.0), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.04, 0.0), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.04, 0.0), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.04, 0.0), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.04, 0.0), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.04, 0.0), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.04, 0.0), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.04, 0.0), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.04, 0.0), 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.04, 0.0), } with tf.device('/gpu:2'): dense1, logit = c3d_model.inference_c3d(images_placeholder, 1.0, weights, biases) return images_placeholder, labels_placeholder, dense1
def run_testing(): with tf.Graph().as_default(): global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.005), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.005), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.005), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.005), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.005), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.005), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.005), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.005), #'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.005), #'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.005), #'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.000), 'bc2': _variable_with_weight_decay('bc2', [128], 0.000), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.000), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.000), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.000), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.000), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.000), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.000), #'bd1': _variable_with_weight_decay('bd1', [4096], 0.000), #'bd2': _variable_with_weight_decay('bd2', [4096], 0.000), #'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.000), } fcn_weights = { 'wconv6': _variable_with_weight_decay('conv6', [1, 4, 4, 512, 512], 0.005), 'wup6': _variable_with_weight_decay('up6', [2, 1, 1, 4096, 512], 0.005), 'wup7': _variable_with_weight_decay('up7', [2, 1, 1, 4096, 4096], 0.005), 'wup8': _variable_with_weight_decay( 'up8', [2, 1, 1, fcn_model.NUM_CLASSES, 4096], 0.005), } fcn_biases = { 'bconv6': _variable_with_weight_decay('bconv6', [512], 0.000), 'bup6': _variable_with_weight_decay('bup6', [4096], 0.000), 'bup7': _variable_with_weight_decay('bup7', [4096], 0.000), 'bup8': _variable_with_weight_decay('bup8', [fcn_model.NUM_CLASSES], 0.000), } with tf.name_scope('inputs'): images_placeholder, labels_placeholder, keep_pro = placeholder_inputs( FLAGS.batch_size) feature_map = c3d_model.inference_c3d(images_placeholder, keep_pro, FLAGS.batch_size, weights, biases) logit = fcn_model.inference_fcn5(feature_map, keep_pro, FLAGS.batch_size, fcn_weights, fcn_biases) loss = fcn_model_loss(logit, labels_placeholder, FLAGS.batch_size) accuracy = tower_acc(logit, labels_placeholder, FLAGS.batch_size) predictions = tf.nn.top_k(logit, 1) # Create a saver for writing training checkpoints. new_saver = tf.train.Saver(weights.values() + biases.values() + fcn_weights.values() + fcn_biases.values()) init = tf.global_variables_initializer() # Create a session for running Ops on the Graph. sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) sess.run(init) ckpt = tf.train.get_checkpoint_state(pre_model_save_dir) if ckpt and ckpt.model_checkpoint_path: print "loading checkpoint,waiting......" new_saver.restore(sess, ckpt.model_checkpoint_path) print "load complete!" if FLAGS.output_to_file: # all output will be stored in 'output.txt' print('outputs will be stored in test.txt') sys.stdout = open('test.txt', 'a', 1) predict_list = [] label_list = [] for i in xrange(3358): start_time = time.time() test_images, test_labels, _, _, _, _ = input_test_data.read_clip_and_label( filename='annotation/test.list', batch_size=1, start_pos=-1, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP, crop_size=c3d_model.CROP_SIZE, video_list=[]) acc, predict = sess.run( [accuracy, predictions], feed_dict={ images_placeholder: test_images, labels_placeholder: test_labels, keep_pro: 1 }) print('acc: {}'.format(acc)) print('predict: {}'.format(np.reshape(predict[1], [32]))) predict_list.append(np.reshape(predict[1], [32])) print('labels: {}'.format(np.reshape(test_labels, [32]))) label_list.append(np.reshape(test_labels, [32])) np.save('./test/predict', predict_list) np.save('./test/label', label_list)
def run_training(): # Get the sets of images and labels for training, validation, and # Tell TensorFlow that the model will be built into the default Graph. # Create model directory if not os.path.exists(model_save_dir): os.makedirs(model_save_dir) use_pretrained_model = True model_filename = "./sports1m_finetuning_ucf101.model" with tf.Graph().as_default(): global_step = tf.get_variable( 'global_step', [], initializer=tf.constant_initializer(0), trainable=False ) images_placeholder, labels_placeholder = placeholder_inputs( FLAGS.batch_size * gpu_num ) tower_grads1 = [] tower_grads2 = [] logits = [] opt_stable = tf.train.AdamOptimizer(1e-4) opt_finetuning = tf.train.AdamOptimizer(1e-3) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.0005), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.0005), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.0005), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.0005), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.0005), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.0005), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.0005), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.0005), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.0005), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.0005), 'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.0005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.000), 'bc2': _variable_with_weight_decay('bc2', [128], 0.000), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.000), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.000), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.000), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.000), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.000), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.000), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.000), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.000), 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.000), } for gpu_index in range(0, gpu_num): with tf.device('/gpu:%d' % gpu_index): varlist2 = [ weights['out'],biases['out'] ] varlist1 = list( set(weights.values() + biases.values()) - set(varlist2) ) logit = c3d_model.inference_c3d( images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size,:,:,:,:], 0.5, FLAGS.batch_size, weights, biases ) loss_name_scope = ('gpud_%d_loss' % gpu_index) loss = tower_loss( loss_name_scope, logit, labels_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size] ) grads1 = opt_stable.compute_gradients(loss, varlist1) grads2 = opt_finetuning.compute_gradients(loss, varlist2) tower_grads1.append(grads1) tower_grads2.append(grads2) logits.append(logit) logits = tf.concat(logits,0) accuracy = tower_acc(logits, labels_placeholder) tf.summary.scalar('accuracy', accuracy) grads1 = average_gradients(tower_grads1) grads2 = average_gradients(tower_grads2) apply_gradient_op1 = opt_stable.apply_gradients(grads1) apply_gradient_op2 = opt_finetuning.apply_gradients(grads2, global_step=global_step) variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY) variables_averages_op = variable_averages.apply(tf.trainable_variables()) train_op = tf.group(apply_gradient_op1, apply_gradient_op2, variables_averages_op) null_op = tf.no_op() # Create a saver for writing training checkpoints. saver = tf.train.Saver(weights.values() + biases.values()) init = tf.global_variables_initializer() # Create a session for running Ops on the Graph. sess = tf.Session( config=tf.ConfigProto(allow_soft_placement=True) ) sess.run(init) if os.path.isfile(model_filename) and use_pretrained_model: saver.restore(sess, model_filename) # Create summary writter merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter('./visual_logs/train', sess.graph) test_writer = tf.summary.FileWriter('./visual_logs/test', sess.graph) for step in xrange(FLAGS.max_steps): start_time = time.time() train_images, train_labels, _, _, _ = input_data.read_clip_and_label( filename='list/train.list', batch_size=FLAGS.batch_size * gpu_num, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP, crop_size=c3d_model.CROP_SIZE, shuffle=True ) sess.run(train_op, feed_dict={ images_placeholder: train_images, labels_placeholder: train_labels }) duration = time.time() - start_time print('Step %d: %.3f sec' % (step, duration)) # Save a checkpoint and evaluate the model periodically. if (step) % 10 == 0 or (step + 1) == FLAGS.max_steps: saver.save(sess, os.path.join(model_save_dir, 'c3d_ucf_model'), global_step=step) print('Training Data Eval:') summary, acc = sess.run( [merged, accuracy], feed_dict={images_placeholder: train_images, labels_placeholder: train_labels }) print ("accuracy: " + "{:.5f}".format(acc)) train_writer.add_summary(summary, step) print('Validation Data Eval:') val_images, val_labels, _, _, _ = input_data.read_clip_and_label( filename='list/test.list', batch_size=FLAGS.batch_size * gpu_num, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP, crop_size=c3d_model.CROP_SIZE, shuffle=True ) summary, acc = sess.run( [merged, accuracy], feed_dict={ images_placeholder: val_images, labels_placeholder: val_labels }) print ("accuracy: " + "{:.5f}".format(acc)) test_writer.add_summary(summary, step) print("done")
def run_training(): # Get the sets of images and labels for training, validation, and # Tell TensorFlow that the model will be built into the default Graph. # Create model directory global loss_per_step with tf.Graph().as_default(): # global_step = tf.get_variable( # 'global_step', # [], # initializer=tf.constant_initializer(0), # trainable=False # ) images_placeholder, labels_placeholder = placeholder_inputs(BATCH_SIZE) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], wd=0.0005), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], wd=0.0005), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], wd=0.0005), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], wd=0.0005), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], wd=0.0005), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], wd=0.0005), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], wd=0.0005), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], wd=0.0005), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], wd=0.0005), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], wd=0.0005), # 'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], wd=0.0005), 'in': _variable_with_weight_decay('in', [4096, c3d_model.NUM_HIDDEN_UNIT], wd=0.0005), 'out': _variable_with_weight_decay( 'out', [c3d_model.NUM_HIDDEN_UNIT, c3d_model.NUM_CLASSES], wd=0.0005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.000), 'bc2': _variable_with_weight_decay('bc2', [128], 0.000), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.000), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.000), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.000), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.000), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.000), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.000), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.000), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.000), # 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.000), 'in': _variable_with_weight_decay('in', [c3d_model.NUM_HIDDEN_UNIT], 0.000), 'out': _variable_with_weight_decay('out', [c3d_model.NUM_CLASSES], 0.000) } dense1 = c3d_model.inference_c3d(images_placeholder, 0.6, BATCH_SIZE, weights, biases) logit = c3d_model.RNN(dense1, batch_size=BATCH_SIZE, weights=weights, biases=biases) total_loss, one_hot_labels = loss(logit, labels_placeholder) prediction = tf.nn.softmax(logit) accuracy = tower_acc(prediction, labels_placeholder) train_step = tf.train.AdamOptimizer(LEARNING_RATE).minimize(total_loss) # train_step = tf.train.GradientDescentOptimizer(0.1).minimize(total_loss) init = tf.global_variables_initializer() config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(init) saver = tf.train.Saver() save_path = saver.save(sess, "my_net/save_net.ckpt") print("Save to path: ", save_path) # sess.run(tf.Print(weights['wc1'],[weights['wc1']],message='wc1:',summarize=100)) plt.ion() for step in xrange(MAX_STEPS): start_time = time.time() train_images, train_labels, _, _, _ = input_data.read_clip_and_label( filename='dataset/train_data/', batch_size=BATCH_SIZE, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP, crop_size=c3d_model.CROP_SIZE, shuffle=True) sess.run(train_step, feed_dict={ images_placeholder: train_images, labels_placeholder: train_labels }) duration = time.time() - start_time print('Step %d: %.3f sec' % (step, duration)) step_loss = sess.run(total_loss, feed_dict={ images_placeholder: train_images, labels_placeholder: train_labels }) print("loss: " + "{:.5f}".format(step_loss)) loss_per_step.append(step_loss) # plt.plot(loss_per_step) # plt.xlabel('clip') # plt.ylabel('loss') # plt.title('16 frames per clip, equal to 66.67ms') # plt.pause(0.05) # while True: # plt.pause(0.05) # sess.run(tf.Print(images_placeholder, [images_placeholder], message='images_placeholder:', summarize=100), # feed_dict={images_placeholder: train_images}) # sess.run(tf.Print(labels_placeholder, [labels_placeholder], message='labels:', summarize=100), # feed_dict={labels_placeholder: train_labels}) # sess.run(tf.Print(one_hot_labels, [labels_placeholder], message='one_hot_labels:', summarize=100), # feed_dict={labels_placeholder: train_labels}) # sess.run(tf.Print(conv1, [conv1], message='conv1:', summarize=100), # feed_dict={images_placeholder: train_images}) # sess.run(tf.Print(tf.shape(conv1), [tf.shape(conv1)], message='conv1.shape:', summarize=100), # feed_dict={images_placeholder: train_images}) # sess.run(tf.Print(pool1, [pool1], message='pool1:', summarize=100), # feed_dict={images_placeholder: train_images}) # sess.run(tf.Print(tf.shape(pool1), [tf.shape(pool1)], message='pool1.shape:', summarize=100), # feed_dict={images_placeholder: train_images}) # sess.run(tf.Print(dense1, [dense1], message='dense1:', summarize=100), # feed_dict={images_placeholder: train_images}) # sess.run(tf.Print(dense2, [dense2], message='dense2:', summarize=100), # feed_dict={images_placeholder: train_images}) # sess.run(tf.Print(logit, [logit], message='Logit:', summarize=100), # feed_dict={images_placeholder: train_images}) if (step) % 20 == 0: print('Training Data Eval:') test_images, test_labels, _, _, _ = input_data.read_clip_and_label( filename='dataset/test_data/', batch_size=BATCH_SIZE, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP, crop_size=c3d_model.CROP_SIZE, shuffle=True) acc = sess.run(accuracy, feed_dict={ images_placeholder: test_images, labels_placeholder: test_labels }) print("accuracy: " + "{:.5f}".format(acc)) return loss_per_step
def run_training(): # Get the sets of images and labels for training, validation, and # Tell TensorFlow that the model will be built into the default Graph. # Create model directory if not os.path.exists(model_save_dir): os.makedirs(model_save_dir) use_pretrained_model = False model_filename = "./sports1m_finetuning_ucf101.model" train_out = "train_out.txt" val_out = "val_out.txt" if os.path.exists(train_out): os.remove(train_out) if os.path.exists(val_out): os.remove(val_out) with tf.Graph().as_default(): global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False) images_placeholder, labels_placeholder = placeholder_inputs( FLAGS.batch_size * gpu_num) tower_grads1 = [] tower_grads2 = [] logits = [] opt1 = tf.train.AdamOptimizer(1e-4) opt2 = tf.train.AdamOptimizer(2e-4) for gpu_index in range(0, gpu_num): with tf.device('/gpu:%d' % gpu_index): with tf.name_scope('%s_%d' % ('dextro-research', gpu_index)) as scope: with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay( 'wc1', [3, 3, 3, 3, 64], 0.0005), 'wc2': _variable_with_weight_decay( 'wc2', [3, 3, 3, 64, 128], 0.0005), 'wc3a': _variable_with_weight_decay( 'wc3a', [3, 3, 3, 128, 256], 0.0005), 'wc3b': _variable_with_weight_decay( 'wc3b', [3, 3, 3, 256, 256], 0.0005), 'wc4a': _variable_with_weight_decay( 'wc4a', [3, 3, 3, 256, 512], 0.0005), 'wc4b': _variable_with_weight_decay( 'wc4b', [3, 3, 3, 512, 512], 0.0005), 'wc5a': _variable_with_weight_decay( 'wc5a', [3, 3, 3, 512, 512], 0.0005), 'wc5b': _variable_with_weight_decay( 'wc5b', [3, 3, 3, 512, 512], 0.0005), 'wd1': _variable_with_weight_decay( 'wd1', [8192, 4096], 0.0005), 'wd2': _variable_with_weight_decay( 'wd2', [4096, 4096], 0.0005), 'out': _variable_with_weight_decay( 'wout', [4096, c3d_model.NUM_CLASSES], 0.0005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.000), 'bc2': _variable_with_weight_decay('bc2', [128], 0.000), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.000), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.000), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.000), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.000), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.000), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.000), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.000), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.000), 'out': _variable_with_weight_decay( 'bout', [c3d_model.NUM_CLASSES], 0.000), } varlist1 = weights.values() varlist2 = biases.values() logit = c3d_model.inference_c3d( images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size, :, :, :, :], 0.5, FLAGS.batch_size, weights, biases) loss = tower_loss( scope, logit, labels_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size]) grads1 = opt1.compute_gradients(loss, varlist1) grads2 = opt2.compute_gradients(loss, varlist2) tower_grads1.append(grads1) tower_grads2.append(grads2) logits.append(logit) tf.get_variable_scope().reuse_variables() logits = tf.concat(0, logits) accuracy = tower_acc(logits, labels_placeholder) tf.scalar_summary('accuracy', accuracy) grads1 = average_gradients(tower_grads1) grads2 = average_gradients(tower_grads2) apply_gradient_op1 = opt1.apply_gradients(grads1) apply_gradient_op2 = opt2.apply_gradients(grads2, global_step=global_step) variable_averages = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY) variables_averages_op = variable_averages.apply( tf.trainable_variables()) train_op = tf.group(apply_gradient_op1, apply_gradient_op2, variables_averages_op) null_op = tf.no_op() # Create a saver for writing training checkpoints. saver = tf.train.Saver(weights.values() + biases.values()) init = tf.initialize_all_variables() # Create a session for running Ops on the Graph. sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)) sess.run(init) if os.path.isfile(model_filename) and use_pretrained_model: saver.restore(sess, model_filename) # Create summary writter merged = tf.merge_all_summaries() train_writer = tf.train.SummaryWriter('./visual_logs/train', sess.graph) test_writer = tf.train.SummaryWriter('./visual_logs/test', sess.graph) for step in xrange(FLAGS.max_steps): start_time = time.time() train_images, train_labels, _, _, _ = input_data.read_clip_and_label( filename='train.list', batch_size=FLAGS.batch_size * gpu_num, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP, crop_size=c3d_model.CROP_SIZE, shuffle=True) sess.run(train_op, feed_dict={ images_placeholder: train_images, labels_placeholder: train_labels }) duration = time.time() - start_time print('Step %d: %.3f sec' % (step, duration)) # Save a checkpoint and evaluate the model periodically. if (step) % 10 == 0 or (step + 1) == FLAGS.max_steps: saver.save(sess, os.path.join(model_save_dir, 'c3d_ucf_model'), global_step=step) print('Training Data Eval:') summary, acc = sess.run( [merged, accuracy], feed_dict={ images_placeholder: train_images, labels_placeholder: train_labels }) print("accuracy: " + "{:.5f}".format(acc)) str1 = str(step) + ", " + "{:.5f}".format(acc) out1 = open(train_out, 'a') print >> out1, str1 out1.close() train_writer.add_summary(summary, step) print('Validation Data Eval:') val_images, val_labels, _, _, _ = input_data.read_clip_and_label( filename='test.list', batch_size=FLAGS.batch_size * gpu_num, num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP, crop_size=c3d_model.CROP_SIZE, shuffle=True) summary, acc = sess.run([merged, accuracy], feed_dict={ images_placeholder: val_images, labels_placeholder: val_labels }) print("accuracy: " + "{:.5f}".format(acc)) str2 = str(step) + ", " + "{:.5f}".format(acc) out2 = open(val_out, 'a') print >> out2, str2 out2.close() test_writer.add_summary(summary, step) print("done")
def run_test(): model_name = "./sports1m_finetuning_ucf101.model" test_list_file = 'list/test.list' num_test_videos = len(list(open(test_list_file, 'r'))) print("Number of test videos={}".format(num_test_videos)) # Get the sets of images and labels for training, validation, and images_placeholder, labels_placeholder = placeholder_inputs( FLAGS.batch_size * gpu_num) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.04, 0.00), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.00), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.00), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.00), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.00), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.04, 0.001), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.04, 0.002), 'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.04, 0.005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.04, 0.0), 'bc2': _variable_with_weight_decay('bc2', [128], 0.04, 0.0), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.04, 0.0), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.04, 0.0), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.04, 0.0), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.04, 0.0), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.04, 0.0), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.04, 0.0), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.04, 0.0), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.04, 0.0), 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.04, 0.0), } logits = [] for gpu_index in range(0, gpu_num): with tf.device('/gpu:%d' % gpu_index): logit = c3d_model.inference_c3d( images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size, :, :, :, :], 0.6, FLAGS.batch_size, weights, biases) logits.append(logit) logits = tf.concat(logits, 0) norm_score = tf.nn.softmax(logits) saver = tf.train.Saver() config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True sess = tf.Session(config=config) init = tf.global_variables_initializer() sess.run(init) # Create a saver for writing training checkpoints. saver.restore(sess, model_name) # And then after everything is built, start the training loop. bufsize = 0 write_file = open("predict_ret.txt", "w+") next_start_pos = 0 all_steps = int((num_test_videos - 1) / (FLAGS.batch_size * gpu_num) + 1) accuracy, cnt = 0, 0 for step in xrange(all_steps): # Fill a feed dictionary with the actual set of images and labels # for this particular training step. start_time = time.time() test_images, test_labels, next_start_pos, _, valid_len = \ input_data.read_clip_and_label( test_list_file, FLAGS.batch_size * gpu_num, start_pos=next_start_pos ) predict_score = norm_score.eval( session=sess, feed_dict={images_placeholder: test_images}) for i in range(0, valid_len): true_label = test_labels[i], top1_predicted_label = np.argmax(predict_score[i]) # Write results: true label, class prob for true label, predicted label, class prob for predicted label write_file.write('{}, {}, {}, {}\n'.format( true_label[0], predict_score[i][true_label], top1_predicted_label, predict_score[i][top1_predicted_label])) cnt += 1 if true_label[0] == top1_predicted_label: accuracy += 1 print("Test Accuracy={}".format(float(accuracy) / float(cnt))) write_file.close() print("done")
def run_test(): model_name = "./sports1m_finetuning_ucf101.model" test_list_file = 'list/test.list' num_test_videos = len(list(open(test_list_file,'r'))) print("Number of test videos={}".format(num_test_videos)) # Get the sets of images and labels for training, validation, and images_placeholder, labels_placeholder = placeholder_inputs(FLAGS.batch_size * gpu_num) with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.04, 0.00), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.00), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.00), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.00), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.00), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.04, 0.001), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.04, 0.002), 'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.04, 0.005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.04, 0.0), 'bc2': _variable_with_weight_decay('bc2', [128], 0.04, 0.0), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.04, 0.0), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.04, 0.0), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.04, 0.0), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.04, 0.0), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.04, 0.0), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.04, 0.0), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.04, 0.0), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.04, 0.0), 'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.04, 0.0), } logits = [] for gpu_index in range(0, gpu_num): with tf.device('/gpu:%d' % gpu_index): logit = c3d_model.inference_c3d(images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size,:,:,:,:], 0.6, FLAGS.batch_size, weights, biases) logits.append(logit) logits = tf.concat(logits,0) norm_score = tf.nn.softmax(logits) saver = tf.train.Saver() sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) init = tf.global_variables_initializer() sess.run(init) # Create a saver for writing training checkpoints. saver.restore(sess, model_name) # And then after everything is built, start the training loop. bufsize = 0 write_file = open("predict_ret.txt", "w+", bufsize) next_start_pos = 0 all_steps = int((num_test_videos - 1) / (FLAGS.batch_size * gpu_num) + 1) for step in xrange(all_steps): # Fill a feed dictionary with the actual set of images and labels # for this particular training step. start_time = time.time() test_images, test_labels, next_start_pos, _, valid_len = \ input_data.read_clip_and_label( test_list_file, FLAGS.batch_size * gpu_num, start_pos=next_start_pos ) predict_score = norm_score.eval( session=sess, feed_dict={images_placeholder: test_images} ) for i in range(0, valid_len): true_label = test_labels[i], top1_predicted_label = np.argmax(predict_score[i]) # Write results: true label, class prob for true label, predicted label, class prob for predicted label write_file.write('{}, {}, {}, {}\n'.format( true_label[0], predict_score[i][true_label], top1_predicted_label, predict_score[i][top1_predicted_label])) write_file.close() print("done")