Ejemplo n.º 1
0
def train(sess, dataset, epoch, images_placeholder, phase_train_placeholder,
          global_step, embeddings, loss, train_op, summary_op, summary_writer):
    batch_number = 0
    while batch_number < FLAGS.epoch_size:
        print('Loading training data')
        # Sample people and load new data
        image_paths, num_per_class = facenet.sample_people(
            dataset, FLAGS.people_per_batch, FLAGS.images_per_person)
        image_data = facenet.load_data(image_paths, FLAGS.random_crop,
                                       FLAGS.random_flip, FLAGS.image_size)

        print('Selecting suitable triplets for training')
        start_time = time.time()
        emb_list = []
        # Run a forward pass for the sampled images
        nrof_examples_per_epoch = FLAGS.people_per_batch * FLAGS.images_per_person
        nrof_batches_per_epoch = int(
            np.floor(nrof_examples_per_epoch / FLAGS.batch_size))
        for i in xrange(nrof_batches_per_epoch):
            batch = facenet.get_batch(image_data, FLAGS.batch_size, i)
            feed_dict = {
                images_placeholder: batch,
                phase_train_placeholder: True
            }
            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
        # Select triplets based on the embeddings
        triplets, nrof_random_negs, nrof_triplets = facenet.select_training_triplets(
            emb_array, num_per_class, image_data, FLAGS.people_per_batch,
            FLAGS.alpha)
        duration = time.time() - start_time
        print(
            '(nrof_random_negs, nrof_triplets) = (%d, %d): time=%.3f seconds' %
            (nrof_random_negs, nrof_triplets, duration))

        # Perform training on the selected triplets
        i = 0
        while i * FLAGS.batch_size < nrof_triplets * 3 and batch_number < FLAGS.epoch_size:
            start_time = time.time()
            batch = facenet.get_triplet_batch(triplets, i, FLAGS.batch_size)
            feed_dict = {
                images_placeholder: batch,
                phase_train_placeholder: True
            }
            err, _, step = sess.run([loss, train_op, global_step],
                                    feed_dict=feed_dict)
            if (batch_number % 20 == 0):
                summary_str, step = sess.run([summary_op, global_step],
                                             feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, global_step=step)
            duration = time.time() - start_time
            print('Epoch: [%d][%d/%d]\tTime %.3f\ttripErr %2.3f' %
                  (epoch, batch_number, FLAGS.epoch_size, duration, err))
            batch_number += 1
            i += 1
    return step
Ejemplo n.º 2
0
def train(args, sess, dataset, epoch, images_placeholder, phase_train_placeholder,
          global_step, embeddings, loss, train_op, summary_op, summary_writer):
    batch_number = 0
    while batch_number < args.epoch_size:
        print('Loading training data')
        # Sample people and load new data
        start_time = time.time()
        image_paths, num_per_class = facenet.sample_people(dataset, args.people_per_batch, args.images_per_person)
        image_data = facenet.load_data(image_paths, args.random_crop, args.random_flip, args.image_size)
        load_time = time.time() - start_time
        print('Loaded %d images in %.2f seconds' % (image_data.shape[0], load_time))

        print('Selecting suitable triplets for training')
        start_time = time.time()
        emb_list = []
        # Run a forward pass for the sampled images
        nrof_examples_per_epoch = args.people_per_batch * args.images_per_person
        nrof_batches_per_epoch = int(np.floor(nrof_examples_per_epoch / args.batch_size))
        for i in xrange(nrof_batches_per_epoch):
            batch = facenet.get_batch(image_data, args.batch_size, i)
            feed_dict = {images_placeholder: batch, phase_train_placeholder: True}
            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
        # Select triplets based on the embeddings
        triplets, nrof_random_negs, nrof_triplets = facenet.select_triplets(
            emb_array, num_per_class, image_data, args.people_per_batch, args.alpha)
        selection_time = time.time() - start_time
        print('(nrof_random_negs, nrof_triplets) = (%d, %d): time=%.3f seconds' % (
        nrof_random_negs, nrof_triplets, selection_time))

        # Perform training on the selected triplets
        train_time = 0
        i = 0
        while i * args.batch_size < nrof_triplets * 3 and batch_number < args.epoch_size:
            start_time = time.time()
            batch = facenet.get_triplet_batch(triplets, i, args.batch_size)
            feed_dict = {images_placeholder: batch, phase_train_placeholder: True}
            err, _, step = sess.run([loss, train_op, global_step], feed_dict=feed_dict)
            if (batch_number % 20 == 0):
                summary_str, step = sess.run([summary_op, global_step], feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, global_step=step)
            duration = time.time() - start_time
            print('Epoch: [%d][%d/%d]\tTime %.3f\ttripErr %2.3f' %
                  (epoch, batch_number, args.epoch_size, duration, err))
            batch_number += 1
            i += 1
            train_time += duration
        # Add validation loss and accuracy to summary
        summary = tf.Summary()
        #pylint: disable=maybe-no-member
        summary.value.add(tag='time/load', simple_value=load_time)
        summary.value.add(tag='time/selection', simple_value=selection_time)
        summary.value.add(tag='time/train', simple_value=train_time)
        summary.value.add(tag='time/total', simple_value=load_time+selection_time+train_time)
        summary_writer.add_summary(summary, step)
    return step
Ejemplo n.º 3
0
def train(args, sess, dataset, epoch, images_placeholder, phase_train_placeholder,
          global_step, embeddings, loss, train_op, summary_op, summary_writer):
    batch_number = 0
    while batch_number < args.epoch_size:
        print('Loading training data')
        # Sample people and load new data
        start_time = time.time()
        image_paths, num_per_class = facenet.sample_people(dataset, args.people_per_batch, args.images_per_person)
        image_data = facenet.load_data(image_paths, args.random_crop, args.random_flip, args.image_size)
        load_time = time.time() - start_time
        print('Loaded %d images in %.2f seconds' % (image_data.shape[0], load_time))

        print('Selecting suitable triplets for training')
        start_time = time.time()
        emb_list = []
        # Run a forward pass for the sampled images
        nrof_examples_per_epoch = args.people_per_batch * args.images_per_person
        nrof_batches_per_epoch = int(np.floor(nrof_examples_per_epoch / args.batch_size))
        for i in xrange(nrof_batches_per_epoch):
            batch = facenet.get_batch(image_data, args.batch_size, i)
            feed_dict = {images_placeholder: batch, phase_train_placeholder: True}
            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
        # Select triplets based on the embeddings
        triplets, nrof_random_negs, nrof_triplets = facenet.select_triplets(
            emb_array, num_per_class, image_data, args.people_per_batch, args.alpha)
        selection_time = time.time() - start_time
        print('(nrof_random_negs, nrof_triplets) = (%d, %d): time=%.3f seconds' % (
        nrof_random_negs, nrof_triplets, selection_time))

        # Perform training on the selected triplets
        train_time = 0
        i = 0
        while i * args.batch_size < nrof_triplets * 3 and batch_number < args.epoch_size:
            start_time = time.time()
            batch = facenet.get_triplet_batch(triplets, i, args.batch_size)
            feed_dict = {images_placeholder: batch, phase_train_placeholder: True}
            err, _, step = sess.run([loss, train_op, global_step], feed_dict=feed_dict)
            if (batch_number % 20 == 0):
                summary_str, step = sess.run([summary_op, global_step], feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, global_step=step)
            duration = time.time() - start_time
            print('Epoch: [%d][%d/%d]\tTime %.3f\ttripErr %2.3f' %
                  (epoch, batch_number, args.epoch_size, duration, err))
            batch_number += 1
            i += 1
            train_time += duration
        # Add validation loss and accuracy to summary
        summary = tf.Summary()
        summary.value.add(tag='time/load', simple_value=load_time)
        summary.value.add(tag='time/selection', simple_value=selection_time)
        summary.value.add(tag='time/train', simple_value=train_time)
        summary.value.add(tag='time/total', simple_value=load_time+selection_time+train_time)
        summary_writer.add_summary(summary, step)
    return step
Ejemplo n.º 4
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')
Ejemplo n.º 5
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')
Ejemplo n.º 6
0
def train(sess, dataset, epoch, images_placeholder, phase_train_placeholder,
          global_step, embeddings, loss, train_op, summary_op, summary_writer):
    batch_number = 0
    while batch_number < FLAGS.epoch_size:
        print('Loading training data')
        # Sample people and load new data
        image_paths, num_per_class = facenet.sample_people(dataset, FLAGS.people_per_batch, FLAGS.images_per_person)
        image_data = facenet.load_data(image_paths, FLAGS.random_crop, FLAGS.random_flip, FLAGS.image_size)

        print('Selecting suitable triplets for training')
        start_time = time.time()
        emb_list = []
        # Run a forward pass for the sampled images
        nrof_examples_per_epoch = FLAGS.people_per_batch * FLAGS.images_per_person
        nrof_batches_per_epoch = int(np.floor(nrof_examples_per_epoch / FLAGS.batch_size))
        for i in xrange(nrof_batches_per_epoch):
            batch = facenet.get_batch(image_data, FLAGS.batch_size, i)
            feed_dict = {images_placeholder: batch, phase_train_placeholder: True}
            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
        # Select triplets based on the embeddings
        triplets, nrof_random_negs, nrof_triplets = facenet.select_training_triplets(emb_array, num_per_class, 
                                                                                     image_data, FLAGS.people_per_batch, 
                                                                                     FLAGS.alpha)
        duration = time.time() - start_time
        print('(nrof_random_negs, nrof_triplets) = (%d, %d): time=%.3f seconds' % (
        nrof_random_negs, nrof_triplets, duration))

        # Perform training on the selected triplets
        i = 0
        while i * FLAGS.batch_size < nrof_triplets * 3 and batch_number < FLAGS.epoch_size:
            start_time = time.time()
            batch = facenet.get_triplet_batch(triplets, i, FLAGS.batch_size)
            feed_dict = {images_placeholder: batch, phase_train_placeholder: True}
            err, _, step = sess.run([loss, train_op, global_step], feed_dict=feed_dict)
            if (batch_number % 20 == 0):
                summary_str, step = sess.run([summary_op, global_step], feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, global_step=step)
            duration = time.time() - start_time
            print('Epoch: [%d][%d/%d]\tTime %.3f\ttripErr %2.3f' %
                  (epoch, batch_number, FLAGS.epoch_size, duration, err))
            batch_number += 1
            i += 1
    return step
Ejemplo n.º 7
0
def train(args, sess, dataset, epoch, images_placeholder,
          phase_train_placeholder, learning_rate_placeholder, global_step,
          embeddings, loss, train_op, summary_op, summary_writer):  #each epoch
    batch_number = 0
    if args.learning_rate > 0.0:
        lr = args.learning_rate
    else:
        lr = get_learning_rate_from_file('../data/learning_rate_schedule.txt',
                                         epoch)

    while batch_number < args.epoch_size:  #this batch_number means batch_num in train, not in getting feature
        print('Loading training data')
        # Sample people and load new data
        start_time = time.time()
        image_paths, num_per_class = facenet.sample_people(
            dataset, args.people_per_epoch, args.images_per_person)
        image_data = facenet.load_data(image_paths, args.random_crop,
                                       args.random_flip, args.image_size)
        load_time = time.time() - start_time
        print('Loaded %d images in %.2f seconds' %
              (image_data.shape[0], load_time))

        print('Selecting suitable triplets for training')
        start_time = time.time()
        emb_list = []
        # Run a forward pass for the sampled images
        nrof_examples_per_epoch = args.people_per_epoch * args.images_per_person  #have no connection with epoch_size
        nrof_batches_per_epoch = int(
            np.floor(nrof_examples_per_epoch / args.batch_size)
        )  #the variable is only for batched in getting feature
        for i in xrange(nrof_batches_per_epoch):
            batch = facenet.get_batch(image_data, args.batch_size, i)
            feed_dict = {
                images_placeholder: batch,
                phase_train_placeholder: True,
                learning_rate_placeholder: lr
            }
            emb_list += sess.run(
                [embeddings], feed_dict=feed_dict
            )  #step 1:get the feature of all image in a epoch
        emb_array = np.vstack(
            emb_list
        )  # Stack the embeddings to a nrof_examples_per_epoch x 128 matrix
        # Select triplets based on the embeddings
        # the data from lfw dataset makes the embeddings'size different from each other
        triplets, nrof_random_negs, nrof_triplets = facenet.select_triplets(
            emb_array, num_per_class, image_data, args.people_per_epoch,
            args.alpha
        )  #Here the information from embedding and information from sample_people()
        #print("num_triplets: "+str(len(triplets)))
        selection_time = time.time() - start_time
        print(
            '(nrof_random_negs, nrof_triplets) = (%d, %d): time=%.3f seconds' %
            (nrof_random_negs, nrof_triplets, selection_time))

        # Perform training on the selected triplets
        train_time = 0
        i = 0
        #if tripplets is less than batch_size, go back to the start of the epoch
        while i * args.batch_size < nrof_triplets * 3 and batch_number < args.epoch_size:
            #Why not refresh the triplet every batch
            start_time = time.time()
            batch = facenet.get_triplet_batch(triplets, i, args.batch_size)
            feed_dict = {
                images_placeholder: batch,
                phase_train_placeholder: True,
                learning_rate_placeholder: lr
            }
            err, _, step = sess.run([loss, train_op, global_step],
                                    feed_dict=feed_dict)
            if (batch_number % 20 == 1):
                #does the line below waste time?
                summary_str, step = sess.run([summary_op, global_step],
                                             feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, global_step=step)
            duration = time.time() - start_time
            print('Epoch: [%d][%d/%d]\tTime %.3f\ttripErr %2.3f' %
                  (epoch, batch_number, args.epoch_size, duration, err))
            batch_number += 1
            i += 1
            train_time += duration
        # Add validation loss and accuracy to summary
        summary = tf.Summary()
        #pylint: disable=maybe-no-member
        summary.value.add(tag='time/load', simple_value=load_time)
        summary.value.add(tag='time/selection', simple_value=selection_time)
        summary.value.add(tag='time/train', simple_value=train_time)
        summary.value.add(tag='time/total',
                          simple_value=load_time + selection_time + train_time)
        summary_writer.add_summary(summary, step)
    return step