def main(): align = align_dlib.AlignDlib(os.path.expanduser(FLAGS.dlib_face_predictor)) batch_size = 2 image_paths = [FLAGS.image1, FLAGS.image2] landmarkIndices = align_dlib.AlignDlib.OUTER_EYES_AND_NOSE with tf.Graph().as_default(): # Placeholder for input images images_placeholder = tf.placeholder(tf.float32, shape=(batch_size, FLAGS.image_size, FLAGS.image_size, 3), name='input') # Placeholder for phase_train phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') # Build the inference graph embeddings = facenet.inference_nn4_max_pool_96(images_placeholder, FLAGS.pool_type, FLAGS.use_lrn, 1.0, phase_train=phase_train_placeholder) # Create a saver for restoring variable averages ema = tf.train.ExponentialMovingAverage(1.0) saver = tf.train.Saver(ema.variables_to_restore()) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(os.path.expanduser(FLAGS.model_dir)) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: raise ValueError('Checkpoint not found') images = load_and_align_data(image_paths, FLAGS.image_size, align, landmarkIndices) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb = sess.run(embeddings, feed_dict=feed_dict) dist = np.sqrt(np.mean(np.square(np.subtract(emb[0,:], emb[1,:])))) print('Distance between the embeddings: %3.6f' % dist)
def main(): pairs = read_pairs(os.path.expanduser(FLAGS.lfw_pairs)) paths, actual_issame = get_paths(os.path.expanduser(FLAGS.lfw_dir), pairs) with tf.Graph().as_default(): # Creates graph from saved GraphDef # NOTE: This does not work at the moment. Needs tensorflow to store variables in the graph_def. # graphdef_filename = os.path.join(os.path.expanduser(FLAGS.model_dir), 'graphdef', 'graph_def.pb') # with gfile.FastGFile(graphdef_filename, 'rb') as f: # graph_def = tf.GraphDef() # graph_def.ParseFromString(f.read()) # images_placeholder, phase_train_placeholder, embeddings = tf.import_graph_def(graph_def, return_elements = ['input', 'phase_train', 'embeddings'], name='') # Placeholder for input images images_placeholder = tf.placeholder(tf.float32, shape=(FLAGS.batch_size, FLAGS.image_size, FLAGS.image_size, 3), name='input') # Placeholder for phase_train phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') # Build the inference graph embeddings = facenet.inference_nn4_max_pool_96(images_placeholder, FLAGS.pool_type, FLAGS.use_lrn, 1.0, phase_train=phase_train_placeholder) # Create a saver for restoring variable averages ema = tf.train.ExponentialMovingAverage(1.0) saver = tf.train.Saver(ema.variables_to_restore()) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(os.path.expanduser(FLAGS.model_dir)) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: raise ValueError('Checkpoint not found') nrof_images = len(paths) nrof_batches = int(nrof_images / FLAGS.batch_size) # Run forward pass on the remainder in the last batch emb_list = [] for i in range(nrof_batches): start_time = time.time() paths_batch = paths[i*FLAGS.batch_size:(i+1)*FLAGS.batch_size] images = facenet.load_data(paths_batch, False, False, FLAGS.image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_list += sess.run([embeddings], feed_dict=feed_dict) duration = time.time() - start_time print('Calculated embeddings for batch %d of %d: time=%.3f seconds' % (i+1,nrof_batches, duration)) emb_array = np.vstack(emb_list) # Stack the embeddings to a nrof_examples_per_epoch x 128 matrix thresholds = np.arange(0, 4, 0.01) embeddings1 = emb_array[0::2] embeddings2 = emb_array[1::2] tpr, fpr, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), FLAGS.seed) print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy))) facenet.plot_roc(fpr, tpr, 'NN4')
def main(): align = align_dlib.AlignDlib(os.path.expanduser(FLAGS.dlib_face_predictor)) batch_size = 2 image_paths = [FLAGS.image1, FLAGS.image2] landmarkIndices = align_dlib.AlignDlib.OUTER_EYES_AND_NOSE with tf.Graph().as_default(): # Placeholder for input images images_placeholder = tf.placeholder(tf.float32, shape=(batch_size, FLAGS.image_size, FLAGS.image_size, 3), name='input') # Placeholder for phase_train phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') # Build the inference graph embeddings = facenet.inference_nn4_max_pool_96( images_placeholder, FLAGS.pool_type, FLAGS.use_lrn, 1.0, phase_train=phase_train_placeholder) # Create a saver for restoring variable averages ema = tf.train.ExponentialMovingAverage(1.0) saver = tf.train.Saver(ema.variables_to_restore()) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state( os.path.expanduser(FLAGS.model_dir)) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: raise ValueError('Checkpoint not found') images = load_and_align_data(image_paths, FLAGS.image_size, align, landmarkIndices) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb = sess.run(embeddings, feed_dict=feed_dict) dist = np.sqrt( np.mean(np.square(np.subtract(emb[0, :], emb[1, :])))) print('Distance between the embeddings: %3.6f' % dist)
def main(): # Creates graph from saved GraphDef # NOTE: This does not work at the moment. Needs tensorflow to store variables in the graph_def. #create_graph(os.path.join(FLAGS.model_dir, 'graphdef', 'graph_def.pb')) pairs = read_pairs(os.path.expanduser(FLAGS.lfw_pairs)) paths, actual_issame = get_paths(os.path.expanduser(FLAGS.lfw_dir), pairs) mpl.interactive(True) with tf.Graph().as_default(): # Placeholder for input images images_placeholder = tf.placeholder(tf.float32, shape=(FLAGS.batch_size, FLAGS.image_size, FLAGS.image_size, 3), name='Input') # Placeholder for phase_train phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') # Build the inference graph embeddings = facenet.inference_nn4_max_pool_96(images_placeholder, phase_train=phase_train_placeholder) # Create a saver for restoring variable averages ema = tf.train.ExponentialMovingAverage(1.0) saver = tf.train.Saver(ema.variables_to_restore()) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(os.path.expanduser(FLAGS.model_dir)) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: raise ValueError('Checkpoint not found') # Run test on LFW to check accuracy for different horizontal/vertical translations of input images if True: offsets = np.arange(-30, 31, 3) horizontal_offset_accuracy = [None] * len(offsets) for idx, offset in enumerate(offsets): accuracy = evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, embeddings, paths, actual_issame, translate_images, (offset,0)) print('Hoffset: %1.3f Accuracy: %1.3f%c%1.3f' % (offset, np.mean(accuracy), u"\u00B1", np.std(accuracy))) horizontal_offset_accuracy[idx] = np.mean(accuracy) vertical_offset_accuracy = [None] * len(offsets) for idx, offset in enumerate(offsets): accuracy = evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, embeddings, paths, actual_issame, translate_images, (0,offset)) print('Voffset: %1.3f Accuracy: %1.3f%c%1.3f' % (offset, np.mean(accuracy), u"\u00B1", np.std(accuracy))) vertical_offset_accuracy[idx] = np.mean(accuracy) fig = plt.figure(1) plt.plot(offsets, horizontal_offset_accuracy, label='Horizontal') plt.plot(offsets, vertical_offset_accuracy, label='Vertical') plt.legend() plt.grid(True) plt.title('Translation invariance on LFW') plt.xlabel('Offset [pixels]') plt.ylabel('Accuracy') plt.show() result_dir = os.path.expanduser(FLAGS.model_dir) print('Saving results in %s' % result_dir) fig.savefig(os.path.join(result_dir, 'invariance_translation.png')) save_result(offsets, horizontal_offset_accuracy, os.path.join(result_dir, 'invariance_translation_horizontal.txt')) save_result(offsets, vertical_offset_accuracy, os.path.join(result_dir, 'invariance_translation_vertical.txt')) # Run test on LFW to check accuracy for different rotation of input images if True: angles = np.arange(-30, 31, 3) rotation_accuracy = [None] * len(angles) for idx, angle in enumerate(angles): accuracy = evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, embeddings, paths, actual_issame, rotate_images, angle) print('Angle: %1.3f Accuracy: %1.3f%c%1.3f' % (angle, np.mean(accuracy), u"\u00B1", np.std(accuracy))) rotation_accuracy[idx] = np.mean(accuracy) fig = plt.figure(2) plt.plot(angles, rotation_accuracy) plt.grid(True) plt.title('Rotation invariance on LFW') plt.xlabel('Angle [deg]') plt.ylabel('Accuracy') plt.show() result_dir = os.path.expanduser(FLAGS.model_dir) print('Saving results in %s' % result_dir) fig.savefig(os.path.join(result_dir, 'invariance_rotation.png')) save_result(angles, rotation_accuracy, os.path.join(result_dir, 'invariance_rotation.txt')) # Run test on LFW to check accuracy for different scaling of input images if True: scales = np.arange(0.5, 1.5, 0.05) scale_accuracy = [None] * len(scales) for scale_idx, scale in enumerate(scales): accuracy = evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, embeddings, paths, actual_issame, scale_images, scale) print('Scale: %1.3f Accuracy: %1.3f%c%1.3f' % (scale, np.mean(accuracy), u"\u00B1", np.std(accuracy))) scale_accuracy[scale_idx] = np.mean(accuracy) fig = plt.figure(3) plt.plot(scales, scale_accuracy) plt.grid(True) plt.title('Scale invariance on LFW') plt.xlabel('Scale') plt.ylabel('Accuracy') plt.show() result_dir = os.path.expanduser(FLAGS.model_dir) print('Saving results in %s' % result_dir) fig.savefig(os.path.join(result_dir, 'invariance_scale.png')) save_result(scales, scale_accuracy, os.path.join(result_dir, 'invariance_scale.txt'))
def main(argv=None): # pylint: disable=unused-argument if FLAGS.model_name: subdir = FLAGS.model_name preload_model = True else: subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S') preload_model = False log_dir = os.path.join(os.path.expanduser(FLAGS.logs_base_dir), subdir) model_dir = os.path.join(os.path.expanduser(FLAGS.models_base_dir), subdir) if not os.path.isdir(model_dir): # Create the model directory if it doesn't exist os.mkdir(model_dir) np.random.seed(seed=FLAGS.seed) dataset = facenet.get_dataset(FLAGS.data_dir) train_set, validation_set = facenet.split_dataset(dataset, FLAGS.train_set_fraction, FLAGS.split_mode) print('Model directory: %s' % model_dir) with tf.Graph().as_default(): tf.set_random_seed(FLAGS.seed) global_step = tf.Variable(0, trainable=False) # Placeholder for input images images_placeholder = tf.placeholder(tf.float32, shape=(FLAGS.batch_size, FLAGS.image_size, FLAGS.image_size, 3), name='input') # Placeholder for phase_train phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') # Build the inference graph embeddings = facenet.inference_nn4_max_pool_96(images_placeholder, phase_train=phase_train_placeholder) # Split example embeddings into anchor, positive and negative anchor, positive, negative = tf.split(0, 3, embeddings) # Calculate triplet loss loss = facenet.triplet_loss(anchor, positive, negative) # Build a Graph that trains the model with one batch of examples and updates the model parameters train_op, _ = facenet.train(loss, global_step) # Create a saver saver = tf.train.Saver(tf.all_variables(), max_to_keep=0) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.merge_all_summaries() # Build an initialization operation to run below. init = tf.initialize_all_variables() # Start running operations on the Graph. sess = tf.Session(config=tf.ConfigProto(log_device_placement=FLAGS.log_device_placement)) sess.run(init) summary_writer = tf.train.SummaryWriter(log_dir, sess.graph) with sess.as_default(): if preload_model: ckpt = tf.train.get_checkpoint_state(model_dir) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: raise ValueError('Checkpoint not found') # Training and validation loop for epoch in range(FLAGS.max_nrof_epochs): # Train for one epoch step = train(sess, train_set, epoch, images_placeholder, phase_train_placeholder, global_step, embeddings, loss, train_op, summary_op, summary_writer) # Validate epoch validate(sess, validation_set, epoch, images_placeholder, phase_train_placeholder, global_step, embeddings, loss, train_op, summary_op, summary_writer) # Save the model checkpoint after each epoch print('Saving checkpoint') checkpoint_path = os.path.join(model_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) graphdef_dir = os.path.join(model_dir, 'graphdef') graphdef_filename = 'graph_def.pb' if (not os.path.exists(os.path.join(graphdef_dir, graphdef_filename))): print('Saving graph definition') tf.train.write_graph(sess.graph_def, graphdef_dir, graphdef_filename, False)
def main(argv=None): # pylint: disable=unused-argument if FLAGS.model_name: subdir = FLAGS.model_name preload_model = True else: subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S') preload_model = False log_dir = os.path.join(os.path.expanduser(FLAGS.logs_base_dir), subdir) if not os.path.isdir( log_dir): # Create the log directory if it doesn't exist os.mkdir(log_dir) model_dir = os.path.join(os.path.expanduser(FLAGS.models_base_dir), subdir) if not os.path.isdir( model_dir): # Create the model directory if it doesn't exist os.mkdir(model_dir) # Store some git revision info in a text file in the log directory src_path, _ = os.path.split(os.path.realpath(__file__)) store_training_info(src_path, log_dir, ' '.join(argv)) np.random.seed(seed=FLAGS.seed) dataset = facenet.get_dataset(FLAGS.data_dir) train_set, validation_set = facenet.split_dataset(dataset, FLAGS.train_set_fraction, FLAGS.split_mode) print('Model directory: %s' % model_dir) with tf.Graph().as_default(): tf.set_random_seed(FLAGS.seed) global_step = tf.Variable(0, trainable=False) # Placeholder for input images images_placeholder = tf.placeholder(tf.float32, shape=(FLAGS.batch_size, FLAGS.image_size, FLAGS.image_size, 3), name='input') # Placeholder for phase_train phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') # Build the inference graph embeddings = facenet.inference_nn4_max_pool_96( images_placeholder, FLAGS.pool_type, FLAGS.use_lrn, FLAGS.keep_probability, phase_train=phase_train_placeholder) # Split example embeddings into anchor, positive and negative anchor, positive, negative = tf.split(0, 3, embeddings) # Calculate triplet loss loss = facenet.triplet_loss(anchor, positive, negative, FLAGS.alpha) # Build a Graph that trains the model with one batch of examples and updates the model parameters train_op, _ = facenet.train(loss, global_step, FLAGS.optimizer, FLAGS.learning_rate, FLAGS.moving_average_decay) # Create a saver saver = tf.train.Saver(tf.all_variables(), max_to_keep=0) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.merge_all_summaries() # Build an initialization operation to run below. init = tf.initialize_all_variables() # Start running operations on the Graph. sess = tf.Session(config=tf.ConfigProto( log_device_placement=FLAGS.log_device_placement)) sess.run(init) summary_writer = tf.train.SummaryWriter(log_dir, sess.graph) with sess.as_default(): if preload_model: ckpt = tf.train.get_checkpoint_state(model_dir) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: raise ValueError('Checkpoint not found') # Training and validation loop for epoch in range(FLAGS.max_nrof_epochs): # Train for one epoch step = train(sess, train_set, epoch, images_placeholder, phase_train_placeholder, global_step, embeddings, loss, train_op, summary_op, summary_writer) # Test on validation set validate(sess, validation_set, epoch, images_placeholder, phase_train_placeholder, global_step, embeddings, loss, 'validation', summary_writer) # Test on training set validate(sess, train_set, epoch, images_placeholder, phase_train_placeholder, global_step, embeddings, loss, 'training', summary_writer) if (epoch % FLAGS.checkpoint_period == 0) or (epoch == FLAGS.max_nrof_epochs - 1): # Save the model checkpoint print('Saving checkpoint') checkpoint_path = os.path.join(model_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) # Save the model if it hasn't been saved before graphdef_dir = os.path.join(model_dir, 'graphdef') graphdef_filename = 'graph_def.pb' if (not os.path.exists( os.path.join(graphdef_dir, graphdef_filename))): print('Saving graph definition') tf.train.write_graph(sess.graph_def, graphdef_dir, graphdef_filename, False)
def main(): # Creates graph from saved GraphDef # NOTE: This does not work at the moment. Needs tensorflow to store variables in the graph_def. #create_graph(os.path.join(FLAGS.model_dir, 'graphdef', 'graph_def.pb')) pairs = read_pairs(os.path.expanduser(FLAGS.lfw_pairs)) paths, actual_issame = get_paths(os.path.expanduser(FLAGS.lfw_dir), pairs) mpl.interactive(True) with tf.Graph().as_default(): global_step = tf.Variable(0, trainable=False) # Placeholder for input images images_placeholder = tf.placeholder(tf.float32, shape=(FLAGS.batch_size, FLAGS.image_size, FLAGS.image_size, 3), name='Input') # Placeholder for phase_train phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') # Build the inference graph embeddings = facenet.inference_nn4_max_pool_96(images_placeholder, phase_train=phase_train_placeholder) # Create a saver for restoring variable averages ema = tf.train.ExponentialMovingAverage(1.0) saver = tf.train.Saver(ema.variables_to_restore()) # Start running operations on the Graph. sess = tf.Session(config=tf.ConfigProto(log_device_placement=False)) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(os.path.expanduser(FLAGS.model_dir)) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: raise ValueError('Checkpoint not found') # Run test on LFW to check accuracy for different horizontal/vertical translations of input images if True: offsets = np.arange(-30, 31, 3) horizontal_offset_accuracy = [None] * len(offsets) for idx, offset in enumerate(offsets): accuracy = evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, embeddings, paths, actual_issame, translate_images, (offset,0)) print('Hoffset: %1.3f Accuracy: %1.3f%c%1.3f' % (offset, np.mean(accuracy), u"\u00B1", np.std(accuracy))) horizontal_offset_accuracy[idx] = np.mean(accuracy) vertical_offset_accuracy = [None] * len(offsets) for idx, offset in enumerate(offsets): accuracy = evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, embeddings, paths, actual_issame, translate_images, (0,offset)) print('Voffset: %1.3f Accuracy: %1.3f%c%1.3f' % (offset, np.mean(accuracy), u"\u00B1", np.std(accuracy))) vertical_offset_accuracy[idx] = np.mean(accuracy) fig = plt.figure(1) plt.plot(offsets, horizontal_offset_accuracy, label='Horizontal') plt.plot(offsets, vertical_offset_accuracy, label='Vertical') plt.legend() plt.grid(True) plt.title('Translation invariance on LFW') plt.xlabel('Offset [pixels]') plt.ylabel('Accuracy') plt.show() result_dir = os.path.expanduser(FLAGS.model_dir) print('Saving results in %s' % result_dir) fig.savefig(os.path.join(result_dir, 'invariance_translation.png')) save_result(offsets, horizontal_offset_accuracy, os.path.join(result_dir, 'invariance_translation_horizontal.txt')) save_result(offsets, vertical_offset_accuracy, os.path.join(result_dir, 'invariance_translation_vertical.txt')) xxx = 1 # Run test on LFW to check accuracy for different rotation of input images if True: angles = np.arange(-30, 31, 3) rotation_accuracy = [None] * len(angles) for idx, angle in enumerate(angles): accuracy = evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, embeddings, paths, actual_issame, rotate_images, angle) print('Angle: %1.3f Accuracy: %1.3f%c%1.3f' % (angle, np.mean(accuracy), u"\u00B1", np.std(accuracy))) rotation_accuracy[idx] = np.mean(accuracy) fig = plt.figure(2) plt.plot(angles, rotation_accuracy) plt.grid(True) plt.title('Rotation invariance on LFW') plt.xlabel('Angle [deg]') plt.ylabel('Accuracy') plt.show() result_dir = os.path.expanduser(FLAGS.model_dir) print('Saving results in %s' % result_dir) fig.savefig(os.path.join(result_dir, 'invariance_rotation.png')) save_result(angles, rotation_accuracy, os.path.join(result_dir, 'invariance_rotation.txt')) xxx = 1 # Run test on LFW to check accuracy for different scaling of input images if True: scales = np.arange(0.5, 1.5, 0.05) scale_accuracy = [None] * len(scales) for scale_idx, scale in enumerate(scales): accuracy = evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, embeddings, paths, actual_issame, scale_images, scale) print('Scale: %1.3f Accuracy: %1.3f%c%1.3f' % (scale, np.mean(accuracy), u"\u00B1", np.std(accuracy))) scale_accuracy[scale_idx] = np.mean(accuracy) fig = plt.figure(3) plt.plot(scales, scale_accuracy) plt.grid(True) plt.title('Scale invariance on LFW') plt.xlabel('Scale') plt.ylabel('Accuracy') plt.show() result_dir = os.path.expanduser(FLAGS.model_dir) print('Saving results in %s' % result_dir) fig.savefig(os.path.join(result_dir, 'invariance_scale.png')) save_result(scales, scale_accuracy, os.path.join(result_dir, 'invariance_scale.txt')) xxx = 1