Beispiel #1
0
def main(args):

    with tf.Graph().as_default():

        with tf.Session() as sess:

            # Read the file containing the pairs used for testing
            pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))

            # Get the paths for the corresponding images
            paths, actual_issame = lfw.get_paths(
                os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)

            # Load the model
            print('Loading model "%s"' % args.model_file)
            facenet.load_model(args.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")

            tpr, fpr, accuracy, val, val_std, far = lfw.validate(
                sess, paths, actual_issame, args.seed, 60, images_placeholder,
                phase_train_placeholder, embeddings)
            print('Accuracy: %1.3f+-%1.3f' %
                  (np.mean(accuracy), np.std(accuracy)))
            print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' %
                  (val, val_std, far))

            facenet.plot_roc(fpr, tpr, 'NN4')
def main(args):
    with tf.Graph().as_default():
        images_placeholder = tf.placeholder(tf.float32, shape=(None,args.image_size,args.image_size,3), name='input')
        with tf.Session() as sess:
            print('Loading graphdef: %s' % args.model_file)
            with gfile.FastGFile(os.path.expanduser(args.model_file),'rb') as f:
                graph_def = tf.GraphDef()
                graph_def.ParseFromString(f.read())
                return_elements = ['phase_train:0', 'embeddings:0']
                phase_train_placeholder, embeddings = tf.import_graph_def(graph_def, input_map={'input':images_placeholder}, 
                    return_elements=return_elements, name='import')
  
        # Read the file containing the pairs used for testing
        pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))

        # Get the paths for the corresponding images
        paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)

        # Evaluate
        tpr, fpr, accuracy, val, val_std, far = lfw.validate(sess, paths, 
            actual_issame, args.seed, 60, 
            images_placeholder, phase_train_placeholder, embeddings, nrof_folds=args.lfw_nrof_folds)
        print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy)))
        print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far))
        
        facenet.plot_roc(fpr, tpr, 'NN4')
Beispiel #3
0
def main(args):
  
    with tf.Graph().as_default():

        with tf.Session() as sess:
            
            # Read the file containing the pairs used for testing
            pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))

            # Get the paths for the corresponding images
            paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)
            
            # Load the model
            print('Loading model "%s"' % args.model_file)
            facenet.load_model(args.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")

            tpr, fpr, accuracy, val, val_std, far = lfw.validate(sess, 
                paths, actual_issame, args.seed, 60, 
                images_placeholder, phase_train_placeholder, embeddings, nrof_folds=args.lfw_nrof_folds)
            print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy)))
            print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far))
            
            facenet.plot_roc(fpr, tpr, 'NN4')
Beispiel #4
0
def main(args):

    with tf.Graph().as_default():

        with tf.Session() as sess:

            # Read the file containing the pairs used for testing
            pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))

            # Get the paths for the corresponding images
            paths, actual_issame = lfw.get_paths(
                os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)

            # Load the model
            print('Model directory: %s' % args.model_dir)
            meta_file, ckpt_file = facenet.get_model_filenames(
                os.path.expanduser(args.model_dir))
            print('Metagraph file: %s' % meta_file)
            print('Checkpoint file: %s' % ckpt_file)
            facenet.load_model(args.model_dir, meta_file, ckpt_file)

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")

            image_size = images_placeholder.get_shape()[1]
            embedding_size = embeddings.get_shape()[1]

            # Run forward pass to calculate embeddings
            print('Runnning forward pass on LFW images')
            batch_size = args.lfw_batch_size
            nrof_images = len(paths)
            nrof_batches = int(math.ceil(1.0 * nrof_images / batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))
            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}
                emb_array[start_index:end_index, :] = sess.run(
                    embeddings, feed_dict=feed_dict)

            tpr, fpr, accuracy, val, val_std, far = lfw.evaluate(
                emb_array,
                args.seed,
                actual_issame,
                nrof_folds=args.lfw_nrof_folds)

            print('Accuracy: %1.3f+-%1.3f' %
                  (np.mean(accuracy), np.std(accuracy)))
            print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' %
                  (val, val_std, far))

            facenet.plot_roc(fpr, tpr, 'NN4')
Beispiel #5
0
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(args):
  
    with tf.Graph().as_default():
      
        with tf.Session() as sess:
            
            # Read the file containing the pairs used for testing
            pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))

            # Get the paths for the corresponding images
            paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)

            # Load the model
            print('Model directory: %s' % args.model_dir)
            meta_file, ckpt_file = facenet.get_model_filenames(os.path.expanduser(args.model_dir))
            
            print('Metagraph file: %s' % meta_file)
            print('Checkpoint file: %s' % ckpt_file)
            facenet.load_model(args.model_dir, meta_file, ckpt_file)
            
            # Get input and output tensors
            image_paths_placeholder = tf.get_default_graph().get_tensor_by_name("image_paths:0")
            labels_placeholder = tf.get_default_graph().get_tensor_by_name("labels:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            batch_size_placeholder = tf.get_default_graph().get_tensor_by_name("batch_size:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            
            enqueue_op = tf.get_default_graph().get_operation_by_name("enqueue_op")
            label_batch = tf.get_default_graph().get_tensor_by_name("label_batch:0")
            
            # Run forward pass to calculate embeddings
            print('Runnning forward pass on LFW images')
            # Enqueue one epoch of image paths and labels
            labels_array = np.expand_dims(np.arange(0,len(paths)),1)
            image_paths_array = np.expand_dims(np.array(paths),1)
            sess.run(enqueue_op, {image_paths_placeholder: image_paths_array, labels_placeholder: labels_array})
            
            embedding_size = embeddings.get_shape()[1]
            nrof_images = len(actual_issame)*2
            nrof_batches = nrof_images // args.lfw_batch_size
            emb_array = np.zeros((nrof_images, embedding_size))
            lab_array = np.zeros((nrof_images,))
            for _ in range(nrof_batches):
                feed_dict = {phase_train_placeholder:False, batch_size_placeholder:args.lfw_batch_size}
                emb, lab = sess.run([embeddings, label_batch], feed_dict=feed_dict)
                lab_array[lab] = lab
                emb_array[lab] = emb
                
            assert np.array_equal(lab_array, np.arange(nrof_images))==True, 'Wrong labels used for evaluation, possibly caused by training examples left in the input pipeline'
        
            tpr, fpr, accuracy, val, val_std, far = lfw.evaluate(emb_array, 
                actual_issame, nrof_folds=args.lfw_nrof_folds)

            print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy)))
            print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far))
            
            facenet.plot_roc(fpr, tpr, 'NN4')
Beispiel #7
0
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 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')
Beispiel #9
0
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')
Beispiel #10
0
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')
Beispiel #11
0
def main(args):
  
    with tf.Graph().as_default():
      
        with tf.Session() as sess:
            
            # Read the file containing the pairs used for testing
            pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))

            # Get the paths for the corresponding images
            paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)

            # Load the model
            print('Model directory: %s' % args.model_dir)
            meta_file, ckpt_file = facenet.get_model_filenames(os.path.expanduser(args.model_dir))
            print('Metagraph file: %s' % meta_file)
            print('Checkpoint file: %s' % ckpt_file)
            facenet.load_model(args.model_dir, meta_file, ckpt_file)
            
            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            
            image_size = images_placeholder.get_shape()[1]
            embedding_size = embeddings.get_shape()[1]
        
            # Run forward pass to calculate embeddings
            print('Runnning forward pass on LFW images')
            batch_size = args.lfw_batch_size
            nrof_images = len(paths)
            nrof_batches = int(math.ceil(1.0*nrof_images / batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))
            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 }
                emb_array[start_index:end_index,:] = sess.run(embeddings, feed_dict=feed_dict)
        
            tpr, fpr, accuracy, val, val_std, far = lfw.evaluate(emb_array, 
                args.seed, actual_issame, nrof_folds=args.lfw_nrof_folds)

            print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy)))
            print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far))
            
            facenet.plot_roc(fpr, tpr, 'NN4')
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')
Beispiel #13
0
def main(args):

    with tf.Graph().as_default():

        with tf.Session() as sess:

            # Read the file containing the pairs used for testing
            pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))
            np.random.shuffle(pairs)

            # Get the paths for the corresponding images
            paths, actual_issame = lfw.get_paths(
                os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)
            #actual_issame= actual_issame[0:args.lfw_batch_size]

            # Load the model
            print('Model directory: %s' % args.model_dir)
            meta_file, ckpt_file = facenet.get_model_filenames(
                os.path.expanduser(args.model_dir))

            print('Metagraph file: %s' % meta_file)
            print('Checkpoint file: %s' % ckpt_file)
            facenet.load_model(args.model_dir, meta_file, ckpt_file)

            # Get input and output tensors
            #images_placeholder = tf.get_default_graph().get_tensor_by_name("image_batch:0")
            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            #keep_probability_placeholder = tf.get_default_graph().get_tensor_by_name('keep_probability:0')
            #weight_decay_placeholder = tf.get_default_graph().get_tensor_by_name('weight_decay:0')
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name('phase_train:0')

            image_size = images_placeholder.get_shape()[1]
            embedding_size = embeddings.get_shape()[1]

            # Run forward pass to calculate embeddings
            print('Runnning forward pass on LFW images')
            batch_size = args.lfw_batch_size
            nrof_images = len(paths)
            nrof_batches = int(math.ceil(1.0 * nrof_images / batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))
            #emb_array = np.zeros((2*batch_size, embedding_size))
            for i in range(nrof_batches):
                print("Test batch:%d/%d\n" % (i, 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 = {phase_train_placeholder: False, images_placeholder:images, keep_probability_placeholder:1.0, weight_decay_placeholder:0.0}
                feed_dict = {
                    phase_train_placeholder: False,
                    images_placeholder: images
                }
                emb_array[start_index:end_index, :] = sess.run(
                    embeddings, feed_dict=feed_dict)

            print('Evaluate_model: %s' % args.evaluate_mode)
            if args.evaluate_mode == 'Euclidian':

                tpr, fpr, accuracy, val, val_std, far, fp_idx, fn_idx, best_threshold, val_threshold = lfw.evaluate(
                    emb_array, actual_issame, nrof_folds=args.lfw_nrof_folds)
            if args.evaluate_mode == 'similarity':
                #pca = PCA(whiten=True)
                pca = PCA(n_components=128)
                pca.fit(emb_array)
                emb_array_pca = pca.transform(emb_array)

                tpr, fpr, accuracy, val, val_std, far, fp_idx, fn_idx, best_threshold, val_threshold = lfw.evaluate_cosine(
                    emb_array_pca,
                    actual_issame,
                    nrof_folds=args.lfw_nrof_folds)

            ################### edit by mzh 12012017: select the false positive/negative images ####################
            nrof_test_paths = nrof_batches * batch_size
            nrof_test_tp_pairs = sum(actual_issame[0:int(nrof_test_paths / 2)])
            nrof_test_tn_pairs = len(
                actual_issame[0:int(nrof_test_paths / 2)]) - nrof_test_tp_pairs
            paths_pairs = [
                paths[0:nrof_test_paths:2], paths[1:nrof_test_paths:2]
            ]  # paths_pairs shape: [2, number of pairs], each column is corresponding to a pair of images
            paths_pairs_array = np.array(paths_pairs)
            fp_images_paths = paths_pairs_array[:, fp_idx]
            fn_images_paths = paths_pairs_array[:, fn_idx]
            _, nrof_fp_pairs = fp_images_paths.shape
            _, nrof_fn_pairs = fn_images_paths.shape

            ################### edit by mzh 12012017: select the false positive/negative images ####################

            print('Accuracy: %1.3f+-%1.3f @ Best threshold %f\n' %
                  (np.mean(accuracy), np.std(accuracy), best_threshold))
            print(
                'Validation rate: %2.5f+-%2.5f @ FAR=%2.5f @val_threshold %f\n'
                % (val, val_std, far, val_threshold))

            auc = metrics.auc(fpr, tpr)
            print('Area Under Curve (AUC): %1.3f\n' % auc)
            eer = brentq(lambda x: 1. - x - interpolate.interp1d(fpr, tpr)(x),
                         0., 1.)
            print('Equal Error Rate (EER): %1.3f\n' % eer)

            subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S')
            log_dir = os.path.join(os.path.expanduser(args.logs_base_dir),
                                   subdir)
            print('log_dir: %s\n' % log_dir)
            if not os.path.isdir(
                    log_dir):  # Create the log directory if it doesn't exist
                os.makedirs(log_dir)
            with open(os.path.join(log_dir, 'validation_on_dataset.txt'),
                      'at') as f:
                print('Saving the evaluation results...\n')
                f.write('arguments: %s\n--------------------\n' %
                        ' '.join(sys.argv))
                f.write('Accuracy: %1.3f+-%1.3f\n' %
                        (np.mean(accuracy), np.std(accuracy)))
                f.write('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f\n' %
                        (val, val_std, far))
                f.write('Best threshold: %2.5f \n' % (best_threshold))
                f.write('Validation threshold: %2.5f \n' % (val_threshold))
                f.write('Area Under Curve (AUC): %1.3f\n' % auc)
                f.write('Equal Error Rate (EER): %1.3f\n' % eer)
                print('Saving the False positive pairs images ...\n ')
                f.write(
                    'False positive pairs: %d / %d ---------------------------------------------\n'
                    % (nrof_fp_pairs, nrof_test_tp_pairs))
                for i in range(nrof_fp_pairs):
                    f.write('%d %s\n' % (i, fp_images_paths[:, i]))
                print('Saving the False negative pairs images ...\n ')
                f.write(
                    'False negative pairs: %d / %d ---------------------------------------------\n'
                    % (nrof_fn_pairs, nrof_test_tn_pairs))
                for i in range(nrof_fn_pairs):
                    f.write('%d %s\n' % (i, fn_images_paths[:, i]))
            ################### edit by mzh 12012017: write the false positive/negative images to the file  ####################

            false_images_list = os.path.join(log_dir,
                                             'validation_on_dataset.txt')
            save_dir = log_dir
            save_false_images.save_false_images(false_images_list, save_dir)

            with open(os.path.join(log_dir, 'validation_on_dataset.txt'),
                      'at') as f:
                print('Saving the tpr, fpr of ROC ...\n ')
                f.write(
                    'ROC: tpr, fpr --------------------------------------------------------------\n'
                )
                for tp, fp in zip(tpr, fpr):
                    f.write('tpr/fpr: %f/%f\n' % (tp, fp))

            facenet.plot_roc(fpr, tpr, 'ROC')
Beispiel #14
0
def main(args):
  
    with tf.Graph().as_default():
      
        with tf.Session() as sess:
            
            # Read the file containing the pairs used for testing
            pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))

            # Get the paths for the corresponding images
            paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)

            # Load the model
            print('Model directory: %s' % args.model_dir)
            meta_file, ckpt_file = facenet.get_model_filenames(os.path.expanduser(args.model_dir))
            
            print('Metagraph file: %s' % meta_file)
            print('Checkpoint file: %s' % ckpt_file)
            facenet.load_model(args.model_dir, meta_file, ckpt_file)
            
            # Get input and output tensors
            #images_placeholder = tf.get_default_graph().get_tensor_by_name("image_batch:0")
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            #keep_probability_placeholder = tf.get_default_graph().get_tensor_by_name('keep_probability:0')
            #weight_decay_placeholder = tf.get_default_graph().get_tensor_by_name('weight_decay:0')
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name('phase_train:0')
            
            image_size = images_placeholder.get_shape()[1]
            embedding_size = embeddings.get_shape()[1]
        
            # Run forward pass to calculate embeddings
            print('Runnning forward pass on LFW images')
            batch_size = args.lfw_batch_size
            nrof_images = len(paths)
            nrof_batches = int(math.ceil(1.0*nrof_images / batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))
            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 = {phase_train_placeholder: False, images_placeholder:images, keep_probability_placeholder:1.0, weight_decay_placeholder:0.0}
                feed_dict = {phase_train_placeholder: False, images_placeholder: images}
                emb_array[start_index:end_index,:] = sess.run(embeddings, feed_dict=feed_dict)

            print('Evaluate_model: %s' % args.evaluate_mode)
            if args.evaluate_mode == 'Euclidian':

                tpr, fpr, accuracy, val, val_std, far = lfw.evaluate(emb_array,
                    actual_issame, nrof_folds=args.lfw_nrof_folds)
            if args.evaluate_mode == 'similarity':
                #pca = PCA(whiten=True)
                pca = PCA(n_components=128)
                pca.fit(emb_array)
                emb_array_pca = pca.transform(emb_array)

                tpr, fpr, accuracy, val, val_std, far = lfw.evaluate_cosine(emb_array_pca,
                    actual_issame, nrof_folds=args.lfw_nrof_folds)

            print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy)))
            print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far))
            
            facenet.plot_roc(fpr, tpr, 'NN4')
Beispiel #15
0
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')