def validate(sess, paths, actual_issame, seed, batch_size, images_placeholder, phase_train_placeholder, embeddings, nrof_folds=10): image_size = images_placeholder.get_shape()[1] # Run forward pass to calculate embeddings print('Runnning forward pass on LFW images') nrof_images = len(paths) nrof_batches = int(math.ceil(1.0*nrof_images / batch_size)) emb_list = [] for i in range(nrof_batches): start_index = i*batch_size end_index = min((i+1)*batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_list += sess.run([embeddings], feed_dict=feed_dict) emb_array = np.vstack(emb_list) # Stack the embeddings to a nrof_examples_per_epoch x 128 matrix # Calculate evaluation metrics thresholds = np.arange(0, 4, 0.01) embeddings1 = emb_array[0::2] embeddings2 = emb_array[1::2] tpr, fpr, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), seed, nrof_folds=nrof_folds) thresholds = np.arange(0, 4, 0.001) val, val_std, far = facenet.calculate_val(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), 1e-3, seed, nrof_folds=nrof_folds) return tpr, fpr, accuracy, val, val_std, far
def compute_facial_encodings(sess,images_placeholder,embeddings,phase_train_placeholder,image_size, embedding_size,nrof_images,nrof_batches,emb_array,batch_size,paths): """ Compute Facial Encodings Given a set of images, compute the facial encodings of each face detected in the images and return them. If no faces, or more than one face found, return nothing for that image. Inputs: image_paths: a list of image paths Outputs: facial_encodings: (image_path, facial_encoding) dictionary of facial encodings """ for i in range(nrof_batches): start_index = i*batch_size end_index = min((i+1)*batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, image_size) feed_dict = { images_placeholder:images, phase_train_placeholder:False } emb_array[start_index:end_index,:] = sess.run(embeddings, feed_dict=feed_dict) facial_encodings = {} for x in range(nrof_images): facial_encodings[paths[x]] = emb_array[x,:] return facial_encodings
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 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
def main(args): train_set = facenet.get_dataset(args.data_dir) image_list, label_list = facenet.get_image_paths_and_labels(train_set) label_strings = [name for name in os.listdir(os.path.expanduser(args.data_dir)) if os.path.isdir(os.path.join(os.path.expanduser(args.data_dir), name))] with tf.Graph().as_default(): with tf.Session() as sess: # Load the model facenet.load_model(args.model_dir) # 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") phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0") # Run forward pass to calculate embeddings nrof_images = len(image_list) print('Number of images: ', nrof_images) batch_size = args.image_batch if nrof_images % batch_size == 0: nrof_batches = nrof_images // batch_size else: nrof_batches = (nrof_images // batch_size) + 1 print('Number of batches: ', nrof_batches) embedding_size = embeddings.get_shape()[1] emb_array = np.zeros((nrof_images, embedding_size)) start_time = time.time() for i in range(nrof_batches): if i == nrof_batches -1: n = nrof_images else: n = i*batch_size + batch_size # Get images for the batch if args.is_aligned is True: images = facenet.load_data(image_list[i*batch_size:n], False, False, args.image_size) else: images = load_and_align_data(image_list[i*batch_size:n], args.image_size, args.margin, args.gpu_memory_fraction) feed_dict = { images_placeholder: images, phase_train_placeholder:False } # Use the facenet model to calcualte embeddings embed = sess.run(embeddings, feed_dict=feed_dict) emb_array[i*batch_size:n, :] = embed print('Completed batch', i+1, 'of', nrof_batches) run_time = time.time() - start_time print('Run time: ', run_time) # export emedings and labels label_list = np.array(label_list) np.save(args.embeddings_name, emb_array) np.save(args.labels_name, label_list) np.save(args.labels_strings_name, label_strings)
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") 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 = { images_placeholder:images, phase_train_placeholder:False } emb_array[start_index:end_index,:] = sess.run(embeddings, feed_dict=feed_dict) 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)) auc = metrics.auc(fpr, tpr) print('Area Under Curve (AUC): %1.3f' % auc) eer = brentq(lambda x: 1. - x - interpolate.interp1d(fpr, tpr)(x), 0., 1.) print('Equal Error Rate (EER): %1.3f' % eer)
def main(args): with tf.Graph().as_default(): with tf.Session() as sess: # create output directory if it doesn't exist output_dir = os.path.expanduser(args.output_dir) if not os.path.isdir(output_dir): os.makedirs(output_dir) # load the model print("Loading trained model...\n") meta_file, ckpt_file = facenet.get_model_filenames(os.path.expanduser(args.trained_model_dir)) facenet.load_model(args.trained_model_dir, meta_file, ckpt_file) # grab all image paths and labels print("Finding image paths and targets...\n") data = load_files(args.data_dir, load_content=False, shuffle=False) labels_array = data['target'] paths = data['filenames'] # 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") 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('Generating embeddings from images...\n') start_time = time.time() batch_size = args.batch_size nrof_images = len(paths) nrof_batches = int(np.ceil(1.0*nrof_images / batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in xrange(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, do_random_crop=False, do_random_flip=False, image_size=image_size, do_prewhiten=True) feed_dict = { images_placeholder:images, phase_train_placeholder:False} emb_array[start_index:end_index,:] = sess.run(embeddings, feed_dict=feed_dict) time_avg_forward_pass = (time.time() - start_time) / float(nrof_images) print("Forward pass took avg of %.3f[seconds/image] for %d images\n" % (time_avg_forward_pass, nrof_images)) print("Finally saving embeddings and gallery to: %s" % (output_dir)) # save the gallery and embeddings (signatures) as numpy arrays to disk np.save(os.path.join(output_dir, "gallery.npy"), labels_array) np.save(os.path.join(output_dir, "signatures.npy"), emb_array)
def main(): pairs = read_pairs(os.path.expanduser(FLAGS.lfw_pairs)) paths, actual_issame = get_paths(os.path.expanduser(FLAGS.lfw_dir), pairs) with tf.Graph().as_default(): with tf.Session() as sess: print('Reading model "%s"' % FLAGS.model_file) new_saver = tf.train.import_meta_graph(os.path.expanduser(FLAGS.model_file+'.meta')) all_vars_dict = {var.name: var for var in tf.all_variables()} restore_vars = {} for var_name in all_vars_dict: if '/ExponentialMovingAverage' in var_name: param_name = var_name.replace('/ExponentialMovingAverage', '') if param_name in all_vars_dict: param = all_vars_dict[param_name] print('%s\t%s' % (var_name, param)) restore_vars[var_name] = param saver = tf.train.Saver(restore_vars, saver_def=new_saver.as_saver_def()) saver.restore(sess, os.path.expanduser(FLAGS.model_file)) images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0") phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0") embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") image_size = images_placeholder.get_shape()[1] nrof_images = len(paths) nrof_batches = int(nrof_images / FLAGS.batch_size) # Run forward pass on the remainder in the last batch emb_list = [] for i in range(nrof_batches): start_time = time.time() paths_batch = paths[i*FLAGS.batch_size:(i+1)*FLAGS.batch_size] images = facenet.load_data(paths_batch, False, False, image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_list += sess.run([embeddings], feed_dict=feed_dict) duration = time.time() - start_time print('Calculated embeddings for batch %d of %d: time=%.3f seconds' % (i+1,nrof_batches, duration)) emb_array = np.vstack(emb_list) # Stack the embeddings to a nrof_examples_per_epoch x 128 matrix thresholds = np.arange(0, 4, 0.01) embeddings1 = emb_array[0::2] embeddings2 = emb_array[1::2] tpr, fpr, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), FLAGS.seed) print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy))) facenet.plot_roc(fpr, tpr, 'NN4')
def load_testset(size): # Load images paths and labels pairs = lfw.read_pairs(pairs_path) paths, labels = lfw.get_paths(testset_path, pairs, file_extension) # Random choice permutation = np.random.choice(len(labels), size, replace=False) paths_batch_1 = [] paths_batch_2 = [] for index in permutation: paths_batch_1.append(paths[index * 2]) paths_batch_2.append(paths[index * 2 + 1]) labels = np.asarray(labels)[permutation] paths_batch_1 = np.asarray(paths_batch_1) paths_batch_2 = np.asarray(paths_batch_2) # Load images faces1 = facenet.load_data(paths_batch_1, False, False, image_size) faces2 = facenet.load_data(paths_batch_2, False, False, image_size) # Change pixel values to 0 to 1 values min_pixel = min(np.min(faces1), np.min(faces2)) max_pixel = max(np.max(faces1), np.max(faces2)) faces1 = (faces1 - min_pixel) / (max_pixel - min_pixel) faces2 = (faces2 - min_pixel) / (max_pixel - min_pixel) # Convert labels to one-hot vectors onehot_labels = [] for index in range(len(labels)): if labels[index]: onehot_labels.append([1, 0]) else: onehot_labels.append([0, 1]) return faces1, faces2, np.array(onehot_labels)
def validate(sess, dataset, epoch, images_placeholder, phase_train_placeholder, global_step, embeddings, loss, train_op, summary_op, summary_writer): people_per_batch = FLAGS.people_per_batch * 4 print('Loading validation data') # Sample people and load new data image_paths, num_per_class = facenet.sample_people(dataset, people_per_batch, FLAGS.images_per_person) image_data = facenet.load_data(image_paths) print('Selecting random triplets for validation') triplets, nrof_triplets = facenet.select_validation_triplets(num_per_class, people_per_batch, image_data) start_time = time.time() anchor_list = [] positive_list = [] negative_list = [] triplet_loss_list = [] # Run a forward pass for the sampled images print('Running forward pass on validation set') nrof_batches_per_epoch = nrof_triplets * 3 // FLAGS.batch_size for i in xrange(nrof_batches_per_epoch): batch = facenet.get_triplet_batch(triplets, i) feed_dict = {images_placeholder: batch, phase_train_placeholder: False} emb, triplet_loss, step = sess.run([embeddings, loss, global_step], feed_dict=feed_dict) nrof_batch_triplets = emb.shape[0] / 3 anchor_list.append(emb[(0 * nrof_batch_triplets):(1 * nrof_batch_triplets), :]) positive_list.append(emb[(1 * nrof_batch_triplets):(2 * nrof_batch_triplets), :]) negative_list.append(emb[(2 * nrof_batch_triplets):(3 * nrof_batch_triplets), :]) triplet_loss_list.append(triplet_loss) anchor = np.vstack(anchor_list) positive = np.vstack(positive_list) negative = np.vstack(negative_list) duration = time.time() - start_time thresholds = np.arange(0, 4, 0.01) embeddings1 = np.vstack([anchor, anchor]) embeddings2 = np.vstack([positive, negative]) actual_issame = np.asarray([True] * anchor.shape[0] + [False] * anchor.shape[0]) tpr, fpr, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, actual_issame) print('Epoch: [%d]\tTime %.3f\ttripErr %2.3f\taccuracy %1.3f%c%1.3f' % ( epoch, duration, np.mean(triplet_loss_list), np.mean(accuracy), u"\u00B1", np.std(accuracy))) # Add validation loss and accuracy to summary summary = tf.Summary() summary.value.add(tag='validation_loss', simple_value=np.mean(triplet_loss_list).astype(float)) summary.value.add(tag='accuracy', simple_value=np.mean(accuracy)) summary_writer.add_summary(summary, step) if False: facenet.plot_roc(fpr, tpr, 'NN4')
def evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, embeddings, paths, actual_issame, augument_images, aug_value): nrof_images = len(paths) nrof_batches = int(nrof_images / FLAGS.batch_size) # Run forward pass on the remainder in the last batch emb_list = [] for i in range(nrof_batches): paths_batch = paths[i*FLAGS.batch_size:(i+1)*FLAGS.batch_size] images = facenet.load_data(paths_batch) images_aug = augument_images(images, aug_value) feed_dict = { images_placeholder: images_aug, phase_train_placeholder: False } emb_list += sess.run([embeddings], feed_dict=feed_dict) emb_array = np.vstack(emb_list) # Stack the embeddings to a nrof_examples_per_epoch x 128 matrix thresholds = np.arange(0, 4, 0.01) embeddings1 = emb_array[0::2] embeddings2 = emb_array[1::2] _, _, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame)) return accuracy
def 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
def main(argv=None): pairs = read_pairs(os.path.expanduser(FLAGS.lfw_pairs)) paths, actual_issame = get_paths(os.path.expanduser(FLAGS.lfw_dir), pairs) with tf.Graph().as_default(): with tf.Session() as sess: # Load the model print('Loading model "%s"' % FLAGS.model_file) facenet.load_model(FLAGS.model_file) # Get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0") phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0") embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") image_size = images_placeholder.get_shape()[1] # Run forward pass to calculate embeddings nrof_images = len(paths) nrof_batches = int(nrof_images / FLAGS.batch_size) emb_list = [] for i in range(nrof_batches): start_time = time.time() paths_batch = paths[i*FLAGS.batch_size:(i+1)*FLAGS.batch_size] images = facenet.load_data(paths_batch, False, False, image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_list += sess.run([embeddings], feed_dict=feed_dict) duration = time.time() - start_time print('Calculated embeddings for batch %d of %d: time=%.3f seconds' % (i+1,nrof_batches, duration)) emb_array = np.vstack(emb_list) # Stack the embeddings to a nrof_examples_per_epoch x 128 matrix # Calculate evaluation metrics thresholds = np.arange(0, 4, 0.01) embeddings1 = emb_array[0::2] embeddings2 = emb_array[1::2] tpr, fpr, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), FLAGS.seed) print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy))) thresholds = np.arange(0, 4, 0.001) val, val_std, far = facenet.calculate_val(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), 1e-3, FLAGS.seed) print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far)) facenet.plot_roc(fpr, tpr, 'NN4')
def evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, image_size, embeddings, paths, actual_issame, augument_images, aug_value, batch_size, orig_image_size, seed): nrof_images = len(paths) nrof_batches = int(math.ceil(1.0*nrof_images / batch_size)) emb_list = [] for i in range(nrof_batches): start_index = i*batch_size end_index = min((i+1)*batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, orig_image_size) images_aug = augument_images(images, aug_value, image_size) feed_dict = { images_placeholder: images_aug, phase_train_placeholder: False } emb_list += sess.run([embeddings], feed_dict=feed_dict) emb_array = np.vstack(emb_list) # Stack the embeddings to a nrof_examples_per_epoch x 128 matrix thresholds = np.arange(0, 4, 0.01) embeddings1 = emb_array[0::2] embeddings2 = emb_array[1::2] _, _, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), seed) return accuracy
def main(): image_size = 96 old_dataset = '/home/david/datasets/facescrub/fs_aligned_new_oean/' new_dataset = '/home/david/datasets/facescrub/facescrub_110_96/' eq = 0 num = 0 l = [] dataset = facenet.get_dataset(old_dataset) for cls in dataset: new_class_dir = os.path.join(new_dataset, cls.name) for image_path in cls.image_paths: try: filename = os.path.splitext(os.path.split(image_path)[1])[0] new_filename = os.path.join(new_class_dir, filename+'.png') #print(image_path) if os.path.exists(new_filename): a = facenet.load_data([image_path, new_filename], False, False, image_size, do_prewhiten=False) if np.array_equal(a[0], a[1]): eq+=1 num+=1 err = np.sum(np.square(np.subtract(a[0], a[1]))) #print(err) l.append(err) if err>2000: fig = plt.figure(1) p1 = fig.add_subplot(121) p1.imshow(a[0]) p2 = fig.add_subplot(122) p2.imshow(a[1]) print('%6.1f: %s\n' % (err, new_filename)) pass else: pass #print('File not found: %s' % new_filename) except: pass
def main(args): with tf.Graph().as_default(): with tf.Session() as sess: dataset = facenet.get_dataset(args.data_dir) paths, labels = facenet.get_image_paths_and_labels(dataset) print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) # Load the model print('Loading feature extraction model') facenet.load_model(args.model) # 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") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1] start_time = time.time() # Run forward pass to calculate embeddings print('Calculating features for images') nrof_images = len(paths) nrof_batches_per_epoch = int( math.ceil(1.0 * nrof_images / args.batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches_per_epoch): start_index = i * args.batch_size end_index = min((i + 1) * args.batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, args.image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) duration = time.time() - start_time print('Calculating features Time %.3f' % duration) ''' # save emb_array and labels scipy.io.savemat(args.save_path,{'emb_array':emb_array,'labels':labels}) print('Embeddings data saved') ''' if (args.mode == 'SVM'): classifier_filename_exp = os.path.expanduser( args.classifier_filename_svm) start_time = time.time() # Classify images print('Testing classifier') with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) print('Loaded classifier model from file "%s"' % classifier_filename_exp) predictions = model.predict_proba(emb_array) best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] duration = time.time() - start_time print('classifier Time %.3f' % duration) accuracy = np.mean(np.equal(best_class_indices, labels)) print('Accuracy: %.3f' % accuracy) elif (args.mode == 'FNN'): start_time = time.time() #load meta graph and restore weights saver = tf.train.import_meta_graph(classifier_filename_fnn) saver.restore( sess, tf.train.latest_checkpoint(checkpoint_filename_fnn)) # Get input and output tensors graph = tf.get_default_graph() x = graph.get_tensor_by_name("input/x-input:0") keep_prob = graph.get_tensor_by_name("dropout/keep_prob:0") y_hat = graph.get_tensor_by_name("output/activation:0") feed_dict = {x: emb_array, keep_prob: 1.0} predictions = tf.argmax(y_hat, 1) best_class_indices = sess.run(predictions, feed_dict=feed_dict) duration = time.time() - start_time print('classifier Time %.3f' % duration) accuracy = np.mean(np.equal(best_class_indices, labels)) print('Accuracy: %.3f' % accuracy)
def main(args): with tf.Graph().as_default(): with tf.Session() as sess: np.random.seed(seed=args.seed) if args.use_split_dataset: print("Printing the data_dir args!") print(args.data_dir) dataset_tmp = facenet.get_dataset(args.data_dir) train_set, test_set = split_dataset( dataset_tmp, args.min_nrof_images_per_class, args.nrof_train_images_per_class) if (args.mode == 'TRAIN'): dataset = train_set elif (args.mode == 'CLASSIFY'): dataset = test_set else: dataset = facenet.get_dataset(args.data_dir) # Check that there are at least one training image per class for cls in dataset: assert ( len(cls.image_paths) > 0, 'There must be at least one image for each class in the dataset' ) ### The paths and labels here has been flattened and they are pair and pair accordingly ### ### The labels here are just ids ### paths, labels = facenet.get_image_paths_and_labels(dataset) print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) # Load the image to vector embedding model print('Loading feature extraction model') print(args.model) facenet.load_model(args.model) # 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") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") ### The embedding size here is 512 because the author claims that increasing it to 512 ### ### From 128 seems to give a better performance ! ### embedding_size = embeddings.get_shape()[1] print(embedding_size) # Run forward pass to calculate embeddings print('Calculating features for images') nrof_images = len(paths) nrof_batches_per_epoch = int( math.ceil(1.0 * nrof_images / args.batch_size)) print(nrof_images) print(nrof_batches_per_epoch) emb_array = np.zeros((nrof_images, embedding_size)) print(emb_array.shape) ### embedding array here has a shape of (1202, 512) basically has 1202 rows and 512 columns for embeddings ### for i in range(nrof_batches_per_epoch): start_index = i * args.batch_size end_index = min((i + 1) * args.batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, args.image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) print(emb_array) classifier_filename_exp = os.path.expanduser( args.classifier_filename) print(classifier_filename_exp) if (args.mode == 'TRAIN'): # Train classifier print('Training classifier') model = SVC(kernel='linear', probability=True) model.fit(emb_array, labels) # Create a list of class names class_names = [cls.name.replace('_', ' ') for cls in dataset] # Saving classifier model with open(classifier_filename_exp, 'wb') as outfile: pickle.dump((model, class_names), outfile) print('Saved classifier model to file "%s"' % classifier_filename_exp) elif (args.mode == 'CLASSIFY'): # Classify images print('Testing classifier') with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) print('Loaded classifier model from file "%s"' % classifier_filename_exp) ### emb_array is already in the format that the prediction will take ### predictions = model.predict_proba(emb_array) print("Output shape of prediction here is: ", predictions.shape) best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] for i in range(len(best_class_indices)): print('%4d %s: %.3f' % (i, class_names[best_class_indices[i]], best_class_probabilities[i])) # Simply find the average accuracy by finding the mean of the entire accuracy whenever # There is a class that matches print(labels) print(best_class_indices) accuracy = np.mean(np.equal(best_class_indices, labels)) print('Accuracy: %.3f' % accuracy)
def main(args): train_set = facenet.get_dataset(args.data_dir) image_list, label_list = facenet.get_image_paths_and_labels(train_set) label_strings = [ name for name in os.listdir(os.path.expanduser(args.data_dir)) if os.path.isdir(os.path.join(os.path.expanduser(args.data_dir), name)) ] with tf.Graph().as_default(): with tf.Session() as sess: # Load the model facenet.load_model(args.model_dir) # 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") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") # Run forward pass to calculate embeddings nrof_images = len(image_list) print('Number of images: ', nrof_images) batch_size = args.image_batch if nrof_images % batch_size == 0: nrof_batches = nrof_images // batch_size else: nrof_batches = (nrof_images // batch_size) + 1 print('Number of batches: ', nrof_batches) embedding_size = embeddings.get_shape()[1] emb_array = np.zeros((nrof_images, embedding_size)) start_time = time.time() for i in range(nrof_batches): if i == nrof_batches - 1: n = nrof_images else: n = i * batch_size + batch_size # Get images for the batch if args.is_aligned is True: images = facenet.load_data(image_list[i * batch_size:n], False, False, args.image_size) else: images = load_and_align_data(image_list[i * batch_size:n], args.image_size, args.margin, args.gpu_memory_fraction) feed_dict = { images_placeholder: images, phase_train_placeholder: False } # Use the facenet model to calcualte embeddings embed = sess.run(embeddings, feed_dict=feed_dict) emb_array[i * batch_size:n, :] = embed print('Completed batch', i + 1, 'of', nrof_batches) run_time = time.time() - start_time print('Run time: ', run_time) # export emedings and labels label_list = np.array(label_list) np.save(args.embeddings_name, emb_array) np.save(args.labels_name, label_list) np.save(args.labels_strings_name, label_strings)
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] image_size = image_size.value # pdb.set_trace() # 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)) print('nrof_batches :{}'.format(nrof_batches)) all_time = 0 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] # pdb.set_trace() images = facenet.load_data(paths_batch, False, False, image_size) feed_dict = {images_placeholder:images} start = time() emb_array[start_index:end_index,:] = sess.run(embeddings, feed_dict=feed_dict) end = time() all_time += (end - start) print('index: {} time: {}'.format(i, (end-start))) # pdb.set_trace() print('all_time :', all_time) msgpack_numpy.dump((paths, emb_array, actual_issame), open('lfw_feature.p', 'wb')) 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)))
def main(args): with tf.Graph().as_default(): with tf.Session() as sess: np.random.seed(seed=args.seed) if args.use_split_dataset: dataset_tmp = facenet.get_dataset(args.data_dir) train_set, test_set = split_dataset(dataset_tmp, args.min_nrof_images_per_class, args.nrof_train_images_per_class) if (args.mode == 'TRAIN'): dataset = train_set elif (args.mode == 'CLASSIFY'): dataset = test_set else: dataset = facenet.get_dataset(args.data_dir) # Check that there are at least one training image per class for cls in dataset: assert(len(cls.image_paths) > 0, 'There must be at least one image for each class in the dataset') paths, labels = facenet.get_image_paths_and_labels(dataset) print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) # Load the model print('Loading feature extraction model') facenet.load_model(args.model) # 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") phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings print('Calculating features for images') nrof_images = len(paths) nrof_batches_per_epoch = int(math.ceil(1.0 * nrof_images / args.batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches_per_epoch): start_index = i * args.batch_size end_index = min((i + 1) * args.batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, args.image_size) feed_dict = {images_placeholder: images, phase_train_placeholder: False} emb_array[start_index:end_index, :] = sess.run(embeddings, feed_dict=feed_dict) classifier_filename_exp = os.path.expanduser(args.classifier_filename) if (args.mode == 'TRAIN'): # Train classifier print('Training classifier') model = SVC(kernel='linear', probability=True) model.fit(emb_array, labels) # Create a list of class names class_names = [cls.name.replace('_', ' ') for cls in dataset] # Saving classifier model with open(classifier_filename_exp, 'wb') as outfile: pickle.dump((model, class_names), outfile) print('Saved classifier model to file "%s"' % classifier_filename_exp) elif (args.mode == 'CLASSIFY'): # Classify images print('Testing classifier') with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) print('Loaded classifier model from file "%s"' % classifier_filename_exp) predictions = model.predict_proba(emb_array) best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[np.arange(len(best_class_indices)), best_class_indices] for i in range(len(best_class_indices)): print('%4d %s: %.3f' % (i, class_names[best_class_indices[i]], best_class_probabilities[i])) accuracy = np.mean(np.equal(best_class_indices, labels)) print('Accuracy: %.3f' % accuracy)
def classify(src, dir): with tf.Graph().as_default(): with tf.Session() as sess: datadir = src dataset = facenet.get_dataset(datadir) paths, labels = facenet.get_image_paths_and_labels(dataset) print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) print('Loading feature extraction model') modeldir = '20180408-102900/20180408-102900.pb' facenet.load_model(modeldir) images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings print('Calculating features for images') batch_size = 1000 image_size = 160 nrof_images = len(paths) nrof_batches_per_epoch = int( math.ceil(1.0 * nrof_images / batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches_per_epoch): start_index = i * batch_size end_index = min((i + 1) * batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) classifier_filename = dir classifier_filename_exp = os.path.expanduser(classifier_filename) # Train classifier print('Training classifier') model = SVC(kernel='linear', probability=True) model.fit(emb_array, labels) # Create a list of class names class_names = [cls.name.replace('_', ' ') for cls in dataset] print(class_names) # Saving classifier model with open(classifier_filename_exp, 'wb') as outfile: pickle.dump((model, class_names), outfile) print('Saved classifier model to file "%s"' % classifier_filename_exp) print('Goodluck')
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") 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 = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) 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')
def main(args): # tf_config = tf.ConfigProto() # tf_config.gpu_options.per_process_gpu_memory_fraction = 1 with tf.Graph().as_default(): with tf.Session() as sess: np.random.seed(seed=args.seed) if args.use_split_dataset: dataset_tmp = facenet.get_dataset(args.data_dir) train_set, test_set = split_dataset( dataset_tmp, args.min_nrof_images_per_class, args.nrof_train_images_per_class) if (args.mode == 'TRAIN'): dataset = train_set elif (args.mode == 'CLASSIFY'): dataset = test_set else: dataset = facenet.get_dataset(args.data_dir) # Check that there are at least one training image per class for cls in dataset: assert ( len(cls.image_paths) > 0, 'There must be at least one image for each class in the dataset' ) raw_paths, raw_labels = facenet.get_image_paths_and_labels(dataset) random_list = range(0, len(raw_paths)) random.shuffle(random_list) paths = [] labels = [] for index in random_list: paths.append(raw_paths[index]) labels.append(raw_labels[index]) print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) # Load the model print('Loading feature extraction model') facenet.load_model(args.model) # 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") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings print('Calculating features for images') nrof_images = len(paths) nrof_batches_per_epoch = int( math.ceil(1.0 * nrof_images / args.batch_size)) #emb_array = np.zeros((args.batch_size, embedding_size)) mlp_images_placeholder = tf.placeholder( tf.float32, shape=(None, int(embedding_size))) mlp_labels_placeholder = tf.placeholder(tf.int32, shape=(None)) global_step = tf.Variable(0, name='global_step', trainable=False) regularizer = tf.contrib.layers.l2_regularizer( args.regularization_rate) #learning_rate = tf.train.exponential_decay(args.learning_rate_base, global_step, args.round_numbers, args.learning_rate_decay, staircase=True) learning_rate = 0.01 tf.summary.scalar('learning_rate', learning_rate) logits = mlp.inference(mlp_images_placeholder, int(embedding_size), args.hidden1, args.hidden2, args.number_class, regularizer) entropy_loss = mlp.loss(logits, mlp_labels_placeholder) tf.summary.scalar('entropy_loss', entropy_loss) regularizer_loss = tf.add_n(tf.get_collection('regularizer_item')) tf.summary.scalar('regularization_loss', regularizer_loss) total_loss = entropy_loss + regularizer_loss tf.summary.scalar('total_loss', total_loss) train_op = mlp.training(total_loss, learning_rate, global_step) summary = tf.summary.merge_all() init = tf.global_variables_initializer() saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=100000) summary_writer = tf.summary.FileWriter(args.log_dir, sess.graph) sess.run(init) i = -1 for step in xrange(1000000): i = i + 1 if i >= nrof_batches_per_epoch: i = 0 start_index = i * args.batch_size end_index = min((i + 1) * args.batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, args.image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array = np.zeros((end_index - start_index, embedding_size)) emb_array[0:end_index - start_index, :] = sess.run( embeddings, feed_dict=feed_dict) label_array = labels[start_index:end_index] start_time = time.time() _, loss_value = sess.run( [train_op, total_loss], { mlp_images_placeholder: emb_array, mlp_labels_placeholder: label_array }) duration = time.time() - start_time # write the summaries and print an overview fairly often. print('step {}: loss = {} ({} sec)'.format( step, loss_value, duration)) if step % 100 == 0: # update the events file. summary_str = sess.run( summary, { mlp_images_placeholder: emb_array, mlp_labels_placeholder: label_array }) summary_writer.add_summary(summary_str, step) summary_writer.flush() # save a checkpoint and evaluate the model periodically. #if (step + 1) % 1000 == 0 or (step + 1) == args.max_steps: if step >= 10000 and step % 1000 == 0: checkpoint_path = os.path.join(args.log_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step)
def train_classify(data_dir, nrof_train_images_per_class, classifier_filename, mode='TRAIN', use_split_dataset=False, model="./model/20170511-185253.pb", image_size=160, batch_size=64, min_nrof_images_per_class=1): with tf.Graph().as_default(): with tf.Session() as sess: if use_split_dataset == True: dataset_tmp = facenet.get_dataset(data_dir) train_set, test_set = split_dataset( dataset_tmp, min_nrof_images_per_class, nrof_train_images_per_class) if (mode == 'TRAIN'): dataset = train_set elif (mode == 'CLASSIFY'): dataset = test_set else: dataset = facenet.get_dataset(data_dir) # Check that there are at least one training image per class for cls in dataset: assert ( len(cls.image_paths) > 0, 'There must be at least one image for each class in the dataset' ) paths, labels = facenet.get_image_paths_and_labels(dataset) print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) # Load the model print('Loading feature extraction model') t0 = time() facenet.load_model(model) # 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") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1] t1 = time() print('Model Loading : ', t1 - t0) # Run forward pass to calculate embeddings print('Calculating features for images') t0 = time() nrof_images = len(paths) nrof_batches_per_epoch = int( math.ceil(1.0 * nrof_images / batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches_per_epoch): start_index = i * batch_size end_index = min((i + 1) * batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) classifier_filename_exp = os.path.expanduser(classifier_filename) t1 = time() print('Feature Extraction : ', t1 - t0) if (mode == 'TRAIN'): # Train classifier print('Training classifier') t0 = time() model = SVC(kernel='linear', probability=True) model.fit(emb_array, labels) '''model = RandomForestClassifier() # Choose some parameter combinations to try parameters = {'n_estimators': [4, 6, 9], 'max_features': ['log2', 'sqrt','auto'], 'criterion': ['entropy', 'gini'], 'max_depth': [2, 3, 5, 10], 'min_samples_split': [2, 3, 5], 'min_samples_leaf': [1,5,8] } # Type of scoring used to compare parameter combinations acc_scorer = make_scorer(accuracy_score) # Run the grid search grid_obj = GridSearchCV(model, parameters, scoring=acc_scorer) grid_obj = grid_obj.fit(emb_array, labels) # Set the clf to the best combination of parameters model = grid_obj.best_estimator_ # Fit the best algorithm to the data. model.fit(emb_array, labels)''' t1 = time() print('Training Time : ', t1 - t0) # Create a list of class names class_names = [cls.name.replace('_', ' ') for cls in dataset] # Saving classifier model with open(classifier_filename_exp, 'wb') as outfile: pickle.dump((model, class_names), outfile) print('Saved classifier model to file "%s"' % classifier_filename_exp) elif (mode == 'CLASSIFY'): # Classify images print('Testing classifier') with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) class_names.append('Unknown') print('Loaded classifier model from file "%s"' % classifier_filename_exp) predictions = model.predict_proba(emb_array) best_class_indices = np.argmax(predictions, axis=1) #print(best_class_indices) best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] #print(best_class_probabilities) for i in range(len(best_class_indices)): if best_class_probabilities[i] > 0.300: print('%4d %s: %.3f' % (i, class_names[best_class_indices[i]], best_class_probabilities[i])) else: print( '%4d %s: %.3f' % (i, class_names[-1], best_class_probabilities[i])) accuracy = np.mean(np.equal(best_class_indices, labels)) print('Accuracy: %.3f' % accuracy)
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 facenet.load_model(args.model) # TODO: replace near 0 parameters to 0 # trainable_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES) # print("trainable_vars", len(trainable_vars)) # zero_threshold = 1e-2 # n_var_elements = 0 # n_non_zero = 0 # n_non_zero_after = 0 # assign_ops = [] # for var in trainable_vars: # matrix = var.eval(sess) # # if var.name.endswith("weights:0"): # # print(matrix) # n_var_elements += np.size(matrix) # n_non_zero += np.count_nonzero(matrix) # # matrix[np.abs(matrix)<=zero_threshold] = 0 # # n_non_zero_after += np.count_nonzero(matrix) # # assign_ops.append(var.assign(matrix)) # # print("non_zero: ",n_non_zero,n_var_elements,n_non_zero/n_var_elements) # print("non_zero after: ",n_non_zero_after,n_var_components,n_non_zero_after/n_var_components) # sess.run(assign_ops) # Get input and output tensors graph = tf.get_default_graph() images_placeholder = graph.get_tensor_by_name("input:0") embeddings = graph.get_tensor_by_name("embeddings:0") phase_train_placeholder = graph.get_tensor_by_name("phase_train:0") #image_size = images_placeholder.get_shape()[1] # For some reason this doesn't work for frozen graphs image_size = args.image_size embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings print('Runnning forward pass on LFW images') start_time = time.time() 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, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) duration = time.time() - start_time print('Forward pass duration in %.3f seconds' % duration) tpr, fpr, recall, precision, 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('Precision: %1.3f+-%1.3f' % (np.mean(precision), np.std(precision))) print('Recall: %1.3f+-%1.3f' % (np.mean(recall), np.std(recall))) print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far)) auc = metrics.auc(fpr, tpr) print('Area Under Curve (AUC): %1.3f' % auc) eer = brentq(lambda x: 1. - x - interpolate.interp1d(fpr, tpr)(x), 0., 1.) print('Equal Error Rate (EER): %1.3f' % eer)
def main(args): with tf.Graph().as_default(): with tf.Session() as sess: # create output directory if it doesn't exist output_dir = os.path.expanduser(args.output_dir) if not os.path.isdir(output_dir): os.makedirs(output_dir) # load the model print("Loading trained model...\n") meta_file, ckpt_file = facenet.get_model_filenames(os.path.expanduser(args.trained_model_dir)) facenet.load_model(args.trained_model_dir) # grab all image paths and labels print("Finding image paths and targets...\n") data = load_files(args.data_dir, load_content=False, shuffle=False) print(data.keys()) labels_array = data['target'] paths = data['filenames'] # 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") 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('Generating embeddings from images...\n') start_time = time.time() batch_size = args.batch_size nrof_images = len(paths) nrof_batches = int(np.ceil(1.0 * nrof_images / batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in xrange(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, do_random_crop=False, do_random_flip=False, image_size=image_size, do_prewhiten=True) feed_dict = {images_placeholder: images, phase_train_placeholder: False} emb_array[start_index:end_index, :] = sess.run(embeddings, feed_dict=feed_dict) time_avg_forward_pass = (time.time() - start_time) / float(nrof_images) print("Forward pass took avg of %.3f[seconds/image] for %d images\n" % (time_avg_forward_pass, nrof_images)) print("Finally saving embeddings and gallery to: %s" % (output_dir)) # save the gallery and embeddings (signatures) as numpy arrays to disk np.save(os.path.join(output_dir, "gallery.npy"), labels_array) np.save(os.path.join(output_dir, "signatures.npy"), emb_array) labels_name_array = [] for i in range(len(labels_array)): labels_name_array += [data['target_names'][labels_array[i]]] np.save(os.path.join(output_dir, "labels_name.npy"), labels_name_array) #data_frame = pd.DataFrame( data={'name': labels_name_array, 'feature': emb_array}) #data_frame.to_csv(os.path.join(output_dir, 'feature.csv')) labels = np.load(os.path.join(output_dir, "gallery.npy")) labels_name = np.load(os.path.join(output_dir, "labels_name.npy")) emb = np.load(os.path.join(output_dir, "signatures.npy")) print(labels_array.shape,emb_array.shape,labels.shape,emb.shape) print(labels_name)
def main(args): with tf.Graph().as_default(): with tf.Session() as sess: # create output directory if it doesn't exist output_dir = os.path.expanduser(args.output_dir) if not os.path.isdir(output_dir): os.makedirs(output_dir) # load the model print("Loading trained model...\n") meta_file, ckpt_file = facenet.get_model_filenames( os.path.expanduser(args.trained_model_dir)) facenet.load_model(args.trained_model_dir) # grab all image paths and labels print("Finding image paths and targets...\n") data = load_files(args.data_dir, load_content=False, shuffle=False) labels_array = data['target'] paths = data['filenames'] # 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") 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('Generating embeddings from images...\n') start_time = time.time() batch_size = args.batch_size nrof_images = len(paths) nrof_batches = int(np.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, do_random_crop=False, do_random_flip=False, image_size=image_size, do_prewhiten=True) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) time_avg_forward_pass = (time.time() - start_time) / float(nrof_images) print( "Forward pass took avg of %.3f[seconds/image] for %d images\n" % (time_avg_forward_pass, nrof_images)) for emb_idx, emb in enumerate(emb_array): norm_emb = emb / np.linalg.norm(emb) print(norm_emb) encoded_word = "" q = 30 for i, wi in enumerate(norm_emb): val = wi * q val_abs = math.fabs(val) prefix = ("F" if val > 0 else "f") if (val_abs < 0.5): val = 0 else: val = math.floor(val_abs) for j in range(val): encoded_word += prefix + str(i) print("----> " + str(emb_idx)) print(encoded_word)
def main(args): with tf.Graph().as_default(): with tf.compat.v1.Session() as sess: np.random.seed(seed=args.seed) if args.use_split_dataset: dataset_tmp = facenet.get_dataset(args.data_dir) train_set, test_set = split_dataset( dataset_tmp, args.min_nrof_images_per_class, args.nrof_train_images_per_class) if (args.mode == 'TRAIN'): dataset = train_set elif (args.mode == 'CLASSIFY'): dataset = test_set else: dataset = facenet.get_dataset(args.data_dir) # Check that there are at least one training image per class for cls in dataset: assert ( len(cls.image_paths) > 0, 'There must be at least one image for each class in the dataset' ) paths, labels = facenet.get_image_paths_and_labels(dataset) print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) # Load the model print('Loading feature extraction model') facenet.load_model(args.model) # Get input and output tensors images_placeholder = tf.compat.v1.get_default_graph( ).get_tensor_by_name("input:0") embeddings = tf.compat.v1.get_default_graph().get_tensor_by_name( "embeddings:0") phase_train_placeholder = tf.compat.v1.get_default_graph( ).get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings print('Calculating features for images') nrof_images = len(paths) nrof_batches_per_epoch = int( math.ceil(1.0 * nrof_images / args.batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) pathName = [] for i in range(nrof_batches_per_epoch): start_index = i * args.batch_size end_index = min((i + 1) * args.batch_size, nrof_images) paths_batch = paths[start_index:end_index] for j in range(len(paths_batch)): pathName.append(paths_batch[j]) images = facenet.load_data(paths_batch, False, False, args.image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) classifier_filename_exp = os.path.expanduser( args.classifier_filename) if (args.mode == 'TRAIN'): # Train classifier print('Training classifier') model = SVC(kernel='linear', probability=True) model.fit(emb_array, labels) # Create a list of class names class_names = [cls.name.replace('_', ' ') for cls in dataset] # Saving classifier model with open(classifier_filename_exp, 'wb') as outfile: pickle.dump((model, class_names), outfile) print('Saved classifier model to file "%s"' % classifier_filename_exp) elif (args.mode == 'CLASSIFY'): # Classify images print('Testing classifier') with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile, encoding="latin1") print('Loaded classifier model from file "%s"' % classifier_filename_exp) predictions = model.predict_proba(emb_array) #print (predictions) best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] scoreDic = {} for i in range(len(predictions[1])): #print(class_names[i],predictions[1][i]) scoreDic[class_names[i]] = predictions[1][i] sorted_d = sorted(scoreDic.items(), key=lambda x: x[1]) #print(sorted_d) # equivalent version # sorted_d = sorted(d.items(), key=lambda (k,v): v) print(emb_array) #file1 = open('classify_result2.txt','w') resultDic = {} print(predictions.shape) print( sorted(range(len(predictions[1])), key=lambda i: predictions[1][i], reverse=True)[:5]) top5 = sorted(range(len(predictions[1])), key=lambda i: predictions[1][i], reverse=True)[:5] for i in top5: print(class_names[i]) print(predictions[1][i]) for i in range(len(best_class_indices)): print('%s\t%s\t%.3f' % (pathName[i], class_names[best_class_indices[i]], best_class_probabilities[i])) #print class_names #print #file1.write('%s\t%s\t%.3f' % (pathName[i], class_names[best_class_indices[i]], best_class_probabilities[i])+'\n') resultDic[pathName[i]] = ( class_names[best_class_indices[i]], best_class_probabilities[i], emb_array[i]) #file1.write(pathName[i]+'\t'+class_names[best_class_indices[i]]+'\t'+best_class_probabilities[i]+'\n') accuracy = np.mean(np.equal(best_class_indices, labels)) print('Accuracy: %.3f' % accuracy) pickle.dump(resultDic, open("results/" + args.output + ".p", "wb"))
embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings print('Calculating features for images') batch_size = 1000 image_size = 160 nrof_images = len(paths) nrof_batches_per_epoch = int(math.ceil(1.0 * nrof_images / batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches_per_epoch): start_index = i * batch_size end_index = min((i + 1) * batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, image_size) feed_dict = {images_placeholder: images, phase_train_placeholder: False} emb_array[start_index:end_index, :] = sess.run(embeddings, feed_dict=feed_dict) classifier_filename = './myclassifier/my_classifier.pkl' classifier_filename_exp = os.path.expanduser(classifier_filename) # Train classifier print('Training classifier') model = SVC(kernel='linear', probability=True) model.fit(emb_array, labels) # Create a list of class names class_names = [cls.name.replace('_', ' ') for cls in dataset] # Saving classifier model
def main(args): with tf.Graph().as_default(): with tf.Session() as sess: np.random.seed(seed=args.seed) if args.use_split_dataset: dataset_tmp = facenet.get_dataset(args.data_dir) train_set, test_set = split_dataset( dataset_tmp, args.min_nrof_images_per_class, args.nrof_train_images_per_class) if (args.mode == 'TRAIN'): dataset = train_set elif (args.mode == 'CLASSIFY'): dataset = test_set else: dataset = facenet.get_dataset(args.data_dir) # Check that there are at least one training image per class for cls in dataset: assert ( len(cls.image_paths) > 0, 'There must be at least one image for each class in the dataset' ) paths, labels = facenet.get_image_paths_and_labels(dataset) print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) # Load the model print('Loading feature extraction model') facenet.load_model(args.model) # 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") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings print('Calculating features for images') nrof_images = len(paths) nrof_batches_per_epoch = int( math.ceil(1.0 * nrof_images / args.batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches_per_epoch): start_index = i * args.batch_size end_index = min((i + 1) * args.batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, args.image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) classifier_filename_exp = os.path.expanduser( args.classifier_filename) #embfilename='20180402-114759' #if not os.path.exists('D:\\facenet\\descriptors\\'+embfilename): # os.mkdir('D:\\facenet\\descriptors\\'+embfilename) #np.savetxt('D:\\facenet\\descriptors\\'+embfilename+'\\log1.gz', emb_array, fmt='%.32f', delimiter=',', newline='\n') #print('Saved feature embeddings to file "%s"' % embfilename) if (args.mode == 'TRAIN'): # Train classifier print('Training classifier') model = SVC(kernel='linear', probability=True) model.fit(emb_array, labels) # Create a list of class names class_names = [cls.name.replace('_', ' ') for cls in dataset] # Saving classifier model with open(classifier_filename_exp, 'wb') as outfile: pickle.dump((model, class_names), outfile) print('Saved classifier model to file "%s"' % classifier_filename_exp) elif (args.mode == 'CLASSIFY'): # Classify images print('Testing classifier') with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) print('Loaded classifier model from file "%s"' % classifier_filename_exp) predictions = model.predict_proba(emb_array) best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] for i in range(len(best_class_indices)): print('%4d %s: %.3f' % (i, class_names[best_class_indices[i]], best_class_probabilities[i])) accuracy = np.mean(np.equal(best_class_indices, labels)) print('Accuracy: %.3f' % accuracy) labels = label_binarize(np.array(labels), classes=range(1, 21)) best_class_indices = label_binarize( np.array(best_class_indices), classes=range(1, 21)) precision, recall, _ = precision_recall_curve( labels.ravel(), best_class_indices.ravel()) average_precision = average_precision_score(labels, best_class_indices, average="micro") print( 'Average precision score, micro-averaged over all classes: {0:0.2f}' .format(average_precision)) plt.figure() plt.step(recall, precision, color='b', alpha=0.2, where='post') plt.fill_between(recall, precision, step='post', alpha=0.2, color='b') plt.xlabel('Recall') plt.ylabel('Precision') plt.ylim([0.0, 1.05]) plt.xlim([0.0, 1.0]) plt.title( 'Average precision score, micro-averaged over all classes: AP={0:0.2f}' .format(average_precision))
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)
def main(args): print(args.image_files) print("args.request=", args.request) #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction) gpu_options = tf.GPUOptions(allow_growth=True) with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) as sess: print('Preparing mtcnn') pnet, rnet, onet = align.detect_face.create_mtcnn(sess, None) print('done') print('Preparing ResNet') # Load the model facenet.load_model(args.model) # 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") phase_train_placeholder = tf.get_default_graph().get_tensor_by_name( "phase_train:0") embedding_size = embeddings.get_shape()[1] print('done') print('load svm classifier') classifier_filename_exp = os.path.expanduser(args.classifier_filename) with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) #读入model 和类名 print('done') #while True: if args.request == 'COMPARE': stamp = time.time() # Run forward pass to calculate embeddings images = align_image(args.image_files, args.image_size, args.margin, pnet, rnet, onet, args.gpu_memory_fraction) elapsed = time.time() - stamp print('align spent: %1.4f s' % elapsed) stamp = time.time() feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb = sess.run(embeddings, feed_dict=feed_dict) elapsed = time.time() - stamp print('embedding spent: %1.4f s' % elapsed) dist = np.sqrt(np.sum(np.square(np.subtract(emb[0, :], emb[1, :])))) print(' %1.4f ' % dist) elif args.request == 'TRAIN': dataset = facenet.get_dataset( args.data_dir) #返回ImageClass [(name,path)] # Check that there are at least one training image per class for cls in dataset: assert len( cls.image_paths ) > 0 # 'There must be at least one image for each class in the dataset' paths, labels = facenet.get_image_paths_and_labels( dataset) #路径列表,类别列表 print(labels) print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) # Run forward pass to calculate embeddings print('Calculating features for images') nrof_images = len(paths) nrof_batches_per_epoch = int( math.ceil(1.0 * nrof_images / args.batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches_per_epoch): start_index = i * args.batch_size end_index = min((i + 1) * args.batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, args.image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) print('Training classifier') model = SVC(kernel='linear', probability=True) model.fit(emb_array, labels) # Create a list of class names class_names = [cls.name.replace('_', ' ') for cls in dataset] # Saving classifier model with open(classifier_filename_exp, 'wb') as outfile: pickle.dump((model, class_names), outfile) print('Saved classifier model to file "%s"' % classifier_filename_exp) elif args.request == 'CLASSIFY': dataset = facenet.get_dataset(args.data_dir) for cls in dataset: assert len( cls.image_paths ) > 0 # 'There must be at least one image for each class in the dataset' paths, labels = facenet.get_image_paths_and_labels( dataset) #路径列表,类别列表 print(labels) print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) # Run forward pass to calculate embeddings print('Calculating features for images') nrof_images = len(paths) nrof_batches_per_epoch = int( math.ceil(1.0 * nrof_images / args.batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches_per_epoch): start_index = i * args.batch_size end_index = min((i + 1) * args.batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, args.image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) predictions = model.predict_proba( emb_array) #predictions.shape=(10, 5749) best_class_indices = np.argmax(predictions, axis=1) #概率最大的类下标 best_class_probabilities = predictions[np.arange( len(best_class_indices)), best_class_indices] #概率 for i in range(len(best_class_indices)): print(labels[i]) print('%4d %s: %.3f' % (i, class_names[best_class_indices[i]], best_class_probabilities[i])) print(best_class_indices) print(labels) accuracy = np.mean(np.equal(best_class_indices, labels)) print('Accuracy: %.3f' % accuracy) else: print('wrong input')
def main(args): with tf.Graph().as_default(): with tf.Session() as sess: np.random.seed(seed=args.seed) if args.use_split_dataset: dataset_tmp = facenet.get_dataset(args.data_dir) train_set, test_set = split_dataset(dataset_tmp, args.min_nrof_images_per_class, args.nrof_train_images_per_class) if (args.mode=='TRAIN'): dataset = train_set elif (args.mode=='CLASSIFY'): dataset = test_set else: dataset = facenet.get_dataset(args.data_dir) # Check that there are at least one training image per class for cls in dataset: assert(len(cls.image_paths)>0, 'There must be at least one image for each class in the dataset') paths, labels = facenet.get_image_paths_and_labels(dataset) print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) # Load the model print('Loading feature extraction model') facenet.load_model(args.model) # 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") phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings print('Calculating features for images') nrof_images = len(paths) nrof_batches_per_epoch = int(math.ceil(1.0*nrof_images / args.batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches_per_epoch): start_index = i*args.batch_size end_index = min((i+1)*args.batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, args.image_size) feed_dict = { images_placeholder:images, phase_train_placeholder:False } emb_array[start_index:end_index,:] = sess.run(embeddings, feed_dict=feed_dict) classifier_filename_exp = os.path.expanduser(args.classifier_filename) if (args.mode=='TRAIN'): # Train classifier print('Training classifier') model = SVC(kernel='linear', probability=True) model.fit(emb_array, labels) # Create a list of class names class_names = [ cls.name.replace('_', ' ') for cls in dataset] # Saving classifier model with open(classifier_filename_exp, 'wb') as outfile: pickle.dump((model, class_names), outfile) print('Saved classifier model to file "%s"' % classifier_filename_exp) elif (args.mode=='CLASSIFY'): # Classify images print('Testing classifier') with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) print('Loaded classifier model from file "%s"' % classifier_filename_exp) predictions = model.predict_proba(emb_array) best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[np.arange(len(best_class_indices)), best_class_indices] for i in range(len(best_class_indices)): print('%4d %s: %.3f' % (i, class_names[best_class_indices[i]], best_class_probabilities[i])) accuracy = np.mean(np.equal(best_class_indices, labels)) print('Accuracy: %.3f' % accuracy)
def main(args): use_mlboard = False mlboard = None if client: mlboard = client.Client() try: mlboard.apps.get() except Exception: mlboard = None print('Do not use mlboard.') else: print('Use mlboard parameters logging.') use_mlboard = True if args.use_split_dataset: dataset_tmp = facenet.get_dataset(args.data_dir) train_set, test_set = split_dataset(dataset_tmp, args.min_nrof_images_per_class, args.nrof_train_images_per_class) if args.mode == 'TRAIN': dataset = train_set elif args.mode == 'CLASSIFY': dataset = test_set else: dataset = facenet.get_dataset(args.data_dir) update_data({'mode': args.mode}, use_mlboard, mlboard) # Check that there are at least one training image per class for cls in dataset: assert len( cls.image_paths ) > 0, 'There must be at least one image for each class in the dataset' paths, labels = facenet.get_image_paths_and_labels(dataset) print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) data = { 'num_classes': len(dataset), 'num_images': len(paths), 'model_path': args.model, 'image_size': args.image_size, 'data_dir': args.data_dir, 'batch_size': args.batch_size, } update_data(data, use_mlboard, mlboard) # Load the model print('Loading feature extraction model') # Load driver drv = driver.load_driver(args.driver) # Instantinate driver serving = drv() serving.load_model( args.model, inputs='input:0,phase_train:0', outputs='embeddings:0', device=args.device, ) # Run forward pass to calculate embeddings print('Calculating features for images') nrof_images = len(paths) nrof_batches_per_epoch = int(math.ceil(1.0 * nrof_images / args.batch_size)) emb_array = np.zeros((nrof_images, 512)) for i in range(nrof_batches_per_epoch): start_index = i * args.batch_size end_index = min((i + 1) * args.batch_size, nrof_images) paths_batch = paths[start_index:end_index] for j in range(end_index - start_index): print('Batch {} <-> {}'.format(paths_batch[j], labels[start_index + j])) images = facenet.load_data(paths_batch, False, False, args.image_size) if serving.driver_name == 'tensorflow': feed_dict = {'input:0': images, 'phase_train:0': False} elif serving.driver_name == 'openvino': input_name = list(serving.inputs.keys())[0] # Transpose image for channel first format images = images.transpose([0, 3, 1, 2]) feed_dict = {input_name: images} else: raise RuntimeError('Driver %s currently not supported' % serving.driver_name) outputs = serving.predict(feed_dict) emb_array[start_index:end_index, :] = list(outputs.values())[0] classifier_filename_exp = os.path.expanduser(args.classifier_filename) if args.mode == 'TRAIN': # Train classifier print('Training classifier') model = svm.SVC(kernel='linear', probability=True) model.fit(emb_array, labels) # Create a list of class names class_names = [cls.name.replace('_', ' ') for cls in dataset] print('Classes:') print(class_names) # Saving classifier model with open(classifier_filename_exp, 'wb') as outfile: pickle.dump((model, class_names), outfile, protocol=2) print('Saved classifier model to file "%s"' % classifier_filename_exp) elif args.mode == 'CLASSIFY': # Classify images print('Testing classifier') with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) print('Loaded classifier model from file "%s"' % classifier_filename_exp) predictions = model.predict_proba(emb_array) best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] for i in range(len(best_class_indices)): print('%4d %s: %.3f' % (i, class_names[best_class_indices[i]], best_class_probabilities[i])) accuracy = np.mean(np.equal(best_class_indices, labels)) update_data({'accuracy': accuracy}, use_mlboard, mlboard) print('Accuracy: %.3f' % accuracy) if args.upload_model and accuracy >= args.upload_threshold: timestamp = datetime.datetime.now().strftime('%s') model_name = 'facenet-classifier' version = '1.0.0-%s-%s' % (args.driver, timestamp) print('Uploading model as %s:%s' % (model_name, version)) upload_model(use_mlboard, mlboard, classifier_filename_exp, model_name, version)
def retrain_model(self, incremental): global labels global emb_array global class_names if incremental is True: dataset, append_index = facenet.append_dataset( data_dir, class_names) paths, self.append_labels = facenet.get_image_paths_and_labels( dataset, append_index) self.append_class_names = [ cls.name.replace('_', ' ') for cls in dataset ] else: dataset, append_index = facenet.get_dataset(data_dir) paths, labels = facenet.get_image_paths_and_labels( dataset, append_index) class_names = [cls.name.replace('_', ' ') for cls in dataset] np.random.seed(seed=666) # Check that there are at least one training image per class for cls in dataset: assert ( len(cls.image_paths) > 0, 'There must be at least one image for each class in the dataset' ) # Create a list of class names print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) if incremental is True: print("new people added: ") print(self.append_class_names) # Get input and output tensors images_placeholder = self.graph.get_tensor_by_name("input:0") embeddings = self.graph.get_tensor_by_name("embeddings:0") phase_train_placeholder = self.graph.get_tensor_by_name( "phase_train:0") embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings print('Calculating features for images') nrof_images = len(paths) nrof_batches_per_epoch = int(math.ceil(1.0 * nrof_images / 90)) self.append_emb_array = np.zeros((nrof_images, embedding_size)) for i in tqdm(range(nrof_batches_per_epoch)): start_index = i * 90 end_index = min((i + 1) * 90, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, 160) feed_dict = { images_placeholder: images, phase_train_placeholder: False } self.append_emb_array[start_index:end_index, :] = self.sess.run( embeddings, feed_dict=feed_dict) classifier_filename_exp = os.path.expanduser(classifier_model) print('Training classifier') if incremental is True: self.incremental_training(self.append_emb_array, self.append_labels, self.append_class_names) print(len(emb_array)) print(class_names) #X_train, X_test, y_train, y_test = train_test_split(emb_array, labels, test_size=0.25) #self.model = SGDClassifier(loss='log', verbose=True, n_jobs=-1, n_iter=1000, alpha=1e-5, # tol=None, shuffle=True, random_state=100, penalty='l2') #self.model = SVC(kernel='rbf', probability=True, verbose=True, cache_size=1024) #self.model = KNeighborsClassifier(n_neighbors=1, algorithm='auto') print('Start building model') start = time.time() #param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5], # 'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], } #self.model = GridSearchCV(SVC(kernel='rbf', cache_size=2048, probability=True), param_grid, n_jobs=-1) model.fit(emb_array, labels) #self.model.fit(X_train, y_train) end = time.time() print("Fit Time: {0:4f}s".format(end - start)) print('Build model done') if incremental is False: # Saving classifier model with open(classifier_filename_exp, 'wb') as outfile: pickle.dump((emb_array, labels, class_names), outfile) print('Saved classifier model to file "%s"' % classifier_filename_exp) return 'Success'
phase_train_placeholder = tf.get_default_graph().get_tensor_by_name( "phase_train:0") embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings print('Calculating features for images') batch_size = 1000 image_size = 160 nrof_images = len(paths) nrof_batches_per_epoch = int(math.ceil(1.0 * nrof_images / batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches_per_epoch): start_index = i * batch_size end_index = min((i + 1) * batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run(embeddings, feed_dict=feed_dict) # classifier_filename = '/..Path to save classifier../my_classifier.pkl' classifier_filename = './my_classifier.pkl' classifier_filename_exp = os.path.expanduser(classifier_filename) # Train classifier print('Training classifier') model = SVC(kernel='linear', probability=True) ##model = SVC(kernel='rbf', probability=True)
def main(args): with tf.Graph().as_default(): with tf.Session() as sess: emb_file = 'embeddings.pkl' # Extract embeddings in trainset if (args.mode == 'EXTRACT'): np.random.seed(seed=666) dataset = facenet.get_dataset(args.data_dir) # Check that there are at least one training image per class for cls in dataset: assert ( len(cls.image_paths) > 0, 'There must be at least one image for each class in the dataset' ) paths, labels = facenet.get_image_paths_and_labels(dataset) print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) # Load the model print('Loading feature extraction model') facenet.load_model(args.model) # 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") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings print('Calculating features for images') nrof_images = len(paths) nrof_batches_per_epoch = int( math.ceil(1.0 * nrof_images / args.batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches_per_epoch): start_index = i * args.batch_size end_index = min((i + 1) * args.batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, args.image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) with open(emb_file, 'wb') as outfile: pickle.dump((emb_array, labels, paths), outfile) print('Embeddings save to file "%s"' % emb_file) # Perform compare task else: # Load embeddings print('Loading embeddings') with open(emb_file, 'rb') as infile: emb_array, labels, paths = pickle.load(infile) # Load the model print('Loading feature extraction model') facenet.load_model(args.model) images = load_and_align_data(args.image_files, args.image_size) # 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") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") # Run forward pass to calculate embeddings feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb = sess.run(embeddings, feed_dict=feed_dict) nrof_images = len(args.image_files) print('Images and embeddings:') for i in range(nrof_images): min_result = min_distance(emb[i], emb_array) min_result_label = labels[min_result] min_result_path = paths[min_result] print( 'Compared image: %s, compared result: %s, label: %s, path: %s' % (args.image_files[i], min_result, min_result_label, min_result_path))
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)
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) print('Selecting random triplets from %s set' % prefix_str) triplets, nrof_triplets = facenet.select_validation_triplets( num_per_class, nrof_people, 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 %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')