Пример #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
Пример #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
Пример #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
Пример #4
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
Пример #5
0
def train():
  dataset = facenet.get_dataset(FLAGS.data_dir)
  train_set, test_set = facenet.split_dataset(dataset, 0.9)
  
  fileName = "/home/david/debug4.h5"
  f = h5py.File(fileName,  "r")
  for item in f.values():
    print(item)
  
  w1 = f['1w'][:]
  b1 = f['1b'][:]
  f.close()
  print(w1.shape)
  print(b1.shape)
  
  """Train CIFAR-10 for a number of steps."""
  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, 96, 96, 3), name='Input')
    
    # Build a Graph that computes the logits predictions from the inference model
    #embeddings = facenet.inference_nn4_max_pool_96(images_placeholder, phase_train=True)
    
    conv1 = _conv(images_placeholder, 3, 64, 7, 7, 2, 2, 'SAME', 'conv1_7x7', phase_train=False, use_batch_norm=False, init_weight=w1, init_bias=b1)
    resh1 = tf.reshape(conv1, [-1, 294912])
    embeddings = _affine(resh1, 294912, 128)
    
        
    # Split example embeddings into anchor, positive and negative
    a, p, n = tf.split(0, 3, embeddings)

    # Calculate triplet loss
    loss = facenet.triplet_loss(a, p, n)

    # Build a Graph that trains the model with one batch of examples and updates the model parameters
    train_op, grads = facenet.train(loss, global_step)
    
    # Create a saver
    saver = tf.train.Saver(tf.all_variables())

    # 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(FLAGS.train_dir, graph_def=sess.graph_def)
    
    epoch = 0
    
    with sess.as_default():

      while epoch<FLAGS.max_nrof_epochs:
        batch_number = 0
        while batch_number<FLAGS.epoch_size:
          print('Loading new data')
          image_data, num_per_class, image_paths = facenet.load_data(train_set)
      
          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))
          if True:
            for i in xrange(nrof_batches_per_epoch):
              feed_dict, _ = facenet.get_batch(images_placeholder, image_data, i)
              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
            apn, nrof_random_negs, nrof_triplets = facenet.select_triplets(emb_array, num_per_class, image_data)
            duration = time.time() - start_time
            print('(nrof_random_negs, nrof_triplets) = (%d, %d): time=%.3f seconds' % (nrof_random_negs, nrof_triplets, duration))
            
            count = 0
            while count<nrof_triplets*3 and batch_number<FLAGS.epoch_size:
              start_time = time.time()
              feed_dict, batch = facenet.get_batch(images_placeholder, apn, batch_number)
              if (batch_number%20==0):
                err, summary_str, _  = sess.run([loss, summary_op, train_op], feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, FLAGS.epoch_size*epoch+batch_number)
              else:
                err, _  = sess.run([loss, train_op], feed_dict=feed_dict)
              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
              count+=FLAGS.batch_size

          else:
  
            while batch_number<FLAGS.epoch_size:
              start_time = time.time()
              feed_dict, _ = facenet.get_batch(images_placeholder, image_data, batch_number)
              
              grad_tensors, grad_vars = zip(*grads)
              eval_list = (train_op, loss) + grad_tensors
              result  = sess.run(eval_list, feed_dict=feed_dict)
              grads_eval = result[2:]
              nrof_parameters = 0
              for gt, gv in zip(grads_eval, grad_vars):
                print('%40s: %6d' % (gv.op.name, np.size(gt)))
                nrof_parameters += np.size(gt)
              print('Total number of parameters: %d' % nrof_parameters)
              err = result[1]
              batch_number+=1
        epoch+=1

      # Save the model checkpoint periodically.
      checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt')
      saver.save(sess, checkpoint_path, global_step=epoch*FLAGS.epoch_size+batch_number)
Пример #6
0
def train_classifier(args, sess, dataset, epoch, images_placeholder,
                     labels_placeholder, phase_train_placeholder,
                     learning_rate_placeholder, global_step, loss, train_op,
                     summary_op, summary_writer, regularization_losses):
    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:
        print('Loading training data')
        # Sample people and load new data
        start_time = time.time()
        image_paths, labels = facenet.sample_random_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))

        # Perform training on the selected triplets
        train_time = 0
        i = 0
        while i * args.batch_size < image_data.shape[
                0] and batch_number < args.epoch_size:
            start_time = time.time()
            batch = facenet.get_batch(image_data, args.batch_size, i)
            label_batch = facenet.get_label_batch(np.asarray(labels, np.int64),
                                                  args.batch_size, i)
            feed_dict = {
                images_placeholder: batch,
                labels_placeholder: label_batch,
                phase_train_placeholder: True,
                learning_rate_placeholder: lr
            }
            err, _, step, reg_loss = sess.run(
                [loss, train_op, global_step, regularization_losses],
                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\tLoss %2.3f\tRegLoss %2.3f' %
                  (epoch, batch_number, args.epoch_size, duration, err,
                   np.sum(reg_loss)))
            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=0.0)
        summary.value.add(tag='time/train', simple_value=train_time)
        summary.value.add(tag='time/total',
                          simple_value=load_time + 0.0 + train_time)
        summary_writer.add_summary(summary, step)
    return step
Пример #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
Пример #8
0
def train():
    dataset = facenet.get_dataset(FLAGS.data_dir)
    train_set, test_set = facenet.split_dataset(dataset, 0.9)

    fileName = "/home/david/debug4.h5"
    f = h5py.File(fileName, "r")
    for item in f.values():
        print(item)

    w1 = f['1w'][:]
    b1 = f['1b'][:]
    f.close()
    print(w1.shape)
    print(b1.shape)
    """Train CIFAR-10 for a number of steps."""
    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, 96, 96,
                                                   3),
                                            name='Input')

        # Build a Graph that computes the logits predictions from the inference model
        #embeddings = facenet.inference_nn4_max_pool_96(images_placeholder, phase_train=True)

        conv1 = _conv(images_placeholder,
                      3,
                      64,
                      7,
                      7,
                      2,
                      2,
                      'SAME',
                      'conv1_7x7',
                      phase_train=False,
                      use_batch_norm=False,
                      init_weight=w1,
                      init_bias=b1)
        resh1 = tf.reshape(conv1, [-1, 294912])
        embeddings = _affine(resh1, 294912, 128)

        # Split example embeddings into anchor, positive and negative
        a, p, n = tf.split(0, 3, embeddings)

        # Calculate triplet loss
        loss = facenet.triplet_loss(a, p, n)

        # Build a Graph that trains the model with one batch of examples and updates the model parameters
        train_op, grads = facenet.train(loss, global_step)

        # Create a saver
        saver = tf.train.Saver(tf.all_variables())

        # 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(FLAGS.train_dir,
                                                graph_def=sess.graph_def)

        epoch = 0

        with sess.as_default():

            while epoch < FLAGS.max_nrof_epochs:
                batch_number = 0
                while batch_number < FLAGS.epoch_size:
                    print('Loading new data')
                    image_data, num_per_class, image_paths = facenet.load_data(
                        train_set)

                    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))
                    if True:
                        for i in xrange(nrof_batches_per_epoch):
                            feed_dict, _ = facenet.get_batch(
                                images_placeholder, image_data, i)
                            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
                        apn, nrof_random_negs, nrof_triplets = facenet.select_triplets(
                            emb_array, num_per_class, image_data)
                        duration = time.time() - start_time
                        print(
                            '(nrof_random_negs, nrof_triplets) = (%d, %d): time=%.3f seconds'
                            % (nrof_random_negs, nrof_triplets, duration))

                        count = 0
                        while count < nrof_triplets * 3 and batch_number < FLAGS.epoch_size:
                            start_time = time.time()
                            feed_dict, batch = facenet.get_batch(
                                images_placeholder, apn, batch_number)
                            if (batch_number % 20 == 0):
                                err, summary_str, _ = sess.run(
                                    [loss, summary_op, train_op],
                                    feed_dict=feed_dict)
                                summary_writer.add_summary(
                                    summary_str,
                                    FLAGS.epoch_size * epoch + batch_number)
                            else:
                                err, _ = sess.run([loss, train_op],
                                                  feed_dict=feed_dict)
                            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
                            count += FLAGS.batch_size

                    else:

                        while batch_number < FLAGS.epoch_size:
                            start_time = time.time()
                            feed_dict, _ = facenet.get_batch(
                                images_placeholder, image_data, batch_number)

                            grad_tensors, grad_vars = zip(*grads)
                            eval_list = (train_op, loss) + grad_tensors
                            result = sess.run(eval_list, feed_dict=feed_dict)
                            grads_eval = result[2:]
                            nrof_parameters = 0
                            for gt, gv in zip(grads_eval, grad_vars):
                                print('%40s: %6d' % (gv.op.name, np.size(gt)))
                                nrof_parameters += np.size(gt)
                            print('Total number of parameters: %d' %
                                  nrof_parameters)
                            err = result[1]
                            batch_number += 1
                epoch += 1

            # Save the model checkpoint periodically.
            checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt')
            saver.save(sess,
                       checkpoint_path,
                       global_step=epoch * FLAGS.epoch_size + batch_number)
Пример #9
0
def train():
    dataset = facenet.get_dataset(FLAGS.data_dir)
    train_set, test_set = facenet.split_dataset(dataset, 0.9)
    """Train CIFAR-10 for a number of steps."""
    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, 96, 96,
                                                   3),
                                            name='Input')

        # Build a Graph that computes the logits predictions from the inference model
        embeddings = facenet.inference_no_batch_norm_deeper(
            images_placeholder, tf.constant(True))
        #embeddings = facenet.inference(images_placeholder, tf.constant(False))

        # Split example embeddings into anchor, positive and negative
        #a, p, n = tf.split(0, 3, embeddings)

        # Calculate triplet loss
        loss = facenet.triplet_loss_modified(embeddings)

        # Build a Graph that trains the model with one batch of examples and updates the model parameters
        train_op, grads = facenet.train(loss, global_step)

        # Create a saver
        saver = tf.train.Saver(tf.all_variables())

        # 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()

        check_num = tf.add_check_numerics_ops()

        # 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(FLAGS.train_dir,
                                                graph_def=sess.graph_def)

        epoch = 1

        with sess.as_default():

            while epoch < FLAGS.max_nrof_epochs:
                batch_number = 0
                while batch_number < FLAGS.epoch_size:
                    print('Loading new data')
                    image_data, num_per_class = facenet.load_data(train_set)

                    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):
                    #feed_dict = facenet.get_batch(images_placeholder, image_data, i)
                    #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
                    #apn, nrof_random_negs, nrof_triplets = facenet.select_triplets(emb_array, num_per_class, image_data)
                    #duration = time.time() - start_time
                    #print('(nrof_random_negs, nrof_triplets) = (%d, %d): time=%.3f seconds' % (nrof_random_negs, nrof_triplets, duration))

                    count = 0
                    #        while count<nrof_triplets*3 and batch_number<FLAGS.epoch_size:
                    while batch_number < FLAGS.epoch_size:
                        start_time = time.time()
                        #         feed_dict = facenet.get_batch(images_placeholder, apn, batch_number)
                        feed_dict = facenet.get_batch(images_placeholder,
                                                      image_data, batch_number)

                        grad_tensors, grad_vars = zip(*grads)
                        grads_eval = sess.run(grad_tensors,
                                              feed_dict=feed_dict)
                        for gt, gv in zip(grads_eval, grad_vars):
                            print('%40s: %6d %6f  %6f' %
                                  (gv.op.name, np.sum(
                                      np.isnan(gt)), np.max(gt), np.min(gt)))

                        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
                        count += FLAGS.batch_size
                epoch += 1

            # Save the model checkpoint periodically.
            checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt')
            saver.save(sess,
                       checkpoint_path,
                       global_step=epoch * FLAGS.epoch_size + batch_number)
Пример #10
0
def train():
  dataset = facenet.get_dataset(FLAGS.data_dir)
  train_set, test_set = facenet.split_dataset(dataset, 0.9)
  
  """Train CIFAR-10 for a number of steps."""
  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, 96, 96, 3), name='Input')
    
    # Build a Graph that computes the logits predictions from the inference model
    embeddings = facenet.inference_no_batch_norm_deeper(images_placeholder, tf.constant(True))
    #embeddings = facenet.inference(images_placeholder, tf.constant(False))
    
    # Split example embeddings into anchor, positive and negative
    #a, p, n = tf.split(0, 3, embeddings)

    # Calculate triplet loss
    loss = facenet.triplet_loss_modified(embeddings)

    # Build a Graph that trains the model with one batch of examples and updates the model parameters
    train_op, grads = facenet.train(loss, global_step)
    
    # Create a saver
    saver = tf.train.Saver(tf.all_variables())

    # 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()
    
    check_num = tf.add_check_numerics_ops()

    # 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(FLAGS.train_dir, graph_def=sess.graph_def)
    
    epoch = 1
    
    with sess.as_default():

      while epoch<FLAGS.max_nrof_epochs:
        batch_number = 0
        while batch_number<FLAGS.epoch_size:
          print('Loading new data')
          image_data, num_per_class = facenet.load_data(train_set)
      
          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):
            #feed_dict = facenet.get_batch(images_placeholder, image_data, i)
            #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
          #apn, nrof_random_negs, nrof_triplets = facenet.select_triplets(emb_array, num_per_class, image_data)
          #duration = time.time() - start_time
          #print('(nrof_random_negs, nrof_triplets) = (%d, %d): time=%.3f seconds' % (nrof_random_negs, nrof_triplets, duration))
    
          count = 0
  #        while count<nrof_triplets*3 and batch_number<FLAGS.epoch_size:
          while batch_number<FLAGS.epoch_size:
            start_time = time.time()
   #         feed_dict = facenet.get_batch(images_placeholder, apn, batch_number)
            feed_dict = facenet.get_batch(images_placeholder, image_data, batch_number)
            
            grad_tensors, grad_vars = zip(*grads)
            grads_eval  = sess.run(grad_tensors, feed_dict=feed_dict)
            for gt, gv in zip(grads_eval, grad_vars):
              print('%40s: %6d %6f  %6f' % (gv.op.name, np.sum(np.isnan(gt)), np.max(gt), np.min(gt)))
            
            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
            count+=FLAGS.batch_size
        epoch+=1

      # Save the model checkpoint periodically.
      checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt')
      saver.save(sess, checkpoint_path, global_step=epoch*FLAGS.epoch_size+batch_number)