def validate(sess, paths, actual_issame, seed, batch_size, images_placeholder, phase_train_placeholder, embeddings, nrof_folds=10): image_size = images_placeholder.get_shape()[1] # Run forward pass to calculate embeddings print('Runnning forward pass on LFW images') nrof_images = len(paths) nrof_batches = int(math.ceil(1.0*nrof_images / batch_size)) emb_list = [] for i in range(nrof_batches): start_index = i*batch_size end_index = min((i+1)*batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_list += sess.run([embeddings], feed_dict=feed_dict) emb_array = np.vstack(emb_list) # Stack the embeddings to a nrof_examples_per_epoch x 128 matrix # Calculate evaluation metrics 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), seed, nrof_folds=nrof_folds) thresholds = np.arange(0, 4, 0.001) val, val_std, far = facenet.calculate_val(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), 1e-3, seed, nrof_folds=nrof_folds) return tpr, fpr, accuracy, val, val_std, far
def evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, embeddings, paths, actual_issame, augument_images, aug_value): 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): paths_batch = paths[i * FLAGS.batch_size:(i + 1) * FLAGS.batch_size] images = facenet.load_data(paths_batch, False, False, FLAGS.orig_image_size) images_aug = augument_images(images, aug_value) feed_dict = { images_placeholder: images_aug, phase_train_placeholder: False } emb_list += sess.run([embeddings], feed_dict=feed_dict) 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] _, _, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), FLAGS.seed) return accuracy
def evaluate(embeddings, actual_issame, nrof_folds=10, distance_metric=0): # Calculate evaluation metrics thresholds = np.arange(0, 4, 0.01) if distance_metric == 1: thresholds = np.arange(0, 1, 0.001) embeddings1 = embeddings[0::2] embeddings2 = embeddings[1::2] tpr, fpr, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), nrof_folds=nrof_folds, distance_metric=distance_metric) thresholds = np.arange(0, 4, 0.001) if distance_metric == 1: thresholds = np.arange(0, 1, 0.001) val, val_std, far = facenet.calculate_val(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), 1e-3, nrof_folds=nrof_folds, distance_metric=distance_metric) f1 = 2 * np.mean(accuracy) * val / (np.mean(accuracy) + val) return tpr, fpr, accuracy, val, val_std, far, f1
def evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, image_size, embeddings, paths, actual_issame, augment_images, aug_value, batch_size, orig_image_size, seed): nrof_images = len(paths) nrof_batches = int(math.ceil(1.0 * nrof_images / batch_size)) emb_list = [] for i in range(nrof_batches): start_index = i * batch_size end_index = min((i + 1) * batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, orig_image_size) images_aug = augment_images(images, aug_value, image_size) feed_dict = { images_placeholder: images_aug, phase_train_placeholder: False } emb_list += sess.run([embeddings], feed_dict=feed_dict) 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] _, _, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), seed) return accuracy
def evaluate(embeddings, actual_issame, nrof_folds=10, distance_metric=0, subtract_mean=False): # Calculate evaluation metrics thresholds = np.arange(0, 4, 0.01) embeddings1 = embeddings[0::2] embeddings2 = embeddings[1::2] tpr, fpr, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), nrof_folds=nrof_folds, distance_metric=distance_metric, subtract_mean=subtract_mean) thresholds = np.arange(0, 4, 0.001) val, val_std, far = facenet.calculate_val(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), 1e-3, nrof_folds=nrof_folds, distance_metric=distance_metric, subtract_mean=subtract_mean) return tpr, fpr, accuracy, val, val_std, far
def evaluate(embeddings, actual_issame, nrof_folds=10): # Calculate evaluation metrics thresholds = np.arange(0, 4, 0.01) embeddings1 = embeddings[0::2] embeddings2 = embeddings[1::2] # tpr, fpr, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, # np.asarray(actual_issame), nrof_folds=nrof_folds) # thresholds = np.arange(0, 4, 0.001) # val, val_std, far = facenet.calculate_val(thresholds, embeddings1, embeddings2, # np.asarray(actual_issame), 1e-3, nrof_folds=nrof_folds) tpr, fpr, accuracy, fp_idx, fn_idx, best_threshold_acc = facenet.calculate_roc( thresholds, embeddings1, embeddings2, np.asarray(actual_issame), nrof_folds=nrof_folds) thresholds = np.arange(0, 4, 0.001) val, val_std, far, threshold_val = facenet.calculate_val( thresholds, embeddings1, embeddings2, np.asarray(actual_issame), 1e-3, nrof_folds=nrof_folds) return tpr, fpr, accuracy, val, val_std, far, fp_idx, fn_idx, best_threshold_acc, threshold_val
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(): 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(): with tf.Session() as sess: # Load the model print('Loading model "%s"' % FLAGS.model_file) load_model(FLAGS.model_file) # Get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") image_size = images_placeholder.get_shape()[1] # Run forward pass to calculate embeddings nrof_images = len(paths) nrof_batches = int(nrof_images / FLAGS.batch_size) 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, 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 # Calculate evaluation metrics 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 evaluate(embeddings, seed, actual_issame, nrof_folds=10): # Calculate evaluation metrics thresholds = np.arange(0, 4, 0.01) embeddings1 = embeddings[0::2] embeddings2 = embeddings[1::2] tpr, fpr, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), seed, nrof_folds=nrof_folds) thresholds = np.arange(0, 4, 0.001) val, val_std, far = facenet.calculate_val(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), 1e-3, seed, nrof_folds=nrof_folds) return tpr, fpr, accuracy, val, val_std, far
def evaluate(embeddings, actual_issame, nrof_folds=10): # Calculate evaluation metrics thresholds = np.arange(0, 4, 0.01) embeddings1 = embeddings[0::2] embeddings2 = embeddings[1::2] tpr, fpr, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), nrof_folds=nrof_folds) thresholds = np.arange(0, 4, 0.001) val, val_std, far = facenet.calculate_val(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), 1e-3, nrof_folds=nrof_folds) return tpr, fpr, accuracy, val, val_std, far
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(): with tf.Session() as sess: print('Reading model "%s"' % FLAGS.model_file) new_saver = tf.train.import_meta_graph(os.path.expanduser(FLAGS.model_file+'.meta')) all_vars_dict = {var.name: var for var in tf.all_variables()} restore_vars = {} for var_name in all_vars_dict: if '/ExponentialMovingAverage' in var_name: param_name = var_name.replace('/ExponentialMovingAverage', '') if param_name in all_vars_dict: param = all_vars_dict[param_name] print('%s\t%s' % (var_name, param)) restore_vars[var_name] = param saver = tf.train.Saver(restore_vars, saver_def=new_saver.as_saver_def()) saver.restore(sess, os.path.expanduser(FLAGS.model_file)) images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0") phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0") embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") image_size = images_placeholder.get_shape()[1] 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, 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 validate(sess, dataset, epoch, images_placeholder, phase_train_placeholder, global_step, embeddings, loss, prefix_str, summary_writer): nrof_people = FLAGS.people_per_batch * 4 print('Loading %s data' % prefix_str) # Sample people and load new data image_paths, num_per_class = facenet.sample_people(dataset, nrof_people, FLAGS.images_per_person) image_data = facenet.load_data(image_paths, False, False, FLAGS.image_size) print('Selecting random triplets from %s set' % prefix_str) triplets, nrof_triplets = facenet.select_validation_triplets(num_per_class, nrof_people, image_data, FLAGS.batch_size) start_time = time.time() anchor_list = [] positive_list = [] negative_list = [] triplet_loss_list = [] # Run a forward pass for the sampled images print('Running forward pass on %s set' % prefix_str) nrof_batches_per_epoch = nrof_triplets * 3 // FLAGS.batch_size for i in xrange(nrof_batches_per_epoch): batch = facenet.get_triplet_batch(triplets, i, FLAGS.batch_size) feed_dict = {images_placeholder: batch, phase_train_placeholder: False} emb, triplet_loss, step = sess.run([embeddings, loss, global_step], feed_dict=feed_dict) nrof_batch_triplets = emb.shape[0] / 3 anchor_list.append(emb[(0 * nrof_batch_triplets):(1 * nrof_batch_triplets), :]) positive_list.append(emb[(1 * nrof_batch_triplets):(2 * nrof_batch_triplets), :]) negative_list.append(emb[(2 * nrof_batch_triplets):(3 * nrof_batch_triplets), :]) triplet_loss_list.append(triplet_loss) anchor = np.vstack(anchor_list) positive = np.vstack(positive_list) negative = np.vstack(negative_list) duration = time.time() - start_time thresholds = np.arange(0, 4, 0.01) embeddings1 = np.vstack([anchor, anchor]) embeddings2 = np.vstack([positive, negative]) actual_issame = np.asarray([True] * anchor.shape[0] + [False] * anchor.shape[0]) tpr, fpr, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, actual_issame, FLAGS.seed) print('Epoch: [%d]\tTime %.3f\ttripErr %2.3f\t%sAccuracy %1.3f+-%1.3f' % ( epoch, duration, np.mean(triplet_loss_list), prefix_str, np.mean(accuracy), np.std(accuracy))) # Add validation loss and accuracy to summary summary = tf.Summary() summary.value.add(tag='{}_loss'.format(prefix_str), simple_value=np.mean(triplet_loss_list).astype(float)) summary.value.add(tag='{}_accuracy'.format(prefix_str), simple_value=np.mean(accuracy)) summary_writer.add_summary(summary, step) if False: facenet.plot_roc(fpr, tpr, 'NN4')
def validate(sess, dataset, epoch, images_placeholder, phase_train_placeholder, global_step, embeddings, loss, train_op, summary_op, summary_writer): people_per_batch = FLAGS.people_per_batch * 4 print('Loading validation data') # Sample people and load new data image_paths, num_per_class = facenet.sample_people(dataset, people_per_batch, FLAGS.images_per_person) image_data = facenet.load_data(image_paths) print('Selecting random triplets for validation') triplets, nrof_triplets = facenet.select_validation_triplets(num_per_class, people_per_batch, image_data) start_time = time.time() anchor_list = [] positive_list = [] negative_list = [] triplet_loss_list = [] # Run a forward pass for the sampled images print('Running forward pass on validation set') nrof_batches_per_epoch = nrof_triplets * 3 // FLAGS.batch_size for i in xrange(nrof_batches_per_epoch): batch = facenet.get_triplet_batch(triplets, i) feed_dict = {images_placeholder: batch, phase_train_placeholder: False} emb, triplet_loss, step = sess.run([embeddings, loss, global_step], feed_dict=feed_dict) nrof_batch_triplets = emb.shape[0] / 3 anchor_list.append(emb[(0 * nrof_batch_triplets):(1 * nrof_batch_triplets), :]) positive_list.append(emb[(1 * nrof_batch_triplets):(2 * nrof_batch_triplets), :]) negative_list.append(emb[(2 * nrof_batch_triplets):(3 * nrof_batch_triplets), :]) triplet_loss_list.append(triplet_loss) anchor = np.vstack(anchor_list) positive = np.vstack(positive_list) negative = np.vstack(negative_list) duration = time.time() - start_time thresholds = np.arange(0, 4, 0.01) embeddings1 = np.vstack([anchor, anchor]) embeddings2 = np.vstack([positive, negative]) actual_issame = np.asarray([True] * anchor.shape[0] + [False] * anchor.shape[0]) tpr, fpr, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, actual_issame) print('Epoch: [%d]\tTime %.3f\ttripErr %2.3f\taccuracy %1.3f%c%1.3f' % ( epoch, duration, np.mean(triplet_loss_list), np.mean(accuracy), u"\u00B1", np.std(accuracy))) # Add validation loss and accuracy to summary summary = tf.Summary() summary.value.add(tag='validation_loss', simple_value=np.mean(triplet_loss_list).astype(float)) summary.value.add(tag='accuracy', simple_value=np.mean(accuracy)) summary_writer.add_summary(summary, step) if False: facenet.plot_roc(fpr, tpr, 'NN4')
def evaluate(embeddings, actual_issame, nrof_folds=10): # Calculate evaluation metrics thresholds = np.arange(0, 4, 0.01) # 0.01表示步长 # 四个点原来是取奇数和偶数列 embeddings1 = embeddings[0::2] # 取出奇数行的特征,取出偶数行的特征 embeddings2 = embeddings[1::2] # print(len(embeddings1)) # print('wokao') # print(len(embeddings2)) tpr, fpr, accuracy, threshold_acc = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), nrof_folds=nrof_folds) # np.asarray将列表转换为数组 thresholds = np.arange(0, 4, 0.001) val, val_std, far = facenet.calculate_val(thresholds, threshold_acc, embeddings1, embeddings2, np.asarray(actual_issame), 1e-3, nrof_folds=nrof_folds) return tpr, fpr, accuracy, val, val_std, far
def evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, embeddings, paths, actual_issame, augument_images, aug_value): 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): paths_batch = paths[i*FLAGS.batch_size:(i+1)*FLAGS.batch_size] images = facenet.load_data(paths_batch) images_aug = augument_images(images, aug_value) feed_dict = { images_placeholder: images_aug, phase_train_placeholder: False } emb_list += sess.run([embeddings], feed_dict=feed_dict) 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] _, _, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame)) return accuracy
def main(argv=None): 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(): with tf.Session() as sess: # Load the model print('Loading model "%s"' % FLAGS.model_file) facenet.load_model(FLAGS.model_file) # Get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0") phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0") embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") image_size = images_placeholder.get_shape()[1] # Run forward pass to calculate embeddings nrof_images = len(paths) nrof_batches = int(nrof_images / FLAGS.batch_size) 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, 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 # Calculate evaluation metrics 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))) thresholds = np.arange(0, 4, 0.001) val, val_std, far = facenet.calculate_val(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), 1e-3, FLAGS.seed) print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far)) facenet.plot_roc(fpr, tpr, 'NN4')
def evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, image_size, embeddings, paths, actual_issame, augument_images, aug_value, batch_size, orig_image_size, seed): nrof_images = len(paths) nrof_batches = int(math.ceil(1.0*nrof_images / batch_size)) emb_list = [] for i in range(nrof_batches): start_index = i*batch_size end_index = min((i+1)*batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, orig_image_size) images_aug = augument_images(images, aug_value, image_size) feed_dict = { images_placeholder: images_aug, phase_train_placeholder: False } emb_list += sess.run([embeddings], feed_dict=feed_dict) 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] _, _, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), seed) return accuracy
def evaluate(embeddings, actual_issame, nrof_folds=10): # Calculate evaluation metrics thresholds = np.arange(0, 4, 0.01) # Note there that x[startAt:endBefore:skip] is the notation # so embeddings1 and embeddings2 are simply two lists with # embeddings1[i] and embeddings2[i] being the pairs that we need to validate embeddings1 = embeddings[0::2] embeddings2 = embeddings[1::2] print("thresholds is: {}".format(thresholds)) print("embeddings1 is: {}".format(embeddings1)) print("embeddings2 is: {}".format(embeddings2)) tpr, fpr, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), nrof_folds=nrof_folds) thresholds = np.arange(0, 4, 0.001) # val is the percentage of samples that were classified to be correct # val_std is the std of val # far is the percentage of samples that were classified to be wrong val, val_std, far = facenet.calculate_val(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), 1e-3, nrof_folds=nrof_folds) return tpr, fpr, accuracy, val, val_std, far
def evaluate(embeddings, actual_issame, nrof_folds=10): # Calculate evaluation metrics thresholds = np.arange(0, 4, 0.01) # start from 0, step=2 embeddings1 = embeddings[0::2] # start from 1, step=2 embeddings2 = embeddings[1::2] # embeddings1 is corresponding with embeddings2 tpr, fpr, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), nrof_folds=nrof_folds) thresholds = np.arange(0, 4, 0.001) val, val_std, far = facenet.calculate_val(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), 1e-3, nrof_folds=nrof_folds) return tpr, fpr, accuracy, val, val_std, far
def evaluate(embeddings, actual_issame, nrof_folds=10): # Calculate evaluation metrics thresholds = np.arange(0, 4, 0.01) embeddings1 = embeddings[0::2] embeddings2 = embeddings[1::2] # ipdb.set_trace() tpr, fpr, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), nrof_folds=nrof_folds) thresholds = np.arange(0, 4, 0.001) val, val_std, far = facenet.calculate_val(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), 1e-2, nrof_folds=nrof_folds) dot_product_threshold = np.arange(0.0, 1.0, 0.001) # ipdb.set_trace() best_threshold, acc_dot, recall, fpr_dot, precision_dot, dot_product_all, fp_idxs, fn_idxs, recall_th, precision_th,\ acc_th = facenet.calculate_acc_dot_product(dot_product_threshold, embeddings1, embeddings2, np.asarray(actual_issame)) return tpr, fpr, accuracy, val, val_std, far, best_threshold, acc_dot, recall, fpr_dot, precision_dot,\ dot_product_all, fp_idxs, fn_idxs, recall_th, precision_th, acc_th
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(): with tf.Session() as sess: print('Reading model "%s"' % FLAGS.model_file) new_saver = tf.train.import_meta_graph( os.path.expanduser(FLAGS.model_file + '.meta')) all_vars_dict = {var.name: var for var in tf.all_variables()} restore_vars = {} for var_name in all_vars_dict: if '/ExponentialMovingAverage' in var_name: param_name = var_name.replace('/ExponentialMovingAverage', '') if param_name in all_vars_dict: param = all_vars_dict[param_name] print('%s\t%s' % (var_name, param)) restore_vars[var_name] = param saver = tf.train.Saver(restore_vars, saver_def=new_saver.as_saver_def()) saver.restore(sess, os.path.expanduser(FLAGS.model_file)) images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") image_size = images_placeholder.get_shape()[1] 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, 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')