def main(): with tf.Graph().as_default(): args={ 'lfw_dir':'/Users/unclewang/Downloads/lfw_mtcnnpy_60', 'lfw_batch_size':128, 'model':'/Users/unclewang/PycharmProjects/facenet/models/facenet/20170512-110547/20170512-110547.pb', 'image_size':160, 'lfw_pairs':'/Users/unclewang/PycharmProjects/facenet/data/pairs.txt', 'lfw_file_ext':'png', 'lfw_nrof_folds':10 } 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']) # 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] # 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') 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 evaluate(sess, image_paths, embeddings, labels_batch, image_paths_placeholder, labels_placeholder, batch_size_placeholder, learning_rate_placeholder, phase_train_placeholder, enqueue_op, actual_issame, batch_size, nrof_folds, log_dir, step, summary_writer, embedding_size): start_time = time.time() # Run forward pass to calculate embeddings print('Running forward pass on LFW images: ', end='') nrof_images = len(actual_issame) * 2 assert (len(image_paths) == nrof_images) labels_array = np.reshape(np.arange(nrof_images), (-1, 3)) image_paths_array = np.reshape(np.expand_dims(np.array(image_paths), 1), (-1, 3)) sess.run( enqueue_op, { image_paths_placeholder: image_paths_array, labels_placeholder: labels_array }) emb_array = np.zeros((nrof_images, embedding_size)) nrof_batches = int(np.ceil(nrof_images / batch_size)) label_check_array = np.zeros((nrof_images, )) for i in xrange(nrof_batches): batch_size = min(nrof_images - i * batch_size, batch_size) emb, lab = sess.run( [embeddings, labels_batch], feed_dict={ batch_size_placeholder: batch_size, learning_rate_placeholder: 0.0, phase_train_placeholder: False }) emb_array[lab, :] = emb label_check_array[lab] = 1 print('%.3f' % (time.time() - start_time)) assert (np.all(label_check_array == 1)) _, _, accuracy, val, val_std, far = lfw.evaluate(emb_array, actual_issame, nrof_folds=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)) lfw_time = time.time() - start_time # Add validation loss and accuracy to summary summary = tf.Summary() #pylint: disable=maybe-no-member summary.value.add(tag='lfw/accuracy', simple_value=np.mean(accuracy)) summary.value.add(tag='lfw/val_rate', simple_value=val) summary.value.add(tag='time/lfw', simple_value=lfw_time) summary_writer.add_summary(summary, step) with open(os.path.join(log_dir, 'lfw_result.txt'), 'at') as f: f.write('%d\t%.5f\t%.5f\n' % (step, np.mean(accuracy), val))
def evaluate(sess, enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, embeddings, labels, image_paths, actual_issame, batch_size, nrof_folds, log_dir, step, summary_writer): start_time = time.time() # Run forward pass to calculate embeddings print('Runnning forward pass on LFW images') # Enqueue one epoch of image paths and labels labels_array = np.expand_dims(np.arange(0, len(image_paths)), 1) image_paths_array = np.expand_dims(np.array(image_paths), 1) sess.run( enqueue_op, { image_paths_placeholder: image_paths_array, labels_placeholder: labels_array }) embedding_size = embeddings.get_shape()[1] nrof_images = len(actual_issame) * 2 assert nrof_images % batch_size == 0, 'The number of LFW images must be an integer multiple of the LFW batch size' nrof_batches = nrof_images // batch_size emb_array = np.zeros((nrof_images, embedding_size)) lab_array = np.zeros((nrof_images, )) for _ in range(nrof_batches): feed_dict = { phase_train_placeholder: False, batch_size_placeholder: batch_size } emb, lab = sess.run([embeddings, labels], feed_dict=feed_dict) lab_array[lab] = lab emb_array[lab] = emb assert np.array_equal( lab_array, np.arange(nrof_images) ) == True, 'Wrong labels used for evaluation, possibly caused by training examples left in the input pipeline' _, _, accuracy, val, val_std, far = lfw.evaluate(emb_array, actual_issame, nrof_folds=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)) lfw_time = time.time() - start_time # Add validation loss and accuracy to summary summary = tf.Summary() #pylint: disable=maybe-no-member summary.value.add(tag='lfw/accuracy', simple_value=np.mean(accuracy)) summary.value.add(tag='lfw/val_rate', simple_value=val) summary.value.add(tag='time/lfw', simple_value=lfw_time) summary_writer.add_summary(summary, step) with open(os.path.join(log_dir, 'lfw_result.txt'), 'at') as f: f.write('%d\t%.5f\t%.5f\n' % (step, np.mean(accuracy), val))
def evaluate(sess, enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, control_placeholder, embeddings, labels, image_paths, actual_issame, batch_size, nrof_folds, log_dir, step, summary_writer, stat, epoch, distance_metric, subtract_mean, use_flipped_images, use_fixed_image_standardization): start_time = time.time() # Run forward pass to calculate embeddings print('Runnning forward pass on LFW images') # Enqueue one epoch of image paths and labels nrof_embeddings = len( actual_issame) * 2 # nrof_pairs * nrof_images_per_pair nrof_flips = 2 if use_flipped_images else 1 nrof_images = nrof_embeddings * nrof_flips labels_array = np.expand_dims(np.arange(0, nrof_images), 1) image_paths_array = np.expand_dims( np.repeat(np.array(image_paths), nrof_flips), 1) control_array = np.zeros_like(labels_array, np.int32) if use_fixed_image_standardization: control_array += np.ones_like( labels_array) * facenet.FIXED_STANDARDIZATION if use_flipped_images: # Flip every second image control_array += (labels_array % 2) * facenet.FLIP sess.run( enqueue_op, { image_paths_placeholder: image_paths_array, labels_placeholder: labels_array, control_placeholder: control_array }) embedding_size = int(embeddings.get_shape()[1]) assert nrof_images % batch_size == 0, 'The number of LFW images must be an integer multiple of the LFW batch size' nrof_batches = nrof_images // batch_size emb_array = np.zeros((nrof_images, embedding_size)) lab_array = np.zeros((nrof_images, )) for i in range(nrof_batches): feed_dict = { phase_train_placeholder: False, batch_size_placeholder: batch_size } emb, lab = sess.run([embeddings, labels], feed_dict=feed_dict) lab_array[lab] = lab emb_array[lab, :] = emb if i % 10 == 9: print('.', end='') sys.stdout.flush() print('') embeddings = np.zeros((nrof_embeddings, embedding_size * nrof_flips)) if use_flipped_images: # Concatenate embeddings for flipped and non flipped version of the images embeddings[:, :embedding_size] = emb_array[0::2, :] embeddings[:, embedding_size:] = emb_array[1::2, :] else: embeddings = emb_array assert np.array_equal( lab_array, np.arange(nrof_images) ) == True, 'Wrong labels used for evaluation, possibly caused by training examples left in the input pipeline' _, _, accuracy, val, val_std, far = lfw.evaluate( embeddings, actual_issame, nrof_folds=nrof_folds, distance_metric=distance_metric, subtract_mean=subtract_mean) print('Accuracy: %2.5f+-%2.5f' % (np.mean(accuracy), np.std(accuracy))) print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far)) lfw_time = time.time() - start_time # Add validation loss and accuracy to summary summary = tf.Summary() # pylint: disable=maybe-no-member summary.value.add(tag='lfw/accuracy', simple_value=np.mean(accuracy)) summary.value.add(tag='lfw/val_rate', simple_value=val) summary.value.add(tag='time/lfw', simple_value=lfw_time) summary_writer.add_summary(summary, step) with open(os.path.join(log_dir, 'lfw_result.txt'), 'at') as f: f.write('%d\t%.5f\t%.5f\n' % (step, np.mean(accuracy), val)) stat['lfw_accuracy'][epoch - 1] = np.mean(accuracy) stat['lfw_valrate'][epoch - 1] = val
def evaluate(sess, enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, control_placeholder, embeddings, labels, image_paths, actual_issame, batch_size, nrof_folds, distance_metric, subtract_mean, use_flipped_images, use_fixed_image_standardization): # Run forward pass to calculate embeddings print('Runnning forward pass on LFW images') # Enqueue one epoch of image paths and labels nrof_embeddings = len( actual_issame) * 2 # nrof_pairs * nrof_images_per_pair nrof_flips = 2 if use_flipped_images else 1 nrof_images = nrof_embeddings * nrof_flips labels_array = np.expand_dims(np.arange(0, nrof_images), 1) image_paths_array = np.expand_dims( np.repeat(np.array(image_paths), nrof_flips), 1) control_array = np.zeros_like(labels_array, np.int32) if use_fixed_image_standardization: control_array += np.ones_like( labels_array) * facenet.FIXED_STANDARDIZATION if use_flipped_images: # Flip every second image control_array += (labels_array % 2) * facenet.FLIP sess.run( enqueue_op, { image_paths_placeholder: image_paths_array, labels_placeholder: labels_array, control_placeholder: control_array }) embedding_size = int(embeddings.get_shape()[1]) assert nrof_images % batch_size == 0, 'The number of LFW images must be an integer multiple of the LFW batch size' nrof_batches = nrof_images // batch_size emb_array = np.zeros((nrof_images, embedding_size)) lab_array = np.zeros((nrof_images, )) for i in range(nrof_batches): feed_dict = { phase_train_placeholder: False, batch_size_placeholder: batch_size } emb, lab = sess.run([embeddings, labels], feed_dict=feed_dict) lab_array[lab] = lab emb_array[lab, :] = emb if i % 30 == 9: print('.', end='') sys.stdout.flush() print('') embeddings = np.zeros((nrof_embeddings, embedding_size * nrof_flips)) if use_flipped_images: # Concatenate embeddings for flipped and non flipped version of the images embeddings[:, :embedding_size] = emb_array[0::2, :] embeddings[:, embedding_size:] = emb_array[1::2, :] else: embeddings = emb_array assert np.array_equal( lab_array, np.arange(nrof_images) ) == True, 'Wrong labels used for evaluation, possibly caused by training examples left in the input pipeline' tpr, fpr, accuracy, val, val_std, far = lfw.evaluate( embeddings, actual_issame, nrof_folds=nrof_folds, distance_metric=distance_metric, subtract_mean=subtract_mean) print('Accuracy: %2.5f+-%2.5f' % (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: # Read the file containing the pairs used for testing # 1. 读入之前的pairs.txt文件 # 读入后如[['Abel_Pacheco','1','4']] 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) print("paths shape =", len(paths)) # 12000 print("paths=", paths[0:200]) print('actual_issame shape=', len(actual_issame)) # 6000 print('actual_issame', actual_issame[0:200]) # 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") #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] # 128 # Run forward pass to calculate embeddings print('Runnning forward pass on LFW images') batch_size = args.lfw_batch_size nrof_images = len(paths) # 12000 nrof_batches = int(math.ceil(1.0 * nrof_images / batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) # 12000* 128 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) # 输出调试信息 embeddings1 = emb_array[0::2] # 6000张图片 是每一个Paris中的第一张 embeddings2 = emb_array[1::2] # 6000张图片 是每一个Paris中的第2张 diff = np.subtract(embeddings1, embeddings2) # 每一个Paris中的两个向量相减 dist = np.sum(np.square(diff), 1) # 向量二范数的平方,两个向量之间距离的平方。 每一个Pari之间的欧几里得距离的平方 # #也可以说是两张图片之间特征向量的相似度 print( '------------------------------------------------------------') print('dist.len=', len(dist)) # 把特征向量保存到文件中去 ,由于这里处理的数据不是很靠谱,所以,这里输出的特征无法直接用于聚类处理 f = open( r'E:\MyPythonProjects\HWDB1\train1\pairs/embeddingsOfChinesepairs.txt', 'w') for embedding in emb_array: for data in embedding: f.write(str(data) + ' ') f.write('\n') f.close() # 把误差保存到文件中去 f = open( r'E:\MyPythonProjects\HWDB1\train1\pairs/distInChinesePairs.txt', 'w') for d in dist: f.write(str(d) + '\n') f.close() # *************************************************************************** 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)