def main(): ### Backbone network (Arcface) args = get_args() images = tf.placeholder(name='img_inputs', shape=[112, 112, 3], dtype=tf.float32) train_phase_dropout = tf.placeholder(dtype=tf.bool, shape=None, name='train_phase') train_phase_bn = tf.placeholder(dtype=tf.bool, shape=None, name='train_phase_last') config = yaml.load(open('../configs/config_ms1m_100.yaml')) images = tf.roll(images, shift=1, axis=2) image_ = images / 127.5 - 1 # image_ = tf.roll(image_, shift=2, axis=3) inputs = tf.reshape(image_, [-1, *args.image_size]) embedding_tensor, _ = get_embd(inputs, train_phase_dropout, train_phase_bn, config) t_vars = tf.global_variables() restore_vars = [var for var in t_vars if 'embd_extractor' in var.name] ### Graph & load setup tf_config = tf.ConfigProto(allow_soft_placement=True) tf_config.gpu_options.allow_growth = True with tf.Session(config=tf_config) as sess: tf.global_variables_initializer().run() saver = tf.train.Saver(var_list=restore_vars) saver.restore(sess, '../model/best-m-1006000') generate_tfrecord(sess, images, train_phase_dropout, train_phase_bn, embedding_tensor)
def load_ytf_pairs(basedir, pair_txt, n): with open(pair_txt, 'r') as csvfile: trainrows = list(csv.reader(csvfile, delimiter=',', quoting=csv.QUOTE_NONE, skipinitialspace=True))[1:] train_images = load_images(basedir, trainrows, n) train_labels = np.array(list(map(lambda r: loadLabelsFromRow(r, n), trainrows))) ### Graph args = get_args() images = tf.placeholder(name='img_inputs', shape=[2, 112, 112, 3], dtype=tf.float32) train_phase_dropout = tf.placeholder(dtype=tf.bool, shape=None, name='train_phase') train_phase_bn = tf.placeholder(dtype=tf.bool, shape=None, name='train_phase_last') config = yaml.load(open('./configs/config_ms1m_100.yaml')) images = tf.roll(images, shift=1, axis=3) image_ = images / 127.5 - 1 # image_ = tf.roll(image_, shift=2, axis=3) inputs = tf.reshape(image_, [-1, *args.image_size]) embedding_tensor, _ = get_embd(inputs, train_phase_dropout, train_phase_bn, config) t_vars = tf.global_variables() restore_vars = [var for var in t_vars if 'embd_extractor' in var.name] ### Graph & load setup tf_config = tf.ConfigProto(allow_soft_placement=True) tf_config.gpu_options.allow_growth = True with tf.Session(config=tf_config) as sess: tf.global_variables_initializer().run() saver = tf.train.Saver(var_list=restore_vars) saver.restore(sess, './model/best-m-1006000') train_embeddings = np.zeros((5000, 2, 512)) for i, imgs in enumerate(train_images): embd_dict = { images: imgs, train_phase_dropout: False, train_phase_bn: False } embeddings = sess.run(embedding_tensor, feed_dict=embd_dict) train_embeddings[i] = embeddings return train_embeddings, np.ravel(train_labels)
def load_fs_pairs(num): def get_imgfile(name, file_name): img_file = name + '/' + file_name return imread(img_file) rootpath = '/home/hy/Dataset/face/FaceScrub/' readname = 'facescrub_mtcnn_112/' lst_name = os.listdir(rootpath + readname) match_list = [] nonmatch_list = [] for i in range(num): match_name, nonmatch_name = random.sample(lst_name, 2) read_matchfolder = rootpath + readname + match_name lst_idx = os.listdir(read_matchfolder) m1, m2, nm1 = random.sample(lst_idx, 3) match_list.append([ get_imgfile(read_matchfolder, m1), get_imgfile(read_matchfolder, m2) ]) read_nonmatchfolder = rootpath + readname + nonmatch_name lst_idx = os.listdir(read_nonmatchfolder) nm2 = random.sample(lst_idx, 1)[0] nonmatch_list.append([ get_imgfile(read_matchfolder, nm1), get_imgfile(read_nonmatchfolder, nm2) ]) total_list = np.concatenate( (np.array(match_list), np.array(nonmatch_list)), axis=0) total_label = np.concatenate((np.ones(num), np.zeros(num))) # Graph ### Graph args = get_args() images = tf.placeholder(name='img_inputs', shape=[2, 112, 112, 3], dtype=tf.float32) train_phase_dropout = tf.placeholder(dtype=tf.bool, shape=None, name='train_phase') train_phase_bn = tf.placeholder(dtype=tf.bool, shape=None, name='train_phase_last') config = yaml.load(open('./configs/config_ms1m_100.yaml')) image_ = images / 127.5 - 1 # image_ = tf.roll(image_, shift=2, axis=3) inputs = tf.reshape(image_, [-1, *args.image_size]) embedding_tensor, _ = get_embd(inputs, train_phase_dropout, train_phase_bn, config) t_vars = tf.global_variables() restore_vars = [var for var in t_vars if 'embd_extractor' in var.name] ### Graph & load setup tf_config = tf.ConfigProto(allow_soft_placement=True) tf_config.gpu_options.allow_growth = True with tf.Session(config=tf_config) as sess: tf.global_variables_initializer().run() saver = tf.train.Saver(var_list=restore_vars) saver.restore(sess, './model/best-m-1006000') train_embeddings = np.zeros((num * 2, 2, 512)) for i, imgs in enumerate(total_list): embd_dict = { images: imgs, train_phase_dropout: False, train_phase_bn: False } embeddings = sess.run(embedding_tensor, feed_dict=embd_dict) train_embeddings[i] = embeddings return train_embeddings, total_label ### Graph args = get_args() images = tf.placeholder(name='img_inputs', shape=[2, 112, 112, 3], dtype=tf.float32) train_phase_dropout = tf.placeholder(dtype=tf.bool, shape=None, name='train_phase') train_phase_bn = tf.placeholder(dtype=tf.bool, shape=None, name='train_phase_last') config = yaml.load(open('./configs/config_ms1m_100.yaml')) images = tf.roll(images, shift=1, axis=3) image_ = images / 127.5 - 1 # image_ = tf.roll(image_, shift=2, axis=3) inputs = tf.reshape(image_, [-1, *args.image_size]) embedding_tensor, _ = get_embd(inputs, train_phase_dropout, train_phase_bn, config) t_vars = tf.global_variables() restore_vars = [var for var in t_vars if 'embd_extractor' in var.name] ### Graph & load setup tf_config = tf.ConfigProto(allow_soft_placement=True) tf_config.gpu_options.allow_growth = True with tf.Session(config=tf_config) as sess: tf.global_variables_initializer().run() saver = tf.train.Saver(var_list=restore_vars) saver.restore(sess, './model/best-m-1006000') train_embeddings = np.zeros((5000, 2, 512)) for i, imgs in enumerate(train_images): embd_dict = { images: imgs, train_phase_dropout: False, train_phase_bn: False } embeddings = sess.run(embedding_tensor, feed_dict=embd_dict) train_embeddings[i] = embeddings return train_embeddings, np.ravel(train_labels)
import os from sklearn.metrics import roc_curve import matplotlib.pyplot as plt from src.model import get_embd, get_args from src.youtubeface import load_ytf_data from src.wrapper_basicImg import wrapper_basicImg if __name__ == '__main__': os.environ["CUDA_VISIBLE_DEVICES"] = '0' m = 32 q = 32 n = 5 batch_size = 256 class_num = 1595 args = get_args() ### Fold fold = tf.placeholder(tf.string) ### Get image and label from tfrecord image, label, iterator = {}, {}, {} image['train'], label['train'], iterator['train'] = load_ytf_data( batch_size, 'train') image['gallery'], label['gallery'], iterator['gallery'] = load_ytf_data( batch_size, 'train', eval=True) image['test'], label['test'], iterator['test'] = load_ytf_data( batch_size, 'test') ### Get eer evaluation dataset. Wrapper wrapper = wrapper_basicImg('LFW')
def load_lfw_pairs(): basename = '/home/hy/Dataset/face/LFW/lfw-deepfunneled' pair_txt = '/home/hy/Dataset/face/LFW/pairs.txt' tgz_filename = "{}.tgz".format(basename) tar_filename = "{}.tar".format(basename) tar_subdir = "lfw-deepfunneled" # it will be faster to decompress this tar file all at once print("--> Converting {} to tar".format(tgz_filename)) with gzip.open(tgz_filename, 'rb') as f_in, open(tar_filename, 'wb') as f_out: shutil.copyfileobj(f_in, f_out) tar = tarfile.open(tar_filename) with open(pair_txt, 'r') as csvfile: trainrows = list(csv.reader(csvfile, delimiter='\t'))[1:] train_images = load_images(tar, tar_subdir, trainrows) train_labels = np.array( list(map(lambda r: loadLabelsFromRow(r), trainrows))) train_images = np.asarray([crop_and_downsample(x) for x in train_images]) # train_images = np.reshape(train_images, (10, 600, 2, 112, 112, 3)) # train_labels = np.reshape(train_labels, (10, 600)) train_images = np.reshape(train_images, (6000, 2, 112, 112, 3)) train_labels = np.reshape(train_labels, (6000)) ### Graph args = get_args() images = tf.placeholder(name='img_inputs', shape=[2, 112, 112, 3], dtype=tf.float32) train_phase_dropout = tf.placeholder(dtype=tf.bool, shape=None, name='train_phase') train_phase_bn = tf.placeholder(dtype=tf.bool, shape=None, name='train_phase_last') config = yaml.load(open('./configs/config_ms1m_100.yaml')) image_ = images / 127.5 - 1 # image_ = tf.roll(image_, shift=2, axis=3) inputs = tf.reshape(image_, [-1, *args.image_size]) embedding_tensor, _ = get_embd(inputs, train_phase_dropout, train_phase_bn, config) t_vars = tf.global_variables() restore_vars = [var for var in t_vars if 'embd_extractor' in var.name] ### Graph & load setup tf_config = tf.ConfigProto(allow_soft_placement=True) tf_config.gpu_options.allow_growth = True with tf.Session(config=tf_config) as sess: tf.global_variables_initializer().run() saver = tf.train.Saver(var_list=restore_vars) saver.restore(sess, './model/best-m-1006000') train_embeddings = np.zeros((6000, 2, 512)) for i, imgs in enumerate(train_images): embd_dict = { images: imgs, train_phase_dropout: False, train_phase_bn: False } embeddings = sess.run(embedding_tensor, feed_dict=embd_dict) train_embeddings[i] = embeddings return train_embeddings, train_labels