Ejemplo n.º 1
0
def load_network(model_path):
    sess = tf.Session()
    images_pl = tf.placeholder(tf.float32,
                               shape=[None, 200, 200, 3],
                               name='input_image')
    images_norm = tf.map_fn(
        lambda frame: tf.image.per_image_standardization(frame), images_pl)
    train_mode = tf.placeholder(tf.bool)
    age_logits, gender_logits, _ = inception_resnet_v1.inference(
        images_norm,
        keep_probability=0.8,
        phase_train=train_mode,
        weight_decay=1e-5)
    gender = tf.argmax(tf.nn.softmax(gender_logits), 1)
    age_ = tf.cast(tf.constant([i for i in range(0, 101)]), tf.float32)
    age = tf.reduce_sum(tf.multiply(tf.nn.softmax(age_logits), age_), axis=1)
    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())
    sess.run(init_op)
    saver = tf.train.Saver()
    ckpt = tf.train.get_checkpoint_state(model_path)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("restore model!")
    else:
        pass
    return sess, age, gender, train_mode, images_pl
Ejemplo n.º 2
0
    def __init__(self, face_rec_graph, model_dir):
        """
        :param face_rec_graph: 输入人脸的graph
        :param model_dir: 用于识别人脸的model的路径
        """

        model_path = model_dir + get_model_name(model_dir)
        # print(model_path)
        print('正在加载model...')
        config = tf.ConfigProto()
        # config.allow_soft_placement = True  # 刚一开始分配少量的GPU容量,然后按需慢慢的增加,由于不会释放内存,所以会导致碎片
        config.gpu_options.allow_growth = True
        with face_rec_graph.graph.as_default():
            self.sess = tf.Session(config=config)

            # 默认输入的NN尺寸为128 x 128 x 3
            self.x = tf.placeholder(dtype=tf.float32,
                                    shape=[None, 128, 128, 3])

            self.embeddings = tf.nn.l2_normalize(
                resnet.inference(self.x, 0.6, phase_train=False)[0], 1, 1e-10)

            saver = tf.train.Saver()  # 创建一个Saver来管理模型中的所有变量
            saver.restore(self.sess, model_path)
            print('model加载完毕!')
Ejemplo n.º 3
0
def estimate_age_and_gender(aligned_images, model_path):
    with tf.Graph().as_default():
        session = tf.Session()
        images_pl = tf.placeholder(tf.float32,
                                   shape=[None, 160, 160, 3],
                                   name='input_image')
        images = tf.map_fn(lambda frame: tf.reverse_v2(frame, [-1]),
                           images_pl)  #BGR TO RGB
        images_norm = tf.map_fn(
            lambda frame: tf.image.per_image_standardization(frame), images)
        train_mode = tf.placeholder(tf.bool)
        age_logits, gender_logits, _ = inception_resnet_v1.inference(
            images_norm,
            keep_probability=0.8,
            phase_train=train_mode,
            weight_decay=1e-5)
        gender = tf.argmax(tf.nn.softmax(gender_logits), 1)
        age_ = tf.cast(tf.constant([i for i in range(0, 101)]), tf.float32)
        age = tf.reduce_sum(tf.multiply(tf.nn.softmax(age_logits), age_),
                            axis=1)
        init_op = tf.group(tf.global_variables_initializer(),
                           tf.local_variables_initializer())
        session.run(init_op)
        saver = tf.train.Saver()
        ckpt = tf.train.get_checkpoint_state(model_path)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(session, ckpt.model_checkpoint_path)
        else:
            pass
        return session.run([age, gender],
                           feed_dict={
                               images_pl: aligned_images,
                               train_mode: False
                           })
def tower_loss(scope, images, labels):

    prelogits, _ = inception_resnet_v1.inference(images, keep_probability,
                                     phase_train=True, bottleneck_layer_size=128,
                                     weight_decay=5e-5)
    logits = slim.fully_connected(prelogits, 54, activation_fn=None,
                                  weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
                                  weights_regularizer=slim.l2_regularizer(5e-5),
                                  scope='Logits', reuse=False)


    labels = tf.cast(labels, tf.int64)

    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
      labels=labels, 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)


    #tf.add_n(tf.get_collection('losses'), name='total_loss')


    losses = tf.get_collection('losses', scope)


    total_loss = tf.add_n(losses, name='total_loss')

    return total_loss
Ejemplo n.º 5
0
    def content_extractor(self, images, reuse=False):
        # images: (batch, 32, 32, 3) or (batch, 32, 32, 1)

        if images.get_shape()[3] == 1:
            # For mnist dataset, replicate the gray scale image 3 times.
            images = tf.image.grayscale_to_rgb(images)
        if images.get_shape()[2] < 64 or images.get_shape()[1] < 64:
            print("WARNING:resnet may not support images with small size.")
        prelogits, _ = inference(images, keep_probability=1.0, reuse=reuse)
        ret = tf.expand_dims(tf.expand_dims(prelogits, axis=1), axis=2)
        return ret
    def _build_graph(self, inputs):
        # with tf.device('/gpu:0'):
        image, label = inputs
      
        image = tf.identity(image, name="NETWORK_INPUT")
        tf.summary.image('input-image', image, max_outputs=10)

        # image = (image - 127.5) / 128

        image = tf.map_fn(lambda img: tf.image.per_image_standardization(img), image)

        prelogits, _ = inception_resnet_v1.inference(image, cfg.keep_probability, 
            phase_train=self.train_model, bottleneck_layer_size=cfg.feature_length, 
            weight_decay=cfg.weight_decay)

        logits = slim.fully_connected(prelogits, cfg.nrof_classes, activation_fn=None, 
                weights_initializer=tf.truncated_normal_initializer(stddev=0.1), 
                weights_regularizer=slim.l2_regularizer(cfg.weight_decay),
                scope='Logits', reuse=False)

        #feature for face recognition
        embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings')
        feature = tf.identity(embeddings, name="FEATURE")
        
        # Add center loss
        if cfg.center_loss_factor>0.0:
            prelogits_center_loss, _ = facenet.center_loss(prelogits, label, cfg.center_loss_alfa, cfg.nrof_classes)
            tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, prelogits_center_loss * cfg.center_loss_factor)

        # Add cross entropy loss
        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=label, logits=logits, name='cross_entropy_per_example')
        softmax_loss = tf.reduce_mean(cross_entropy, name='softmax_loss')
        # tf.add_to_collection('softmax_loss', softmax_loss)

        # Calculate the total losses
        center_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
        
        # tf.summary.scalar('regularization_losses', regularization_losses)
        loss = tf.add_n([softmax_loss] + center_loss, name='loss')
 
        center_loss = tf.identity(center_loss, name='center_loss')
        if cfg.weight_decay > 0:
            wd_cost = regularize_cost('.*/W', l2_regularizer(cfg.weight_decay), name='l2_regularize_loss')
            add_moving_summary(loss, wd_cost)
            add_moving_summary(softmax_loss)
            # add_moving_summary(center_loss)
            self.cost = tf.add_n([loss, wd_cost], name='cost')
        else:
            add_moving_summary(softmax_loss)
            # add_moving_summary(center_loss)
            self.cost = tf.identity(loss, name='cost')
Ejemplo n.º 7
0
    def content_extractor(self, images, reuse=False):
        # Assuming standard input -1~1, scale them to 0`255, which I think is the correct scale...
        # images: (batch, 32, 32, 3) or (batch, 32, 32, 1)

        if images.get_shape()[3] == 1:
            # For mnist dataset, replicate the gray scale image 3 times.
            images = tf.image.grayscale_to_rgb(images)
        if images.get_shape()[2] < 64 or images.get_shape()[1] < 64:
            print("WARNING:resnet may not support images with small size.")
        prelogits, _ = inference((images + 1) * 127.5,
                                 keep_probability=1.0,
                                 reuse=reuse)
        if self.mode == 'pretrain':
            batch_norm_params = {
                # Decay for the moving averages.
                'decay': 0.995,
                # epsilon to prevent 0s in variance.
                'epsilon': 0.001,
                # force in-place updates of mean and variance estimates
                'updates_collections': None,
                # Moving averages ends up in the trainable variables collection
                'variables_collections': [tf.GraphKeys.TRAINABLE_VARIABLES],
            }
            bottleneck = slim.fully_connected(
                prelogits,
                self.embedding_size,
                activation_fn=None,
                weights_initializer=tf.truncated_normal_initializer(
                    stddev=0.1),
                weights_regularizer=slim.l2_regularizer(self.weight_decay),
                normalizer_fn=slim.batch_norm,
                normalizer_params=batch_norm_params,
                scope='Bottleneck',
                reuse=False)
            logits = slim.fully_connected(
                bottleneck,
                self.num_classes,
                activation_fn=None,
                weights_initializer=tf.truncated_normal_initializer(
                    stddev=0.1),
                weights_regularizer=slim.l2_regularizer(self.weight_decay),
                scope='Logits',
                reuse=False)
            embeddings = tf.nn.l2_normalize(bottleneck,
                                            1,
                                            1e-10,
                                            name='embeddings')
            return logits
        else:
            ret = tf.expand_dims(tf.expand_dims(prelogits, axis=1), axis=2)
            return ret
Ejemplo n.º 8
0
 def __init__(self,
              face_img_size,
              model_path='models/model-20170512-110547.ckpt-250000'):
     self.face_imgs = tf.placeholder(
         'float', [None, face_img_size, face_img_size, 3])
     self.face_img_size = face_img_size
     self.attr = tf.nn.l2_normalize(
         resnet.inference(self.face_imgs, 0.6, phase_train=False)[0], 1,
         1e-10)
     #some magic numbers that u dont have to care about
     self.sess = tf.Session()
     saver = tf.train.Saver()  #saver load pretrain model
     saver.restore(self.sess, model_path)
     print('Moldes loaded')
def main():

    if (not handle_args()):
        # invalid arguments exit program
        print_usage()
        return 1

    with tf.Graph().as_default():
        image = tf.placeholder("float",
                               shape=[1, image_size, image_size, 3],
                               name='input')
        prelogits, _ = network.inference(image, 1.0, phase_train=False)
        normalized = tf.nn.l2_normalize(prelogits, 1, name='l2_normalize')
        output = tf.identity(normalized, name='output')
        saver = tf.train.Saver(tf.global_variables())

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())
            index_file_name = model_base_file_name + ".index"
            data_file_name = model_base_file_name + ".data-00000-of-00001"
            meta_file_name = model_base_file_name + ".meta"
            if (os.path.isfile(index_file_name)
                    and os.path.isfile(data_file_name)
                    and os.path.isfile(meta_file_name)):
                print('Restoring ' + model_base_file_name)
                saver.restore(sess, model_base_file_name)
            else:
                print('\n')
                print('Error, did not find index, data, and meta files: ')
                print('  ' + index_file_name)
                print('  ' + data_file_name)
                print('  ' + meta_file_name)

                print('These files can be downloaded manually from:')
                print('  https://github.com/davidsandberg/facenet')
                print('    Download: 20170511-185253.zip (web faces), or ')
                print('              20170512-110547.zip (celeb faces)')
                print(
                    'after unzipping be sure to rename them to the file names')
                print('above to match the TensorFlow 1.3 file naming.')
                print('Either the celeb faces or web faces will work')
                print(
                    '***********************************************************'
                )

            # Save the network for fathom
            saver.save(
                sess,
                model_base_file_name + '_ncs/' + model_base_file_name + '_ncs')
Ejemplo n.º 10
0
    def __init__(self, face_rec_graph, model_path = 'models/model-20170512-110547.ckpt-250000'):
    #def __init__(self, face_rec_graph, model_path='models/me.face.model.h5'):
        '''
        :param face_rec_sess: FaceRecSession object
        :param model_path:
        '''
        print("Loading model...")
        with face_rec_graph.graph.as_default():
            self.sess = tf.Session()
            self.x = tf.placeholder('float', [None,160,160,3]); #default input for the NN is 160x160x3
            self.embeddings = tf.nn.l2_normalize(
                                        resnet.inference(self.x, 0.6, phase_train=False)[0], 1, 1e-10); #some magic numbers that u dont have to care about

            saver = tf.train.Saver() #saver load pretrain model
            saver.restore(self.sess, model_path)
            print("Model loaded")
Ejemplo n.º 11
0
def build_model(is_training, images, params):
    '''
       建立模型
       ----------------------------
       Args:
          is_training: bool, 是否是训练阶段,可以从mode中判断
          images:     (batch_size, 120*250*1), 输入数据
          params:      dict, 一些超参数

       Returns:
          out: 输出的embeddings, shape = (batch_size, 128)
    '''
    out = net.inference(images, 0.8, phase_train=is_training)
    out = tf.nn.l2_normalize(out, dim=1)

    return out
Ejemplo n.º 12
0
def inference_recognition_inceptionv1(inputs,
                                      classnums,
                                      reuse=False,
                                      train=True):
    logits, _ = inference(inputs,
                          keep_prob=0.8,
                          phase_train=train,
                          reuse=reuse)
    with tf.variable_scope('recognition_fc', reuse=reuse):
        net = slim.fully_connected(
            logits,
            classnums,
            activation_fn=None,
            normalizer_fn=None,
            weights_initializer=tf.truncated_normal_initializer(stddev=0.02),
            scope='recognition_soft')
    return tf.nn.softmax(net), net, logits
Ejemplo n.º 13
0
def get_loss(x, labels, scope):

    y = inception_resnet_v1.inference(x,
                                      keep_probability=keep_probability,
                                      phase_train=phase_train,
                                      bottleneck_layer_size=embedding_size,
                                      weight_decay=weight_decay)

    cross_entropy = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels,
                                                       logits=y))

    regularization_loss = tf.add_n(tf.get_collection('losses', scope))

    loss = cross_entropy + regularization_loss

    return loss
def main(data, iter_num, labels):

    with tf.Graph().as_default():

        BATCH_SIZE = 866
        train_anchor_data = tf.placeholder(tf.float32,
                                           shape=[BATCH_SIZE, 128, 128, 3],
                                           name='anchor')

        labels_anchor = tf.placeholder(tf.int32, shape=[BATCH_SIZE])

        features, prelogits, logits = inception_resnet_v1.inference(
            train_anchor_data,
            keep_probability=1.0,
            phase_train=False,
            bottleneck_layer_size=30,
            weight_decay=0.0005)
        features = tf.nn.l2_normalize(features, 1, 1e-10, name='embeddings')

        data_train, train_labels = data, labels
        print(data.shape)
        print(labels.shape)
        Saver = tf.train.Saver()
        with tf.Session() as sess:

            sess.run(tf.global_variables_initializer())
            emb_array = np.zeros((iter_num // BATCH_SIZE * BATCH_SIZE, 1792))
            for step in range(iter_num // BATCH_SIZE):
                batch_anchor, batch_labels = data_train[step * BATCH_SIZE:(step + 1) * BATCH_SIZE], \
                                             train_labels[step * BATCH_SIZE:(step + 1) * BATCH_SIZE]
                # batch_anchor = data_train[step * BATCH_SIZE:(step + 1) * BATCH_SIZE]
                Saver.restore(
                    sess,
                    '/home/lenovo/yql/pig_data/triplet_model/logs_tensorboard5/'
                )
                feed_dict = {
                    train_anchor_data: batch_anchor,
                    labels_anchor: batch_labels,
                }

                Logits = sess.run(features, feed_dict=feed_dict)
                emb_array[step * BATCH_SIZE:(step + 1) *
                          BATCH_SIZE, :] = Logits
                print('the triplet loss %g' % step)
        np.savetxt('valid_feature_12_11.txt', emb_array)
        print(np.shape(emb_array))
Ejemplo n.º 15
0
def load_session(model_path=MODEL_PATH):
    global SESSION
    global AGE
    global GENDER
    global IMAGES_PL
    global TRAIN_MODE
    global tf_config

    graph = tf.Graph().as_default()
    sess = tf.Session(config=tf_config)
    images_pl = tf.placeholder(tf.float32,
                               shape=[None, 160, 160, 3],
                               name='input_image')
    images = tf.map_fn(lambda frame: tf.reverse_v2(frame, [-1]),
                       images_pl)  #BGR TO RGB
    images_norm = tf.map_fn(
        lambda frame: tf.image.per_image_standardization(frame), images)
    train_mode = tf.placeholder(tf.bool)
    age_logits, gender_logits, _ = inception_resnet_v1.inference(
        images_norm,
        keep_probability=1.0,  #0.8
        phase_train=train_mode,
        weight_decay=1e-5)
    gender = tf.argmax(tf.nn.softmax(gender_logits), 1)
    age_ = tf.cast(tf.constant([i for i in range(0, 101)]), tf.float32)
    age = tf.reduce_sum(tf.multiply(tf.nn.softmax(age_logits), age_), axis=1)

    GENDER = gender
    AGE = age
    IMAGES_PL = images_pl
    TRAIN_MODE = train_mode

    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())
    sess.run(init_op)
    saver = tf.train.Saver()
    ckpt = tf.train.get_checkpoint_state(model_path)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("restore and continue training! : {}".format(
            ckpt.model_checkpoint_path))
    else:
        sys.exit("Age-Gender Model not found")

    SESSION = sess
    def __init__(self):
        self.model_path = "./models"
        self.shape_detector = "shape_predictor_68_face_landmarks.dat"
        cuda = True
        font_scale = 1
        thickness = 1
        #log('1')
        print('1')
        with tf.Graph().as_default():
            config = tf.ConfigProto()
            config.gpu_options.per_process_gpu_memory_fraction = 0.4
            # session = tf.Session(config=config, ...)

            self.sess = tf.Session(config=config)
            self.images_pl = tf.placeholder(tf.float32,
                                            shape=[None, 160, 160, 3],
                                            name='input_image')
            images = tf.map_fn(lambda frame: tf.reverse_v2(frame, [-1]),
                               self.images_pl)  #BGR TO RGB
            images_norm = tf.map_fn(
                lambda frame: tf.image.per_image_standardization(frame),
                images)
            self.train_mode = tf.placeholder(tf.bool)
            age_logits, gender_logits, _ = inception_resnet_v1.inference(
                images_norm,
                keep_probability=0.8,
                phase_train=self.train_mode,
                weight_decay=1e-5)
            self.gender = tf.argmax(tf.nn.softmax(gender_logits), 1)
            age_ = tf.cast(tf.constant([i for i in range(0, 101)]), tf.float32)
            self.age = tf.reduce_sum(tf.multiply(tf.nn.softmax(age_logits),
                                                 age_),
                                     axis=1)
            init_op = tf.group(tf.global_variables_initializer(),
                               tf.local_variables_initializer())
            self.sess.run(init_op)
            saver = tf.train.Saver()
            ckpt = tf.train.get_checkpoint_state(self.model_path)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(self.sess, ckpt.model_checkpoint_path)
                print("restore and continue training!")
            else:
                pass
Ejemplo n.º 17
0
def main():
    eval_graph = tf.Graph()
    with eval_graph.as_default():
        inp = tf.placeholder(dtype=tf.float32, shape=[None, config.image_size, config.image_size, 3], name='input')
        # phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
        phase_train_placeholder = tf.placeholder_with_default(input=False, shape=[], name='phase_train')
        # batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
        keep_probability_placeholder = tf.placeholder_with_default(input=1., shape=[], name='keep_probability')
        # keep_probability_placeholder = tf.placeholder(tf.float32, name='keep_probability')
        with tf.variable_scope('quantize'):
            prelogits, _ = network.inference(inp,
                                             keep_probability=keep_probability_placeholder,
                                             phase_train=phase_train_placeholder,
                                             bottleneck_layer_size=config.embedding_size,
                                             weight_decay=config.weight_decay)
           
            prelogits = tf.maximum(prelogits, -1e27)

        g = tf.get_default_graph()
        tf.contrib.quantize.create_eval_graph(input_graph=g)
        saver = tf.train.Saver()

        eval_graph.finalize()
        with open('eval.pb', 'w') as f:
            f.write(str(g.as_graph_def()))

    with tf.Session(graph=eval_graph) as session:
        checkpoint = tf.train.latest_checkpoint('/homes/smeshkinfamfard/PycharmProjects/tensorflow-facenet/model/')
        # import pdb;pdb.set_trace()
        saver.restore(session, checkpoint)
        fix_error()
        # fix the input, output, choose types of the weights and activations for the tflite model
        converter = lite.TFLiteConverter.from_session(session, [inp], [prelogits])
        converter.inference_type = tf.uint8
        converter.inference_input_type = tf.uint8
        input_arrays = converter.get_input_arrays()
        converter.quantized_input_stats = {input_arrays[0]: (0., 1.)}

        flatbuffer = converter.convert()

        with open('test.tflite', 'wb') as outfile:
            outfile.write(flatbuffer)
    print('Model successfully converted and saved in the project directory')
Ejemplo n.º 18
0
def main():

    if (not handle_args()):
        # invalid arguments exit program
        print_usage()
        return 1

    with tf.Graph().as_default():
        image = tf.placeholder("float", shape=[1, image_size, image_size, 3], name='input')
        prelogits, _ = network.inference(image, 1.0, phase_train=False)
        normalized = tf.nn.l2_normalize(prelogits, 1, name='l2_normalize')
        output = tf.identity(normalized, name='output')
        saver = tf.train.Saver(tf.global_variables())

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())
            index_file_name = model_base_file_name + ".index"
            data_file_name = model_base_file_name + ".data-00000-of-00001"
            meta_file_name = model_base_file_name + ".meta"
            if (os.path.isfile(index_file_name) and
                os.path.isfile(data_file_name) and
                os.path.isfile(meta_file_name)):
                print('Restoring ' + model_base_file_name)
                saver.restore(sess, model_base_file_name)
            else:
                print('\n')
                print('Error, did not find index, data, and meta files: ')
                print('  ' + index_file_name)
                print('  ' + data_file_name)
                print('  ' + meta_file_name)

                print('These files can be downloaded manually from:')
                print('  https://github.com/davidsandberg/facenet')
                print('    Download: 20170511-185253.zip (web faces), or ')
                print('              20170512-110547.zip (celeb faces)')
                print('after unzipping be sure to rename them to the file names')
                print('above to match the TensorFlow 1.3 file naming.')
                print('Either the celeb faces or web faces will work')
                print('***********************************************************')

            # Save the network for fathom
            saver.save(sess, model_base_file_name + '_ncs/' + model_base_file_name + '_ncs')
 def build_model(self,
                 dropout_keep_prob=0.8,
                 is_training=True,
                 bottleneck_layer_size=128,
                 weight_decay=0.0):
     """
     Builds the inception model.
     :param dropout_keep_prob: float, the fraction to keep before final layer
     :param is_training: Whether is training or not
     :param bottleneck_layer_size: The size of the logits outputs of the model
     :param weight_decay: This penalizes large weights
     :return: The normalized logits outputs of the model
     """
     # A 4-D tensor of size [batch_size, height, width, 3].
     inputs_ph = tf.placeholder(tf.float32,
                                shape=self.inputs_shape,
                                name='anchor')
     net, end_points = inference(inputs_ph, dropout_keep_prob, is_training,
                                 bottleneck_layer_size, weight_decay)
     embeddings = tf.nn.l2_normalize(net, 1, 1e-10, name='embeddings')
     return inputs_ph, embeddings
Ejemplo n.º 20
0
def main(argv=None):

	with tf.Graph(). as_default():

		BATCH_SIZE = 3000
		NUM = 3000

		train_anchor_data = tf.placeholder(tf.float32, shape=[BATCH_SIZE, 128, 128, 3], name='anchor')

		_, prelogits, _ = inception_resnet_v1.inference(train_anchor_data, keep_probability=1.0,
		                                 phase_train=False, bottleneck_layer_size=30,
		                                 weight_decay=0.0005)

		logits = tf.nn.softmax(prelogits)

		Saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)

		data = data_process.input_test_data()
		print(data.shape)

		with tf.Session() as sess:

			sess.run(tf.global_variables_initializer())
			Saver.restore(sess, '/home/lenovo/yql/pig_data/triplet_model/logs_tensorboard5/')
			print('Initialized!')
			prob = []
			for step in range(NUM // BATCH_SIZE):
				batch_anchor = data[step * BATCH_SIZE:(step + 1) * BATCH_SIZE]

				feed_dict = {train_anchor_data: batch_anchor}
				Logits = sess.run(logits, feed_dict=feed_dict)

				print("%d the %s train reslut" % (step, datetime.datetime.now()))
				print('the i %g' % step)
				prob.extend(Logits)
		print(np.shape(prob))
		np.savetxt('test_image_result.csv', prob, fmt='%g')
		for i in np.max(prob, 1):
			print(i)
	print(np.mean(np.max(prob, 1)))
Ejemplo n.º 21
0
def main(args):

    #network = importlib.import_module(args.model_def)

    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
    utils.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__))
    utils.store_revision_info(src_path, log_dir, ' '.join(sys.argv))

    np.random.seed(seed=args.seed)

    train_set = utils.get_dataset(args.data_dir)
    nrof_classes = len(train_set)
    print('nrof_classes: ', nrof_classes)
    image_list, label_list = utils.get_image_paths_and_labels(train_set)
    image_list = np.array(image_list)
    label_list = np.array(label_list, dtype=np.int32)

    dataset_size = len(image_list)
    single_batch_size = args.people_per_batch * args.images_per_person
    indices = range(dataset_size)
    np.random.shuffle(indices)

    def _sample_people_softmax(x):
        global softmax_ind
        if softmax_ind >= dataset_size:
            np.random.shuffle(indices)
            softmax_ind = 0
        true_num_batch = min(single_batch_size, dataset_size - softmax_ind)

        sample_paths = image_list[indices[softmax_ind:softmax_ind +
                                          true_num_batch]]
        sample_labels = label_list[indices[softmax_ind:softmax_ind +
                                           true_num_batch]]

        softmax_ind += true_num_batch

        return (np.array(sample_paths), np.array(sample_labels,
                                                 dtype=np.int32))

    def _sample_people(x):
        '''We sample people based on tf.data, where we can use transform and prefetch.

        '''

        image_paths, num_per_class = sample_people(
            train_set, args.people_per_batch * (args.num_gpus - 1),
            args.images_per_person)
        labels = []
        for i in range(len(num_per_class)):
            labels.extend([i] * num_per_class[i])
        return (np.array(image_paths), np.array(labels, dtype=np.int32))

    def _parse_function(filename, label):
        file_contents = tf.read_file(filename)
        image = tf.image.decode_image(file_contents, channels=3)
        #image = tf.image.decode_jpeg(file_contents, channels=3)
        print(image.shape)

        if args.random_crop:
            print('use random crop')
            image = tf.random_crop(image,
                                   [args.image_size, args.image_size, 3])
        else:
            print('Not use random crop')
            #image.set_shape((args.image_size, args.image_size, 3))
            image.set_shape((None, None, 3))
            image = tf.image.resize_images(image,
                                           size=(args.image_height,
                                                 args.image_width))
            #print(image.shape)
        if args.random_flip:
            image = tf.image.random_flip_left_right(image)

        #pylint: disable=no-member
        #image.set_shape((args.image_size, args.image_size, 3))
        image.set_shape((args.image_height, args.image_width, 3))
        if debug:
            image = tf.cast(image, tf.float32)
        else:
            image = tf.image.per_image_standardization(image)
        return image, label

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

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

        # Placeholder for the learning rate
        learning_rate_placeholder = tf.placeholder(tf.float32,
                                                   name='learning_rate')

        phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')

        #the image is generated by sequence
        with tf.device("/cpu:0"):

            softmax_dataset = tf_data.Dataset.range(args.epoch_size *
                                                    args.max_nrof_epochs * 100)
            softmax_dataset = softmax_dataset.map(lambda x: tf.py_func(
                _sample_people_softmax, [x], [tf.string, tf.int32]))
            softmax_dataset = softmax_dataset.flat_map(_from_tensor_slices)
            softmax_dataset = softmax_dataset.map(_parse_function,
                                                  num_threads=8,
                                                  output_buffer_size=2000)
            softmax_dataset = softmax_dataset.batch(args.num_gpus *
                                                    single_batch_size)
            softmax_iterator = softmax_dataset.make_initializable_iterator()
            softmax_next_element = softmax_iterator.get_next()
            softmax_next_element[0].set_shape(
                (args.num_gpus * single_batch_size, args.image_height,
                 args.image_width, 3))
            softmax_next_element[1].set_shape(args.num_gpus *
                                              single_batch_size)
            batch_image_split = tf.split(softmax_next_element[0],
                                         args.num_gpus)
            batch_label_split = tf.split(softmax_next_element[1],
                                         args.num_gpus)

        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)

        print('Using optimizer: {}'.format(args.optimizer))
        if args.optimizer == 'ADAGRAD':
            opt = tf.train.AdagradOptimizer(learning_rate)
        elif args.optimizer == 'MOM':
            opt = tf.train.MomentumOptimizer(learning_rate, 0.9)
        elif args.optimizer == 'ADAM':
            opt = tf.train.AdamOptimizer(learning_rate,
                                         beta1=0.9,
                                         beta2=0.999,
                                         epsilon=0.1)
        else:
            raise Exception("Not supported optimizer: {}".format(
                args.optimizer))
        tower_losses = []
        tower_cross = []
        tower_dist = []
        tower_reg = []
        for i in range(args.num_gpus):
            with tf.device("/gpu:" + str(i)):
                with tf.name_scope("tower_" + str(i)) as scope:
                    with slim.arg_scope([slim.model_variable, slim.variable],
                                        device="/cpu:0"):
                        with tf.variable_scope(
                                tf.get_variable_scope()) as var_scope:
                            reuse = False if i == 0 else True
                            #with slim.arg_scope(resnet_v2.resnet_arg_scope(args.weight_decay)):
                            #prelogits, end_points = resnet_v2.resnet_v2_50(batch_image_split[i],is_training=True,
                            #        output_stride=16,num_classes=args.embedding_size,reuse=reuse)
                            #prelogits, end_points = network.inference(batch_image_split[i], args.keep_probability,
                            #    phase_train=phase_train_placeholder, bottleneck_layer_size=args.embedding_size,
                            #    weight_decay=args.weight_decay, reuse=reuse)
                            if args.network == 'sphere_network':
                                prelogits = network.infer(batch_image_split[i])
                            elif args.network == 'resface':
                                prelogits, _ = resface.inference(
                                    batch_image_split[i],
                                    1.0,
                                    weight_decay=args.weight_decay,
                                    reuse=reuse)
                            elif args.network == 'inception_net':
                                prelogits, endpoints = inception_net.inference(
                                    batch_image_split[i],
                                    1,
                                    phase_train=True,
                                    bottleneck_layer_size=args.embedding_size,
                                    weight_decay=args.weight_decay,
                                    reuse=reuse)

                            elif args.network == 'resnet_v2':
                                with slim.arg_scope(
                                        resnet_v2.resnet_arg_scope(
                                            args.weight_decay)):
                                    prelogits, end_points = resnet_v2.resnet_v2_50(
                                        batch_image_split[i],
                                        is_training=True,
                                        output_stride=16,
                                        num_classes=args.embedding_size,
                                        reuse=reuse)
                                    prelogits = tf.squeeze(prelogits,
                                                           axis=[1, 2])

                            else:
                                raise Exception(
                                    "Not supported network: {}".format(
                                        args.network))
                            if args.fc_bn:

                                prelogits = slim.batch_norm(
                                    prelogits,
                                    is_training=True,
                                    decay=0.997,
                                    epsilon=1e-5,
                                    scale=True,
                                    updates_collections=tf.GraphKeys.
                                    UPDATE_OPS,
                                    reuse=reuse,
                                    scope='softmax_bn')
                            if args.loss_type == 'softmax':
                                cross_entropy_mean = utils.softmax_loss(
                                    prelogits, batch_label_split[i],
                                    len(train_set), args.weight_decay, reuse)
                                regularization_losses = tf.get_collection(
                                    tf.GraphKeys.REGULARIZATION_LOSSES)
                                tower_cross.append(cross_entropy_mean)
                                #loss = cross_entropy_mean + args.weight_decay*tf.add_n(regularization_losses)
                                loss = cross_entropy_mean + tf.add_n(
                                    regularization_losses)
                                #tower_dist.append(0)
                                #tower_cross.append(cross_entropy_mean)
                                #tower_th.append(0)
                                tower_losses.append(loss)
                                tower_reg.append(regularization_losses)
                            elif args.loss_type == 'cosface':
                                label_reshape = tf.reshape(
                                    batch_label_split[i], [single_batch_size])
                                label_reshape = tf.cast(
                                    label_reshape, tf.int64)
                                coco_loss = utils.cos_loss(prelogits,
                                                           label_reshape,
                                                           len(train_set),
                                                           reuse,
                                                           alpha=args.alpha,
                                                           scale=args.scale)
                                #regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
                                #reg_loss  = args.weight_decay*tf.add_n(regularization_losses)
                                reg_loss = tf.get_collection(
                                    tf.GraphKeys.REGULARIZATION_LOSSES)
                                loss = coco_loss + reg_loss

                                tower_losses.append(loss)
                                tower_reg.append(reg_loss)

                            #loss = tf.add_n([cross_entropy_mean] + regularization_losses, name='total_loss')
                            tf.get_variable_scope().reuse_variables()
        total_loss = tf.reduce_mean(tower_losses)
        total_reg = tf.reduce_mean(tower_reg)
        losses = {}
        losses['total_loss'] = total_loss
        losses['total_reg'] = total_reg

        grads = opt.compute_gradients(total_loss,
                                      tf.trainable_variables(),
                                      colocate_gradients_with_ops=True)
        apply_gradient_op = opt.apply_gradients(grads, global_step=global_step)
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
        with tf.control_dependencies(update_ops):
            train_op = tf.group(apply_gradient_op)

        save_vars = [
            var for var in tf.global_variables()
            if 'Adagrad' not in var.name and 'global_step' not in var.name
        ]

        #saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)
        saver = tf.train.Saver(save_vars, 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,
                                                allow_soft_placement=True))

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

        #sess.run(iterator.initializer)
        sess.run(softmax_iterator.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():
            #pdb.set_trace()

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

            # Training and validation loop
            epoch = 0
            while epoch < args.max_nrof_epochs:
                step = sess.run(global_step, feed_dict=None)
                epoch = step // args.epoch_size
                if debug:
                    debug_train(args, sess, train_set, epoch,
                                image_batch_gather, enqueue_op,
                                batch_size_placeholder, image_batch_split,
                                image_paths_split, num_per_class_split,
                                image_paths_placeholder,
                                image_paths_split_placeholder,
                                labels_placeholder, labels_batch,
                                num_per_class_placeholder,
                                num_per_class_split_placeholder, len(gpus))
                # Train for one epoch
                train(args, sess, epoch, learning_rate_placeholder,
                      phase_train_placeholder, global_step, losses, train_op,
                      summary_op, summary_writer,
                      args.learning_rate_schedule_file)

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

    return model_dir
def main(argv=None):

    with tf.Graph().as_default():

        global_step = tf.Variable(0, trainable=False)
        CENTER_LOSS_ALPHA = 1.25
        BATCH_SIZE = 256
        ITERATION = 2000000
        data_num = 16640
        NUM_CLASSES = 30
        train_anchor_data = tf.placeholder(tf.float32,
                                           shape=[BATCH_SIZE, 128, 128, 3],
                                           name='anchor')

        labels_anchor = tf.placeholder(tf.int32, shape=[BATCH_SIZE])
        tf.summary.image('input_image', train_anchor_data, 10)

        features, prelogits, logits = inception_resnet_v1.inference(
            train_anchor_data,
            keep_probability=0.8,
            phase_train=True,
            bottleneck_layer_size=30,
            weight_decay=0.0005)
        print(features, prelogits, logits)
        with tf.name_scope('loss'):
            with tf.name_scope('center_loss'):
                center_loss, centers, centers_update_op = utils.get_center_loss(
                    features, labels_anchor, CENTER_LOSS_ALPHA, NUM_CLASSES)
            with tf.name_scope('softmax_loss'):
                softmax_loss = tf.reduce_mean(
                    tf.nn.sparse_softmax_cross_entropy_with_logits(
                        labels=labels_anchor, logits=prelogits))
            with tf.name_scope('total_loss'):
                total_loss = softmax_loss + center_loss

        # global_step = tf.Variable(tf.constant(431, dtype=tf.int64))
        lr = tf.train.exponential_decay(learning_rate=0.001,
                                        global_step=global_step,
                                        decay_steps=data_num // BATCH_SIZE,
                                        decay_rate=0.99,
                                        staircase=True)
        with tf.control_dependencies([centers_update_op]):
            train_op = tf.train.AdamOptimizer(learning_rate=lr).minimize(
                total_loss, global_step=global_step)
        Saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)

        tf.summary.scalar('total_loss', total_loss)
        tf.summary.scalar('learning_rate', lr)
        tf.summary.scalar('softmax_loss', softmax_loss)
        tf.summary.scalar('center_loss', center_loss)
        merged = tf.summary.merge_all()

        data, labels = data_process.input_data()
        print(data.shape)
        print(labels.shape)
        dataShufflu = DataShuffle(data, labels)
        with tf.Session() as sess:
            train_write = tf.summary.FileWriter(
                '/home/lenovo/yql/pig_data/triplet_model/logs_tensorboard4/',
                sess.graph)

            sess.run(tf.global_variables_initializer())
            # sess.run(tf.local_variables_initializer())
            # Saver.restore(sess, '/home/lenovo/yql/pig_data/triplet_model/logs_tensorboard2/')

            for step in range(ITERATION):
                batch_anchor, batch_labels_anchor = dataShufflu.get_triplet(
                    n_labels=30, n_triplet=BATCH_SIZE)

                feed_dict = {
                    train_anchor_data: batch_anchor,
                    labels_anchor: batch_labels_anchor
                }

                _, l, summary, Loss = sess.run(
                    [train_op, total_loss, merged, softmax_loss],
                    feed_dict=feed_dict)

                print("%d the %s train reslut" %
                      (step, datetime.datetime.now()))
                print('the softmax loss %g' % Loss)
                print('the total loss %g' % l)

                train_write.add_summary(summary, step)

                if step % 500 == 0:
                    Saver.save(
                        sess,
                        '/home/lenovo/yql/pig_data/triplet_model/logs_tensorboard4/'
                    )

            train_write.close()
Ejemplo n.º 23
0
def main():
    image_size = (config.image_size, config.image_size)
    #创建graph和model存放目录
    if not os.path.exists(config.graph_dir):
        os.mkdir(config.graph_dir)
    if not os.path.exists(config.model_dir):
        os.mkdir(config.model_dir)
    #获取图片地址和类别
    dataset = get_dataset(config.data_dir)
    #划分训练验证集
    if config.validation_set_split_ratio > 0.0:
        train_set, val_set = split_dataset(
            dataset, config.validation_set_split_ratio,
            config.min_nrof_val_images_per_class)
    else:
        train_set, val_set = dataset, []
    #训练集的种类数量
    nrof_classes = len(train_set)
    with tf.Graph().as_default():
        global_step = tf.Variable(0, trainable=False)
        #获取所有图像位置和相应类别
        image_list, label_list = get_image_paths_and_labels(train_set)
        assert len(image_list) > 0, '训练集不能为空'
        val_image_list, val_label_list = get_image_paths_and_labels(val_set)

        labels = tf.convert_to_tensor(label_list, dtype=tf.int32)
        #样本数量
        range_size = labels.get_shape().as_list()[0]
        #每一各epoch的batch数量
        epoch_size = range_size // config.batch_size
        #创建一个队列
        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 * epoch_size, 'index_dequeue')

        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='label')
        keep_probability_placeholder = tf.placeholder(tf.float32,
                                                      name='keep_probability')

        nrof_preprocess_threads = 4
        #输入队列
        input_queue = tf.FIFOQueue(capacity=2000000,
                                   dtypes=[tf.string, tf.int32],
                                   shapes=[(1, ), (1, )],
                                   shared_name=None,
                                   name=None)
        enqueue_op = input_queue.enqueue_many(
            [image_paths_placeholder, labels_placeholder], name='enqueue_op')
        #获取图像,label的batch形式
        image_batch, label_batch = 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,
            keep_probability_placeholder,
            phase_train=phase_train_placeholder,
            bottleneck_layer_size=config.embedding_size,
            weight_decay=config.weight_decay)
        #用于计算loss
        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)
        #正则化的embeddings主要用于测试,对比两张图片差异
        embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings')
        #计算centerloss
        prelogits_center_loss, _ = center_loss(prelogits, label_batch,
                                               config.center_loss_alfa,
                                               nrof_classes)
        tf.identity(prelogits_center_loss, name='center_loss')
        tf.summary.scalar('center_loss', prelogits_center_loss)
        #学习率
        boundaries = [
            int(epoch * range_size / config.batch_size)
            for epoch in config.LR_EPOCH
        ]
        lr_values = [
            config.learning_rate * (0.1**x)
            for x in range(0,
                           len(config.LR_EPOCH) + 1)
        ]
        learning_rate = tf.train.piecewise_constant(global_step, boundaries,
                                                    lr_values)
        tf.identity(learning_rate, name='learning_rate')
        tf.summary.scalar('learning_rate', learning_rate)
        #交叉熵损失
        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.identity(cross_entropy_mean, name='cross_entropy_mean')
        tf.summary.scalar('cross_entropy_mean', cross_entropy_mean)
        #l2正则loss
        L2_loss = tf.add_n(
            tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
        #总的loss
        total_loss = cross_entropy_mean + config.center_loss_factor * prelogits_center_loss + L2_loss
        tf.identity(total_loss, name='total_loss')
        tf.summary.scalar('total_loss', 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)
        tf.identity(accuracy, name='accuracy')
        tf.summary.scalar('accuracy', accuracy)

        train_op = optimize(total_loss, global_step, learning_rate,
                            config.moving_average_decay, tf.global_variables())
        saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)
        summary_op = tf.summary.merge_all()
        sess = tf.Session()
        sess.run(tf.global_variables_initializer())
        #训练和验证的graph保存地址
        train_writer = tf.summary.FileWriter(config.graph_dir + 'train/',
                                             sess.graph)
        val_writer = tf.summary.FileWriter(config.graph_dir + 'val/',
                                           sess.graph)
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(coord=coord, sess=sess)

        with sess.as_default():
            if os.path.exists(config.model_dir):
                model_file = tf.train.latest_checkpoint(config.model_dir)
                if model_file:
                    saver.restore(sess, model_file)
                    print('重载模型训练')

            if not os.path.exists(config.model_dir):
                os.mkdir(config.model_dir)
            for epoch in range(1, config.max_nrof_epochs + 1):
                step = sess.run(global_step, feed_dict=None)
                #训练
                batch_number = 0
                #获取image和label
                index_epoch = sess.run(index_dequeue_op)
                label_epoch = np.array(label_list)[index_epoch]
                image_epoch = np.array(image_list)[index_epoch]

                labels_array = np.expand_dims(np.array(label_epoch), 1)
                image_paths_array = np.expand_dims(np.array(image_epoch), 1)
                #运行输入队列
                sess.run(
                    enqueue_op, {
                        image_paths_placeholder: image_paths_array,
                        labels_placeholder: labels_array
                    })
                while batch_number < epoch_size:

                    feed_dict = {
                        phase_train_placeholder: True,
                        batch_size_placeholder: config.batch_size,
                        keep_probability_placeholder: config.keep_probability
                    }
                    tensor_list = [
                        total_loss, train_op, global_step, learning_rate,
                        prelogits, cross_entropy_mean, accuracy,
                        prelogits_center_loss
                    ]
                    #每经过100个batch更新一次graph
                    if batch_number % 100 == 0:

                        loss_, _, step_, lr_, prelogits_, cross_entropy_mean_, accuracy_, center_loss_, summary_str = sess.run(
                            tensor_list + [summary_op], feed_dict=feed_dict)
                        train_writer.add_summary(summary_str,
                                                 global_step=step_)
                        saver.save(sess=sess,
                                   save_path=config.model_dir + 'model.ckpt',
                                   global_step=(step_))
                        print('epoch:%d/%d' % (epoch, config.max_nrof_epochs))
                        print(
                            "Step: %d/%d, accuracy: %3f, center loss: %4f, cross loss: %4f, Total Loss: %4f ,lr:%f "
                            % (step_, epoch_size * config.max_nrof_epochs,
                               accuracy_, center_loss_, cross_entropy_mean_,
                               loss_, lr_))
                    else:
                        loss_, _, step_, lr_, prelogits_, cross_entropy_mean_, accuracy_, center_loss_, = sess.run(
                            tensor_list, feed_dict=feed_dict)
                    batch_number += 1
                train_writer.add_summary(summary_str, global_step=step_)
                #验证
                nrof_val_batches = len(val_label_list) // config.batch_size
                nrof_val_images = nrof_val_batches * config.batch_size

                labels_val_array = np.expand_dims(
                    np.array(val_label_list[:nrof_val_images]), 1)
                image_paths_val_array = np.expand_dims(
                    np.array(val_image_list[:nrof_val_images]), 1)
                #运行输入队列
                sess.run(
                    enqueue_op, {
                        image_paths_placeholder: image_paths_val_array,
                        labels_placeholder: labels_val_array
                    })
                loss_val_mean = 0
                center_loss_val_mean = 0
                cross_entropy_mean_val_mean = 0
                accuracy_val_mean = 0
                for i in range(nrof_val_batches):
                    feed_dict = {
                        phase_train_placeholder: False,
                        batch_size_placeholder: config.batch_size,
                        keep_probability_placeholder: 1.0
                    }
                    loss_val, center_loss_val, cross_entropy_mean_val, accuracy_val, summary_val = sess.run(
                        [
                            total_loss, prelogits_center_loss,
                            cross_entropy_mean, accuracy, summary_op
                        ],
                        feed_dict=feed_dict)
                    loss_val_mean += loss_val
                    center_loss_val_mean += center_loss_val
                    cross_entropy_mean_val_mean += cross_entropy_mean_val
                    accuracy_val_mean += accuracy_val
                    if i % 10 == 9:
                        print('.', end='')
                        sys.stdout.flush()
                val_writer.add_summary(summary_val, global_step=epoch)
                loss_val_mean /= nrof_val_batches
                center_loss_val_mean /= nrof_val_batches
                cross_entropy_mean_val_mean /= nrof_val_batches
                accuracy_val_mean /= nrof_val_batches
                print('到这了!')
                print(
                    "val: accuracy: %3f, center loss: %4f, cross loss: %4f, Total Loss: %4f "
                    % (accuracy_val_mean, center_loss_val_mean,
                       cross_entropy_mean_val_mean, loss_val_mean))
Ejemplo n.º 24
0
def main(args):
    # # 导入 model_def 代表的网络结构
    # network = importlib.import_module(args.model_def)

    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
    # 将 参数 写入到 text 文件中
    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
    # 将一些 git 修订信息存储在日志目录的文本文件中
    src_path, _ = os.path.split(os.path.realpath(__file__))
    facenet.store_revision_info(src_path, log_dir, ' '.join(sys.argv))

    # 获取 facenet 的数据集
    np.random.seed(seed=args.seed)
    # train_set = facenet.get_dataset(args.data_dir)

    # 训练数据集
    train_set = facenet.dataset_from_list(args.data_dir, args.list_file)
    nrof_classes = len(train_set)
    print('nrof_classes: ', nrof_classes)
    # 获取 图像 的 路径 和 labels
    image_list, label_list = facenet.get_image_paths_and_labels(train_set)
    print('total images: ', len(image_list))
    image_list = np.array(image_list)
    label_list = np.array(label_list, dtype=np.int32)

    dataset_size = len(image_list)
    # 单个 batch_size  = 每个 batch 中的人数 * 每个人的图像数
    single_batch_size = args.people_per_batch * args.images_per_person
    indices = range(dataset_size)
    np.random.shuffle(indices)

    # 从 dataset 中抽取 样本,将 image_path 和 image_label 返回
    def _sample_people_softmax(x):
        global softmax_ind
        if softmax_ind >= dataset_size:
            np.random.shuffle(indices)
            softmax_ind = 0
        true_num_batch = min(single_batch_size, dataset_size - softmax_ind)

        sample_paths = image_list[indices[softmax_ind:softmax_ind +
                                          true_num_batch]]
        sample_labels = label_list[indices[softmax_ind:softmax_ind +
                                           true_num_batch]]

        softmax_ind += true_num_batch

        return (np.array(sample_paths), np.array(sample_labels,
                                                 dtype=np.int32))

    def _sample_people(x):
        '''We sample people based on tf.data, where we can use transform and prefetch.
        Desc:
            我们基于 tf.data 对人进行抽样,这样我们可以使用 transform 和 prefetch 。
        '''

        image_paths, num_per_class = sample_people(
            train_set, args.people_per_batch * (args.num_gpus - 1),
            args.images_per_person)
        labels = []
        for i in range(len(num_per_class)):
            labels.extend([i] * num_per_class[i])
        return (np.array(image_paths), np.array(labels, dtype=np.int32))

    # 解析函数,将 image 的路径和 label 解析出来,对应着 image 和 label
    def _parse_function(filename, label):
        # 使用 tf.read_file() 进行读取,并使用 tf.image.decode_image() 进行转换为 tensor 的形式
        file_contents = tf.read_file(filename)
        image = tf.image.decode_image(file_contents, channels=3)
        #image = tf.image.decode_jpeg(file_contents, channels=3)
        print(image.shape)

        # 判断是否对图像进行随机裁剪
        if args.random_crop:
            print('use random crop')
            image = tf.random_crop(image,
                                   [args.image_size, args.image_size, 3])
        else:
            print('Not use random crop')
            #image.set_shape((args.image_size, args.image_size, 3))
            image.set_shape((None, None, 3))
            # 将图片进行 resize ,转换为我们传入的参数的大小
            image = tf.image.resize_images(image,
                                           size=(args.image_height,
                                                 args.image_width))
            #print(image.shape)
        # 判断是否进行随机水平翻转
        if args.random_flip:
            image = tf.image.random_flip_left_right(image)

        #pylint: disable=no-member
        #image.set_shape((args.image_size, args.image_size, 3))
        image.set_shape((args.image_height, args.image_width, 3))
        # 强制转换数据类型
        image = tf.cast(image, tf.float32)
        image = tf.subtract(image, 127.5)
        image = tf.div(image, 128.)
        #image = tf.image.per_image_standardization(image)
        return image, label

    # 将 model 目录和 log 目录先打印一下
    print('Model directory: %s' % model_dir)
    print('Log directory: %s' % log_dir)
    # # 如果已经提供了 预训练好的模型
    # if args.pretrained_model:
    #     # os.path.expanduser() 把路径中包含 ~ 或者 ~user 的地方转换为用户目录
    #     print('Pre-trained model: %s' % os.path.expanduser(args.pretrained_model))
    # # 如果提供了 lfw 数据,读取 lfw 目录中的 pairs 和 lfw 数据集中图像的 path
    # 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)
        # 全局的 step
        global_step = tf.Variable(0, trainable=False, name='global_step')

        # Placeholder for the learning rate
        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,3), name='image_paths')
        # labels_placeholder = tf.placeholder(tf.int64, shape=(None,3), name='labels')

        # input_queue = data_flow_ops.FIFOQueue(capacity=100000,
        #                             dtypes=[tf.string, tf.int64],
        #                             shapes=[(3,), (3,)],
        #                             shared_name=None, name=None)
        # enqueue_op = input_queue.enqueue_many([image_paths_placeholder, labels_placeholder])

        # the image is generated by sequence
        # 在 cpu 中将 训练数据集的 batch 进行拆分,分成 num_gpus 份数据
        with tf.device("/cpu:0"):

            softmax_dataset = tf_data.Dataset.range(args.epoch_size *
                                                    args.max_nrof_epochs * 100)
            softmax_dataset = softmax_dataset.map(lambda x: tf.py_func(
                _sample_people_softmax, [x], [tf.string, tf.int32]))
            softmax_dataset = softmax_dataset.flat_map(_from_tensor_slices)
            softmax_dataset = softmax_dataset.map(_parse_function,
                                                  num_parallel_calls=2000)
            softmax_dataset = softmax_dataset.batch(args.num_gpus *
                                                    single_batch_size)
            softmax_iterator = softmax_dataset.make_initializable_iterator()
            softmax_next_element = softmax_iterator.get_next()
            softmax_next_element[0].set_shape(
                (args.num_gpus * single_batch_size, args.image_height,
                 args.image_width, 3))
            softmax_next_element[1].set_shape(args.num_gpus *
                                              single_batch_size)
            batch_image_split = tf.split(softmax_next_element[0],
                                         args.num_gpus)
            batch_label_split = tf.split(softmax_next_element[1],
                                         args.num_gpus)

            # # 在整体数据集上选出 3 元组(triplets)
            # select_start_time = time.time()
            # # Select triplets based on the embeddings
            # print('Selecting suitable triplets for training')
            # # 修改版本的 triplets
            # nrof_examples = args.people_per_batch * args.images_per_person
            # triplets, nrof_random_negs, nrof_triplets = select_triplets(emb_array, num_per_class,
            #     image_paths, args.people_per_batch, args.alpha)
            # selection_time = time.time() - start_time
            # print('(nrof_random_negs, nrof_triplets) = (%d, %d): time=%.3f seconds' %
            #     (nrof_random_negs, nrof_triplets, selection_time))

        # 学习率设置
        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)
        # 优化器设置
        print('Using optimizer: {}'.format(args.optimizer))
        if args.optimizer == 'ADAGRAD':
            opt = tf.train.AdagradOptimizer(learning_rate)
        elif args.optimizer == 'MOM':
            opt = tf.train.MomentumOptimizer(learning_rate, 0.9)
        elif args.optimizer == 'ADAM':
            opt = tf.train.AdamOptimizer(learning_rate,
                                         beta1=0.9,
                                         beta2=0.999,
                                         epsilon=0.1)
        else:
            raise Exception("Not supported optimizer: {}".format(
                args.optimizer))

        tower_losses = []
        tower_cross = []
        tower_dist = []
        tower_reg = []
        for i in range(args.num_gpus):
            with tf.device("/gpu:" + str(i)):
                with tf.name_scope("tower_" + str(i)) as scope:
                    with slim.arg_scope([slim.model_variable, slim.variable],
                                        device="/cpu:0"):
                        with tf.variable_scope(
                                tf.get_variable_scope()) as var_scope:
                            reuse = False if i == 0 else True
                            if args.network == 'inception_resnet_v1':
                                with tf.variable_scope(name_or_scope='',
                                                       reuse=tf.AUTO_REUSE):
                                    prelogits, _ = inception_net.inference(
                                        batch_image_split[i],
                                        args.keep_probability,
                                        phase_train=phase_train_placeholder,
                                        bottleneck_layer_size=args.
                                        embedding_size,
                                        weight_decay=args.weight_decay)
                                print(prelogits)

                            # elif args.network == 'inception_net_v2':
                            #     with tf.variable_scope(name_or_scope='', reuse=tf.AUTO_REUSE):
                            #         prelogits, _ = inception_net_v2.inference(image_batch, args.keep_probability,
                            #         phase_train=phase_train_placeholder, bottleneck_layer_size=args.embedding_size,
                            #         weight_decay=args.weight_decay)
                            #     print(prelogits)
                            # elif args.network == 'squeezenet':
                            #     with tf.variable_scope(name_or_scope='', reuse=tf.AUTO_REUSE):
                            #         prelogits, _ = squeezenet.inference(image_batch, args.keep_probability,
                            #         phase_train=phase_train_placeholder, bottleneck_layer_size=args.embedding_size,
                            #         weight_decay=args.weight_decay)
                            #     print(prelogits)
                            else:
                                raise Exception(
                                    "Not supported network: {}".format(
                                        args.network))
                            if args.fc_bn:

                                prelogits = slim.batch_norm(
                                    prelogits,
                                    is_training=True,
                                    decay=0.997,
                                    epsilon=1e-5,
                                    scale=True,
                                    updates_collections=tf.GraphKeys.
                                    UPDATE_OPS,
                                    reuse=reuse,
                                    scope='softmax_bn')
                            if args.loss_type == 'triplet':
                                embeddings = tf.nn.l2_normalize(
                                    prelogits, 1, 1e-10, name='embeddings')
                                # Split embeddings into anchor, positive and negative and calculate triplet loss
                                anchor, positive, negative = tf.unstack(
                                    tf.reshape(embeddings,
                                               [-1, 3, args.embedding_size]),
                                    3, 1)
                                triplet_loss = facenet.triplet_loss(
                                    anchor, positive, negative, args.alpha)
                                regularization_losses = tf.get_collection(
                                    tf.GraphKeys.REGULARIZATION_LOSSES)
                                if args.network == 'sphere_network':
                                    print(
                                        'reg loss using weight_decay * tf.add_n'
                                    )
                                    reg_loss = args.weight_decay * tf.add_n(
                                        regularization_losses)
                                else:
                                    print('reg loss using tf.add_n')
                                    # reg_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
                                    reg_loss = tf.add_n(regularization_losses)
                                loss = triplet_loss + reg_loss

                                tower_losses.append(loss)
                                tower_reg.append(reg_loss)
                            # elif args.loss_type =='cosface':

                            #loss = tf.add_n([cross_entropy_mean] + regularization_losses, name='total_loss')
                            tf.get_variable_scope().reuse_variables()
        # 计算 total loss
        total_loss = tf.reduce_mean(tower_losses)
        total_reg = tf.reduce_mean(tower_reg)
        losses = {}
        losses['total_loss'] = total_loss
        losses['total_reg'] = total_reg

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

        grads = opt.compute_gradients(total_loss,
                                      tf.trainable_variables(),
                                      colocate_gradients_with_ops=True)
        apply_gradient_op = opt.apply_gradients(grads, global_step=global_step)
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
        with tf.control_dependencies(update_ops):
            train_op = tf.group(apply_gradient_op)

        # Create a saver
        # saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)
        save_vars = [
            var for var in tf.global_variables()
            if 'Adagrad' not in var.name and 'global_step' not in var.name
        ]
        saver = tf.train.Saver(save_vars, 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,
                                                allow_soft_placement=True))

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

        sess.run(softmax_iterator.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():
            # 如果有预训练好的 model,那就进行 restore 操作,将模型加载进行以后的 测试阶段
            if args.pretrained_model:
                print('Restoring pretrained model: %s' % args.pretrained_model)
                saver.restore(sess, os.path.expanduser(args.pretrained_model))

            # Training and validation loop
            epoch = 0
            while epoch < args.max_nrof_epochs:
                step = sess.run(global_step, feed_dict=None)
                epoch = step // args.epoch_size
                # Train for one epoch
                train(args, sess, epoch, learning_rate_placeholder,
                      phase_train_placeholder, global_step, losses, train_op,
                      summary_op, summary_writer,
                      args.learning_rate_schedule_file)

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

                # # Evaluate on LFW
                # if args.lfw_dir:
                #     evaluate(sess, lfw_paths, embeddings, labels_batch, image_paths_placeholder, labels_placeholder,
                #             batch_size_placeholder, learning_rate_placeholder, phase_train_placeholder, enqueue_op, actual_issame, args.batch_size,
                #             args.lfw_nrof_folds, log_dir, step, summary_writer, args.embedding_size)
    # 将训练好的模型 返回
    return model_dir
Ejemplo n.º 25
0
#config paramss
nrof_age_classes = 10
nrof_gender_classes = 2
df_graph = tf.Graph()

with df_graph.as_default():
    image_batch = tf.identity(image_batch, 'input')
    label_age_batch = tf.identity(label_age_batch, 'label_age_batch')
    label_gender_batch = tf.identity(label_gender_batch, 'label_gender_batch')

with df_graph.as_default():

    phase_train_placeholder = tf.placeholder(tf.bool, name='Phase_train')
    age, gender, _ = inception_resnet_v1.inference(image_batch,
                                                   keep_probability=0.8,
                                                   phase_train=True,
                                                   bottleneck_layer_size=128)
    fn_age = slim.fully_connected(
        age,
        nrof_age_classes,
        activation_fn=None,
        weights_initializer=tf.truncated_normal_initializer(stddev=0.01),
        weights_regularizer=slim.l2_regularizer(1e-5),
        scopr='fn_age')
    fn_gender = slim.fully_connected(
        gender,
        nrof_gender_classes,
        activation_fn=None,
        weights_initializer=tf.truncated_normal_initializer(stddev=0.01),
        weights_regularizer=slim.l2_regularizer(1e-5),
        scopr='fn_gender')
for row in c.execute('select * from people_unknown order by old'):
    print(row)

# Load model and run graph
tf.reset_default_graph()
# sess = tf.InteractiveSession()
sess = tf.Session()
images_pl = tf.placeholder(tf.float32,
                           shape=[None, 160, 160, 3],
                           name='input_image')
images_norm = tf.map_fn(
    lambda frame: tf.image.per_image_standardization(frame), images_pl)
train_mode = tf.placeholder(tf.bool)
age_logits, gender_logits, _ = inception_resnet_v1.inference(
    images_norm,
    keep_probability=0.8,
    phase_train=train_mode,
    weight_decay=1e-5)

gender = tf.argmax(tf.nn.softmax(gender_logits), 1)
age_ = tf.cast(tf.constant([i for i in range(0, 101)]), tf.float32)
age = tf.reduce_sum(tf.multiply(tf.nn.softmax(age_logits), age_), axis=1)
init_op = tf.group(tf.global_variables_initializer(),
                   tf.local_variables_initializer())
sess.run(init_op)
saver = tf.train.Saver()
ckpt = tf.train.get_checkpoint_state("../gender_age_tf/models/")
if ckpt and ckpt.model_checkpoint_path:
    saver.restore(sess, ckpt.model_checkpoint_path)
    print("restore model!")
else:
Ejemplo n.º 27
0
 def _predict_drgan_multipie(self, reuse=False):
     '''
     网络训练
     :param reuse: True | False netG_encoder_gamma
     :return:inference_recognition
     '''
     self.softresult, self.logits_cl, self.encode_fr = nets.inference_recognition(
         self.batch_data, classnums=self.class_nums, reuse=reuse)
     noise = tf.random_uniform(shape=(self.batch_size, self.noise_z),
                               minval=-1,
                               maxval=1,
                               dtype=tf.float32,
                               name='input_noise')
     self.encode_add_z = tf.concat([self.encode_fr, noise], 1)
     self.output_syn_middle = nets.netG_deconder_gamma(self.encode_add_z,
                                                       self.output_channel,
                                                       reuse=reuse)
     self.output_syn_middle_profile, self.output_syn_middle_front = tf.split(
         self.output_syn_middle, 2, axis=0)
     self.output_syn_front = nets.merge_net_16_unet(self.output_syn_middle,
                                                    self.batch_data,
                                                    reuse=reuse)
     self.identity_real, _ = inference(self.batch_data,
                                       keep_prob=1,
                                       phase_train=False)
     self.identity_syn, _ = inference(self.output_syn_front,
                                      keep_prob=1,
                                      phase_train=False,
                                      reuse=True)
     self.output_syn, self.output_gt_syn = tf.split(self.output_syn_front,
                                                    2,
                                                    axis=0)
     self.real_logits = nets.Custom_netD_discriminator_adloss(
         self.gt_input_data, reuse=reuse)
     self.fake_logits = nets.Custom_netD_discriminator_adloss(
         self.output_syn, reuse=True)
     self.profile_content, self.front_content = tf.split(self.encode_fr,
                                                         2,
                                                         axis=0)
     # 生成图像的features
     self.syn_softmax,self.fake_logits_all, self.syn_encode \
         = resnet_yd(self.output_syn_front,reuse=True)
     self.syn_content, self.syn_front_content = tf.split(self.syn_encode,
                                                         2,
                                                         axis=0)
     # 计算cosine距离
     self.cosine_real = tf.divide(
         tf.reduce_sum(tf.multiply(self.profile_content,
                                   self.front_content),
                       axis=1),
         tf.multiply(
             tf.sqrt(tf.reduce_sum(tf.square(self.front_content), axis=1)),
             tf.sqrt(tf.reduce_sum(tf.square(self.profile_content),
                                   axis=1))))
     self.cosine_syn = tf.divide(
         tf.reduce_sum(tf.multiply(self.syn_content,
                                   self.syn_front_content),
                       axis=1),
         tf.multiply(
             tf.sqrt(tf.reduce_sum(tf.square(self.syn_content), axis=1)),
             tf.sqrt(
                 tf.reduce_sum(tf.square(self.syn_front_content), axis=1))))
        [image_paths_placeholder, labels_placeholder, control_placeholder],
        name='enqueue_op')

    image_batch, label_batch = data_utils.create_input_pipeline(
        input_queue, image_size, nrof_preprocess_threads, batch_size)

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

with df_graph.as_default():
    # training phase placeholder
    phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')

    # build the model here
    prelogits, _ = model.inference(image_batch,
                                   keep_prob,
                                   phase_train=phase_train_placeholder,
                                   bottleneck_layer_size=embedding_size)
    logits = slim.fully_connected(
        prelogits,
        nrof_classes,
        activation_fn=None,
        weights_initializer=slim.initializers.xavier_initializer(),
        weights_regularizer=slim.l2_regularizer(5e-4),
        scope='Logits',
        reuse=False)

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

with df_graph.as_default():
    # Add center loss
    prelogits_center_loss, _ = model_utils.center_loss(prelogits, label_batch,
Ejemplo n.º 29
0
def main(argv=None):

    with tf.Graph().as_default():

        BATCH_SIZE = 10
        NUM = 860

        train_anchor_data = tf.placeholder(tf.float32,
                                           shape=[BATCH_SIZE, 128, 128, 3],
                                           name='anchor')

        labels_anchor = tf.placeholder(tf.int32, shape=[BATCH_SIZE, 30])

        _, prelogits, _ = inception_resnet_v1.inference(
            train_anchor_data,
            keep_probability=1.0,
            phase_train=False,
            bottleneck_layer_size=30,
            weight_decay=0.0005)

        clas_loss = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(labels=labels_anchor,
                                                    logits=prelogits))
        logits = tf.nn.softmax(prelogits)
        clas_acc = tf.reduce_mean(tf.cast(
            tf.equal(tf.argmax(labels_anchor, 1), tf.argmax(logits, 1)),
            tf.float32),
                                  name='clas_acc')
        Saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)

        data, labels = data_process.input_valid_data()
        print(data.shape)
        print(labels.shape)

        with tf.Session() as sess:

            sess.run(tf.global_variables_initializer())
            Saver.restore(
                sess,
                '/home/lenovo/yql/pig_data/triplet_model/logs_tensorboard2/')
            print('Initialized!')
            loss = 0
            accuracy = 0
            for step in range(NUM // BATCH_SIZE):
                batch_anchor, batch_labels_anchor = data[step * BATCH_SIZE:(step + 1) * BATCH_SIZE],\
                                                    labels[step * BATCH_SIZE:(step + 1) * BATCH_SIZE]

                feed_dict = {
                    train_anchor_data: batch_anchor,
                    labels_anchor: batch_labels_anchor
                }
                Clas_loss, Clas_acc = sess.run([clas_loss, clas_acc],
                                               feed_dict=feed_dict)

                print("%d the %s train reslut" %
                      (step, datetime.datetime.now()))
                print('the Clas_loss %g' % Clas_loss)
                print('the Clas_acc %g' % Clas_acc)
                loss += Clas_loss
                accuracy += Clas_acc
            loss = loss / float(NUM // BATCH_SIZE)
            accuracy = accuracy / float(NUM // BATCH_SIZE)
            print(loss)
        print(accuracy)
Ejemplo n.º 30
0
def run_training(image_path, batch_size, epoch, model_path, log_dir, start_lr,
                 wd, kp):
    # Tell TensorFlow that the model will be built into the default Graph.
    with tf.Graph().as_default():
        # Create a session for running operations in the Graph.
        sess = tf.Session()

        # Input images and labels.
        images, age_labels, gender_labels, _ = inputs(
            path=get_files_name(image_path),
            batch_size=batch_size,
            num_epochs=epoch)

        # load network
        # face_resnet = face_resnet_v2_generator(101, 'channels_first')
        train_mode = tf.placeholder(tf.bool)
        age_logits, gender_logits, _ = inception_resnet_v1.inference(
            images,
            keep_probability=kp,
            phase_train=train_mode,
            weight_decay=wd)
        # Build a Graph that computes predictions from the inference model.
        # logits = face_resnet(images, train_mode)

        # if you want to transfer weight from another model,please uncomment below codes
        # sess = restore_from_source(sess,'./models')
        # if you want to transfer weight from another model,please uncomment above codes

        # Add to the Graph the loss calculation.
        age_cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=age_labels, logits=age_logits)
        age_cross_entropy_mean = tf.reduce_mean(age_cross_entropy)

        gender_cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=gender_labels, logits=gender_logits)
        gender_cross_entropy_mean = tf.reduce_mean(gender_cross_entropy)

        # l2 regularization
        total_loss = tf.add_n(
            [gender_cross_entropy_mean, age_cross_entropy_mean] +
            tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))

        age_ = tf.cast(tf.constant([i for i in range(0, 101)]), tf.float32)
        age = tf.reduce_sum(tf.multiply(tf.nn.softmax(age_logits), age_),
                            axis=1)
        abs_loss = tf.losses.absolute_difference(age_labels, age)

        gender_acc = tf.reduce_mean(
            tf.cast(tf.nn.in_top_k(gender_logits, gender_labels, 1),
                    tf.float32))

        tf.summary.scalar("age_cross_entropy", age_cross_entropy_mean)
        tf.summary.scalar("gender_cross_entropy", gender_cross_entropy_mean)
        tf.summary.scalar("total loss", total_loss)
        tf.summary.scalar("train_abs_age_error", abs_loss)
        tf.summary.scalar("gender_accuracy", gender_acc)

        # Add to the Graph operations that train the model.
        global_step = tf.Variable(0, name="global_step", trainable=False)
        lr = tf.train.exponential_decay(start_lr,
                                        global_step=global_step,
                                        decay_steps=3000,
                                        decay_rate=0.9,
                                        staircase=True)
        optimizer = tf.train.AdamOptimizer(lr)
        tf.summary.scalar("lr", lr)
        update_ops = tf.get_collection(
            tf.GraphKeys.UPDATE_OPS)  # update batch normalization layer
        with tf.control_dependencies(update_ops):
            train_op = optimizer.minimize(total_loss, global_step)

        # if you want to transfer weight from another model,please comment below codes
        init_op = tf.group(tf.global_variables_initializer(),
                           tf.local_variables_initializer())
        sess.run(init_op)
        # if you want to transfer weight from another model, please comment above codes

        merged = tf.summary.merge_all()
        train_writer = tf.summary.FileWriter(log_dir, sess.graph)

        # if you want to transfer weight from another model,please uncomment below codes
        # sess, new_saver = save_to_target(sess,target_path='./models/new/',max_to_keep=100)
        # if you want to transfer weight from another model, please uncomment above codes

        # if you want to transfer weight from another model,please comment below codes
        new_saver = tf.train.Saver(max_to_keep=100)
        ckpt = tf.train.get_checkpoint_state(model_path)
        if ckpt and ckpt.model_checkpoint_path:
            new_saver.restore(sess, ckpt.model_checkpoint_path)
            print("restore and continue training!")
        else:
            pass
        # if you want to transfer weight from another model, please comment above codes

        # Start input enqueue threads.
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        try:
            step = sess.run(global_step)
            start_time = time.time()
            while not coord.should_stop():
                # start_time = time.time()
                # Run one step of the model.  The return values are
                # the activations from the `train_op` (which is
                # discarded) and the `loss` op.  To inspect the values
                # of your ops or variables, you may include them in
                # the list passed to sess.run() and the value tensors
                # will be returned in the tuple from the call.
                _, summary = sess.run([train_op, merged], {train_mode: True})
                train_writer.add_summary(summary, step)
                # duration = time.time() - start_time
                # # Print an overview fairly often.
                if step % 100 == 0:
                    duration = time.time() - start_time
                    print('%.3f sec' % duration)
                    start_time = time.time()
                if step % 1000 == 0:
                    save_path = new_saver.save(sess,
                                               os.path.join(
                                                   model_path, "model.ckpt"),
                                               global_step=global_step)
                    print("Model saved in file: %s" % save_path)
                step = sess.run(global_step)
        except tf.errors.OutOfRangeError:
            print('Done training for %d epochs, %d steps.' % (epoch, step))
        finally:
            # When done, ask the threads to stop.
            save_path = new_saver.save(sess,
                                       os.path.join(model_path, "model.ckpt"),
                                       global_step=global_step)
            print("Model saved in file: %s" % save_path)
            coord.request_stop()
        # Wait for threads to finish.
        coord.join(threads)
        sess.close()
Ejemplo n.º 31
0
def test_once(image_path, batch_size, model_checkpoint_path):
    with tf.Graph().as_default():
        sess = tf.Session()
        images, age_labels, gender_labels, file_paths = inputs(
            path=get_files_name(image_path),
            batch_size=batch_size,
            num_epochs=1,
            allow_smaller_final_batch=True)
        train_mode = tf.placeholder(tf.bool)
        age_logits, gender_logits, _ = inception_resnet_v1.inference(
            images,
            keep_probability=0.8,
            phase_train=train_mode,
            weight_decay=1e-5)
        age_cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=age_labels, logits=age_logits)
        age_cross_entropy_mean = tf.reduce_mean(age_cross_entropy)

        gender_cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=gender_labels, logits=gender_logits)
        gender_cross_entropy_mean = tf.reduce_mean(gender_cross_entropy)
        total_loss = tf.add_n(
            [gender_cross_entropy_mean, age_cross_entropy_mean] +
            tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES),
            name="total_loss")

        age_ = tf.cast(tf.constant([i for i in range(0, 101)]), tf.float32)
        prob_age = tf.reduce_sum(tf.multiply(tf.nn.softmax(age_logits), age_),
                                 axis=1)
        abs_age_error = tf.losses.absolute_difference(prob_age, age_labels)

        prob_gender = tf.argmax(tf.nn.softmax(gender_logits), 1)
        gender_acc = tf.reduce_mean(
            tf.cast(tf.nn.in_top_k(gender_logits, gender_labels, 1),
                    tf.float32))
        init_op = tf.group(tf.global_variables_initializer(),
                           tf.local_variables_initializer())
        sess.run(init_op)
        saver = tf.train.Saver()
        saver.restore(sess, model_checkpoint_path)

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

        mean_error_age, mean_gender_acc, mean_loss = [], [], []
        try:
            while not coord.should_stop():
                prob_gender_val, real_gender, prob_age_val, real_age, image_val, gender_acc_val, abs_age_error_val, cross_entropy_mean_val, file_names = sess.run(
                    [
                        prob_gender, gender_labels, prob_age, age_labels,
                        images, gender_acc, abs_age_error, total_loss,
                        file_paths
                    ], {train_mode: False})
                mean_error_age.append(abs_age_error_val)
                mean_gender_acc.append(gender_acc_val)
                mean_loss.append(cross_entropy_mean_val)
                print("Age_MAE:%.2f,Gender_Acc:%.2f%%,Loss:%.2f" %
                      (abs_age_error_val, gender_acc_val * 100,
                       cross_entropy_mean_val))
        except tf.errors.OutOfRangeError:
            print('!!!TESTING DONE!!!')
        finally:
            # When done, ask the threads to stop.
            coord.request_stop()
        coord.join(threads)
        sess.close()
        return prob_age_val, real_age, prob_gender_val, real_gender, image_val, np.mean(
            mean_error_age), np.mean(mean_gender_acc), np.mean(
                mean_loss), file_names