예제 #1
0
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))
            print('')
            print('pair: ', pairs)
            print('')
            print('args:', os.path.expanduser(args.lfw_pairs))
            print('')

            # Get the paths for the corresponding images
            paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs)
            
            image_paths_placeholder = tf.placeholder(tf.string, shape=(None,1), name='image_paths')
            labels_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='labels')
            batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
            control_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='control')
            phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
 
            nrof_preprocess_threads = 4
            image_size = (args.image_size, args.image_size)
            eval_input_queue = data_flow_ops.FIFOQueue(capacity=2000000,
                                        dtypes=[tf.string, tf.int32, tf.int32],
                                        shapes=[(1,), (1,), (1,)],
                                        shared_name=None, name=None)
            eval_enqueue_op = eval_input_queue.enqueue_many([image_paths_placeholder, labels_placeholder, control_placeholder], name='eval_enqueue_op')
            image_batch, label_batch = facenet.create_input_pipeline(eval_input_queue, image_size, nrof_preprocess_threads, batch_size_placeholder)
     
            # Load the model
            input_map = {'image_batch': image_batch, 'label_batch': label_batch, 'phase_train': phase_train_placeholder}
            facenet.load_model(args.model, input_map=input_map)

            # Get output tensor
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
#              
            coord = tf.train.Coordinator()
            tf.train.start_queue_runners(coord=coord, sess=sess)

            evaluate(sess,
                     eval_enqueue_op,
                     image_paths_placeholder,
                     labels_placeholder,
                     phase_train_placeholder,
                     batch_size_placeholder,
                     control_placeholder,
                     embeddings,
                     label_batch,
                     paths,
                     actual_issame,
                     args.lfw_batch_size,
                     args.lfw_nrof_folds,
                     args.distance_metric,
                     args.subtract_mean,
                     args.use_flipped_images,
                     args.use_fixed_image_standardization)
예제 #2
0
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)
            
            image_paths_placeholder = tf.placeholder(tf.string, shape=(None,1), name='image_paths')
            labels_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='labels')
            batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
            control_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='control')
            phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
 
            nrof_preprocess_threads = 4
            image_size = (args.image_size, args.image_size)
            eval_input_queue = data_flow_ops.FIFOQueue(capacity=2000000,
                                        dtypes=[tf.string, tf.int32, tf.int32],
                                        shapes=[(1,), (1,), (1,)],
                                        shared_name=None, name=None)
            eval_enqueue_op = eval_input_queue.enqueue_many([image_paths_placeholder, labels_placeholder, control_placeholder], name='eval_enqueue_op')
            image_batch, label_batch = facenet.create_input_pipeline(eval_input_queue, image_size, nrof_preprocess_threads, batch_size_placeholder)
     
            # Load the model
            input_map = {'image_batch': image_batch, 'label_batch': label_batch, 'phase_train': phase_train_placeholder}
            facenet.load_model(args.model, input_map=input_map)

            # Get output tensor
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
#              
            coord = tf.train.Coordinator()
            tf.train.start_queue_runners(coord=coord, sess=sess)

            evaluate(sess, eval_enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, control_placeholder,
                embeddings, label_batch, paths, actual_issame, args.lfw_batch_size, args.lfw_nrof_folds, args.distance_metric, args.subtract_mean,
                args.use_flipped_images, args.use_fixed_image_standardization)
예제 #3
0
def main():
    with tf.Graph().as_default():
        with tf.Session() as sess:
            # Read the file containing the pairs used for testing
            pairs = read_pairs(os.path.expanduser(ACD_PAIRS))
            
            # Get the paths for the corresponding images
            paths, actual_issame = get_paths(os.path.expanduser(ACD_DIR), pairs)
            
            image_paths_placeholder = tf.placeholder(tf.string, shape=(None,1), name='image_paths')
            labels_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='labels')
            batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
            control_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='control')
            phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
            
            nrof_preprocess_threads = 4
            image_size = (IMAGE_SIZE, IMAGE_SIZE)
            eval_input_queue = data_flow_ops.FIFOQueue(capacity=2000000,
                                        dtypes=[tf.string, tf.int32, tf.int32],
                                        shapes=[(1,), (1,), (1,)],
                                        shared_name=None, name=None)
            eval_enqueue_op = eval_input_queue.enqueue_many([image_paths_placeholder, labels_placeholder, control_placeholder], name='eval_enqueue_op')
            image_batch, label_batch = facenet.create_input_pipeline(eval_input_queue, image_size, nrof_preprocess_threads, batch_size_placeholder)
            
            # Load the model
            input_map = {'image_batch': image_batch, 'label_batch': label_batch, 'phase_train': phase_train_placeholder}
            facenet.load_model(MODEL_DIR, input_map=input_map)
            
            # Get output tensor
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            
            coord = tf.train.Coordinator()
            tf.train.start_queue_runners(coord=coord, sess=sess)

            evaluate_tsdvface(sess, eval_enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, control_placeholder,
                embeddings, label_batch, paths, actual_issame, ACD_BATCH_SIZE, ACD_NROF_FOLDS, DISTANCE_METRIC, SUBTRACT_MEAN,
                USE_FLIPPED_IMAGES, USE_FIXED_STD)
예제 #4
0
def get_similarity(config_, input_path_a, input_path_b, multiple_pairs=False):
    config_.display()
    # input image size, can be changed in config.py
    image_size = (config_.im_height, config_.im_width)
    if not multiple_pairs:
        # preprocess the input images
        preprocess(config_, input_path_a, input_path_b)
        # get the updated values of image names
        tf.reset_default_graph()
        image_path_a, ext_a = os.path.splitext(os.path.split(input_path_a)[1])
        image_path_b, ext_b = os.path.splitext(os.path.split(input_path_b)[1])
        image_path_a = os.path.join(config_.preprocessed_out_dir_test,
                                    image_path_a + '_cropped' + ext_a)
        image_path_b = os.path.join(config_.preprocessed_out_dir_test,
                                    image_path_b + '_cropped' + ext_b)
        # create the paths for inference
        paths, actual_issame = [(image_path_a, image_path_b),
                                (image_path_a, image_path_b),
                                (image_path_a, image_path_b)], [False] * 3
    if multiple_pairs:
        input_path_a_list = list(pd.read_csv(input_path_a)["paths"])
        input_path_b_list = list(pd.read_csv(input_path_b)["paths"])
        preprocess(config_, input_path_a, input_path_b, 'true')
        paths, actual_issame = [], []
        for i in range(len(input_path_a_list)):
            input_path_a, input_path_b = input_path_a_list[
                i], input_path_b_list[i]
            image_path_a, ext_a = os.path.splitext(
                os.path.split(input_path_a)[1])
            image_path_b, ext_b = os.path.splitext(
                os.path.split(input_path_b)[1])
            image_path_a = os.path.join(config_.preprocessed_out_dir_test,
                                        image_path_a + '_cropped' + ext_a)
            image_path_b = os.path.join(config_.preprocessed_out_dir_test,
                                        image_path_b + '_cropped' + ext_b)
            paths.append((image_path_a, image_path_b))
            actual_issame.append(False)  # dummy values

    with tf.Graph().as_default():
        with tf.Session() as sess:

            image_paths_placeholder = tf.placeholder(tf.string,
                                                     shape=(None, 1),
                                                     name='image_paths')
            labels_placeholder = tf.placeholder(
                tf.int32, shape=(None, 1),
                name='labels')  # labels of the input pairs
            batch_size_placeholder = tf.placeholder(
                tf.int32,
                name='batch_size')  # batch size for evaluation, default =1
            control_placeholder = tf.placeholder(
                tf.int32, shape=(None, 1), name='control'
            )  # control argument for data augmentation, not needed for testing
            phase_train_placeholder = tf.placeholder(
                tf.bool, name='phase_train')  # flag train(True)/test(False)

            nrof_preprocess_threads = 4
            eval_input_queue = data_flow_ops.FIFOQueue(
                capacity=2000000,
                dtypes=[tf.string, tf.int32, tf.int32],
                shapes=[(1, ), (1, ), (1, )],
                shared_name=None,
                name=None)
            eval_enqueue_op = eval_input_queue.enqueue_many(
                [
                    image_paths_placeholder, labels_placeholder,
                    control_placeholder
                ],
                name='eval_enqueue_op')
            # input pipeline, needed to define the input/output signatures
            image_batch, label_batch = facenet.create_input_pipeline(
                eval_input_queue, image_size, nrof_preprocess_threads,
                batch_size_placeholder)

            # Load the model, modify the path to pretrained model in config.py
            input_map = {
                'image_batch': image_batch,
                'label_batch': label_batch,
                'phase_train': phase_train_placeholder
            }
            facenet.load_model(config_.pretrained_model, input_map=input_map)

            # Get output tensor, needed to create inference graph
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")

            coord = tf.train.Coordinator()
            tf.train.start_queue_runners(coord=coord, sess=sess)

            similarity = evaluate(sess, eval_enqueue_op,
                                  image_paths_placeholder, labels_placeholder,
                                  phase_train_placeholder,
                                  batch_size_placeholder, control_placeholder,
                                  embeddings, label_batch, paths,
                                  actual_issame, config_.pred_batch_size)
            # write the output to json
            # similarity threshold calculated based on validation performance
            are_same = ["Yes" if dist < 0.38 else "No" for dist in similarity]
            similarity = [str(dist) for dist in similarity]
            out_data = {
                "similarity_score": similarity,
                "are_same_people": are_same
            }
            #save the info to json file
            return similarity, out_data
예제 #5
0
def main(args):

    with tf.Graph().as_default():

        with tf.Session() as sess:

            # Read the file containing the pairs used for testing
            # 读取包含用于 test 的 pairs 的文件
            pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))
            #pdb.set_trace()

            # Get the paths for the corresponding images
            # 获取相应图像的路径
            paths, actual_issame = lfw.get_paths(
                os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)

            network = importlib.import_module(args.model_def)

            image_paths_placeholder = tf.placeholder(tf.string,
                                                     shape=(None, 1),
                                                     name='image_paths')
            labels_placeholder = tf.placeholder(tf.int32,
                                                shape=(None, 1),
                                                name='labels')
            batch_size_placeholder = tf.placeholder(tf.int32,
                                                    name='batch_size')
            control_placeholder = tf.placeholder(tf.int32,
                                                 shape=(None, 1),
                                                 name='control')
            phase_train_placeholder = tf.placeholder(tf.bool,
                                                     name='phase_train')

            nrof_preprocess_threads = 4
            image_size = (args.image_size, args.image_size)
            eval_input_queue = data_flow_ops.FIFOQueue(
                capacity=2000000,
                dtypes=[tf.string, tf.int32, tf.int32],
                shapes=[(1, ), (1, ), (1, )],
                shared_name=None,
                name=None)
            eval_enqueue_op = eval_input_queue.enqueue_many(
                [
                    image_paths_placeholder, labels_placeholder,
                    control_placeholder
                ],
                name='eval_enqueue_op')
            image_batch, label_batch = facenet.create_input_pipeline(
                eval_input_queue, image_size, nrof_preprocess_threads,
                batch_size_placeholder)

            # Load the model
            input_map = {
                'image_batch': image_batch,
                'label_batch': label_batch,
                'phase_train': phase_train_placeholder
            }
            facenet.load_model(args.model, input_map=input_map)

            prelogits, _ = network.inference(
                image_batch,
                args.keep_probability,
                phase_train=phase_train_placeholder,
                bottleneck_layer_size=args.embedding_size,
                weight_decay=args.weight_decay)

            embeddings = tf.nn.l2_normalize(prelogits,
                                            1,
                                            1e-10,
                                            name='embeddings')
            # embeddings = tf.identity(prelogits)

            # Initialize variables
            sess.run(tf.global_variables_initializer(),
                     feed_dict={phase_train_placeholder: False})
            sess.run(tf.local_variables_initializer(),
                     feed_dict={phase_train_placeholder: False})

            coord = tf.train.Coordinator()
            tf.train.start_queue_runners(coord=coord, sess=sess)

            evaluate(sess, eval_enqueue_op, image_paths_placeholder,
                     labels_placeholder, phase_train_placeholder,
                     batch_size_placeholder, control_placeholder, embeddings,
                     label_batch, paths, actual_issame, args.lfw_batch_size,
                     args.lfw_nrof_folds, args.distance_metric,
                     args.subtract_mean, args.use_flipped_images,
                     args.use_fixed_image_standardization)
예제 #6
0
def main(args):
    # args从文件系统中导入对象
    network = importlib.import_module(
        args.model_def)  # 导入模型,default = 'models.inception_resnet_v1'
    image_size = (args.image_size, args.image_size)  # 导入尺寸,default = 160

    subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S')
    log_dir = os.path.join(os.path.expanduser(args.logs_base_dir),
                           subdir)  # 导入运行日志目录,default = '~/logs/facenet'
    if not os.path.isdir(
            log_dir):  # Create the log directory if it doesn't exist
        os.makedirs(log_dir)
    model_dir = os.path.join(os.path.expanduser(args.models_base_dir),
                             subdir)  # ,导入模型目录,default = '~/models/facenet'
    if not os.path.isdir(
            model_dir):  # Create the model directory if it doesn't exist
        os.makedirs(model_dir)

    stat_file_name = os.path.join(log_dir, 'stat.h5')  # 统计数据,用h5格式保存

    # Write arguments to a text file
    facenet.write_arguments_to_file(args,
                                    os.path.join(log_dir,
                                                 'arguments.txt'))  # 记录命令至运行日志

    # Store some git revision info in a text file in the log directory
    src_path, _ = os.path.split(
        os.path.realpath(__file__))  # os.path.realpath(__file__)是脚本所在的绝对路径
    facenet.store_revision_info(src_path, log_dir, ' '.join(sys.argv))

    np.random.seed(seed=args.seed)  # default = 666
    random.seed(args.seed)
    dataset = facenet.get_dataset(
        args.data_dir
    )  # 导入数据集目录,default = '~/datasets/casia/casia_maxpy_mtcnnalign_182_160'
    if args.filter_filename:  # default = ''
        dataset = filter_dataset(
            dataset,
            os.path.expanduser(args.filter_filename),
            # expanduser把path中包含的"~"和"~user"转换成用户目录.
            args.filter_percentile,
            args.filter_min_nrof_images_per_class
        )  # default = 100.0, default = 0

    if args.validation_set_split_ratio > 0.0:  # default = 0.0
        train_set, val_set = facenet.split_dataset(
            dataset, args.validation_set_split_ratio,
            args.min_nrof_val_images_per_class, 'SPLIT_IMAGES')
    else:
        train_set, val_set = dataset, []

    nrof_classes = len(train_set)

    print('Model directory: %s' % model_dir)
    print('Log directory: %s' % log_dir)
    pretrained_model = None
    if args.pretrained_model:
        pretrained_model = os.path.expanduser(args.pretrained_model)
        print('Pre-trained model: %s' % pretrained_model)

    if args.lfw_dir:
        print('LFW directory: %s' % args.lfw_dir)
        # Read the file containing the pairs used for testing
        pairs = lfw.read_pairs(os.path.expanduser(
            args.lfw_pairs))  # default = 'data/pairs.txt'
        # Get the paths for the corresponding images
        lfw_paths, actual_issame = lfw.get_paths(
            os.path.expanduser(args.lfw_dir), pairs)

    with tf.Graph().as_default():
        tf.set_random_seed(args.seed)  # 图级随机数种子
        global_step = tf.Variable(0, trainable=False)

        # Get a list of image paths and their labels
        image_list, label_list = facenet.get_image_paths_and_labels(train_set)
        assert len(image_list) > 0, 'The training set should not be empty'

        val_image_list, val_label_list = facenet.get_image_paths_and_labels(
            val_set)

        # Create a queue that produces indices into the image_list and label_list
        labels = ops.convert_to_tensor(label_list, dtype=tf.int32)
        range_size = array_ops.shape(labels)[0]
        index_queue = tf.train.range_input_producer(
            range_size,
            num_epochs=None,  # 产生1到range_size的对列,num_epochs不给值的话
            shuffle=True,
            seed=None,
            capacity=32)  # 则默认无限循环,后面用capacity限制容量

        index_dequeue_op = index_queue.dequeue_many(
            args.batch_size * args.epoch_size, 'index_dequeue')  # 使多个元素同时出列并命名

        # 创建张量
        learning_rate_placeholder = tf.placeholder(tf.float32,
                                                   name='learning_rate')
        batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
        phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
        image_paths_placeholder = tf.placeholder(tf.string,
                                                 shape=(None, 1),
                                                 name='image_paths')
        labels_placeholder = tf.placeholder(tf.int32,
                                            shape=(None, 1),
                                            name='labels')
        control_placeholder = tf.placeholder(tf.int32,
                                             shape=(None, 1),
                                             name='control')

        nrof_preprocess_threads = 4

        # 先进先出对列
        input_queue = data_flow_ops.FIFOQueue(
            capacity=2000000,
            dtypes=[tf.string, tf.int32, tf.int32],
            shapes=[(1, ), (1, ), (1, )],
            shared_name=None,
            name=None)
        enqueue_op = input_queue.enqueue_many(
            [image_paths_placeholder, labels_placeholder, control_placeholder],
            name='enqueue_op')
        image_batch, label_batch = facenet.create_input_pipeline(
            input_queue, image_size, nrof_preprocess_threads,
            batch_size_placeholder)

        image_batch = tf.identity(image_batch, 'image_batch')
        image_batch = tf.identity(image_batch, 'input')
        label_batch = tf.identity(label_batch, 'label_batch')

        print('Number of classes in training set: %d' % nrof_classes)
        print('Number of examples in training set: %d' % len(image_list))

        print('Number of classes in validation set: %d' % len(val_set))
        print('Number of examples in validation set: %d' % len(val_image_list))

        print('Building training graph')

        # Build the inference graph
        prelogits, _ = network.inference(
            image_batch,
            args.keep_probability,
            phase_train=phase_train_placeholder,
            bottleneck_layer_size=args.embedding_size,
            weight_decay=args.weight_decay)
        logits = slim.fully_connected(
            prelogits,
            len(train_set),
            activation_fn=None,  # 相当于Dense
            weights_initializer=slim.initializers.xavier_initializer(),
            weights_regularizer=slim.l2_regularizer(args.weight_decay),
            scope='Logits',
            reuse=False)

        embeddings = tf.nn.l2_normalize(
            prelogits, 1, 1e-10, name='embeddings')  # 按行进行L2正则化,同除各行L2范数

        # Norm for the prelogits
        eps = 1e-4
        # 按行计算平均值。norm计算范数,abs取绝对值,ord取1,使用第一范数
        prelogits_norm = tf.reduce_mean(
            tf.norm(tf.abs(prelogits) + eps, ord=args.prelogits_norm_p,
                    axis=1))
        tf.add_to_collection(
            tf.GraphKeys.REGULARIZATION_LOSSES, prelogits_norm *
            args.prelogits_norm_loss_factor)  # 将该种正则化方式储存至graphkeys

        # Add center loss
        prelogits_center_loss, _ = facenet.center_loss(prelogits, label_batch,
                                                       args.center_loss_alfa,
                                                       nrof_classes)
        tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES,
                             prelogits_center_loss * args.center_loss_factor)

        # 指数衰减。参数1:原始学习率。参数2:全局步数,每次自增1,如果不写则不增1.参数3:衰减系数。参数4:衰减速度。参数5:是否取整。True时取整
        # 具体计算:decayed_learning_rate = learning_rate*decay_rate^(global_step/decay_steps)
        learning_rate = tf.train.exponential_decay(
            learning_rate_placeholder,
            global_step,
            args.learning_rate_decay_epochs * args.epoch_size,
            args.learning_rate_decay_factor,
            staircase=True)
        tf.summary.scalar('learning_rate', learning_rate)  # 保存数据

        # Calculate the average cross entropy loss across the batch
        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=label_batch,
            logits=logits,
            name='cross_entropy_per_example')
        cross_entropy_mean = tf.reduce_mean(cross_entropy,
                                            name='cross_entropy')
        tf.add_to_collection('losses', cross_entropy_mean)

        # cast转换数据类型(布尔->浮点数),equal比较两个向量相同位置的元素是否一样
        correct_prediction = tf.cast(
            tf.equal(tf.argmax(logits, 1), tf.cast(label_batch, tf.int64)),
            tf.float32)
        accuracy = tf.reduce_mean(correct_prediction)  # 求平均值即准确率

        # Calculate the total losses
        regularization_losses = tf.get_collection(
            tf.GraphKeys.REGULARIZATION_LOSSES)  # 从graphkeys中把之前存储的loss计算方式取出来
        total_loss = tf.add_n([cross_entropy_mean] + regularization_losses,
                              name='total_loss')

        # Build a Graph that trains the model with one batch of examples and updates the model parameters
        train_op = facenet.train(total_loss, global_step, args.optimizer,
                                 learning_rate, args.moving_average_decay,
                                 tf.global_variables(), args.log_histograms)

        # Create a saver
        saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.summary.merge_all(
        )  # 将所有summary全部保存到磁盘,以便tensorboard显示

        # Start running operations on the Graph.
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.
                                    gpu_memory_fraction)  # 使用gpu,参数为使用的显存上限
        sess = tf.Session(config=tf.ConfigProto(
            gpu_options=gpu_options, log_device_placement=False))  # 会话
        sess.run(tf.global_variables_initializer())  # 返回所有全局变量
        sess.run(tf.local_variables_initializer())  # 返回所有局部变量
        summary_writer = tf.summary.FileWriter(
            log_dir, sess.graph)  # 将训练过程数据保存在filewriter指定的文件中
        coord = tf.train.Coordinator()  # 管理在Session中的多个线程
        """
        启动入队线程,由多个或单个线程,按照设定规则,把文件读入Filename Queue中。函数返回线程ID的列表,一般情况下,系统有多少个核,
        就会启动多少个入队线程(入队具体使用多少个线程在tf.train.batch中定义)
        """
        tf.train.start_queue_runners(coord=coord, sess=sess)

        with sess.as_default():

            if pretrained_model:
                print('Restoring pretrained model: %s' % pretrained_model)
                saver.restore(sess, pretrained_model)

            # Training and validation loop
            print('Running training')
            nrof_steps = args.max_nrof_epochs * args.epoch_size
            nrof_val_samples = int(
                math.ceil(args.max_nrof_epochs / args.validate_every_n_epochs)
            )  # Validate every validate_every_n_epochs as well as in the last epoch
            # ceil返回上入整数
            stat = {
                'loss':
                np.zeros((nrof_steps, ), np.float32),
                'center_loss':
                np.zeros((nrof_steps, ), np.float32),
                'reg_loss':
                np.zeros((nrof_steps, ), np.float32),
                'xent_loss':
                np.zeros((nrof_steps, ), np.float32),
                'prelogits_norm':
                np.zeros((nrof_steps, ), np.float32),
                'accuracy':
                np.zeros((nrof_steps, ), np.float32),
                'val_loss':
                np.zeros((nrof_val_samples, ), np.float32),
                'val_xent_loss':
                np.zeros((nrof_val_samples, ), np.float32),
                'val_accuracy':
                np.zeros((nrof_val_samples, ), np.float32),
                'lfw_accuracy':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'lfw_valrate':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'learning_rate':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'time_train':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'time_validate':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'time_evaluate':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'prelogits_hist':
                np.zeros((args.max_nrof_epochs, 1000), np.float32),
            }
            for epoch in range(1, args.max_nrof_epochs + 1):
                step = sess.run(global_step, feed_dict=None)
                # Train for one epoch
                t = time.time()
                cont = train(
                    args, sess, epoch, image_list, label_list,
                    index_dequeue_op, enqueue_op, image_paths_placeholder,
                    labels_placeholder, learning_rate_placeholder,
                    phase_train_placeholder, batch_size_placeholder,
                    control_placeholder, global_step, total_loss, train_op,
                    summary_op, summary_writer, regularization_losses,
                    args.learning_rate_schedule_file, stat, cross_entropy_mean,
                    accuracy, learning_rate, prelogits, prelogits_center_loss,
                    args.random_rotate, args.random_crop, args.random_flip,
                    prelogits_norm, args.prelogits_hist_max,
                    args.use_fixed_image_standardization)
                stat['time_train'][epoch - 1] = time.time() - t

                if not cont:
                    break

                t = time.time()
                if len(val_image_list) > 0 and (
                    (epoch - 1) % args.validate_every_n_epochs
                        == args.validate_every_n_epochs - 1
                        or epoch == args.max_nrof_epochs):
                    validate(args, sess, epoch, val_image_list, val_label_list,
                             enqueue_op, image_paths_placeholder,
                             labels_placeholder, control_placeholder,
                             phase_train_placeholder, batch_size_placeholder,
                             stat, total_loss, regularization_losses,
                             cross_entropy_mean, accuracy,
                             args.validate_every_n_epochs,
                             args.use_fixed_image_standardization)
                stat['time_validate'][epoch - 1] = time.time() - t

                # Save variables and the metagraph if it doesn't exist already
                save_variables_and_metagraph(sess, saver, summary_writer,
                                             model_dir, subdir, epoch)

                # Evaluate on LFW
                t = time.time()
                if args.lfw_dir:
                    evaluate(sess, enqueue_op, image_paths_placeholder,
                             labels_placeholder, phase_train_placeholder,
                             batch_size_placeholder, control_placeholder,
                             embeddings, label_batch, lfw_paths, actual_issame,
                             args.lfw_batch_size, args.lfw_nrof_folds, log_dir,
                             step, summary_writer, stat, epoch,
                             args.lfw_distance_metric, args.lfw_subtract_mean,
                             args.lfw_use_flipped_images,
                             args.use_fixed_image_standardization)
                stat['time_evaluate'][epoch - 1] = time.time() - t

                print('Saving statistics')
                with h5py.File(stat_file_name, 'w') as f:
                    for key, value in stat.iteritems():
                        f.create_dataset(key, data=value)

    return model_dir
def main(args):

    with tf.Graph().as_default():

        with tf.Session(config=config) 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)

            # add path from check_list
            if args.check_list is not 'None':
                print('Reading check_list')
                paths, actual_issame = check.get_paths(args.lfw_dir,
                                                       args.check_list, paths,
                                                       actual_issame)

            # check query_dir, this directory can only contain one query image
            if args.query_dir is not 'None':
                paths, actual_issame = check.get_query_path(
                    args.query_dir, paths, actual_issame)

            print('{} calculation of embeddings remain'.format(paths.shape[0]))

            if paths.shape[0] == 0:
                sys.exit()

            image_paths_placeholder = tf.placeholder(tf.string,
                                                     shape=(None, 1),
                                                     name='image_paths')
            labels_placeholder = tf.placeholder(tf.int32,
                                                shape=(None, 1),
                                                name='labels')
            batch_size_placeholder = tf.placeholder(tf.int32,
                                                    name='batch_size')
            control_placeholder = tf.placeholder(tf.int32,
                                                 shape=(None, 1),
                                                 name='control')
            phase_train_placeholder = tf.placeholder(tf.bool,
                                                     name='phase_train')

            nrof_preprocess_threads = 4
            image_size = (args.image_size, args.image_size)
            eval_input_queue = data_flow_ops.FIFOQueue(
                capacity=2000000,
                dtypes=[tf.string, tf.int32, tf.int32],
                shapes=[(1, ), (1, ), (1, )],
                shared_name=None,
                name=None)
            eval_enqueue_op = eval_input_queue.enqueue_many(
                [
                    image_paths_placeholder, labels_placeholder,
                    control_placeholder
                ],
                name='eval_enqueue_op')
            image_batch, label_batch = facenet.create_input_pipeline(
                eval_input_queue, image_size, nrof_preprocess_threads,
                batch_size_placeholder)

            # Load the model
            input_map = {
                'image_batch': image_batch,
                'label_batch': label_batch,
                'phase_train': phase_train_placeholder
            }
            facenet.load_model(args.model, input_map=input_map)

            # Get output tensor
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            #
            coord = tf.train.Coordinator()
            tf.train.start_queue_runners(coord=coord, sess=sess)

            # get the best batch size
            batch_size = check.get_batch_size(args.lfw_batch_size,
                                              paths.shape[0])
            print('best batch_size: %d' % batch_size)

            embeddings = evaluate(
                sess, eval_enqueue_op, image_paths_placeholder,
                labels_placeholder, phase_train_placeholder,
                batch_size_placeholder, control_placeholder, embeddings,
                label_batch, paths, actual_issame, batch_size,
                args.lfw_nrof_folds, args.distance_metric, args.subtract_mean,
                args.use_flipped_images, args.use_fixed_image_standardization)

            check.save_embed(paths, embeddings)
예제 #8
0
def main(args):

    network = importlib.import_module(args.model_def)
    image_size = (args.image_size, args.image_size)

    subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S')
    log_dir = os.path.join(os.path.expanduser(args.logs_base_dir),
                           subdir)  #日志保存地址
    if not os.path.isdir(
            log_dir):  # Create the log directory if it doesn't exist
        os.makedirs(log_dir)
    model_dir = os.path.join(os.path.expanduser(args.models_base_dir), subdir)
    if not os.path.isdir(
            model_dir):  # Create the model directory if it doesn't exist
        os.makedirs(model_dir)

    # Write arguments to a text file
    facenet.write_arguments_to_file(args, os.path.join(log_dir,
                                                       'arguments.txt'))

    # Store some git revision info in a text file in the log directory
    # os.path.realpath(__file__)代表返回当前模块真实路径
    # os.path.split(os.path.realpath(__file__))代表返回路径的目录和文件名(元组形式)
    src_path, _ = os.path.split(os.path.realpath(__file__))
    facenet.store_revision_info(src_path, log_dir, ' '.join(sys.argv))

    np.random.seed(seed=args.seed)
    random.seed(args.seed)
    # dataset是列表,列表元素为每一个类的ImageClass对象,定义见facenet.py文件
    dataset = facenet.get_dataset(args.data_dir)
    if args.filter_filename:
        dataset = filter_dataset(dataset,
                                 os.path.expanduser(args.filter_filename),
                                 args.filter_percentile,
                                 args.filter_min_nrof_images_per_class)

    # 划分训练集和测试集,train_set和val_set的形式和dataset一样
    if args.validation_set_split_ratio > 0.0:
        train_set, val_set = facenet.split_dataset(
            dataset, args.validation_set_split_ratio,
            args.min_nrof_val_images_per_class, 'SPLIT_IMAGES')
    else:
        train_set, val_set = dataset, []

    nrof_classes = len(train_set)  #类目数(即有多少人)

    print('Model directory: %s' % model_dir)
    print('Log directory: %s' % log_dir)
    pretrained_model = None
    if args.pretrained_model:
        pretrained_model = os.path.expanduser(args.pretrained_model)
        print('Pre-trained model: %s' % pretrained_model)

    if args.lfw_dir:
        print('LFW directory: %s' % args.lfw_dir)
        # Read the file containing the pairs used for testing
        # pairs也是列表,子元素也是列表,每个子列表包含pairs.txt的每一行
        pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))
        # Get the paths for the corresponding images
        #lfw_paths为两两比较的列表,列表的元素为有2个元素的元祖,actual_issame为列表,表示每个元素是否为同一个人
        lfw_paths, actual_issame = lfw.get_paths(
            os.path.expanduser(args.lfw_dir), pairs)

    with tf.Graph().as_default():
        tf.set_random_seed(args.seed)

        # global_step对应的是全局批的个数,根据这个参数可以更新学习率
        global_step = tf.Variable(0, trainable=False)

        # Get a list of image paths and their labels
        image_list, label_list = facenet.get_image_paths_and_labels(train_set)
        assert len(image_list) > 0, 'The training set should not be empty'

        val_image_list, val_label_list = facenet.get_image_paths_and_labels(
            val_set)

        # Create a queue that produces indices into the image_list and label_list
        labels = ops.convert_to_tensor(label_list, dtype=tf.int32)
        # range_size的大小和样本个数一样
        range_size = array_ops.shape(labels)[0]
        #QueueRunner:保存的是队列中的入列操作,保存在一个list当中,其中每个enqueue运行在一个线程当中
        #range_input_producer:返回的是一个队列,队列中有打乱的整数,范围是从0到range size
        #range_size大小和总的样本个数一样
        #并将一个QueueRunner添加到当前图的QUEUE_RUNNER集合中
        index_queue = tf.train.range_input_producer(
            range_size, num_epochs=None, shuffle=True, seed=None,
            capacity=32)  #capacity代表队列容量

        # 返回的是一个出列操作,每次出列一个epoch需要用到的样本个数
        index_dequeue_op = index_queue.dequeue_many(
            args.batch_size * args.epoch_size, 'index_dequeue')

        learning_rate_placeholder = tf.placeholder(tf.float32,
                                                   name='learning_rate')
        batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
        phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
        image_paths_placeholder = tf.placeholder(tf.string,
                                                 shape=(None, 1),
                                                 name='image_paths')
        labels_placeholder = tf.placeholder(tf.int32,
                                            shape=(None, 1),
                                            name='labels')
        control_placeholder = tf.placeholder(tf.int32,
                                             shape=(None, 1),
                                             name='control')

        # 上面的队列是一个样本索引的出队队列,只用来出列
        # 用来每次出列一个epoch中用到的样本
        # 这里是第二个队列,这个队列用来入列,每个元素的大小为shape=[(1,),(1,),(1,)]
        nrof_preprocess_threads = 4
        # 这里的shape代表每一个元素的维度
        input_queue = data_flow_ops.FIFOQueue(
            capacity=600000,
            dtypes=[tf.string, tf.int32, tf.int32],
            shapes=[(1, ), (1, ), (1, )],
            shared_name=None,
            name=None)

        # 这时一个入列的操作,这个操作将在session run的时候用到
        # 每次入列的是image_paths_placeholder, labels_placeholder对
        # 注意,这里只有2个队列,一个用来出列打乱的元素序号
        # 一个根据对应的需要读取指定的文件
        enqueue_op = input_queue.enqueue_many(
            [image_paths_placeholder, labels_placeholder, control_placeholder],
            name='enqueue_op')
        image_batch, label_batch = facenet.create_input_pipeline(
            input_queue, image_size, nrof_preprocess_threads,
            batch_size_placeholder)

        # image_batch = tf.identity(image_batch, 'image_batch')
        image_batch = tf.identity(image_batch, 'input')
        label_batch = tf.identity(label_batch, 'label_batch')

        print('Total number of classes: %d' % nrof_classes)
        print('Total number of examples: %d' % len(image_list))
        print('Building training graph')

        # Build the inference graph
        prelogits, _ = network.inference(
            image_batch,
            args.keep_probability,
            phase_train=phase_train_placeholder,
            bottleneck_layer_size=args.embedding_size,
            weight_decay=args.weight_decay)

        embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings')
        # arcface_logits = arcface_logits_compute(embeddings, label_batch, args, nrof_classes)

        learning_rate = tf.train.exponential_decay(
            learning_rate_placeholder,
            global_step,
            args.learning_rate_decay_epochs * args.epoch_size,
            args.learning_rate_decay_factor,
            staircase=True)
        tf.summary.scalar('learning_rate', learning_rate)
        with tf.variable_scope('Logits'):
            cosface_logits = AM_logits_compute(embeddings, label_batch, args,
                                               nrof_classes)
            cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
                labels=label_batch,
                logits=cosface_logits,
                name='cross_entropy_per_example')
        cross_entropy_mean = tf.reduce_mean(cross_entropy,
                                            name='cross_entropy')
        tf.add_to_collection('losses', cross_entropy_mean)

        correct_prediction = tf.cast(
            tf.equal(tf.argmax(cosface_logits, 1),
                     tf.cast(label_batch, tf.int64)), tf.float32)
        accuracy = tf.reduce_mean(correct_prediction)

        # for weights in slim.get_variables_by_name('embedding_weights'):
        #    weight_regularization = tf.contrib.layers.l2_regularizer(args.weight_decay)(weights)
        #    tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, weight_regularization)

        regularization_losses = tf.get_collection(
            tf.GraphKeys.REGULARIZATION_LOSSES)

        if args.weight_decay == 0:
            total_loss = tf.add_n([cross_entropy_mean], name='total_loss')
        else:
            total_loss = tf.add_n([cross_entropy_mean] + regularization_losses,
                                  name='total_loss')

        #define two saver in case under 'finetuning on different dataset' situation
        saver_save = tf.train.Saver(tf.trainable_variables(), max_to_keep=1)

        train_op = facenet.train(total_loss, global_step, args.optimizer,
                                 learning_rate, args.moving_average_decay,
                                 tf.global_variables(), args.log_histograms)
        summary_op = tf.summary.merge_all()

        # Start running operations on the Graph.
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                                log_device_placement=False))
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        summary_writer = tf.summary.FileWriter(log_dir, sess.graph)
        coord = tf.train.Coordinator()

        tf.train.start_queue_runners(coord=coord, sess=sess)

        with sess.as_default():
            if pretrained_model:
                print('Restoring pretrained model: %s' % pretrained_model)
                facenet.load_model(pretrained_model)

            print('Running cosface training')

            best_accuracy = 0.0
            for epoch in range(1, args.max_nrof_epochs + 1):
                step = sess.run(global_step, feed_dict=None)
                cont = train(
                    args, sess, epoch, image_list, label_list,
                    index_dequeue_op, enqueue_op, image_paths_placeholder,
                    labels_placeholder, learning_rate_placeholder,
                    phase_train_placeholder, batch_size_placeholder,
                    global_step, total_loss, accuracy, train_op, summary_op,
                    summary_writer, regularization_losses,
                    args.learning_rate_schedule_file, args.random_rotate,
                    args.random_crop, args.random_flip,
                    args.use_fixed_image_standardization, control_placeholder)

                if not cont:
                    break
                print('validation running...')
                if args.lfw_dir:
                    best_accuracy = evaluate(
                        sess, enqueue_op, image_paths_placeholder,
                        labels_placeholder, phase_train_placeholder,
                        batch_size_placeholder, control_placeholder,
                        embeddings, label_batch, lfw_paths, actual_issame,
                        args.lfw_batch_size, args.lfw_nrof_folds, log_dir,
                        step, summary_writer, best_accuracy, saver_save,
                        model_dir, subdir, args.lfw_subtract_mean,
                        args.lfw_use_flipped_images,
                        args.use_fixed_image_standardization,
                        args.lfw_distance_metric)
    return model_dir
예제 #9
0
def main(args):

    with tf.Graph().as_default():

        with tf.Session() as sess:
            print("args", args)
            print("args.lfw_dir", args.lfw_dir)

            # 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)
            #print("paths[0:4]", len(paths), paths[0:4])
            #print("actual_issame[0:4]", len(actual_issame), actual_issame[0:4])

            # init paths:
            ## /home/kangeunsu/progressive_growing_of_gans/celeba-hq-1024x1024_test_extract/
            #args.lfw_dir = "/home/kangeunsu/progressive_growing_of_gans/celeba-hq-1024x1024_test_extract"
            # files like img00009982.png
            files = sorted(os.listdir(args.lfw_dir + "/"))
            frame_files = fnmatch.filter(files, '*.png')

            full_paths = [args.lfw_dir + "/" + file for file in frame_files]
            paths = full_paths  #[0:12000] # full?!
            actual_issame = [True] * int(len(paths) / 2)  #6000 #idk

            print("paths[0:4]", len(paths), paths[0:4])
            print("actual_issame[0:4]", len(actual_issame), actual_issame[0:4])
            # actual_issame is True and False

            image_paths_placeholder = tf.placeholder(tf.string,
                                                     shape=(None, 1),
                                                     name='image_paths')
            labels_placeholder = tf.placeholder(tf.int32,
                                                shape=(None, 1),
                                                name='labels')
            batch_size_placeholder = tf.placeholder(tf.int32,
                                                    name='batch_size')
            control_placeholder = tf.placeholder(tf.int32,
                                                 shape=(None, 1),
                                                 name='control')
            phase_train_placeholder = tf.placeholder(tf.bool,
                                                     name='phase_train')

            nrof_preprocess_threads = 4
            image_size = (args.image_size, args.image_size)
            print("image_size", image_size, "from args.image_size=",
                  args.image_size)
            eval_input_queue = data_flow_ops.FIFOQueue(
                capacity=2000000,
                dtypes=[tf.string, tf.int32, tf.int32],
                shapes=[(1, ), (1, ), (1, )],
                shared_name=None,
                name=None)
            eval_enqueue_op = eval_input_queue.enqueue_many(
                [
                    image_paths_placeholder, labels_placeholder,
                    control_placeholder
                ],
                name='eval_enqueue_op')
            image_batch, label_batch = facenet.create_input_pipeline(
                eval_input_queue, image_size, nrof_preprocess_threads,
                batch_size_placeholder)

            # Load the model
            input_map = {
                'image_batch': image_batch,
                'label_batch': label_batch,
                'phase_train': phase_train_placeholder
            }
            facenet.load_model(args.model, input_map=input_map)

            # Get output tensor
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")

            coord = tf.train.Coordinator()
            tf.train.start_queue_runners(coord=coord, sess=sess)

            evaluate(sess, eval_enqueue_op, image_paths_placeholder,
                     labels_placeholder, phase_train_placeholder,
                     batch_size_placeholder, control_placeholder, embeddings,
                     label_batch, paths, actual_issame, args.lfw_batch_size,
                     args.lfw_nrof_folds, args.distance_metric,
                     args.subtract_mean, args.use_flipped_images,
                     args.use_fixed_image_standardization)
예제 #10
0
def get_embeddings(image_paths, model, batch_size):
    embeddings = None
    with tf.Graph().as_default():

        with tf.Session() as sess:

            paths = image_paths
            actual_issame = None

            image_paths_placeholder = tf.placeholder(tf.string,
                                                     shape=(None, 1),
                                                     name='image_paths')
            labels_placeholder = tf.placeholder(tf.int32,
                                                shape=(None, 1),
                                                name='labels')
            batch_size_placeholder = tf.placeholder(tf.int32,
                                                    name='batch_size')
            control_placeholder = tf.placeholder(tf.int32,
                                                 shape=(None, 1),
                                                 name='control')
            phase_train_placeholder = tf.placeholder(tf.bool,
                                                     name='phase_train')

            nrof_preprocess_threads = 4
            image_size = (100, 100)
            eval_input_queue = data_flow_ops.FIFOQueue(
                capacity=2000000,
                dtypes=[tf.string, tf.int32, tf.int32],
                shapes=[(1, ), (1, ), (1, )],
                shared_name=None,
                name=None)
            eval_enqueue_op = eval_input_queue.enqueue_many(
                [
                    image_paths_placeholder, labels_placeholder,
                    control_placeholder
                ],
                name='eval_enqueue_op')
            image_batch, label_batch = facenet.create_input_pipeline(
                eval_input_queue, image_size, nrof_preprocess_threads,
                batch_size_placeholder)

            # Load the model
            input_map = {
                'image_batch': image_batch,
                'label_batch': label_batch,
                'phase_train': phase_train_placeholder
            }
            facenet.load_model(model, input_map=input_map)

            # Get output tensor
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            coord = tf.train.Coordinator()
            tf.train.start_queue_runners(coord=coord, sess=sess)

            #for the first round save the input map, so that we can visualize images of positives
            embeddings = evaluate(sess, eval_enqueue_op,
                                  image_paths_placeholder, labels_placeholder,
                                  phase_train_placeholder,
                                  batch_size_placeholder, control_placeholder,
                                  embeddings, label_batch, paths, batch_size)
    return embeddings
예제 #11
0
def main(args):
    print('args used to train the model:')
    print(args)

    # =========================================================================== #
    # 数据预处理,路径问题等
    # =========================================================================== #
    network = importlib.import_module(args.model_def)
    image_size = (args.image_size, args.image_size)

    subdir = datetime.strftime(datetime.now(),
                               '%Y%m%d-%H%M%S')  # '20180614-100359'
    subdir += args.dir_postfix
    log_dir = os.path.join(os.path.expanduser(args.logs_base_dir),
                           subdir)  # 本地保存log的路径
    log_dir += args.dir_postfix
    val_log_dir = log_dir  # 本地保存validation result的路径
    if not os.path.isdir(
            log_dir):  # Create the log directory if it doesn't exist
        os.makedirs(log_dir)
    model_dir = os.path.join(os.path.expanduser(args.models_base_dir),
                             subdir)  # 本地保存model的路径,最后一层目录用上面生成的日期命名
    if not os.path.isdir(
            model_dir):  # Create the model directory if it doesn't exist
        os.makedirs(model_dir)

    stat_file_name = os.path.join(log_dir, 'stat.h5')

    # Write arguments to a text file, 把训练时用的所有参数保存到txt中
    facenet.write_arguments_to_file(args, os.path.join(log_dir,
                                                       'arguments.txt'))

    # Store some git revision info in a text file in the log directory
    src_path, _ = os.path.split(os.path.realpath(__file__))
    facenet.store_revision_info(src_path, log_dir, ' '.join(sys.argv))

    np.random.seed(seed=args.seed)
    random.seed(args.seed)
    # dataset中是多个ImageClass对象,每个对象中有类名(name)和该类所有图片的路径(image_paths)
    dataset = facenet.get_dataset(args.data_dir)
    # filter_name default='', 这个训练的时候没用到, 先不管了
    if args.filter_filename:
        dataset = filter_dataset(dataset,
                                 os.path.expanduser(args.filter_filename),
                                 args.filter_percentile,
                                 args.filter_min_nrof_images_per_class)

    # validation_set_split_ratio default=0.0, 如果需要验证操作的话设置这个
    # 划分验证集有两种方式:按类划分和按图片划分(默认)
    if args.validation_set_split_ratio > 0.0:
        train_set, val_set = facenet.split_dataset(
            dataset, args.validation_set_split_ratio,
            args.min_nrof_val_images_per_class, 'SPLIT_IMAGES')
    else:
        train_set, val_set = dataset, []

    nrof_classes = len(train_set)

    print('Model directory: %s' % model_dir)
    print('Log directory: %s' % log_dir)
    pretrained_model = None
    if args.pretrained_model:
        pretrained_model = os.path.expanduser(args.pretrained_model)
        print('Pre-trained model: %s' % pretrained_model)

    if args.lfw_dir:
        print('LFW directory: %s' % args.lfw_dir)
        # Read the file containing the pairs used for testing
        # pairs[0] = ['Abel_Pacheco', '1', '4']
        # args.lfw_pairs: data/pairs.txt, 这个文件中每一行或有三个字段或有四个字段
        # 如果有三个字段表示从一个人的所有图片中选择两个(正样本, actual_issame=True)
        # 如果有四个字段表示从两个人的所有图片中各选一个(负样本, actual_issame=False)
        pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))
        # Get the paths for the corresponding images
        # lfw_paths里的每个值都是一个元组(path0, path1), actual_issame里对应的是这个元组是正样本还是副样本
        lfw_paths, actual_issame = lfw.get_paths(
            os.path.expanduser(args.lfw_dir), pairs)

    # =========================================================================== #
    # 下面开始处理TensorFlow相关的东西
    # =========================================================================== #
    with tf.Graph().as_default():

        # =========================================================================== #
        # Input pipeline
        # =========================================================================== #
        tf.set_random_seed(args.seed)
        global_step = tf.Variable(0, trainable=False)

        # Get a list of image paths and their labels
        # 这两个list是训练集中的所有数据
        image_list, label_list = facenet.get_image_paths_and_labels(train_set)
        assert len(image_list) > 0, 'The training set should not be empty'

        val_image_list, val_label_list = facenet.get_image_paths_and_labels(
            val_set)

        # Create a queue that produces indices into the image_list and label_list
        labels = ops.convert_to_tensor(label_list, dtype=tf.int32)
        range_size = array_ops.shape(labels)[0]
        # num_epochs=None时,循环生成0到range_size-1的数
        index_queue = tf.train.range_input_producer(range_size,
                                                    num_epochs=None,
                                                    shuffle=True,
                                                    seed=None,
                                                    capacity=32)
        # 这个op是用来生成索引值的,每次从队列里取出一个epoch中所有图片数量的下标
        index_dequeue_op = index_queue.dequeue_many(
            args.batch_size * args.epoch_size, 'index_dequeue')

        learning_rate_placeholder = tf.placeholder(tf.float32,
                                                   name='learning_rate')
        batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
        phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
        image_paths_placeholder = tf.placeholder(tf.string,
                                                 shape=(None, 1),
                                                 name='image_paths')
        labels_placeholder = tf.placeholder(tf.int32,
                                            shape=(None, 1),
                                            name='labels')
        control_placeholder = tf.placeholder(tf.int32,
                                             shape=(None, 1),
                                             name='control')

        nrof_preprocess_threads = 4
        input_queue = data_flow_ops.FIFOQueue(
            capacity=2000000,
            dtypes=[tf.string, tf.int32, tf.int32],
            shapes=[(1, ), (1, ), (1, )],
            shared_name=None,
            name=None)
        enqueue_op = input_queue.enqueue_many(
            [image_paths_placeholder, labels_placeholder, control_placeholder],
            name='enqueue_op')
        # create input pipeline, 将上面image path中的文件取出来
        image_batch, label_batch = facenet.create_input_pipeline(
            input_queue, image_size, nrof_preprocess_threads,
            batch_size_placeholder)

        image_batch = tf.identity(image_batch, 'image_batch')
        image_batch = tf.identity(image_batch, 'input')
        label_batch = tf.identity(label_batch, 'label_batch')

        print('Number of classes in training set: %d' % nrof_classes)
        print('Number of examples in training set: %d' % len(image_list))

        print('Number of classes in validation set: %d' % len(val_set))
        print('Number of examples in validation set: %d' % len(val_image_list))

        print('Building training graph')

        ### Build the inference graph
        # prelogits是128维的向量
        prelogits, _ = network.inference(
            image_batch,
            args.keep_probability,
            phase_train=phase_train_placeholder,
            bottleneck_layer_size=args.embedding_size,
            weight_decay=args.weight_decay)

        # TODO: Inject some code here for fine tuning

        # logits的shape: [None, len(train_set)]
        logits = slim.fully_connected(
            prelogits,
            len(train_set),
            activation_fn=None,
            weights_initializer=slim.initializers.xavier_initializer(),
            weights_regularizer=slim.l2_regularizer(args.weight_decay),
            scope='Logits',
            reuse=False)
        # 这步操作之后在测试时就可以拿到这个值了
        logits = tf.identity(logits, name='logits')

        # axis=1, 是把每行进行normalize, 每行是一个样本, 128维(或512维)的向量
        embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings')

        # Norm for the prelogits
        eps = 1e-4
        prelogits_norm = tf.reduce_mean(
            tf.norm(tf.abs(prelogits) + eps, ord=args.prelogits_norm_p,
                    axis=1))
        # 把prelogits进行norm操作得到的是一个数,这个数乘个weight作为loss的一部分,--prelogits_norm_loss_factor 5e-4
        tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES,
                             prelogits_norm * args.prelogits_norm_loss_factor)

        # Add center loss, 这个center loss作为正则化loss的一部分, 出自一篇人脸识别的论文, 先不管
        # 这个loss默认权重是0, 文档中给出的训练脚本并没有用这个loss
        prelogits_center_loss, _ = facenet.center_loss(prelogits, label_batch,
                                                       args.center_loss_alfa,
                                                       nrof_classes)
        tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES,
                             prelogits_center_loss * args.center_loss_factor)

        learning_rate = tf.train.exponential_decay(
            learning_rate_placeholder,
            global_step,
            args.learning_rate_decay_epochs * args.epoch_size,
            args.learning_rate_decay_factor,
            staircase=True)
        tf.summary.scalar('learning_rate', learning_rate)

        # Calculate the average cross entropy loss across the batch
        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=label_batch,
            logits=logits,
            name='cross_entropy_per_example')
        cross_entropy_mean = tf.reduce_mean(cross_entropy,
                                            name='cross_entropy')
        tf.add_to_collection('losses', cross_entropy_mean)

        correct_prediction = tf.cast(
            tf.equal(tf.argmax(logits, 1), tf.cast(label_batch, tf.int64)),
            tf.float32)
        accuracy = tf.reduce_mean(correct_prediction)

        # Calculate the total losses
        regularization_losses = tf.get_collection(
            tf.GraphKeys.REGULARIZATION_LOSSES)
        # 这个total_loss是ce和regular loss的和,应该是已经把所有loss的值都加起来了
        total_loss = tf.add_n([cross_entropy_mean] + regularization_losses,
                              name='total_loss')

        # Build a Graph that trains the model with one batch of examples and updates the model parameters
        train_op = facenet.train(total_loss, global_step, args.optimizer,
                                 learning_rate, args.moving_average_decay,
                                 tf.global_variables(), args.log_histograms)

        # Create a saver
        saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=20)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.summary.merge_all()

        ### Start running operations on the Graph.
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                                log_device_placement=False))
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        summary_writer = tf.summary.FileWriter(log_dir, sess.graph)
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(coord=coord, sess=sess)

        # =========================================================================== #
        # Start training
        # =========================================================================== #
        with sess.as_default():

            if pretrained_model:
                print('Restoring pretrained model: %s' % pretrained_model)
                # saver.restore(sess, pretrained_model)
                if args.checkpoint_exclude_scopes:
                    exclusions = [args.checkpoint_exclude_scopes.split(',')]
                else:
                    exclusions = []
                # load from pre-trained model
                tf.contrib.framework.assign_from_checkpoint_fn(
                    pretrained_model, [
                        var for var in tf.trainable_variables()
                        if all(not var.op.name.startswith(exclusion)
                               for exclusion in exclusions)
                    ],
                    ignore_missing_vars=args.ignore_missing_vars)(sess)

            # Training and validation loop
            print('Running training')

            # max_nrof_epochs: number of epochs to run, default=500
            # epoch_size: number of batches per epoch,default=1000
            nrof_steps = args.max_nrof_epochs * args.epoch_size  # total batch number

            # 在验证集上验证的次数
            nrof_val_samples = int(
                math.ceil(args.max_nrof_epochs / args.validate_every_n_epochs)
            )  # Validate every validate_every_n_epochs as well as in the last epoch
            stat = {
                # 每一个batch记录一次
                'loss':
                np.zeros((nrof_steps, ), np.float32),
                'center_loss':
                np.zeros((nrof_steps, ), np.float32),
                'reg_loss':
                np.zeros((nrof_steps, ), np.float32),
                'xent_loss':
                np.zeros((nrof_steps, ), np.float32),
                'prelogits_norm':
                np.zeros((nrof_steps, ), np.float32),
                'accuracy':
                np.zeros((nrof_steps, ), np.float32),
                # 每验证一次记录一次
                'val_loss':
                np.zeros((nrof_val_samples, ), np.float32),
                'val_xent_loss':
                np.zeros((nrof_val_samples, ), np.float32),
                'val_accuracy':
                np.zeros((nrof_val_samples, ), np.float32),
                # 每个epoch记录一次
                'lfw_accuracy':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'lfw_valrate':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'learning_rate':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'time_train':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'time_validate':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'time_evaluate':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'prelogits_hist':
                np.zeros((args.max_nrof_epochs, 1000), np.float32),
            }
            for epoch in range(1, args.max_nrof_epochs + 1):
                step = sess.run(global_step, feed_dict=None)  # step = 0
                ### Train for one epoch
                t = time.time()

                # =========================================================================== #
                # 一次训练的过程,学习率是在这里设置的,如果参数中的学习率小于0,就用调度文件中的学习率
                # =========================================================================== #
                cont = train(
                    args, sess, epoch, image_list, label_list,
                    index_dequeue_op, enqueue_op, image_paths_placeholder,
                    labels_placeholder, learning_rate_placeholder,
                    phase_train_placeholder, batch_size_placeholder,
                    control_placeholder, global_step, total_loss, train_op,
                    summary_op, summary_writer, regularization_losses,
                    args.learning_rate_schedule_file, stat, cross_entropy_mean,
                    accuracy, learning_rate, prelogits, prelogits_center_loss,
                    args.random_rotate, args.random_crop, args.random_flip,
                    prelogits_norm, args.prelogits_hist_max,
                    args.use_fixed_image_standardization)
                stat['time_train'][epoch - 1] = time.time() - t

                if not cont:
                    # 每次结束一轮训练,train函数返回True
                    break

                ### validate on val_set
                t = time.time()
                if len(val_image_list) > 0 and \
                        ((
                                 epoch - 1) % args.validate_every_n_epochs == args.validate_every_n_epochs - 1 or epoch == args.max_nrof_epochs):
                    # =========================================================================== #
                    # 如果验证集不为空 并且 到了设置的每n个epoch验证一次或者到了最后一个epoch, 则进行一次验证
                    # =========================================================================== #
                    validate(args, sess, epoch, val_image_list, val_label_list,
                             enqueue_op, image_paths_placeholder,
                             labels_placeholder, control_placeholder,
                             phase_train_placeholder, batch_size_placeholder,
                             stat, total_loss, regularization_losses,
                             cross_entropy_mean, accuracy,
                             args.validate_every_n_epochs,
                             args.use_fixed_image_standardization, val_log_dir)
                stat['time_validate'][epoch - 1] = time.time() - t

                # Save variables and the metagraph if it doesn't exist
                save_variables_and_metagraph(sess, saver, summary_writer,
                                             model_dir, subdir, epoch)

                ### Evaluate on LFW
                t = time.time()
                if args.lfw_dir:
                    evaluate(sess, enqueue_op, image_paths_placeholder,
                             labels_placeholder, phase_train_placeholder,
                             batch_size_placeholder, control_placeholder,
                             args.lfw_nrof_folds, log_dir, step,
                             summary_writer, stat, epoch, embeddings,
                             label_batch, lfw_paths, actual_issame,
                             args.lfw_batch_size, args.lfw_distance_metric,
                             args.lfw_subtract_mean,
                             args.lfw_use_flipped_images,
                             args.use_fixed_image_standardization)
                stat['time_evaluate'][epoch - 1] = time.time() - t

                print('Saving statistics')
                with h5py.File(stat_file_name, 'w') as f:
                    for key, value in stat.items():
                        f.create_dataset(key, data=value)

    return model_dir
예제 #12
0
def main(args):
  
    network = importlib.import_module(args.model_def)
    image_size = (args.image_size, args.image_size)

    subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S')
    log_dir = os.path.join(os.path.expanduser(args.logs_base_dir), subdir)
    if not os.path.isdir(log_dir):  # Create the log directory if it doesn't exist
        os.makedirs(log_dir)
    model_dir = os.path.join(os.path.expanduser(args.models_base_dir), subdir)
    if not os.path.isdir(model_dir):  # Create the model directory if it doesn't exist
        os.makedirs(model_dir)

    stat_file_name = os.path.join(log_dir, 'stat.h5')

    # Write arguments to a text file
    facenet.write_arguments_to_file(args, os.path.join(log_dir, 'arguments.txt'))
        
    # Store some git revision info in a text file in the log directory
    src_path,_ = os.path.split(os.path.realpath(__file__))
    facenet.store_revision_info(src_path, log_dir, ' '.join(sys.argv))

    np.random.seed(seed=args.seed)
    random.seed(args.seed)
    dataset = facenet.get_dataset(args.data_dir)
    if args.filter_filename:
        dataset = filter_dataset(dataset, os.path.expanduser(args.filter_filename), 
            args.filter_percentile, args.filter_min_nrof_images_per_class)
        
    if args.validation_set_split_ratio>0.0:
        train_set, val_set = facenet.split_dataset(dataset, args.validation_set_split_ratio, args.min_nrof_val_images_per_class, 'SPLIT_IMAGES')
    else:
        train_set, val_set = dataset, []
        
    nrof_classes = len(train_set)
    
    print('Model directory: %s' % model_dir)
    print('Log directory: %s' % log_dir)
    pretrained_model = None
    if args.pretrained_model:
        pretrained_model = os.path.expanduser(args.pretrained_model)
        print('Pre-trained model: %s' % pretrained_model)
    
    if args.lfw_dir:
        print('LFW directory: %s' % args.lfw_dir)
        # 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
        lfw_paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs)
    
    with tf.Graph().as_default():
        tf.set_random_seed(args.seed)
        global_step = tf.Variable(0, trainable=False)
        
        # Get a list of image paths and their labels
        image_list, label_list = facenet.get_image_paths_and_labels(train_set)
        assert len(image_list)>0, 'The training set should not be empty'
        
        val_image_list, val_label_list = facenet.get_image_paths_and_labels(val_set)

        # Create a queue that produces indices into the image_list and label_list 
        labels = ops.convert_to_tensor(label_list, dtype=tf.int32)
        range_size = array_ops.shape(labels)[0]
        index_queue = tf.train.range_input_producer(range_size, num_epochs=None,
                             shuffle=True, seed=None, capacity=32)
        
        index_dequeue_op = index_queue.dequeue_many(args.batch_size*args.epoch_size, 'index_dequeue')
        
        learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate')
        batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
        phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
        image_paths_placeholder = tf.placeholder(tf.string, shape=(None,1), name='image_paths')
        labels_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='labels')
        control_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='control')
        
        nrof_preprocess_threads = 4
        input_queue = data_flow_ops.FIFOQueue(capacity=2000000,
                                    dtypes=[tf.string, tf.int32, tf.int32],
                                    shapes=[(1,), (1,), (1,)],
                                    shared_name=None, name=None)
        enqueue_op = input_queue.enqueue_many([image_paths_placeholder, labels_placeholder, control_placeholder], name='enqueue_op')
        image_batch, label_batch = facenet.create_input_pipeline(input_queue, image_size, nrof_preprocess_threads, batch_size_placeholder)

        image_batch = tf.identity(image_batch, 'image_batch')
        image_batch = tf.identity(image_batch, 'input')
        label_batch = tf.identity(label_batch, 'label_batch')
        
        print('Number of classes in training set: %d' % nrof_classes)
        print('Number of examples in training set: %d' % len(image_list))

        print('Number of classes in validation set: %d' % len(val_set))
        print('Number of examples in validation set: %d' % len(val_image_list))
        
        print('Building training graph')
        
        # Build the inference graph
        prelogits, _ = network.inference(image_batch, args.keep_probability, 
            phase_train=phase_train_placeholder, bottleneck_layer_size=args.embedding_size, 
            weight_decay=args.weight_decay)
        logits = slim.fully_connected(prelogits, len(train_set), activation_fn=None, 
                weights_initializer=slim.initializers.xavier_initializer(), 
                weights_regularizer=slim.l2_regularizer(args.weight_decay),
                scope='Logits', reuse=False)

        embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings')

        # Norm for the prelogits
        eps = 1e-4
        prelogits_norm = tf.reduce_mean(tf.norm(tf.abs(prelogits)+eps, ord=args.prelogits_norm_p, axis=1))
        tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, prelogits_norm * args.prelogits_norm_loss_factor)

        # Add center loss
        prelogits_center_loss, _ = facenet.center_loss(prelogits, label_batch, args.center_loss_alfa, nrof_classes)
        tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, prelogits_center_loss * args.center_loss_factor)

        learning_rate = tf.train.exponential_decay(learning_rate_placeholder, global_step,
            args.learning_rate_decay_epochs*args.epoch_size, args.learning_rate_decay_factor, staircase=True)
        tf.summary.scalar('learning_rate', learning_rate)

        # Calculate the average cross entropy loss across the batch
        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=label_batch, logits=logits, name='cross_entropy_per_example')
        cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
        tf.add_to_collection('losses', cross_entropy_mean)
        
        correct_prediction = tf.cast(tf.equal(tf.argmax(logits, 1), tf.cast(label_batch, tf.int64)), tf.float32)
        accuracy = tf.reduce_mean(correct_prediction)
        
        # Calculate the total losses
        regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
        total_loss = tf.add_n([cross_entropy_mean] + regularization_losses, name='total_loss')

        # Build a Graph that trains the model with one batch of examples and updates the model parameters
        train_op = facenet.train(total_loss, global_step, args.optimizer, 
            learning_rate, args.moving_average_decay, tf.global_variables(), args.log_histograms)
        
        # Create a saver
        saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.summary.merge_all()

        # Start running operations on the Graph.
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        summary_writer = tf.summary.FileWriter(log_dir, sess.graph)
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(coord=coord, sess=sess)

        with sess.as_default():

            if pretrained_model:
                print('Restoring pretrained model: %s' % pretrained_model)
                saver.restore(sess, pretrained_model)

            # Training and validation loop
            print('Running training')
            nrof_steps = args.max_nrof_epochs*args.epoch_size
            nrof_val_samples = int(math.ceil(args.max_nrof_epochs / args.validate_every_n_epochs))   # Validate every validate_every_n_epochs as well as in the last epoch
            stat = {
                'loss': np.zeros((nrof_steps,), np.float32),
                'center_loss': np.zeros((nrof_steps,), np.float32),
                'reg_loss': np.zeros((nrof_steps,), np.float32),
                'xent_loss': np.zeros((nrof_steps,), np.float32),
                'prelogits_norm': np.zeros((nrof_steps,), np.float32),
                'accuracy': np.zeros((nrof_steps,), np.float32),
                'val_loss': np.zeros((nrof_val_samples,), np.float32),
                'val_xent_loss': np.zeros((nrof_val_samples,), np.float32),
                'val_accuracy': np.zeros((nrof_val_samples,), np.float32),
                'lfw_accuracy': np.zeros((args.max_nrof_epochs,), np.float32),
                'lfw_valrate': np.zeros((args.max_nrof_epochs,), np.float32),
                'learning_rate': np.zeros((args.max_nrof_epochs,), np.float32),
                'time_train': np.zeros((args.max_nrof_epochs,), np.float32),
                'time_validate': np.zeros((args.max_nrof_epochs,), np.float32),
                'time_evaluate': np.zeros((args.max_nrof_epochs,), np.float32),
                'prelogits_hist': np.zeros((args.max_nrof_epochs, 1000), np.float32),
              }
            for epoch in range(1,args.max_nrof_epochs+1):
                step = sess.run(global_step, feed_dict=None)
                # Train for one epoch
                t = time.time()
                cont = train(args, sess, epoch, image_list, label_list, index_dequeue_op, enqueue_op, image_paths_placeholder, labels_placeholder,
                    learning_rate_placeholder, phase_train_placeholder, batch_size_placeholder, control_placeholder, global_step, 
                    total_loss, train_op, summary_op, summary_writer, regularization_losses, args.learning_rate_schedule_file,
                    stat, cross_entropy_mean, accuracy, learning_rate,
                    prelogits, prelogits_center_loss, args.random_rotate, args.random_crop, args.random_flip, prelogits_norm, args.prelogits_hist_max, args.use_fixed_image_standardization)
                stat['time_train'][epoch-1] = time.time() - t
                
                if not cont:
                    break
                  
                t = time.time()
                if len(val_image_list)>0 and ((epoch-1) % args.validate_every_n_epochs == args.validate_every_n_epochs-1 or epoch==args.max_nrof_epochs):
                    validate(args, sess, epoch, val_image_list, val_label_list, enqueue_op, image_paths_placeholder, labels_placeholder, control_placeholder,
                        phase_train_placeholder, batch_size_placeholder, 
                        stat, total_loss, regularization_losses, cross_entropy_mean, accuracy, args.validate_every_n_epochs, args.use_fixed_image_standardization)
                stat['time_validate'][epoch-1] = time.time() - t

                # Save variables and the metagraph if it doesn't exist already
                save_variables_and_metagraph(sess, saver, summary_writer, model_dir, subdir, epoch)

                # Evaluate on LFW
                t = time.time()
                if args.lfw_dir:
                    evaluate(sess, enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, control_placeholder, 
                        embeddings, label_batch, lfw_paths, actual_issame, args.lfw_batch_size, args.lfw_nrof_folds, log_dir, step, summary_writer, stat, epoch, 
                        args.lfw_distance_metric, args.lfw_subtract_mean, args.lfw_use_flipped_images, args.use_fixed_image_standardization)
                stat['time_evaluate'][epoch-1] = time.time() - t

                print('Saving statistics')
                with h5py.File(stat_file_name, 'w') as f:
                    for key, value in stat.iteritems():
                        f.create_dataset(key, data=value)
    
    return model_dir
예제 #13
0
def main(args):

    with tf.Graph().as_default():

        with tf.Session() as sess:

            image_dir1 = os.path.expanduser(args.first_dir)
            image_dir2 = os.path.expanduser(args.second_dir)

            image_paths_placeholder = tf.placeholder(tf.string,
                                                     shape=(None, 1),
                                                     name='image_paths')
            labels_placeholder = tf.placeholder(tf.int32,
                                                shape=(None, 1),
                                                name='labels')
            batch_size_placeholder = tf.placeholder(tf.int32,
                                                    name='batch_size')
            control_placeholder = tf.placeholder(tf.int32,
                                                 shape=(None, 1),
                                                 name='control')
            phase_train_placeholder = tf.placeholder(tf.bool,
                                                     name='phase_train')

            nrof_preprocess_threads = 4
            image_size = (args.image_size, args.image_size)

            eval_input_queue = data_flow_ops.FIFOQueue(
                capacity=2000000,
                dtypes=[tf.string, tf.int32, tf.int32],
                shapes=[(1, ), (1, ), (1, )],
                shared_name=None,
                name=None)
            eval_enqueue_op = eval_input_queue.enqueue_many(
                [
                    image_paths_placeholder, labels_placeholder,
                    control_placeholder
                ],
                name='eval_enqueue_op')
            image_batch, label_batch = facenet.create_input_pipeline(
                eval_input_queue, image_size, nrof_preprocess_threads,
                batch_size_placeholder)

            # Load the model
            input_map = {
                'image_batch': image_batch,
                'label_batch': label_batch,
                'phase_train': phase_train_placeholder
            }
            print('=== model: ')
            print(args.model)
            print('===input_map: ')
            print(input_map)
            facenet.load_model(args.model, input_map=input_map)

            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            # Get output tensor
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            embedding_size = embeddings.get_shape()[1]

            print('facenet embedding模型建立完毕')

            for num in range(0, args.image_num):

                scaled_reshape = []

                image_name1 = "%s/%d.jpg" % (image_dir1, num)
                image_name2 = "%s/%d.jpg" % (image_dir2, num)

                image1 = scipy.misc.imread(image_name1, mode='RGB')
                image1 = cv2.resize(image1, (args.image_size, args.image_size),
                                    interpolation=cv2.INTER_CUBIC)
                image1 = facenet.prewhiten(image1)
                scaled_reshape.append(
                    image1.reshape(-1, args.image_size, args.image_size, 3))
                emb_array1 = np.zeros((1, embedding_size))
                emb_array1[0, :] = sess.run(embeddings,
                                            feed_dict={
                                                images_placeholder:
                                                scaled_reshape[0],
                                                phase_train_placeholder:
                                                False
                                            })[0]

                image2 = scipy.misc.imread(image_name2, mode='RGB')
                image2 = cv2.resize(image2, (args.image_size, args.image_size),
                                    interpolation=cv2.INTER_CUBIC)
                image2 = facenet.prewhiten(image2)
                scaled_reshape.append(
                    image2.reshape(-1, args.image_size, args.image_size, 3))
                emb_array2 = np.zeros((1, embedding_size))
                emb_array2[0, :] = sess.run(embeddings,
                                            feed_dict={
                                                images_placeholder:
                                                scaled_reshape[1],
                                                phase_train_placeholder:
                                                False
                                            })[0]

                dist = np.sqrt(np.sum(np.square(emb_array1[0] -
                                                emb_array2[0])))
                print("第%d组照片特征向量的欧氏距离:%f " % (num, dist))
                if dist < 1.028:
                    print(" 第%d组照片是同一个人 " % num)
                else:
                    print(" 第%d组照片不是同一个人 " % num)
예제 #14
0
def generate_data(feature_size, max_length, batch_size, MAT_folder, img_folder):
    
    # load mat files
    Mat_paths = os.listdir(MAT_folder)
    choose_idx = np.random.randint(len(Mat_paths), size=batch_size)
    Mat_files = []
    seq_names = []
    for n in range(batch_size):
        seq_name = Mat_paths[choose_idx[n]][0:21]+'_sync'
        temp_path = data_folder+'/'+seq_name+'/gt_2.mat'
        temp_mat_file = loadmat(temp_path)
        Mat_files.append(temp_mat_file)
        seq_names.append(seq_name)
    
    X = np.zeros((batch_size,feature_size,max_length,3))
    Y = np.zeros((batch_size,2))
    all_paths = []
    
    
    # positive 
    for n in range(int(batch_size/2)):
        seq_name = seq_names[n]
        fr_num = Mat_files[n]['gtInfo'][0][0][0].shape[0]
        id_num = Mat_files[n]['gtInfo'][0][0][0].shape[1]
        Y[n,0] = 1
        
        X_3d = Mat_files[n]['gtInfo'][0][0][4]
        Y_3d = Mat_files[n]['gtInfo'][0][0][5]
        W_3d = Mat_files[n]['gtInfo'][0][0][6]
        H_3d = Mat_files[n]['gtInfo'][0][0][7]
        try_time = 0
        if try_time>=10:
            continue
        while 1:
            if try_time>=10:
                all_paths.append([])
                #print('err')
                break
            obj_id = np.random.randint(id_num, size=1)[0]
            part_W_mat = Mat_files[n]['gtInfo'][0][0][3][:,obj_id]
            
            non_zero_idx = np.where(part_W_mat>0)[0]
            if np.max(non_zero_idx)-np.min(non_zero_idx)+1!=len(non_zero_idx) or len(non_zero_idx)<=1:
                try_time = try_time+1
                continue
            st_fr = np.min(non_zero_idx)#+np.random.randint(len(non_zero_idx)-1, size=1)[0]
            end_fr = np.max(non_zero_idx)
            abs_fr_t1 = int(st_fr+np.random.randint(len(non_zero_idx)-1, size=1)[0])
            abs_end_fr = min(abs_fr_t1+max_length-1,end_fr)
            abs_fr_t4 = int(abs_end_fr-np.random.randint(abs_end_fr-abs_fr_t1, size=1)[0])
            abs_fr_t2 = int(abs_fr_t1+np.random.randint(abs_fr_t4-abs_fr_t1, size=1)[0])
            abs_fr_t3 = int(abs_fr_t4-np.random.randint(abs_fr_t4-abs_fr_t2, size=1)[0])
            
            t1 = 0
            t2 = abs_fr_t2-abs_fr_t1
            t3 = abs_fr_t3-abs_fr_t1
            t4 = abs_fr_t4-abs_fr_t1
            
            # mask
            X[n,:,t1:t2+1,1] = 1
            X[n,:,t3:t4+1,2] = 1
            
            # X
            X[n,0,t1:t2+1,0] = X_3d[abs_fr_t1:abs_fr_t2+1,obj_id]/loc_scales[0]+noise_scales[0]*np.random.normal(0,1,t2-t1+1)
            X[n,0,t3:t4+1,0] = X_3d[abs_fr_t3:abs_fr_t4+1,obj_id]/loc_scales[0]+noise_scales[0]*np.random.normal(0,1,t4-t3+1)
            
            # Y
            X[n,1,t1:t2+1,0] = Y_3d[abs_fr_t1:abs_fr_t2+1,obj_id]/loc_scales[1]+noise_scales[1]*np.random.normal(0,1,t2-t1+1)
            X[n,1,t3:t4+1,0] = Y_3d[abs_fr_t3:abs_fr_t4+1,obj_id]/loc_scales[1]+noise_scales[1]*np.random.normal(0,1,t4-t3+1)
            
            # W
            X[n,2,t1:t2+1,0] = W_3d[abs_fr_t1:abs_fr_t2+1,obj_id]/loc_scales[2]+noise_scales[2]*np.random.normal(0,1,t2-t1+1)
            X[n,2,t3:t4+1,0] = W_3d[abs_fr_t3:abs_fr_t4+1,obj_id]/loc_scales[2]+noise_scales[2]*np.random.normal(0,1,t4-t3+1)
            
            # H
            X[n,3,t1:t2+1,0] = H_3d[abs_fr_t1:abs_fr_t2+1,obj_id]/loc_scales[3]+noise_scales[3]*np.random.normal(0,1,t2-t1+1)
            X[n,3,t3:t4+1,0] = H_3d[abs_fr_t3:abs_fr_t4+1,obj_id]/loc_scales[3]+noise_scales[3]*np.random.normal(0,1,t4-t3+1)
            '''
            plt.plot(X[n,0,:,0], 'ro')
            plt.show()
            plt.plot(X[n,1,:,0], 'ro')
            plt.show()
            plt.plot(X[n,2,:,0], 'ro')
            plt.show()
            plt.plot(X[n,3,:,0], 'ro')
            plt.show()
            plt.plot(X[n,0,:,1], 'ro')
            plt.show()
            plt.plot(X[n,0,:,2], 'ro')
            plt.show()
            import pdb; pdb.set_trace()
            '''
            temp_paths = []
            for k in range(abs_fr_t1,abs_fr_t2+1):
                class_name = seq_name+'_image_02_'+num_str(obj_id+1,4)
                file_name = class_name+'_'+num_str(k+1,4)+'.png'
                temp_path = img_folder+'/'+class_name+'/'+file_name
                temp_paths.append(temp_path)
            for k in range(abs_fr_t3,abs_fr_t4+1):
                class_name = seq_name+'_image_02_'+num_str(obj_id+1,4)
                file_name = class_name+'_'+num_str(k+1,4)+'.png'
                temp_path = img_folder+'/'+class_name+'/'+file_name
                temp_paths.append(temp_path)
            all_paths.append(temp_paths.copy())
            break
            
        
    # negative
    for n in range(int(batch_size/2),batch_size):
        Y[n,1] = 1
        seq_name = seq_names[n]
        fr_num = Mat_files[n]['gtInfo'][0][0][0].shape[0]
        id_num = Mat_files[n]['gtInfo'][0][0][0].shape[1]
        
        X_3d = Mat_files[n]['gtInfo'][0][0][4]
        Y_3d = Mat_files[n]['gtInfo'][0][0][5]
        W_3d = Mat_files[n]['gtInfo'][0][0][6]
        H_3d = Mat_files[n]['gtInfo'][0][0][7]
        try_time = 0
        
        time_interval = np.zeros((id_num,2))
        for obj_id in range(id_num):
            part_W_mat = Mat_files[n]['gtInfo'][0][0][3][:,obj_id]
            non_zero_idx = np.where(part_W_mat>0)[0]
            t_min = np.min(non_zero_idx)
            t_max = np.max(non_zero_idx)
            if len(non_zero_idx)!=t_max-t_min+1:
                time_interval[obj_id,0] = -1
                time_interval[obj_id,1] = -1
            else:
                time_interval[obj_id,0] = t_min
                time_interval[obj_id,1] = t_max
                
        
        if try_time>=10:
            continue
        while 1:
            if try_time>=10:
                all_paths.append([])
                break
            split_fr = 1+np.random.randint(fr_num-2, size=1)[0]
            
            cand_pairs = []
            for id1 in range(id_num):
                for id2 in range(id_num):
                    if id1==id2:
                        continue
                    if time_interval[id1,0]==-1 or time_interval[id2,0]==-1:
                        continue
                    if time_interval[id1,0]<=split_fr and time_interval[id2,1]>split_fr:
                        t_above = min(split_fr,time_interval[id1,1])
                        t_below = max(split_fr+1,time_interval[id2,0])
                        t_dist = t_below-t_above
                        if t_dist<max_length/4:
                            cand_pairs.append([id1,id2,t_dist])
            if len(cand_pairs)==0:
                try_time = try_time+1
                continue
            choose_pair_idx = np.random.randint(len(cand_pairs), size=1)[0]
            obj_id1 = cand_pairs[choose_pair_idx][0]
            obj_id2 = cand_pairs[choose_pair_idx][1]
            t_below = max(split_fr+1,time_interval[obj_id2,0])
            t_above = min(split_fr,time_interval[obj_id1,1])
            t_min = max(t_below-max_length+1,time_interval[obj_id1,0])
            abs_fr_t1 = int(t_min+np.random.randint(t_above-t_min+1, size=1)[0])
            #abs_fr_t2 = int(abs_fr_t1+np.random.randint(t_above-abs_fr_t1+1, size=1)[0])
            abs_fr_t2 = int(t_above)
            abs_fr_t4 = min(abs_fr_t1+max_length-1,time_interval[obj_id2,1])
            abs_fr_t4 = int(abs_fr_t4-np.random.randint(abs_fr_t4-t_below+1, size=1)[0])
            #abs_fr_t3 = int(abs_fr_t4-np.random.randint(abs_fr_t4-t_below+1, size=1)[0])
            abs_fr_t3 = int(t_below)
            '''
            print(abs_fr_t1)
            print(abs_fr_t2)
            print(abs_fr_t3)
            print(abs_fr_t4)
            #import pdb; pdb.set_trace()
            '''
            t1 = 0
            t2 = abs_fr_t2-abs_fr_t1
            t3 = abs_fr_t3-abs_fr_t1
            t4 = abs_fr_t4-abs_fr_t1
            
            # mask
            X[n,:,t1:t2+1,1] = 1
            X[n,:,t3:t4+1,2] = 1
            
            # X
            X[n,0,t1:t2+1,0] = X_3d[abs_fr_t1:abs_fr_t2+1,obj_id1]/loc_scales[0]+noise_scales[0]*np.random.normal(0,1,t2-t1+1)
            X[n,0,t3:t4+1,0] = X_3d[abs_fr_t3:abs_fr_t4+1,obj_id2]/loc_scales[0]+noise_scales[0]*np.random.normal(0,1,t4-t3+1)
            
            # Y
            X[n,1,t1:t2+1,0] = Y_3d[abs_fr_t1:abs_fr_t2+1,obj_id1]/loc_scales[1]+noise_scales[1]*np.random.normal(0,1,t2-t1+1)
            X[n,1,t3:t4+1,0] = Y_3d[abs_fr_t3:abs_fr_t4+1,obj_id2]/loc_scales[1]+noise_scales[1]*np.random.normal(0,1,t4-t3+1)
            
            # W
            X[n,2,t1:t2+1,0] = W_3d[abs_fr_t1:abs_fr_t2+1,obj_id1]/loc_scales[2]+noise_scales[2]*np.random.normal(0,1,t2-t1+1)
            X[n,2,t3:t4+1,0] = W_3d[abs_fr_t3:abs_fr_t4+1,obj_id2]/loc_scales[2]+noise_scales[2]*np.random.normal(0,1,t4-t3+1)
            
            # H
            X[n,3,t1:t2+1,0] = H_3d[abs_fr_t1:abs_fr_t2+1,obj_id1]/loc_scales[3]+noise_scales[3]*np.random.normal(0,1,t2-t1+1)
            X[n,3,t3:t4+1,0] = H_3d[abs_fr_t3:abs_fr_t4+1,obj_id2]/loc_scales[3]+noise_scales[3]*np.random.normal(0,1,t4-t3+1)
            '''
            plt.plot(X[n,0,:,0], 'ro')
            plt.show()
            plt.plot(X[n,1,:,0], 'ro')
            plt.show()
            plt.plot(X[n,2,:,0], 'ro')
            plt.show()
            plt.plot(X[n,3,:,0], 'ro')
            plt.show()
            plt.plot(X[n,0,:,1], 'ro')
            plt.show()
            plt.plot(X[n,0,:,2], 'ro')
            plt.show()
            import pdb; pdb.set_trace()
            '''
            temp_paths = []
            for k in range(abs_fr_t1,abs_fr_t2+1):
                class_name = seq_name+'_image_02_'+num_str(obj_id1+1,4)
                file_name = class_name+'_'+num_str(k+1,4)+'.png'
                temp_path = img_folder+'/'+class_name+'/'+file_name
                temp_paths.append(temp_path)
            for k in range(abs_fr_t3,abs_fr_t4+1):
                class_name = seq_name+'_image_02_'+num_str(obj_id2+1,4)
                file_name = class_name+'_'+num_str(k+1,4)+'.png'
                temp_path = img_folder+'/'+class_name+'/'+file_name
                temp_paths.append(temp_path)
            all_paths.append(temp_paths.copy())
            break
            

    f_image_size = 160
    distance_metric = 0
    with tf.Graph().as_default():
      
        with tf.Session() as sess:
            
            

            #import pdb; pdb.set_trace()
            
            image_paths_placeholder = tf.placeholder(tf.string, shape=(None,1), name='image_paths')
            labels_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='labels')
            batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
            control_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='control')
            phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
 
            nrof_preprocess_threads = 4
            image_size = (f_image_size, f_image_size)
            eval_input_queue = data_flow_ops.FIFOQueue(capacity=2000000,
                                        dtypes=[tf.string, tf.int32, tf.int32],
                                        shapes=[(1,), (1,), (1,)],
                                        shared_name=None, name=None)
            eval_enqueue_op = eval_input_queue.enqueue_many([image_paths_placeholder, labels_placeholder, control_placeholder], name='eval_enqueue_op')
            image_batch, label_batch = facenet.create_input_pipeline(eval_input_queue, image_size, nrof_preprocess_threads, batch_size_placeholder)
     
            # Load the model
            input_map = {'image_batch': image_batch, 'label_batch': label_batch, 'phase_train': phase_train_placeholder}
            facenet.load_model(triplet_model, input_map=input_map)

            # Get output tensor
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
#              
            coord = tf.train.Coordinator()
            tf.train.start_queue_runners(coord=coord, sess=sess)
            
            for n in range(len(all_paths)):
                #print(n)
                lfw_batch_size = len(all_paths[n])
                if lfw_batch_size==0:
                    continue
                emb_array = evaluate(sess, eval_enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder,
                         batch_size_placeholder, control_placeholder, embeddings, label_batch, all_paths[n], lfw_batch_size, distance_metric)
                
                if X[n,4:,X[n,0,:,1]+X[n,0,:,2]>0.5,0].shape[0]!=emb_array.shape[0]:
                    aa = 0
                    import pdb; pdb.set_trace()
                    
                #import pdb; pdb.set_trace()
                X[n,4:,X[n,0,:,1]+X[n,0,:,2]>0.5,0] = emb_array
              
    #import pdb; pdb.set_trace()
    return X, Y
예제 #15
0
def main(_):

    if FLAGS.csv_file_path:
        if os.path.exists(FLAGS.csv_file_path) is False:
            csv_dir = FLAGS.csv_file_path.rsplit('/', 1)[0]
            if os.path.exists(csv_dir) is False:
                os.makedirs(csv_dir)

            if FLAGS.task_name == 'chalearn/age':
                with open(FLAGS.csv_file_path, 'w') as f:
                    writer = csv.writer(f)
                    writer.writerow([
                        'Pruned rate', 'MAE', 'Acc', 'Epoch No.',
                        'Model size through inference (MB) (Shared part + task-specific part)',
                        'Shared part (MB)', 'Task specific part (MB)',
                        'Whole masks (MB)', 'Task specific masks (MB)',
                        'Task specific batch norm vars (MB)',
                        'Task specific biases (MB)'
                    ])
            else:
                with open(FLAGS.csv_file_path, 'w') as f:
                    writer = csv.writer(f)
                    writer.writerow([
                        'Pruned rate', 'Acc', 'Epoch No.',
                        'Model size through inference (MB) (Shared part + task-specific part)',
                        'Shared part (MB)', 'Task specific part (MB)',
                        'Whole masks (MB)', 'Task specific masks (MB)',
                        'Task specific batch norm vars (MB)',
                        'Task specific biases (MB)'
                    ])

    args, unparsed = parse_arguments(sys.argv[1:])
    FLAGS.filters_expand_ratio = math.sqrt(FLAGS.filters_expand_ratio)
    FLAGS.history_filters_expand_ratios = [
        math.sqrt(float(ratio))
        for ratio in FLAGS.history_filters_expand_ratios
    ]

    with tf.Graph().as_default():

        with tf.Session() as sess:
            if 'emotion' in FLAGS.task_name or 'chalearn' in FLAGS.task_name:
                test_data_path = os.path.join(args.data_dir, 'val')
            else:
                test_data_path = os.path.join(args.data_dir, 'test')

            test_set = utils.get_dataset(test_data_path)

            # Get the paths for the corresponding images
            image_list, label_list = facenet.get_image_paths_and_labels(
                test_set)

            image_paths_placeholder = tf.placeholder(tf.string,
                                                     shape=(None, 1),
                                                     name='image_paths')
            labels_placeholder = tf.placeholder(tf.int32,
                                                shape=(None, 1),
                                                name='labels')
            batch_size_placeholder = tf.placeholder(tf.int32,
                                                    name='batch_size')
            control_placeholder = tf.placeholder(tf.int32,
                                                 shape=(None, 1),
                                                 name='control')
            phase_train_placeholder = tf.placeholder(tf.bool,
                                                     name='phase_train')

            nrof_preprocess_threads = 4
            image_size = (args.image_size, args.image_size)
            eval_input_queue = data_flow_ops.FIFOQueue(
                capacity=2000000,
                dtypes=[tf.string, tf.int32, tf.int32],
                shapes=[(1, ), (1, ), (1, )],
                shared_name=None,
                name=None)
            eval_enqueue_op = eval_input_queue.enqueue_many(
                [
                    image_paths_placeholder, labels_placeholder,
                    control_placeholder
                ],
                name='eval_enqueue_op')
            image_batch, label_batch = facenet.create_input_pipeline(
                eval_input_queue, image_size, nrof_preprocess_threads,
                batch_size_placeholder)
            coord = tf.train.Coordinator()
            tf.train.start_queue_runners(coord=coord, sess=sess)

            # Load the model
            if os.path.isdir(args.model):
                temp_record_file = os.path.join(args.model, 'temp_record.txt')
                checkpoint_file = os.path.join(args.model, 'checkpoint')

                if os.path.exists(temp_record_file) and os.path.exists(
                        checkpoint_file):
                    with open(temp_record_file) as json_file:
                        data = json.load(json_file)
                        max_acc = max(data, key=float)
                        epoch_no = data[max_acc]
                        ckpt_file = args.model + '/model-.ckpt-' + epoch_no

                    with open(checkpoint_file) as f:
                        context = f.read()
                    original_epoch = re.search("(\d)+", context).group()
                    context = context.replace(original_epoch, epoch_no)
                    with open(checkpoint_file, 'w') as f:
                        f.write(context)
                    if os.path.exists(os.path.join(args.model,
                                                   'copied')) is False:
                        os.makedirs(os.path.join(args.model, 'copied'))
                    copyfile(
                        temp_record_file,
                        os.path.join(args.model, 'copied', 'temp_record.txt'))
                    os.remove(temp_record_file)

                elif os.path.exists(checkpoint_file):
                    ckpt = tf.train.get_checkpoint_state(args.model)
                    ckpt_file = ckpt.model_checkpoint_path
                    epoch_no = ckpt_file.rsplit('-', 1)[-1]
                else:
                    print(
                        'No `temp_record.txt` or `checkpoint` in `{}`, you should pass args.model the file path, not the directory'
                        .format(args.model))
                    sys.exit(1)
            else:
                ckpt_file = args.model
                epoch_no = ckpt_file.rsplit('-')[-1]

            prelogits, _ = network.inference(
                image_batch,
                1.0,
                phase_train=phase_train_placeholder,
                bottleneck_layer_size=args.embedding_size,
                weight_decay=0.0)

            with tf.variable_scope('task_{}'.format(FLAGS.task_id)):
                if FLAGS.task_name == 'chalearn/age':
                    logits = slim.fully_connected(prelogits,
                                                  100,
                                                  activation_fn=None,
                                                  scope='Logits',
                                                  reuse=False)
                else:
                    logits = slim.fully_connected(prelogits,
                                                  len(test_set),
                                                  activation_fn=None,
                                                  scope='Logits',
                                                  reuse=False)

            # Get output tensor
            if FLAGS.task_name == 'chalearn/age':
                softmax = tf.nn.softmax(logits=logits)
                labels_range = tf.range(1.0, 101.0)  # [1.0, ..., 100.0]
                labels_matrix = tf.broadcast_to(
                    labels_range,
                    [args.test_batch_size, labels_range.shape[0]])
                result_vector = tf.reduce_sum(softmax * labels_matrix, axis=1)
                MAE_error_vector = tf.abs(result_vector -
                                          tf.cast(label_batch, tf.float32))
                MAE_avg_error = tf.reduce_mean(MAE_error_vector)

                correct_prediction = tf.cast(
                    tf.equal(tf.argmax(logits, 1),
                             tf.cast(label_batch, tf.int64)), tf.float32)
                accuracy = tf.reduce_mean(correct_prediction)
                regularization_losses = tf.get_collection(
                    tf.GraphKeys.REGULARIZATION_LOSSES)
                total_loss = tf.add_n([MAE_avg_error] + regularization_losses)

                criterion = MAE_avg_error
            else:
                cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
                    labels=label_batch,
                    logits=logits,
                    name='cross_entropy_per_example')
                cross_entropy_mean = tf.reduce_mean(cross_entropy)

                correct_prediction = tf.cast(
                    tf.equal(tf.argmax(logits, 1),
                             tf.cast(label_batch, tf.int64)), tf.float32)
                accuracy = tf.reduce_mean(correct_prediction)
                regularization_losses = tf.get_collection(
                    tf.GraphKeys.REGULARIZATION_LOSSES)
                total_loss = tf.add_n([cross_entropy_mean] +
                                      regularization_losses)

                criterion = cross_entropy_mean

            init_fn = slim.assign_from_checkpoint_fn(ckpt_file,
                                                     tf.global_variables())
            init_fn(sess)

            pruned_ratio_relative_to_curr_task = 0.0
            model_size = 0.0
            if FLAGS.print_mem or FLAGS.print_mask_info:
                masks = tf.get_collection('masks')

                if FLAGS.print_mask_info:

                    if masks:
                        num_elems_in_each_task_op = {}
                        num_elems_in_tasks_in_masks_op = {
                        }  # two dimentional dictionary
                        num_elems_in_masks_op = []
                        num_remain_elems_in_masks_op = []

                        for task_id in range(1, FLAGS.task_id + 1):
                            num_elems_in_each_task_op[task_id] = tf.constant(
                                0, dtype=tf.int32)
                            num_elems_in_tasks_in_masks_op[task_id] = {}

                        # Define graph
                        for i, mask in enumerate(masks):
                            num_elems_in_masks_op.append(tf.size(mask))
                            num_remain_elems_in_curr_mask = tf.size(mask)
                            for task_id in range(1, FLAGS.task_id + 1):
                                cnt = tf_count(mask, task_id)
                                num_elems_in_tasks_in_masks_op[task_id][
                                    i] = cnt
                                num_elems_in_each_task_op[task_id] = tf.add(
                                    num_elems_in_each_task_op[task_id], cnt)
                                num_remain_elems_in_curr_mask -= cnt

                            num_remain_elems_in_masks_op.append(
                                num_remain_elems_in_curr_mask)

                        num_elems_in_network_op = tf.add_n(
                            num_elems_in_masks_op)

                        print('Calculate pruning status ...')

                        # Doing operation
                        num_elems_in_masks = sess.run(num_elems_in_masks_op)
                        num_elems_in_each_task = sess.run(
                            num_elems_in_each_task_op)
                        num_elems_in_tasks_in_masks = sess.run(
                            num_elems_in_tasks_in_masks_op)
                        num_elems_in_network = sess.run(
                            num_elems_in_network_op)
                        num_remain_elems_in_masks = sess.run(
                            num_remain_elems_in_masks_op)

                        # Print out the result
                        print('Showing pruning status ...')

                        if FLAGS.verbose:
                            for i, mask in enumerate(masks):
                                print('Layer %s: ' % mask.op.name, end='')
                                for task_id in range(1, FLAGS.task_id + 1):
                                    cnt = num_elems_in_tasks_in_masks[task_id][
                                        i]
                                    print('task_%d -> %d/%d (%.2f%%), ' %
                                          (task_id, cnt, num_elems_in_masks[i],
                                           100 * cnt / num_elems_in_masks[i]),
                                          end='')
                                print('remain -> {:.2f}%'.format(
                                    100 * num_remain_elems_in_masks[i] /
                                    num_elems_in_masks[i]))

                        print('Num elems in network: {}'.format(
                            num_elems_in_network))
                        num_elems_of_usued_weights = num_elems_in_network
                        for task_id in range(1, FLAGS.task_id + 1):
                            print('Num elems in task_{}: {}'.format(
                                task_id, num_elems_in_each_task[task_id]))
                            print('Ratio of task_{} to all: {}'.format(
                                task_id, num_elems_in_each_task[task_id] /
                                num_elems_in_network))
                            num_elems_of_usued_weights -= num_elems_in_each_task[
                                task_id]
                        print('Num usued elems in all masks: {}'.format(
                            num_elems_of_usued_weights))

                        pruned_ratio_relative_to_all_elems = num_elems_of_usued_weights / num_elems_in_network
                        print('Ratio of usused_elem to all: {}'.format(
                            pruned_ratio_relative_to_all_elems))
                        pruned_ratio_relative_to_curr_task = num_elems_of_usued_weights / (
                            num_elems_of_usued_weights +
                            num_elems_in_each_task[FLAGS.task_id])
                        print('Pruning degree relative to task_{}: {:.3f}'.
                              format(FLAGS.task_id,
                                     pruned_ratio_relative_to_curr_task))

                if FLAGS.print_mem:
                    # Analyze param
                    start_time = time.time()
                    (MB_of_model_through_inference, MB_of_shared_variables,
                     MB_of_task_specific_variables, MB_of_whole_masks,
                     MB_of_task_specific_masks,
                     MB_of_task_specific_batch_norm_variables,
                     MB_of_task_specific_biases
                     ) = model_analyzer.analyze_vars_for_current_task(
                         tf.model_variables(),
                         sess=sess,
                         task_id=FLAGS.task_id,
                         verbose=False)
                    duration = time.time() - start_time
                    print('duration time: {}'.format(duration))
            if FLAGS.eval_once:
                validate(
                    args, sess, image_list, label_list, eval_enqueue_op,
                    image_paths_placeholder, labels_placeholder,
                    control_placeholder, phase_train_placeholder,
                    batch_size_placeholder, total_loss, regularization_losses,
                    criterion, accuracy, args.use_fixed_image_standardization,
                    FLAGS.csv_file_path, pruned_ratio_relative_to_curr_task,
                    epoch_no, MB_of_model_through_inference,
                    MB_of_shared_variables, MB_of_task_specific_variables,
                    MB_of_whole_masks, MB_of_task_specific_masks,
                    MB_of_task_specific_batch_norm_variables,
                    MB_of_task_specific_biases)

            return
예제 #16
0
def main(args):
    network = importlib.import_module(args.model_def)
    image_size = (args.image_size, args.image_size)
    
    subdir = datetime.strftime(datatime.now(), '%Y%m%d-%H%M%S')
    log_dir = os.path.join(os.path.expanduser(args.logs_base_dir), subdir)
    if not os.path.isdir(log_dir):
        os.makedirs(log_dir)
    model_dir = os.path.join(os.path.expanduser(args.models_base_dir), subdir)
    if not os.path.isdir(model_dir):
        os.makedirs(model_dir)
        
    stat_file_name = os.path.join(log_dir, 'stat.h5')
    
    facenet.write_arguments_to_file(args, os.path.join(log_dir, 'arguments.txt'))
    
    src_path,_ = os.path.split(os.path.realpath(__file__))
    facenet.store_revision_info(src_path, log_dir, ' '.join(sys.argv))
    
    np.random.seed(seed=args.seed)
    random.seed(args.seed)
    dataset = facenet.get_dataset(args.data_dir)

    if args.validation_set_split_ratio > 0.0:
        train_set, val_set = facenet.split_dataset(dataset, args.validation_set_split_ratio, args.min_nrof_val_images_per_class, 
                                                   'SPLIT_IMAGES')
    else:
        train_set, val_set = dataset, []    
    
    num_classes = len(train_set)

    print('Model directory: %s' % model_dir)
    print('Log directory: %s' % log_dir)
    
    
    pretrained_model = None
    if args.pretrained_model:
        pretrained_model = os.path.expanduser(args.pretrained_model)
        print('Pre-trained model: %s' % pretrained_model)
        
    if args.lfw_dir:
        print('LFW directory: %s' % args.lfw_dir)
        pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))
        lfw_paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs)
        
    with tf.Graph().as_default():
        tf.set_random_state(args.seed)
        global_step = tf.Variable(0, trainable=False)
        
        image_list, label_list = facenet.get_image_paths_and_labels(train_set)
        assert len(image_list) > 0, 'The training set should not be empty'
        
        val_image_list, val_label_list = facenet.get_image_paths_and_labels(val_set)
        
        labels = ops.convert_to_tensor(label_list, dtype=tf.int32)
        range_size = array_ops.shape(labels)[0]
        index_q = tf.train.range_input_producer(range_size, num_epochs=None, shuffle=True, seed=None, capacity=32)
        index_deq_op = index_q.dequeue_many(args.batch_size * args.epoch_size, 'index_dequeue')
        
        learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate')
        batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
        phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
        image_paths_placeholder = tf.placeholder(tf.string, shape=(None, 1), name='image_paths')
        labels_placeholder = tf.placeholder(tf.int32, shape=(None, 1), name='labels')
        control_placeholder = tf.placeholder(tf.int32, shape=(None, 1), name='control')
        
        num_preprocess_threads = 4
        input_q = data_flow_ops.FIFOQueue(capacity=2000000, shared_name=None, name=None,
                                         dtype=[tf.string, tf.int32, tf.int32], 
                                         shapes=[(1, ), (1, ), (1, )])
        enq_op = input_q.enqueue_many([image_paths_placeholder, labels_placeholder, control_placeholder], name='enq_op')

        image_batch, label_batch = facenet.create_input_pipeline(input_q, image_size, num_preprocess_threads, batch_size_placeholder)
        
        image_batch = tf.identity(image_batch, 'image_batch')
        image_batch = tf.identity(image_batch, 'input')
        label_batch = tf.identity(label_batch, 'label_batch')
        
        print('Number of classes in training set: %d' % nrof_classes)
        print('Number of examples in training set: %d' % len(image_list))
        print('Number of classes in validation set: %d' % len(val_set))
        print('Number of examples in validation set: %d' % len(val_image_list))
        print('Building training graph')        
        
        prelogits, _ = network.inference(image_batch, args.keep_probability, phase_train=phase_train_placeholder, 
                                         bottleneck_layer_size=args.embedding_size, weight_decay=args.weight_decay)
        
        logits = slim.fully_connected(prelogits, num_classes, activation_fn=None, reuse=False, scope='Logits', 
                                     weights_initializer=slim.initializers.xavier_initializer(), 
                                     weights_regularizer=slim.l2_regularizer(args.weight_decay))
        
        embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings')
        
        eps = 1e-4
        prelogits_norm = tf.reduce_mean(tf.norm(tf.abs(prelogits) + eps, ord=args.prelogits_norm_p, axis=1))
        tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, prelogits_norm * args.prelogits_norm_loss_factor)
        
        prelogits_center_loss, _ = facenet.center_loss(prelogits, label_batch, args.center_loss_alfa, num_classes)
        tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, prelogits_center_loss * args.center_loss_factor)

        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=label_batch, logits=logits, name='cross_entropy_per_example')
        cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
        tf.add_to_collection('losses', cross_entropy_mean)

        regularization_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)        
        total_loss = tf.add_n([cross_entropy_mean] + regularization_loss, name='total_loss')

        correct_prediction = tf.cast(tf.equal(tf.argmax(logits, 1), tf.cast(label_batch, tf.int64)), tf.float32)
        accuracy = tf.reduce_mean(correct_prediction)
        
        learning_rate = tf.train.exponential_decay(learning_rate_placeholder, global_step, 
                                                  args.learning_rate_decay_epochs * args.epoch_size, 
                                                  args.learning_rate_decay_factor, staircase=True,)
        tf.summary.scalar('learning_rate', learning_rate)
        
        train_op = facenet.train(total_loss, global_step, args.optimizer, learning_rate, 
                                args.moving_average_decay, tf.global_variables(), args.log_histograms)

        saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)
        
        summary_op = tf.summary.merge_all()
        
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        summary_writer = tf.summary.FileWriter(log_dir, sess.graph)
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(coord=coord, sess=sess)
        
        with sess.as_default():
            if pretrained_model:
                print('Restoring pretrained model: %s' % pretrained_model)
                saver.restore(sess, pretrained_model)
            
            print('Running training')
            num_steps = args.max_nrof_epochs * args.epoch_size
            num_val_samples = int(math.ceil(args.max_nrof_epochs / args.validate_every_n_epochs))
            
            stat = {
                'loss': np.zeros((num_steps,), np.float32),
                'center_loss': np.zeros((num_steps,), np.float32),
                'reg_loss': np.zeros((num_steps,), np.float32),
                'xent_loss': np.zeros((num_steps,), np.float32),
                'prelogits_norm': np.zeros((num_steps,), np.float32),
                'accuracy': np.zeros((num_steps,), np.float32),
                'val_loss': np.zeros((num_val_samples,), np.float32),
                'val_xent_loss': np.zeros((num_val_samples,), np.float32),
                'val_accuracy': np.zeros((num_val_samples,), np.float32),
                'lfw_accuracy': np.zeros((args.max_nrof_epochs,), np.float32),
                'lfw_valrate': np.zeros((args.max_nrof_epochs,), np.float32),
                'learning_rate': np.zeros((args.max_nrof_epochs,), np.float32),
                'time_train': np.zeros((args.max_nrof_epochs,), np.float32),
                'time_validate': np.zeros((args.max_nrof_epochs,), np.float32),
                'time_evaluate': np.zeros((args.max_nrof_epochs,), np.float32),
                'prelogits_hist': np.zeros((args.max_nrof_epochs, 1000), np.float32),
              }
            
            for epoch in range(1, args.max_nrof_epochs + 1):
                step = sess.run(global_step, feed_dict=None)
                t = time.time()
                cont = train(args, sess, epoch, image_list, label_list, index_dequeue_op, enqueue_op, 
                             image_paths_placeholder, labels_placeholder, learning_rate_placeholder, phase_train_placeholder, 
                             batch_size_placeholder, control_placeholder, global_step, total_loss, train_op, summary_op, 
                             summary_writer, regularization_losses, args.learning_rate_schedule_file, stat, cross_entropy_mean, 
                             accuracy, learning_rate, prelogits, prelogits_center_loss, args.random_rotate, args.random_crop, 
                             args.random_flip, prelogits_norm, args.prelogits_hist_max, args.use_fixed_image_standardization)
                stat['time_train'][epoch - 1] = time.time() - t
                
                if not cont:
                    break
                    
                t = time.time()
                if len(val_image_list)>0 and ((epoch-1) % args.validate_every_n_epochs == args.validate_every_n_epochs-1 or 
                                              epoch==args.max_nrof_epochs):
                    validate(args, sess, epoch, val_image_list, val_label_list, enqueue_op, image_paths_placeholder, 
                             labels_placeholder, control_placeholder, phase_train_placeholder, batch_size_placeholder, stat, 
                             total_loss, regularization_losses, cross_entropy_mean, accuracy, args.validate_every_n_epochs, 
                             args.use_fixed_image_standardization)
                    stat['time_validate'][epoch - 1] = time.time() - t
                    
                save_variables_and_metagraph(sess, saver, summary_writer, model_dir, subdir, epoch)
                
                t = time.time()
                if args.lfw_dir:
                    evaluate(sess, enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, 
                             batch_size_placeholder, control_placeholder, embeddings, label_batch, lfw_paths, actual_issame, 
                             args.lfw_batch_size, args.lfw_nrof_folds, log_dir, step, summary_writer, stat, epoch, 
                             args.lfw_distance_metric, args.lfw_subtract_mean, args.lfw_use_flipped_images, 
                             args.use_fixed_image_standardization)
                    stat['time_evaluate'][epoch - 1] = time.time() - t
                
                print('Saving statistics')
                with h5py.File(stat_file_name, 'w') as f:
                    for key, value in stat.items():
                        f.create_dataset(key, data=value)
    return model_dir
예제 #17
0
def main(args):
  
    # 模型,定义在inception_resnet_v1 V2里(), --model_def models.inception_resnet_v1  
    network = importlib.import_module(args.model_def)
    image_size = (args.image_size, args.image_size)

    subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S')
    log_dir = os.path.join(os.path.expanduser(args.logs_base_dir), subdir)
    if not os.path.isdir(log_dir):  # Create the log directory if it doesn't exist
        os.makedirs(log_dir)
    model_dir = os.path.join(os.path.expanduser(args.models_base_dir), subdir)
    if not os.path.isdir(model_dir):  # Create the model directory if it doesn't exist
        os.makedirs(model_dir)

    stat_file_name = os.path.join(log_dir, 'stat.h5')

    # Write arguments to a text file
    facenet.write_arguments_to_file(args, os.path.join(log_dir, 'arguments.txt'))
        
    # Store some git revision info in a text file in the log directory
    src_path,_ = os.path.split(os.path.realpath(__file__))
    facenet.store_revision_info(src_path, log_dir, ' '.join(sys.argv))

    np.random.seed(seed=args.seed)
    random.seed(args.seed)
    dataset = facenet.get_dataset(args.data_dir)
    if args.filter_filename:
        dataset = filter_dataset(dataset, os.path.expanduser(args.filter_filename), 
            args.filter_percentile, args.filter_min_nrof_images_per_class)
        
    if args.validation_set_split_ratio>0.0:
        train_set, val_set = facenet.split_dataset(dataset, args.validation_set_split_ratio, args.min_nrof_val_images_per_class, 'SPLIT_IMAGES')
    else:
        train_set, val_set = dataset, []
        
    nrof_classes = len(train_set)
    
    print('Model directory: %s' % model_dir)
    print('Log directory: %s' % log_dir)
    pretrained_model = None
    if args.pretrained_model:
        pretrained_model = os.path.expanduser(args.pretrained_model)
        print('Pre-trained model: %s' % pretrained_model)
    
    if args.lfw_dir:
        print('LFW directory: %s' % args.lfw_dir)
        # 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
        lfw_paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs)
    
    with tf.Graph().as_default():
        tf.set_random_seed(args.seed)
        global_step = tf.Variable(0, trainable=False)
        
        # Get a list of image paths and their labels
        # 训练数据
        image_list, label_list = facenet.get_image_paths_and_labels(train_set)
        assert len(image_list)>0, 'The training set should not be empty'
        
        # 测试数据
        val_image_list, val_label_list = facenet.get_image_paths_and_labels(val_set)

        # Create a queue that produces indices into the image_list and label_list 
        # tf.convert_to_tensor用于将不同数据变成张量:比如可以让数组变成张量、也可以让列表变成张量。
        labels = ops.convert_to_tensor(label_list, dtype=tf.int32)
        range_size = array_ops.shape(labels)[0]
        # 多线程读取数据,shuffle=True表示不是按顺序存储,可以随机获取,并一直循环。
        # https://blog.csdn.net/lyg5623/article/details/69387917
        index_queue = tf.train.range_input_producer(range_size, num_epochs=None,
                             shuffle=True, seed=None, capacity=32)
        
        # epoch 大数据时迭代完一轮时次数,少量数据应该epoch = 全部数据个数/batch
        index_dequeue_op = index_queue.dequeue_many(args.batch_size*args.epoch_size, 'index_dequeue')
        
        learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate')
        batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
        phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
        image_paths_placeholder = tf.placeholder(tf.string, shape=(None,1), name='image_paths')
        labels_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='labels')
        control_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='control')
        
        nrof_preprocess_threads = 4
        input_queue = data_flow_ops.FIFOQueue(capacity=2000000,
                                    dtypes=[tf.string, tf.int32, tf.int32],
                                    shapes=[(1,), (1,), (1,)],
                                    shared_name=None, name=None)
        enqueue_op = input_queue.enqueue_many([image_paths_placeholder, labels_placeholder, control_placeholder], name='enqueue_op')
        image_batch, label_batch = facenet.create_input_pipeline(input_queue, image_size, nrof_preprocess_threads, batch_size_placeholder)

        image_batch = tf.identity(image_batch, 'image_batch')
        image_batch = tf.identity(image_batch, 'input')
        label_batch = tf.identity(label_batch, 'label_batch')
        
        print('Number of classes in training set: %d' % nrof_classes)
        print('Number of examples in training set: %d' % len(image_list))

        print('Number of classes in validation set: %d' % len(val_set))
        print('Number of examples in validation set: %d' % len(val_image_list))
        
        print('Building training graph')
        
        # Build the inference graph
        prelogits, _ = network.inference(image_batch, args.keep_probability, 
            phase_train=phase_train_placeholder, bottleneck_layer_size=args.embedding_size, 
            weight_decay=args.weight_decay)
        # 因为模型输出的(bottleneck_layer_size)没有计算最后一层(映射到图片类型),这里计算最后一层             
        logits = slim.fully_connected(prelogits, len(train_set), activation_fn=None, 
                weights_initializer=slim.initializers.xavier_initializer(), 
                weights_regularizer=slim.l2_regularizer(args.weight_decay),
                scope='Logits', reuse=False)

        # 按行进行泛化,行的平方求和再求平方根,得到的值按行除每个行的元素,对深度层面泛化? interface里最后一层输出为128个节点,slim.fully_connected(net, bottleneck_layer_size, activation_fn=None, 
				#https://blog.csdn.net/abiggg/article/details/79368982
        embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings')

				# 计算loss函数,当然还有其它训练参数也会加到这里来,通过比训练过程中一个weight加到正则化参数里来tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, weight)
				#  模型中最后会把这个加到优化的loss中来。
				#L= L_softmax + λL_cneter = Softmax(W_i + b_yj) + λ1/2||f(x_i) - c_yj ||_2^2
				
        # Norm for the prelogits
        eps = 1e-4
        prelogits_norm = tf.reduce_mean(tf.norm(tf.abs(prelogits)+eps, ord=args.prelogits_norm_p, axis=1))
        # 模型中最后输出(bottleneck_layer_size每个类型的输出值的个数)的平均值加到正则化loss中,但prelogits_norm_loss_factor貌似为0
        tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, prelogits_norm * args.prelogits_norm_loss_factor)

        # 计算中心损失及增加的正则化loss中
        # Add center loss
        prelogits_center_loss, _ = facenet.center_loss(prelogits, label_batch, args.center_loss_alfa, nrof_classes)
        tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, prelogits_center_loss * args.center_loss_factor)

        learning_rate = tf.train.exponential_decay(learning_rate_placeholder, global_step,
            args.learning_rate_decay_epochs*args.epoch_size, args.learning_rate_decay_factor, staircase=True)
        tf.summary.scalar('learning_rate', learning_rate)

        
        # Calculate the average cross entropy loss across the batch
        # 计算预测损失,和上面框架的Softmax(W_i + b_yj) 
        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=label_batch, logits=logits, name='cross_entropy_per_example')
        cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
        # 预测损失平均值加到losses变量中
        tf.add_to_collection('losses', cross_entropy_mean)
        
        correct_prediction = tf.cast(tf.equal(tf.argmax(logits, 1), tf.cast(label_batch, tf.int64)), tf.float32)
        accuracy = tf.reduce_mean(correct_prediction)
        
        #计算总损失,cross_entropy_mean + 前面增加的一些正则化损失(包括模型中增加的),通过tf.GraphKeys.REGULARIZATION_LOSSES获取出来
        # Calculate the total losses
        regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
        total_loss = tf.add_n([cross_entropy_mean] + regularization_losses, name='total_loss')

        # Build a Graph that trains the model with one batch of examples and updates the model parameters
        train_op = facenet.train(total_loss, global_step, args.optimizer, 
            learning_rate, args.moving_average_decay, tf.global_variables(), args.log_histograms)
        
        # Create a saver
        saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.summary.merge_all()

        # Start running operations on the Graph.
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        summary_writer = tf.summary.FileWriter(log_dir, sess.graph)
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(coord=coord, sess=sess)

        with sess.as_default():

            if pretrained_model:
                print('Restoring pretrained model: %s' % pretrained_model)
                saver.restore(sess, pretrained_model)

            # Training and validation loop
            print('Running training')
            nrof_steps = args.max_nrof_epochs*args.epoch_size
            nrof_val_samples = int(math.ceil(args.max_nrof_epochs / args.validate_every_n_epochs))   # Validate every validate_every_n_epochs as well as in the last epoch
            stat = {
                'loss': np.zeros((nrof_steps,), np.float32),
                'center_loss': np.zeros((nrof_steps,), np.float32),
                'reg_loss': np.zeros((nrof_steps,), np.float32),
                'xent_loss': np.zeros((nrof_steps,), np.float32),
                'prelogits_norm': np.zeros((nrof_steps,), np.float32),
                'accuracy': np.zeros((nrof_steps,), np.float32),
                'val_loss': np.zeros((nrof_val_samples,), np.float32),
                'val_xent_loss': np.zeros((nrof_val_samples,), np.float32),
                'val_accuracy': np.zeros((nrof_val_samples,), np.float32),
                'lfw_accuracy': np.zeros((args.max_nrof_epochs,), np.float32),
                'lfw_valrate': np.zeros((args.max_nrof_epochs,), np.float32),
                'learning_rate': np.zeros((args.max_nrof_epochs,), np.float32),
                'time_train': np.zeros((args.max_nrof_epochs,), np.float32),
                'time_validate': np.zeros((args.max_nrof_epochs,), np.float32),
                'time_evaluate': np.zeros((args.max_nrof_epochs,), np.float32),
                'prelogits_hist': np.zeros((args.max_nrof_epochs, 1000), np.float32),
              }
            for epoch in range(1,args.max_nrof_epochs+1):
                step = sess.run(global_step, feed_dict=None)
                # Train for one epoch
                t = time.time()
                # 训练模型
                cont = train(args, sess, epoch, image_list, label_list, index_dequeue_op, enqueue_op, image_paths_placeholder, labels_placeholder,
                    learning_rate_placeholder, phase_train_placeholder, batch_size_placeholder, control_placeholder, global_step, 
                    total_loss, train_op, summary_op, summary_writer, regularization_losses, args.learning_rate_schedule_file,
                    stat, cross_entropy_mean, accuracy, learning_rate,
                    prelogits, prelogits_center_loss, args.random_rotate, args.random_crop, args.random_flip, prelogits_norm, args.prelogits_hist_max, args.use_fixed_image_standardization)
                stat['time_train'][epoch-1] = time.time() - t
                
                if not cont:
                    break
                # 在测试数据上计算正确率  
                t = time.time()
                if len(val_image_list)>0 and ((epoch-1) % args.validate_every_n_epochs == args.validate_every_n_epochs-1 or epoch==args.max_nrof_epochs):
                    validate(args, sess, epoch, val_image_list, val_label_list, enqueue_op, image_paths_placeholder, labels_placeholder, control_placeholder,
                        phase_train_placeholder, batch_size_placeholder, 
                        stat, total_loss, regularization_losses, cross_entropy_mean, accuracy, args.validate_every_n_epochs, args.use_fixed_image_standardization)
                stat['time_validate'][epoch-1] = time.time() - t

                # Save variables and the metagraph if it doesn't exist already
                save_variables_and_metagraph(sess, saver, summary_writer, model_dir, subdir, epoch)

                # Evaluate on LFW
                t = time.time()
                if args.lfw_dir:
                    evaluate(sess, enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, control_placeholder, 
                        embeddings, label_batch, lfw_paths, actual_issame, args.lfw_batch_size, args.lfw_nrof_folds, log_dir, step, summary_writer, stat, epoch, 
                        args.lfw_distance_metric, args.lfw_subtract_mean, args.lfw_use_flipped_images, args.use_fixed_image_standardization)
                stat['time_evaluate'][epoch-1] = time.time() - t

                print('Saving statistics')
                with h5py.File(stat_file_name, 'w') as f:
                    for key, value in stat.items():
                        f.create_dataset(key, data=value)
    
    return model_dir
예제 #18
0
def main(args):
    network = importlib.import_module(args.model_def)
    image_size = (args.image_size, args.image_size)

    np.random.seed(seed=args.seed)
    pretrained_model = None
    pretrained_model = os.path.expanduser(args.pretrained_model)
    print('Pre-trained model: %s' % pretrained_model)

    print('LFW directory: %s' % args.lfw_dir)
    pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))
    lfw_paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir),
                                             pairs)

    with tf.Graph().as_default():
        tf.set_random_seed(args.seed)
        global_step = tf.Variable(0, trainable=False)

        batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
        phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
        image_paths_placeholder = tf.placeholder(tf.string,
                                                 shape=(None, 1),
                                                 name='image_paths')
        labels_placeholder = tf.placeholder(tf.int32,
                                            shape=(None, 1),
                                            name='labels')
        control_placeholder = tf.placeholder(tf.int32,
                                             shape=(None, 1),
                                             name='control')

        nrof_preprocess_threads = 4
        input_queue = data_flow_ops.FIFOQueue(
            capacity=2000000,
            dtypes=[tf.string, tf.int32, tf.int32],
            shapes=[(1, ), (1, ), (1, )],
            shared_name=None,
            name=None)
        enqueue_op = input_queue.enqueue_many(
            [image_paths_placeholder, labels_placeholder, control_placeholder],
            name='enqueue_op')
        image_batch, label_batch = facenet.create_input_pipeline(
            input_queue, image_size, nrof_preprocess_threads,
            batch_size_placeholder)

        image_batch = tf.identity(image_batch, 'image_batch')
        image_batch = tf.identity(image_batch, 'input')
        label_batch = tf.identity(label_batch, 'label_batch')

        prelogits, _ = network.inference(
            image_batch,
            args.keep_probability,
            phase_train=phase_train_placeholder,
            bottleneck_layer_size=args.embedding_size,
            weight_decay=args.weight_decay)
        embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings')
        # Start running operations on the Graph.
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.
                                    gpu_memory_fraction)  #设置GPU显存占用率
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                                log_device_placement=False))
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        # tensorflow中的多进程管理,下面两条语句通常配套使用
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(coord=coord, sess=sess)

        with sess.as_default():

            if pretrained_model:
                print('Restoring pretrained model: %s' % pretrained_model)
                facenet.load_model(pretrained_model)

            # for epoch in range(1,args.max_nrof_epochs+1):
            for epoch in range(1, 2):
                step = sess.run(global_step, feed_dict=None)
                # Evaluate on LFW
                t = time.time()
                if args.lfw_dir:
                    evaluate(sess, enqueue_op, image_paths_placeholder,
                             labels_placeholder, phase_train_placeholder,
                             batch_size_placeholder, control_placeholder,
                             embeddings, label_batch, lfw_paths, actual_issame,
                             args.lfw_batch_size, args.lfw_nrof_folds, step,
                             epoch, args.lfw_distance_metric,
                             args.lfw_subtract_mean,
                             args.lfw_use_flipped_images,
                             args.use_fixed_image_standardization)
예제 #19
0
def PersonCompare(info_queue, msg_q):
    image_size = (160, 160)
    total_dist = 0.0000
    with tf.Graph().as_default():

        with tf.Session() as sess:

            #image_dir1 = os.path.expanduser(args.first_dir)
            #image_dir2 = os.path.expanduser(args.second_dir)

            image_paths_placeholder = tf.placeholder(tf.string,
                                                     shape=(None, 1),
                                                     name='image_paths')
            labels_placeholder = tf.placeholder(tf.int32,
                                                shape=(None, 1),
                                                name='labels')
            batch_size_placeholder = tf.placeholder(tf.int32,
                                                    name='batch_size')
            control_placeholder = tf.placeholder(tf.int32,
                                                 shape=(None, 1),
                                                 name='control')
            phase_train_placeholder = tf.placeholder(tf.bool,
                                                     name='phase_train')

            nrof_preprocess_threads = 4
            #image_size = (image_size, image_size)

            eval_input_queue = data_flow_ops.FIFOQueue(
                capacity=2000000,
                dtypes=[tf.string, tf.int32, tf.int32],
                shapes=[(1, ), (1, ), (1, )],
                shared_name=None,
                name=None)
            eval_enqueue_op = eval_input_queue.enqueue_many(
                [
                    image_paths_placeholder, labels_placeholder,
                    control_placeholder
                ],
                name='eval_enqueue_op')
            image_batch, label_batch = facenet.create_input_pipeline(
                eval_input_queue, image_size, nrof_preprocess_threads,
                batch_size_placeholder)

            # Load the model
            input_map = {
                'image_batch': image_batch,
                'label_batch': label_batch,
                'phase_train': phase_train_placeholder
            }
            print('=== model: ')
            print(model)
            print('===input_map: ')
            print(input_map)
            facenet.load_model(model, input_map=input_map)

            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            # Get output tensor
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            embedding_size = embeddings.get_shape()[1]

            print('facenet embedding模型建立完毕')

            image_num = 0
            pass_num = 0

            while 1:
                input_img = info_queue.get()
                you = "who ?"
                last_dist = 2
                #print(input_img)
                #print("compare dir: %s" % compare_dir)

                for parent, dirnames, filenames in os.walk(compare_dir):
                    #print(dirnames)
                    for username in dirnames:
                        userdir = os.path.join(compare_dir, username)

                        img_files = os.path.join(userdir,
                                                 "*" + image_extension)

                        img_name_list = glob.glob(img_files)

                        for compare_img in img_name_list:
                            scaled_reshape = []
                            image1 = scipy.misc.imread(input_img, mode='RGB')
                            image1 = cv2.resize(image1,
                                                image_size,
                                                interpolation=cv2.INTER_CUBIC)
                            image1 = facenet.prewhiten(image1)
                            scaled_reshape.append(
                                image1.reshape(-1, 160, 160, 3))
                            emb_array1 = np.zeros((1, embedding_size))
                            emb_array1[0, :] = sess.run(
                                embeddings,
                                feed_dict={
                                    images_placeholder: scaled_reshape[0],
                                    phase_train_placeholder: False
                                })[0]

                            image2 = scipy.misc.imread(compare_img, mode='RGB')
                            image2 = cv2.resize(image2,
                                                image_size,
                                                interpolation=cv2.INTER_CUBIC)
                            image2 = facenet.prewhiten(image2)
                            scaled_reshape.append(
                                image2.reshape(-1, 160, 160, 3))
                            emb_array2 = np.zeros((1, embedding_size))
                            emb_array2[0, :] = sess.run(
                                embeddings,
                                feed_dict={
                                    images_placeholder: scaled_reshape[1],
                                    phase_train_placeholder: False
                                })[0]

                            dist = np.sqrt(
                                np.sum(np.square(emb_array1[0] -
                                                 emb_array2[0])))
                            #print("%s:%f "% (username,dist))
                            total_dist += dist
                            #if dist < 0.9:
                            #print("pass num += 1")
                            #pass_num += 1

                            image_num += 1
                            #print(" 第%d组照片是同一个人 "%num)
                        if image_num == 0:
                            #pass_num = 0
                            total_dist = 0
                            continue
                        #pass_rate = pass_num/image_num
                        #print("%s 通过率: %f " % (username,pass_rate))
                        #pass_num = 0
                        mean_dist = total_dist / image_num
                        #print("---- %s  mean dist: %f" % (username,mean_dist))
                        if mean_dist < last_dist:
                            last_dist = mean_dist
                            you = username
                        image_num = 0
                        total_dist = 0
                        #if pass_rate >= 0.8 :
                        #print("=========== 你是: %s\n" % username)
                        #break
                    print("=========== 你是: %s\n" % you)

                    break

                if not msg_q.empty():
                    if msg_q.get() == "complate_msg":
                        break
예제 #20
0
def train_facenet(config_):
    # display the model parameters
    config_.display()
    # Get the image_size
    image_size = (config_.im_height, config_.im_width)
    preprocess(config_)
    # Create dirs to save the logs and checkpoints
    subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S')
    log_dir = os.path.join(os.path.expanduser(config_.logs_base_dir), subdir)
    if not os.path.isdir(
            log_dir):  # Create the log directory if it doesn't exist
        os.makedirs(log_dir)
    model_dir = os.path.join(os.path.expanduser(config_.models_base_dir),
                             subdir)
    if not os.path.isdir(
            model_dir):  # Create the model directory if it doesn't exist
        os.makedirs(model_dir)
    # random seed for reproducability
    np.random.seed(seed=config_.seed)
    random.seed(config_.seed)
    # get the training dataset, the dir should have structure
    # person1
    #    person1_image_1
    #    person1_image_2
    #    .
    #    .
    # person 2
    #    person2_image_1
    #    .
    #    .
    dataset = facenet.get_dataset(config_.data_dir)
    # get train and test dataset
    if config_.validation_set_split_ratio > 0.0:
        train_set, val_set = facenet.split_dataset(
            dataset, config_.validation_set_split_ratio,
            config_.min_nrof_val_images_per_class, 'SPLIT_IMAGES')
    else:
        train_set, val_set = dataset, []

    nrof_classes = len(train_set)

    print('Model directory: %s' % model_dir)
    print('Log directory: %s' % log_dir)
    pretrained_model = None
    if config_.pretrained_model:
        pretrained_model = os.path.expanduser(config_.pretrained_model)
        print('Pre-trained model: %s' % pretrained_model)
    # get the image pairs to test the quality of embedding
    if config_.lfw_dir:
        print('LFW directory: %s' % config_.lfw_dir)
        # Read the file containing the pairs used for testing
        pairs = lfw.read_pairs(os.path.expanduser(config_.lfw_pairs))
        # Get the paths for the corresponding images
        lfw_paths, actual_issame = lfw.get_paths(
            os.path.expanduser(config_.lfw_dir), pairs)

    with tf.Graph().as_default():
        tf.set_random_seed(config_.seed)
        global_step = tf.Variable(0, trainable=False)

        # Get a list of image paths and their labels
        image_list, label_list = facenet.get_image_paths_and_labels(train_set)
        assert len(image_list) > 0, 'The training set should not be empty'

        val_image_list, val_label_list = facenet.get_image_paths_and_labels(
            val_set)

        # Create a queue that produces indices into the image_list and label_list
        labels = ops.convert_to_tensor(label_list, dtype=tf.int32)
        range_size = array_ops.shape(labels)[0]
        index_queue = tf.train.range_input_producer(range_size,
                                                    num_epochs=None,
                                                    shuffle=True,
                                                    seed=None,
                                                    capacity=32)

        index_dequeue_op = index_queue.dequeue_many(
            config_.batch_size * config_.epoch_size, 'index_dequeue')

        learning_rate_placeholder = tf.placeholder(tf.float32,
                                                   name='learning_rate')
        batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
        phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
        image_paths_placeholder = tf.placeholder(tf.string,
                                                 shape=(None, 1),
                                                 name='image_paths')
        labels_placeholder = tf.placeholder(tf.int32,
                                            shape=(None, 1),
                                            name='labels')
        control_placeholder = tf.placeholder(tf.int32,
                                             shape=(None, 1),
                                             name='control')

        nrof_preprocess_threads = 4
        input_queue = data_flow_ops.FIFOQueue(
            capacity=2000000,
            dtypes=[tf.string, tf.int32, tf.int32],
            shapes=[(1, ), (1, ), (1, )],
            shared_name=None,
            name=None)
        enqueue_op = input_queue.enqueue_many(
            [image_paths_placeholder, labels_placeholder, control_placeholder],
            name='enqueue_op')
        image_batch, label_batch = facenet.create_input_pipeline(
            input_queue, image_size, nrof_preprocess_threads,
            batch_size_placeholder)

        image_batch = tf.identity(image_batch, 'image_batch')
        image_batch = tf.identity(image_batch, 'input')
        label_batch = tf.identity(label_batch, 'label_batch')

        print('Number of classes in training set: %d' % nrof_classes)
        print('Number of examples in training set: %d' % len(image_list))

        print('Number of classes in validation set: %d' % len(val_set))
        print('Number of examples in validation set: %d' % len(val_image_list))

        print('Building training graph')

        # Build the inference graph
        prelogits, _ = network.inference(
            image_batch,
            config_.keep_probability,
            phase_train=phase_train_placeholder,
            bottleneck_layer_size=config_.embedding_size,
            weight_decay=config_.weight_decay)
        # logits needed for training, we can ignore this in testing phase
        logits = slim.fully_connected(
            prelogits,
            len(train_set),
            activation_fn=None,
            weights_initializer=slim.initializers.xavier_initializer(),
            weights_regularizer=slim.l2_regularizer(config_.weight_decay),
            scope='Logits',
            reuse=False)
        # learned embeddings, will be used to assess the similarity between two images
        embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings')

        # Norm for the prelogits
        eps = 1e-4
        prelogits_norm = tf.reduce_mean(
            tf.norm(tf.abs(prelogits) + eps,
                    ord=config_.prelogits_norm_p,
                    axis=1))
        tf.add_to_collection(
            tf.GraphKeys.REGULARIZATION_LOSSES,
            prelogits_norm * config_.prelogits_norm_loss_factor)

        # Add center loss
        prelogits_center_loss, _ = facenet.center_loss(
            prelogits, label_batch, config_.center_loss_alfa, nrof_classes)
        tf.add_to_collection(
            tf.GraphKeys.REGULARIZATION_LOSSES,
            prelogits_center_loss * config_.center_loss_factor)

        learning_rate = tf.train.exponential_decay(
            learning_rate_placeholder,
            global_step,
            config_.learning_rate_decay_epochs * config_.epoch_size,
            config_.learning_rate_decay_factor,
            staircase=True)
        tf.summary.scalar('learning_rate', learning_rate)

        # Calculate the average cross entropy loss across the batch
        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=label_batch,
            logits=logits,
            name='cross_entropy_per_example')
        cross_entropy_mean = tf.reduce_mean(cross_entropy,
                                            name='cross_entropy')
        tf.add_to_collection('losses', cross_entropy_mean)

        correct_prediction = tf.cast(
            tf.equal(tf.argmax(logits, 1), tf.cast(label_batch, tf.int64)),
            tf.float32)
        accuracy = tf.reduce_mean(correct_prediction)

        # Calculate the total losses
        regularization_losses = tf.get_collection(
            tf.GraphKeys.REGULARIZATION_LOSSES)
        total_loss = tf.add_n([cross_entropy_mean] + regularization_losses,
                              name='total_loss')

        # Build a Graph that trains the model with one batch of examples and updates the model parameters
        train_op = facenet.train(total_loss, global_step, config_.optimizer,
                                 learning_rate, config_.moving_average_decay,
                                 tf.global_variables(), config_.log_histograms)

        # Create a saver
        all_vars = tf.trainable_variables()
        var_to_restore = [
            v for v in all_vars if not v.name.startswith('Logits')
        ]
        saver = tf.train.Saver(var_to_restore,
                               max_to_keep=5,
                               keep_checkpoint_every_n_hours=1.0)
        #saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.summary.merge_all()

        # Start running operations on the Graph.
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=config_.gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                                log_device_placement=False))
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        summary_writer = tf.summary.FileWriter(log_dir, sess.graph)
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(coord=coord, sess=sess)

        with sess.as_default():

            if pretrained_model:
                try:
                    print('Restoring pretrained model: %s' % pretrained_model)
                    #saver.restore(sess, pretrained_model)
                    facenet.load_model(pretrained_model)
                except:
                    print(
                        'Error loading model(maybe checkpoint path is wrong).. training from scratch'
                    )
            # Training and validation loop
            print('Running training')
            nrof_steps = config_.max_nrof_epochs * config_.epoch_size
            nrof_val_samples = int(
                math.ceil(config_.max_nrof_epochs /
                          config_.validate_every_n_epochs)
            )  # Validate every validate_every_n_epochs as well as in the last epoch
            stat = {
                'loss':
                np.zeros((nrof_steps, ), np.float32),
                'center_loss':
                np.zeros((nrof_steps, ), np.float32),
                'reg_loss':
                np.zeros((nrof_steps, ), np.float32),
                'xent_loss':
                np.zeros((nrof_steps, ), np.float32),
                'prelogits_norm':
                np.zeros((nrof_steps, ), np.float32),
                'accuracy':
                np.zeros((nrof_steps, ), np.float32),
                'val_loss':
                np.zeros((nrof_val_samples, ), np.float32),
                'val_xent_loss':
                np.zeros((nrof_val_samples, ), np.float32),
                'val_accuracy':
                np.zeros((nrof_val_samples, ), np.float32),
                'lfw_accuracy':
                np.zeros((config_.max_nrof_epochs, ), np.float32),
                'lfw_valrate':
                np.zeros((config_.max_nrof_epochs, ), np.float32),
                'learning_rate':
                np.zeros((config_.max_nrof_epochs, ), np.float32),
                'time_train':
                np.zeros((config_.max_nrof_epochs, ), np.float32),
                'time_validate':
                np.zeros((config_.max_nrof_epochs, ), np.float32),
                'time_evaluate':
                np.zeros((config_.max_nrof_epochs, ), np.float32),
                'prelogits_hist':
                np.zeros((config_.max_nrof_epochs, 1000), np.float32),
            }
            for epoch in range(1, config_.max_nrof_epochs + 1):
                step = sess.run(global_step, feed_dict=None)
                # Train for one epoch
                t = time.time()
                cont = train(
                    config_, sess, epoch, image_list, label_list,
                    index_dequeue_op, enqueue_op, image_paths_placeholder,
                    labels_placeholder, learning_rate_placeholder,
                    phase_train_placeholder, batch_size_placeholder,
                    control_placeholder, global_step, total_loss, train_op,
                    summary_op, summary_writer, regularization_losses,
                    config_.learning_rate_schedule_file, stat,
                    cross_entropy_mean, accuracy, learning_rate, prelogits,
                    prelogits_center_loss, config_.random_rotate,
                    config_.random_crop, config_.random_flip, prelogits_norm,
                    config_.prelogits_hist_max,
                    config_.use_fixed_image_standardization)
                stat['time_train'][epoch - 1] = time.time() - t

                if not cont:
                    break

                t = time.time()
                if len(val_image_list) > 0 and (
                    (epoch - 1) % config_.validate_every_n_epochs
                        == config_.validate_every_n_epochs - 1
                        or epoch == config_.max_nrof_epochs):
                    validate(config_, sess, epoch, val_image_list,
                             val_label_list, enqueue_op,
                             image_paths_placeholder, labels_placeholder,
                             control_placeholder, phase_train_placeholder,
                             batch_size_placeholder, stat, total_loss,
                             regularization_losses, cross_entropy_mean,
                             accuracy, config_.validate_every_n_epochs,
                             config_.use_fixed_image_standardization)
                stat['time_validate'][epoch - 1] = time.time() - t

                # Save variables and the metagraph if it doesn't exist already
                save_variables_and_metagraph(sess, saver, summary_writer,
                                             model_dir, subdir, epoch)

                # Evaluate on LFW
                t = time.time()
                if config_.lfw_dir:
                    evaluate(sess, enqueue_op, image_paths_placeholder,
                             labels_placeholder, phase_train_placeholder,
                             batch_size_placeholder, control_placeholder,
                             embeddings, label_batch, lfw_paths, actual_issame,
                             config_.lfw_batch_size, config_.lfw_nrof_folds,
                             log_dir, step, summary_writer, stat, epoch,
                             config_.lfw_distance_metric,
                             config_.lfw_subtract_mean,
                             config_.lfw_use_flipped_images,
                             config_.use_fixed_image_standardization)
                stat['time_evaluate'][epoch - 1] = time.time() - t

                print('Saving statistics')

    return model_dir
예제 #21
0
def main(_):
    if FLAGS.csv_file_path:
        if os.path.exists(FLAGS.csv_file_path) is False:
            csv_dir = FLAGS.csv_file_path.rsplit('/', 1)[0]
            if os.path.exists(csv_dir) is False:
                os.makedirs(csv_dir)
            with open(FLAGS.csv_file_path, 'w') as f:
                writer = csv.writer(f)
                writer.writerow([
                    'Pruned rate', 'Acc Mean', 'Acc Std', 'Epoch No.',
                    'Model size through inference (MB) (Shared part + task-specific part)',
                    'Shared part (MB)', 'Task specific part (MB)',
                    'Whole masks (MB)', 'Task specific masks (MB)',
                    'Task specific batch norm vars (MB)',
                    'Task specific biases (MB)'
                ])

    args, unparsed = parse_arguments(sys.argv[1:])
    FLAGS.filters_expand_ratio = math.sqrt(FLAGS.model_size_expand_ratio)
    FLAGS.history_filters_expand_ratios = [
        math.sqrt(ratio) for ratio in FLAGS.history_model_size_expand_ratio
    ]

    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)

            # img = Image.open(paths[0])

            image_paths_placeholder = tf.placeholder(tf.string,
                                                     shape=(None, 1),
                                                     name='image_paths')
            labels_placeholder = tf.placeholder(tf.int32,
                                                shape=(None, 1),
                                                name='labels')
            batch_size_placeholder = tf.placeholder(tf.int32,
                                                    name='batch_size')
            control_placeholder = tf.placeholder(tf.int32,
                                                 shape=(None, 1),
                                                 name='control')
            phase_train_placeholder = tf.placeholder(tf.bool,
                                                     name='phase_train')

            nrof_preprocess_threads = 4
            image_size = (args.image_size, args.image_size)
            eval_input_queue = data_flow_ops.FIFOQueue(
                capacity=2000000,
                dtypes=[tf.string, tf.int32, tf.int32],
                shapes=[(1, ), (1, ), (1, )],
                shared_name=None,
                name=None)
            eval_enqueue_op = eval_input_queue.enqueue_many(
                [
                    image_paths_placeholder, labels_placeholder,
                    control_placeholder
                ],
                name='eval_enqueue_op')
            image_batch, label_batch = facenet.create_input_pipeline(
                eval_input_queue, image_size, nrof_preprocess_threads,
                batch_size_placeholder)
            coord = tf.train.Coordinator()
            tf.train.start_queue_runners(coord=coord, sess=sess)

            # Load the model
            if os.path.isdir(args.model):
                temp_record_file = os.path.join(args.model, 'temp_record.txt')
                checkpoint_file = os.path.join(args.model, 'checkpoint')

                if os.path.exists(temp_record_file) and os.path.exists(
                        checkpoint_file):
                    with open(temp_record_file) as json_file:
                        data = json.load(json_file)
                        max_acc = max(data, key=float)
                        epoch_no = data[max_acc]
                        ckpt_file = args.model + '/model-.ckpt-' + epoch_no

                    with open(checkpoint_file) as f:
                        context = f.read()
                    original_epoch = re.search("(\d)+", context).group()
                    context = context.replace(original_epoch, epoch_no)
                    with open(checkpoint_file, 'w') as f:
                        f.write(context)
                    if os.path.exists(os.path.join(args.model,
                                                   'copied')) is False:
                        os.makedirs(os.path.join(args.model, 'copied'))
                    copyfile(
                        temp_record_file,
                        os.path.join(args.model, 'copied', 'temp_record.txt'))
                    os.remove(temp_record_file)

                elif os.path.exists(checkpoint_file):
                    ckpt = tf.train.get_checkpoint_state(args.model)
                    ckpt_file = ckpt.model_checkpoint_path
                    epoch_no = ckpt_file.rsplit('-', 1)[-1]
                else:
                    print(
                        'No `temp_record.txt` or `checkpoint` in `{}`, you should pass args.model the file path, not the directory'
                        .format(args.model))
                    sys.exit(1)
            else:
                ckpt_file = args.model
                epoch_no = ckpt_file.rsplit('-')[-1]

            # Cannot use meta graph, because we need to dynamically decide batch normalization in regard to current task_id
            # facenet.load_model(args.model, input_map=input_map)
            # embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            prelogits, _ = network.inference(
                image_batch,
                1.0,
                phase_train=phase_train_placeholder,
                bottleneck_layer_size=args.embedding_size,
                weight_decay=0.0)

            embeddings = tf.nn.l2_normalize(prelogits,
                                            1,
                                            1e-10,
                                            name='embeddings')

            init_fn = slim.assign_from_checkpoint_fn(ckpt_file,
                                                     tf.global_variables())
            init_fn(sess)

            pruned_ratio = 0.0
            model_size = 0.0

            if FLAGS.print_mem or FLAGS.print_mask_info:
                masks = tf.get_collection('masks')

                if FLAGS.print_mask_info:

                    if masks:
                        num_elems_in_each_task_op = {}
                        num_elems_in_tasks_in_masks_op = {
                        }  # two dimentional dictionary
                        num_elems_in_masks_op = []
                        num_remain_elems_in_masks_op = []

                        for task_id in range(1, FLAGS.task_id + 1):
                            num_elems_in_each_task_op[task_id] = tf.constant(
                                0, dtype=tf.int32)
                            num_elems_in_tasks_in_masks_op[task_id] = {}

                        # Define graph
                        for i, mask in enumerate(masks):
                            num_elems_in_masks_op.append(tf.size(mask))
                            num_remain_elems_in_curr_mask = tf.size(mask)
                            for task_id in range(1, FLAGS.task_id + 1):
                                cnt = tf_count(mask, task_id)
                                num_elems_in_tasks_in_masks_op[task_id][
                                    i] = cnt
                                num_elems_in_each_task_op[task_id] = tf.add(
                                    num_elems_in_each_task_op[task_id], cnt)
                                num_remain_elems_in_curr_mask -= cnt

                            num_remain_elems_in_masks_op.append(
                                num_remain_elems_in_curr_mask)

                        num_elems_in_network_op = tf.add_n(
                            num_elems_in_masks_op)

                        print('Calculate pruning status ...')

                        # Doing operation
                        num_elems_in_masks = sess.run(num_elems_in_masks_op)
                        num_elems_in_each_task = sess.run(
                            num_elems_in_each_task_op)
                        num_elems_in_tasks_in_masks = sess.run(
                            num_elems_in_tasks_in_masks_op)
                        num_elems_in_network = sess.run(
                            num_elems_in_network_op)
                        num_remain_elems_in_masks = sess.run(
                            num_remain_elems_in_masks_op)

                        # Print out the result
                        print('Showing pruning status ...')

                        if FLAGS.verbose:
                            for i, mask in enumerate(masks):
                                print('Layer %s: ' % mask.op.name, end='')
                                for task_id in range(1, FLAGS.task_id + 1):
                                    cnt = num_elems_in_tasks_in_masks[task_id][
                                        i]
                                    print('task_%d -> %d/%d (%.2f%%), ' %
                                          (task_id, cnt, num_elems_in_masks[i],
                                           100 * cnt / num_elems_in_masks[i]),
                                          end='')
                                print('remain -> {:.2f}%'.format(
                                    100 * num_remain_elems_in_masks[i] /
                                    num_elems_in_masks[i]))

                        print('Num elems in network: {}'.format(
                            num_elems_in_network))
                        num_elems_of_usued_weights = num_elems_in_network
                        for task_id in range(1, FLAGS.task_id + 1):
                            print('Num elems in task_{}: {}'.format(
                                task_id, num_elems_in_each_task[task_id]))
                            print('Ratio of task_{} to all: {}'.format(
                                task_id, num_elems_in_each_task[task_id] /
                                num_elems_in_network))
                            num_elems_of_usued_weights -= num_elems_in_each_task[
                                task_id]
                        print('Num usued elems in all masks: {}'.format(
                            num_elems_of_usued_weights))

                        # pruned_ratio = num_elems_of_usued_weights / num_elems_in_network
                        # print('Ratio of usused_elem to all: {}'.format(pruned_ratio))
                        # print('Pruning degree relative to task_{}: {:.3f}'.format(FLAGS.task_id, num_elems_of_usued_weights / (num_elems_of_usued_weights + num_elems_in_each_task[FLAGS.task_id])))
                        pruned_ratio_relative_to_all_elems = num_elems_of_usued_weights / num_elems_in_network
                        print('Ratio of usused_elem to all: {}'.format(
                            pruned_ratio_relative_to_all_elems))
                        pruned_ratio_relative_to_curr_task = num_elems_of_usued_weights / (
                            num_elems_of_usued_weights +
                            num_elems_in_each_task[FLAGS.task_id])
                        print('Pruning degree relative to task_{}: {:.3f}'.
                              format(FLAGS.task_id,
                                     pruned_ratio_relative_to_curr_task))

                if FLAGS.print_mem:
                    # Analyze param
                    start_time = time.time()
                    (MB_of_model_through_inference, MB_of_shared_variables,
                     MB_of_task_specific_variables, MB_of_whole_masks,
                     MB_of_task_specific_masks,
                     MB_of_task_specific_batch_norm_variables,
                     MB_of_task_specific_biases
                     ) = model_analyzer.analyze_vars_for_current_task(
                         tf.model_variables(),
                         sess=sess,
                         task_id=FLAGS.task_id,
                         verbose=False)
                    duration = time.time() - start_time
                    print('duration time: {}'.format(duration))

            if FLAGS.eval_once:
                evaluate(
                    sess, eval_enqueue_op, image_paths_placeholder,
                    labels_placeholder, phase_train_placeholder,
                    batch_size_placeholder, control_placeholder, embeddings,
                    label_batch, paths, actual_issame, args.lfw_batch_size,
                    args.lfw_nrof_folds, args.distance_metric,
                    args.subtract_mean, args.use_flipped_images,
                    args.use_fixed_image_standardization, FLAGS.csv_file_path,
                    pruned_ratio, epoch_no, MB_of_model_through_inference,
                    MB_of_shared_variables, MB_of_task_specific_variables,
                    MB_of_whole_masks, MB_of_task_specific_masks,
                    MB_of_task_specific_batch_norm_variables,
                    MB_of_task_specific_biases)
def main(args):
    with tf.Graph().as_default():

        with tf.Session() as sess:

            paths = get_dir(args.aligned_face_dir)

            image_paths_placeholder = tf.placeholder(tf.string,
                                                     shape=(None, 1),
                                                     name='image_paths')
            labels_placeholder = tf.placeholder(tf.int32,
                                                shape=(None, 1),
                                                name='labels')
            batch_size_placeholder = tf.placeholder(tf.int32,
                                                    name='batch_size')
            control_placeholder = tf.placeholder(tf.int32,
                                                 shape=(None, 1),
                                                 name='control')
            phase_train_placeholder = tf.placeholder(tf.bool,
                                                     name='phase_train')

            nrof_preprocess_threads = 4
            image_size = (args.image_size, args.image_size)
            #创建一个先入先出队列
            eval_input_queue = data_flow_ops.FIFOQueue(
                capacity=2000000,
                dtypes=[tf.string, tf.int32, tf.int32],
                shapes=[(1, ), (1, ), (1, )],
                shared_name=None,
                name=None)
            # 多值入队
            eval_enqueue_op = eval_input_queue.enqueue_many(
                [
                    image_paths_placeholder, labels_placeholder,
                    control_placeholder
                ],
                name='eval_enqueue_op')
            # 将 input_queue 中的 (image, label, control) 元祖 dequeue 出来,根据 control 里的内容
            #   对 image 进行各种预处理,然后将处理后的 (image, label) 打包成真正输入 model 的 batch
            image_batch, label_batch = facenet.create_input_pipeline(
                eval_input_queue, image_size, nrof_preprocess_threads,
                batch_size_placeholder)

            # Load the model
            input_map = {
                'image_batch': image_batch,
                'label_batch': label_batch,
                'phase_train': phase_train_placeholder
            }
            facenet.load_model(args.model, input_map=input_map)

            # Get output tensor
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")

            coord = tf.train.Coordinator()
            tf.train.start_queue_runners(coord=coord, sess=sess)

            nrof_images = len(paths)
            labels_array = np.expand_dims(np.arange(0, nrof_images), 1)
            image_paths_array = np.expand_dims(np.array(paths), 1)

            control_array = np.zeros_like(labels_array, np.int32)
            if args.use_fixed_image_standardization:
                control_array += np.ones_like(
                    labels_array) * facenet.FIXED_STANDARDIZATION

            sess.run(eval_enqueue_op, {image_paths_placeholder: image_paths_array, \
                    labels_placeholder: labels_array, control_placeholder: control_array})

            embedding_size = int(embeddings.get_shape()[1])
            nrof_batches = nrof_images // args.batch_size
            last_batch = nrof_images % args.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: args.batch_size
                }
                emb, lab = sess.run([embeddings, label_batch],
                                    feed_dict=feed_dict)
                lab_array[lab] = lab
                emb_array[lab, :] = emb
                if i % 10 == 9:
                    print('.', end='')
                    sys.stdout.flush()

            feed_dict = {
                phase_train_placeholder: False,
                batch_size_placeholder: last_batch
            }
            emb, lab = sess.run([embeddings, label_batch], feed_dict=feed_dict)
            lab_array[lab] = lab
            emb_array[lab, :] = emb

            print('')
            embeddings = np.zeros((nrof_images, embedding_size))
            embeddings = emb_array

            embeddings0 = np.tile(embeddings[0], (len(embeddings), 1))
            face_dists1 = facenet.distance(embeddings0,
                                           embeddings,
                                           distance_metric=1)
            face_dists0 = facenet.distance(embeddings0,
                                           embeddings,
                                           distance_metric=0)
            # face_dists = 1 - np.dot(embeddings, embeddings[2]) / \
            #             (np.linalg.norm(embeddings[2])*np.linalg.norm(embeddings, axis = 1))
            # face_dists1 = np.arccos(face_dists) / math.pi
            np.savetxt(face_dists0_path, face_dists0)
            np.savetxt(face_dists1_path, face_dists1)
            np.savetxt(embeddings_path, embeddings)
            print(face_dists0)
            print(face_dists1)
예제 #23
0
def main(args):

    network = importlib.import_module(args.model_def)
    image_size = (args.image_size, args.image_size)

    subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S')
    log_dir = os.path.join(os.path.expanduser(args.logs_base_dir), subdir)
    if not os.path.isdir(
            log_dir):  # Create the log directory if it doesn't exist
        try:
            os.makedirs(log_dir)
        except OSError as exc:
            print(exc)
    model_dir = os.path.join(os.path.expanduser(args.models_base_dir), subdir)
    if not os.path.isdir(
            model_dir):  # Create the model directory if it doesn't exist
        try:
            os.makedirs(model_dir)
        except OSError as exc:
            print(exc)

    stat_file_name = os.path.join(log_dir, 'stat.h5')

    # Write arguments to a text file
    facenet.write_arguments_to_file(args, os.path.join(log_dir,
                                                       'arguments.txt'))

    # Store some git revision info in a text file in the log directory
    src_path, _ = os.path.split(os.path.realpath(__file__))
    facenet.store_revision_info(src_path, log_dir, ' '.join(sys.argv))

    np.random.seed(seed=args.seed)
    random.seed(args.seed)
    dataset = facenet.get_dataset(args.data_dir)
    if args.filter_filename:
        dataset = filter_dataset(dataset,
                                 os.path.expanduser(args.filter_filename),
                                 args.filter_percentile,
                                 args.filter_min_nrof_images_per_class)

    if args.validation_set_split_ratio > 0.0:
        train_set, val_set = facenet.split_dataset(
            dataset, args.validation_set_split_ratio,
            args.min_nrof_val_images_per_class, 'SPLIT_IMAGES')
    else:
        train_set, val_set = dataset, []

    nrof_classes = len(train_set)

    print('Model directory: %s' % model_dir)
    print('Log directory: %s' % log_dir)
    pretrained_model = None
    if args.pretrained_model:
        pretrained_model = os.path.expanduser(args.pretrained_model)
        print('Pre-trained model: %s' % pretrained_model)

    if args.lfw_dir:
        print('LFW directory: %s' % args.lfw_dir)
        # 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
        lfw_paths, actual_issame = lfw.get_paths(
            os.path.expanduser(args.lfw_dir), pairs)

    with tf.Graph().as_default():
        tf.set_random_seed(args.seed)
        global_step = tf.Variable(0, trainable=False)

        # Get a list of image paths and their labels
        image_list, label_list = facenet.get_image_paths_and_labels(train_set)
        assert len(image_list) > 0, 'The training set should not be empty'

        val_image_list, val_label_list = facenet.get_image_paths_and_labels(
            val_set)

        # Create a queue that produces indices into the image_list and label_list
        labels = ops.convert_to_tensor(label_list, dtype=tf.int32)
        range_size = array_ops.shape(labels)[0]
        index_queue = tf.train.range_input_producer(range_size,
                                                    num_epochs=None,
                                                    shuffle=True,
                                                    seed=None,
                                                    capacity=32)

        index_dequeue_op = index_queue.dequeue_many(
            args.batch_size * args.epoch_size, 'index_dequeue')

        learning_rate_placeholder = tf.placeholder(tf.float32,
                                                   name='learning_rate')
        batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
        phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
        image_paths_placeholder = tf.placeholder(tf.string,
                                                 shape=(None, 1),
                                                 name='image_paths')
        labels_placeholder = tf.placeholder(tf.int32,
                                            shape=(None, 1),
                                            name='labels')
        control_placeholder = tf.placeholder(tf.int32,
                                             shape=(None, 1),
                                             name='control')

        nrof_preprocess_threads = 4
        input_queue = data_flow_ops.FIFOQueue(
            capacity=2000000,
            dtypes=[tf.string, tf.int32, tf.int32],
            shapes=[(1, ), (1, ), (1, )],
            shared_name=None,
            name=None)
        enqueue_op = input_queue.enqueue_many(
            [image_paths_placeholder, labels_placeholder, control_placeholder],
            name='enqueue_op')
        image_batch, label_batch = facenet.create_input_pipeline(
            input_queue, image_size, nrof_preprocess_threads,
            batch_size_placeholder)

        image_batch = tf.identity(image_batch, 'image_batch')
        image_batch = tf.identity(image_batch, 'input')
        label_batch = tf.identity(label_batch, 'label_batch')

        print('Number of classes in training set: %d' % nrof_classes)
        print('Number of examples in training set: %d' % len(image_list))

        print('Number of classes in validation set: %d' % len(val_set))
        print('Number of examples in validation set: %d' % len(val_image_list))

        print('Building training graph')

        # Build the inference graph
        prelogits, _ = network.inference(
            image_batch,
            args.keep_probability,
            phase_train=phase_train_placeholder,
            bottleneck_layer_size=args.embedding_size,
            weight_decay=args.weight_decay)
        logits = slim.fully_connected(
            prelogits,
            len(train_set),
            activation_fn=None,
            weights_initializer=slim.initializers.xavier_initializer(),
            weights_regularizer=slim.l2_regularizer(args.weight_decay),
            scope='Logits',
            reuse=False)

        embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings')

        # Norm for the prelogits
        eps = 1e-4
        prelogits_norm = tf.reduce_mean(
            tf.norm(tf.abs(prelogits) + eps, ord=args.prelogits_norm_p,
                    axis=1))
        tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES,
                             prelogits_norm * args.prelogits_norm_loss_factor)

        # Add center loss
        prelogits_center_loss, _ = facenet.center_loss(prelogits, label_batch,
                                                       args.center_loss_alfa,
                                                       nrof_classes)
        tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES,
                             prelogits_center_loss * args.center_loss_factor)

        learning_rate = tf.train.exponential_decay(
            learning_rate_placeholder,
            global_step,
            args.learning_rate_decay_epochs * args.epoch_size,
            args.learning_rate_decay_factor,
            staircase=True)
        tf.summary.scalar('learning_rate', learning_rate)

        # Calculate the average cross entropy loss across the batch
        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=label_batch,
            logits=logits,
            name='cross_entropy_per_example')
        cross_entropy_mean = tf.reduce_mean(cross_entropy,
                                            name='cross_entropy')
        tf.add_to_collection('losses', cross_entropy_mean)

        correct_prediction = tf.cast(
            tf.equal(tf.argmax(logits, 1), tf.cast(label_batch, tf.int64)),
            tf.float32)
        accuracy = tf.reduce_mean(correct_prediction)

        # Calculate the total losses
        regularization_losses = tf.get_collection(
            tf.GraphKeys.REGULARIZATION_LOSSES)
        total_loss = tf.add_n([cross_entropy_mean] + regularization_losses,
                              name='total_loss')

        # Build a Graph that trains the model with one batch of examples and updates the model parameters
        train_op = facenet.train(total_loss, global_step, args.optimizer,
                                 learning_rate, args.moving_average_decay,
                                 tf.global_variables(), args.log_histograms)

        # Create a saver
        saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.summary.merge_all()

        # Start running operations on the Graph.
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        config = tf.ConfigProto(gpu_options=gpu_options,
                                log_device_placement=False)
        config.gpu_options.visible_device_list = str(hvd.local_rank())
        sess = tf.Session(config=config)
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        # Horovod: broadcast initial variable states from rank 0 to all other processes.
        # This is necessary to ensure consistent initialization of all workers when
        # training is started with random weights or restored from a checkpoint.
        bcast = hvd.broadcast_global_variables(0)

        summary_writer = tf.summary.FileWriter(log_dir, sess.graph)
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(coord=coord, sess=sess)

        with sess.as_default():

            if pretrained_model:
                print('Restoring pretrained model: %s' % pretrained_model)
                saver.restore(sess, pretrained_model)

            # Training and validation loop
            print('Running training')
            nrof_steps = args.max_nrof_epochs * args.epoch_size
            nrof_val_samples = int(
                math.ceil(args.max_nrof_epochs / args.validate_every_n_epochs)
            )  # Validate every validate_every_n_epochs as well as in the last epoch
            stat = {
                'loss':
                np.zeros((nrof_steps, ), np.float32),
                'center_loss':
                np.zeros((nrof_steps, ), np.float32),
                'reg_loss':
                np.zeros((nrof_steps, ), np.float32),
                'xent_loss':
                np.zeros((nrof_steps, ), np.float32),
                'prelogits_norm':
                np.zeros((nrof_steps, ), np.float32),
                'accuracy':
                np.zeros((nrof_steps, ), np.float32),
                'val_loss':
                np.zeros((nrof_val_samples, ), np.float32),
                'val_xent_loss':
                np.zeros((nrof_val_samples, ), np.float32),
                'val_accuracy':
                np.zeros((nrof_val_samples, ), np.float32),
                'lfw_accuracy':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'lfw_valrate':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'learning_rate':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'time_train':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'time_validate':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'time_evaluate':
                np.zeros((args.max_nrof_epochs, ), np.float32),
                'prelogits_hist':
                np.zeros((args.max_nrof_epochs, 1000), np.float32),
            }
            for epoch in range(1, args.max_nrof_epochs + 1):
                step = sess.run(global_step, feed_dict=None)
                # Train for one epoch
                t = time.time()
                cont = train(
                    args, sess, epoch, image_list, label_list,
                    index_dequeue_op, enqueue_op, image_paths_placeholder,
                    labels_placeholder, learning_rate_placeholder,
                    phase_train_placeholder, batch_size_placeholder,
                    control_placeholder, global_step, total_loss, train_op,
                    summary_op, summary_writer, regularization_losses,
                    args.learning_rate_schedule_file, stat, cross_entropy_mean,
                    accuracy, learning_rate, prelogits, prelogits_center_loss,
                    args.random_rotate, args.random_crop, args.random_flip,
                    prelogits_norm, args.prelogits_hist_max,
                    args.use_fixed_image_standardization)
                stat['time_train'][epoch - 1] = time.time() - t

                if not cont:
                    break

                t = time.time()
                if len(val_image_list) > 0 and (
                    (epoch - 1) % args.validate_every_n_epochs
                        == args.validate_every_n_epochs - 1
                        or epoch == args.max_nrof_epochs):
                    validate(args, sess, epoch, val_image_list, val_label_list,
                             enqueue_op, image_paths_placeholder,
                             labels_placeholder, control_placeholder,
                             phase_train_placeholder, batch_size_placeholder,
                             stat, total_loss, regularization_losses,
                             cross_entropy_mean, accuracy,
                             args.validate_every_n_epochs,
                             args.use_fixed_image_standardization)
                stat['time_validate'][epoch - 1] = time.time() - t

                # Save variables and the metagraph if it doesn't exist already
                save_variables_and_metagraph(sess, saver, summary_writer,
                                             model_dir, subdir, epoch)

                # Evaluate on LFW
                t = time.time()
                if args.lfw_dir:
                    evaluate(sess, enqueue_op, image_paths_placeholder,
                             labels_placeholder, phase_train_placeholder,
                             batch_size_placeholder, control_placeholder,
                             embeddings, label_batch, lfw_paths, actual_issame,
                             args.lfw_batch_size, args.lfw_nrof_folds, log_dir,
                             step, summary_writer, stat, epoch,
                             args.lfw_distance_metric, args.lfw_subtract_mean,
                             args.lfw_use_flipped_images,
                             args.use_fixed_image_standardization)
                stat['time_evaluate'][epoch - 1] = time.time() - t

                print('Saving statistics')
                with h5py.File(stat_file_name, 'w') as f:
                    for key, value in stat.items():
                        f.create_dataset(key, data=value)

    return model_dir
def generate_data(feature_size, max_length, batch_size, MAT_folder, img_folder):
    noise_scale = 0.15  # 0.15
    # connect_thresh = 0.95
    connect_thresh = np.random.uniform(0.6, 1)
    # sample_p = np.array([0.0852,0.1996,0.2550,0.0313,0.0854,0.1546,0.1890])
    sample_p = np.array(sample_prob)
    sample_p = list(sample_p / sum(sample_p))

    # load mat files
    Mat_paths = sorted(os.listdir(MAT_folder))
    choose_idx = np.random.choice(len(Mat_paths), size=batch_size, p=sample_p)
    Mat_files = []
    for n in range(batch_size):
        temp_path = MAT_folder + '/' + Mat_paths[choose_idx[n]]
        # temp_mat_file = loadmat(temp_path)
        f = open(temp_path, 'rb')
        gt_info = pickle.load(f)
        Mat_files.append({'gtInfo': gt_info})

    # X = np.zeros((batch_size,1,max_length,1+4+512))
    X = np.zeros((batch_size, feature_size, max_length, 3))
    Y = np.zeros((batch_size, 2))
    crop_bbox = []

    # positive 
    for n in range(int(batch_size / 2)):
        print('positive', n, Mat_paths[choose_idx[n]], Mat_files[n]['gtInfo']['X'].shape)
        fr_num = Mat_files[n]['gtInfo']['X'].shape[0]
        id_num = Mat_files[n]['gtInfo']['X'].shape[1]
        Y[n, 0] = 1

        X_2d = Mat_files[n]['gtInfo']['X']
        Y_2d = Mat_files[n]['gtInfo']['Y']
        W_2d = Mat_files[n]['gtInfo']['W']
        H_2d = Mat_files[n]['gtInfo']['H']

        #########################################
        X_2d = X_2d - margin * W_2d
        Y_2d = Y_2d - margin * H_2d
        W_2d = (1 + 2 * margin) * W_2d
        H_2d = (1 + 2 * margin) * H_2d
        ##########################################

        if len(Mat_files[n]['gtInfo']) <= 4:
            V_2d = []
        else:
            V_2d = Mat_files[n]['gtInfo']['V']
        if len(Mat_files[n]['gtInfo']) == 6:
            img_size = Mat_files[n]['gtInfo']['img_size']
        else:
            img_size = [1920, 1080]

        while 1:
            obj_id = np.random.randint(id_num, size=1)[0]

            bbox_tracklet, t_interval, err_flag = split_track(X_2d, Y_2d, W_2d, H_2d, V_2d, img_size, obj_id,
                                                              noise_scale, connect_thresh)
            if err_flag == 1:
                continue

            cand_pairs = []
            if len(bbox_tracklet) <= 1:
                continue
            for k1 in range(len(bbox_tracklet) - 1):
                for k2 in range(k1 + 1, len(bbox_tracklet)):
                    if t_interval[k1, 0] + max_length > t_interval[k2, 0]:
                        t_dist = t_interval[k2, 0] - t_interval[k1, 1]
                        cand_pairs.append([k1, k2, t_dist])
            if len(cand_pairs) == 0:
                continue

            cand_pairs = np.array(cand_pairs)
            rand_num = np.random.rand(1)[0]
            # print(rand_num)
            if rand_num < 0.7:
                select_p = np.exp(-np.power(cand_pairs[:, 2], 2) / 100)
                select_p = select_p / sum(select_p)
                # print(select_p)
                pair_idx = np.random.choice(len(cand_pairs), size=1, p=select_p)[0]
            else:
                pair_idx = np.random.randint(len(cand_pairs), size=1)[0]
            select_pair = cand_pairs[pair_idx]
            select_pair = select_pair.astype(int)

            abs_fr_t1 = int(t_interval[select_pair[0], 0])
            abs_fr_t2 = int(t_interval[select_pair[0], 1])
            abs_fr_t3 = int(t_interval[select_pair[1], 0])
            abs_fr_t4 = int(min(abs_fr_t1 + max_length - 1, t_interval[select_pair[1], 1]))

            t1 = 0
            t2 = abs_fr_t2 - abs_fr_t1
            t3 = abs_fr_t3 - abs_fr_t1
            t4 = abs_fr_t4 - abs_fr_t1

            # mask
            X[n, :, t1:t2 + 1, 1] = 1
            X[n, :, t3:t4 + 1, 2] = 1
            # X[n,4:,t1:t2+1,1] = bbox_tracklet[select_pair[0]][:,5]
            # X[n,4:,t3:t4+1,2] = bbox_tracklet[select_pair[1]][0:t4-t3+1,5]

            # X    
            X[n, 0, t1:t2 + 1, 0] = 0.5 * (bbox_tracklet[select_pair[0]][:, 1] + bbox_tracklet[select_pair[0]][:, 3]) / \
                                    img_size[0]
            X[n, 0, t3:t4 + 1, 0] = 0.5 * (
                    bbox_tracklet[select_pair[1]][0:t4 - t3 + 1, 1] + bbox_tracklet[select_pair[1]][0:t4 - t3 + 1,
                                                                      3]) / img_size[0]

            # Y
            X[n, 1, t1:t2 + 1, 0] = 0.5 * (bbox_tracklet[select_pair[0]][:, 2] + bbox_tracklet[select_pair[0]][:, 4]) / \
                                    img_size[1]
            X[n, 1, t3:t4 + 1, 0] = 0.5 * (
                    bbox_tracklet[select_pair[1]][0:t4 - t3 + 1, 2] + bbox_tracklet[select_pair[1]][0:t4 - t3 + 1,
                                                                      4]) / img_size[1]

            # W
            X[n, 2, t1:t2 + 1, 0] = (bbox_tracklet[select_pair[0]][:, 3] - bbox_tracklet[select_pair[0]][:, 1]) / \
                                    img_size[0]
            X[n, 2, t3:t4 + 1, 0] = (bbox_tracklet[select_pair[1]][0:t4 - t3 + 1, 3] - bbox_tracklet[select_pair[1]][
                                                                                       0:t4 - t3 + 1, 1]) / img_size[0]

            # H
            X[n, 3, t1:t2 + 1, 0] = (bbox_tracklet[select_pair[0]][:, 4] - bbox_tracklet[select_pair[0]][:, 2]) / \
                                    img_size[1]
            X[n, 3, t3:t4 + 1, 0] = (bbox_tracklet[select_pair[1]][0:t4 - t3 + 1, 4] - bbox_tracklet[select_pair[1]][
                                                                                       0:t4 - t3 + 1, 2]) / img_size[1]
            '''       
            plt.plot(X[n,0,:,0], 'ro')
            plt.show()
            plt.plot(X[n,1,:,0], 'ro')
            plt.show()
            plt.plot(X[n,2,:,0], 'ro')
            plt.show()
            plt.plot(X[n,3,:,0], 'ro')
            plt.show()
            plt.plot(X[n,0,:,1], 'ro')
            plt.show()
            plt.plot(X[n,0,:,2], 'ro')
            plt.show()    
            '''

            # save all bbox
            temp_crop_bbox = np.concatenate(
                (bbox_tracklet[select_pair[0]], bbox_tracklet[select_pair[1]][0:t4 - t3 + 1, :]), axis=0)
            temp_crop_bbox = temp_crop_bbox.astype(int)
            crop_bbox.append(temp_crop_bbox)
            break

    # negative
    for n in range(int(batch_size / 2), batch_size):
        print('negative', n, Mat_paths[choose_idx[n]], Mat_files[n]['gtInfo']['X'].shape)
        fr_num = Mat_files[n]['gtInfo']['X'].shape[0]
        id_num = Mat_files[n]['gtInfo']['X'].shape[1]
        Y[n, 1] = 1

        X_2d = Mat_files[n]['gtInfo']['X']
        Y_2d = Mat_files[n]['gtInfo']['Y']
        W_2d = Mat_files[n]['gtInfo']['W']
        H_2d = Mat_files[n]['gtInfo']['H']

        #########################################
        X_2d = X_2d - margin * W_2d
        Y_2d = Y_2d - margin * H_2d
        W_2d = (1 + 2 * margin) * W_2d
        H_2d = (1 + 2 * margin) * H_2d
        ##########################################

        if len(Mat_files[n]['gtInfo']) <= 4:
            V_2d = []
        else:
            V_2d = Mat_files[n]['gtInfo']['V']
        if len(Mat_files[n]['gtInfo']) == 6:
            img_size = Mat_files[n]['gtInfo']['img_size']
        else:
            img_size = [1920, 1080]

        # check candidate obj pairs
        # pair_mat = np.zeros((id_num,id_num))
        cand_idx_pairs = []
        for n1 in range(id_num - 1):
            for n2 in range(n1 + 1, id_num):
                cand_fr1 = np.where(W_2d[:, n1] > 0)[0]
                cand_fr2 = np.where(W_2d[:, n2] > 0)[0]
                if max(cand_fr1[0], cand_fr2[0]) < min(cand_fr1[-1], cand_fr2[-1]):  # cand_fr1 and cand_fr2 are overlapped
                    cand_idx_pairs.append([n1, n2])
                    # pair_mat[n1,n2] = 1

        # cand_pairs = np.nonzero(pair_mat)
        while 1:
            if len(cand_idx_pairs) == 0:
                import pdb
                pdb.set_trace()
            pair_idx = np.random.randint(len(cand_idx_pairs), size=1)[0]
            obj_id1 = cand_idx_pairs[pair_idx][0]
            obj_id2 = cand_idx_pairs[pair_idx][1]

            part_W_mat1 = W_2d[:, obj_id1]
            non_zero_idx1 = np.where(part_W_mat1 > 0)[0]
            part_W_mat2 = W_2d[:, obj_id2]
            non_zero_idx2 = np.where(part_W_mat2 > 0)[0]
            if len(non_zero_idx1) == 0 or len(non_zero_idx2) == 0 or \
                    max(non_zero_idx1) + max_length < min(non_zero_idx2) or min(non_zero_idx1) > max(non_zero_idx2):
                continue

            bbox_tracklet1, t_interval1, err_flag = split_track(X_2d, Y_2d, W_2d, H_2d, V_2d, img_size, obj_id1,
                                                                noise_scale, connect_thresh)
            if err_flag == 1:
                continue
            bbox_tracklet2, t_interval2, err_flag = split_track(X_2d, Y_2d, W_2d, H_2d, V_2d, img_size, obj_id2,
                                                                noise_scale, connect_thresh)
            if err_flag == 1:
                continue

            cand_pairs = []
            if len(bbox_tracklet1) <= 1 or len(bbox_tracklet2) <= 1:
                continue
            for k1 in range(len(bbox_tracklet1)):
                for k2 in range(len(bbox_tracklet2)):
                    if t_interval1[k1, 0] + max_length > t_interval2[k2, 0] and t_interval1[k1, 1] < t_interval2[k2, 0]:
                        t_dist = t_interval2[k2, 0] - t_interval1[k1, 1]
                        cand_pairs.append([k1, k2, t_dist])
            if len(cand_pairs) == 0:
                continue

            cand_pairs = np.array(cand_pairs)
            rand_num = np.random.rand(1)[0]
            # print(rand_num)
            if rand_num < 0.7:
                select_p = np.exp(-np.power(cand_pairs[:, 2], 2) / 100)
                select_p = select_p / sum(select_p)
                # print(select_p)
                pair_idx = np.random.choice(len(cand_pairs), size=1, p=select_p)[0]
            else:
                pair_idx = np.random.randint(len(cand_pairs), size=1)[0]
            select_pair = cand_pairs[pair_idx]
            select_pair = select_pair.astype(int)

            abs_fr_t1 = int(t_interval1[select_pair[0], 0])
            abs_fr_t2 = int(t_interval1[select_pair[0], 1])
            abs_fr_t3 = int(t_interval2[select_pair[1], 0])
            abs_fr_t4 = int(min(abs_fr_t1 + max_length - 1, t_interval2[select_pair[1], 1]))

            t1 = 0
            t2 = abs_fr_t2 - abs_fr_t1
            t3 = abs_fr_t3 - abs_fr_t1
            t4 = abs_fr_t4 - abs_fr_t1

            # mask
            X[n, :, t1:t2 + 1, 1] = 1
            X[n, :, t3:t4 + 1, 2] = 1
            # X[n,4:,t1:t2+1,1] = bbox_tracklet1[select_pair[0]][:,5]
            # X[n,4:,t3:t4+1,2] = bbox_tracklet2[select_pair[1]][0:t4-t3+1,5]

            # X    
            X[n, 0, t1:t2 + 1, 0] = 0.5 * (
                    bbox_tracklet1[select_pair[0]][:, 1] + bbox_tracklet1[select_pair[0]][:, 3]) / img_size[0]
            X[n, 0, t3:t4 + 1, 0] = 0.5 * (
                    bbox_tracklet2[select_pair[1]][0:t4 - t3 + 1, 1] + bbox_tracklet2[select_pair[1]][0:t4 - t3 + 1,
                                                                       3]) / img_size[0]

            # Y
            X[n, 1, t1:t2 + 1, 0] = 0.5 * (
                    bbox_tracklet1[select_pair[0]][:, 2] + bbox_tracklet1[select_pair[0]][:, 4]) / img_size[1]
            X[n, 1, t3:t4 + 1, 0] = 0.5 * (
                    bbox_tracklet2[select_pair[1]][0:t4 - t3 + 1, 2] + bbox_tracklet2[select_pair[1]][0:t4 - t3 + 1,
                                                                       4]) / img_size[1]

            # W
            X[n, 2, t1:t2 + 1, 0] = (bbox_tracklet1[select_pair[0]][:, 3] - bbox_tracklet1[select_pair[0]][:, 1]) / \
                                    img_size[0]
            X[n, 2, t3:t4 + 1, 0] = (bbox_tracklet2[select_pair[1]][0:t4 - t3 + 1, 3] - bbox_tracklet2[select_pair[1]][
                                                                                        0:t4 - t3 + 1, 1]) / img_size[0]

            # H
            X[n, 3, t1:t2 + 1, 0] = (bbox_tracklet1[select_pair[0]][:, 4] - bbox_tracklet1[select_pair[0]][:, 2]) / \
                                    img_size[1]
            X[n, 3, t3:t4 + 1, 0] = (bbox_tracklet2[select_pair[1]][0:t4 - t3 + 1, 4] - bbox_tracklet2[select_pair[1]][
                                                                                        0:t4 - t3 + 1, 2]) / img_size[1]
            '''
            plt.plot(X[n,0,:,0], 'ro')
            plt.show()
            plt.plot(X[n,1,:,0], 'ro')
            plt.show()
            plt.plot(X[n,2,:,0], 'ro')
            plt.show()
            plt.plot(X[n,3,:,0], 'ro')
            plt.show()
            plt.plot(X[n,0,:,1], 'ro')
            plt.show()
            plt.plot(X[n,0,:,2], 'ro')
            plt.show()    
            '''
            # save all bbox
            temp_crop_bbox = np.concatenate(
                (bbox_tracklet1[select_pair[0]], bbox_tracklet2[select_pair[1]][0:t4 - t3 + 1, :]), axis=0)
            temp_crop_bbox = temp_crop_bbox.astype(int)
            crop_bbox.append(temp_crop_bbox)
            break

    # crop data to a temp folder
    print('cropping data to tmp folder ...')
    if not os.path.exists(temp_folder):
        os.makedirs(temp_folder)
    all_paths = []
    for n in range(batch_size):
        temp_all_path = []
        seq_name = Mat_paths[choose_idx[n]][:-4]
        # img_path = img_folder + '/' + seq_name + '/img1/'
        img_path = img_folder + '/' + seq_name + '/'
        track_name = file_name(n + 1, 4)
        save_path = temp_folder + '/' + track_name
        if not os.path.exists(save_path):
            os.makedirs(save_path)

        for k in range(len(crop_bbox[n])):
            fr_id = crop_bbox[n][k, 0] + 1
            temp_img_path = img_path + file_name(fr_id, 6) + '.jpg'
            img = scipy.ndimage.imread(temp_img_path)
            bbox_img = img[crop_bbox[n][k, 2]:crop_bbox[n][k, 4], crop_bbox[n][k, 1]:crop_bbox[n][k, 3], :]
            bbox_img = scipy.misc.imresize(bbox_img, size=(bbox_size, bbox_size))
            bbox_img_path = save_path + '/' + file_name(k, 4) + '.png'
            temp_all_path.append(bbox_img_path)
            scipy.misc.imsave(bbox_img_path, bbox_img)
        all_paths.append(temp_all_path)

    print('start training ...')
    f_image_size = 160
    distance_metric = 0
    with tf.Graph().as_default():
        with tf.Session() as sess:
            image_paths_placeholder = tf.placeholder(tf.string, shape=(None, 1), name='image_paths')
            labels_placeholder = tf.placeholder(tf.int32, shape=(None, 1), name='labels')
            batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
            control_placeholder = tf.placeholder(tf.int32, shape=(None, 1), name='control')
            phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')

            nrof_preprocess_threads = 4
            image_size = (f_image_size, f_image_size)
            eval_input_queue = data_flow_ops.FIFOQueue(capacity=2000000,
                                                       dtypes=[tf.string, tf.int32, tf.int32],
                                                       shapes=[(1,), (1,), (1,)],
                                                       shared_name=None, name=None)
            eval_enqueue_op = eval_input_queue.enqueue_many(
                [image_paths_placeholder, labels_placeholder, control_placeholder], name='eval_enqueue_op')
            image_batch, label_batch = facenet.create_input_pipeline(eval_input_queue, image_size,
                                                                     nrof_preprocess_threads, batch_size_placeholder)

            # Load the model
            input_map = {'image_batch': image_batch, 'label_batch': label_batch, 'phase_train': phase_train_placeholder}
            facenet.load_model(triplet_model, input_map=input_map)

            # Get output tensor
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            #
            coord = tf.train.Coordinator()
            tf.train.start_queue_runners(coord=coord, sess=sess)

            for n in range(len(all_paths)):
                # print(n)
                lfw_batch_size = len(all_paths[n])

                emb_array = evaluate(sess, eval_enqueue_op, image_paths_placeholder, labels_placeholder,
                                     phase_train_placeholder,
                                     batch_size_placeholder, control_placeholder, embeddings, label_batch, all_paths[n],
                                     lfw_batch_size, distance_metric)

                if X[n, 4:, X[n, 0, :, 1] + X[n, 0, :, 2] > 0.5, 0].shape[0] != emb_array.shape[0]:
                    aa = 0
                    import pdb
                    pdb.set_trace()

                X[n, 4:, X[n, 0, :, 1] + X[n, 0, :, 2] > 0.5, 0] = emb_array

    return X, Y