Exemple #1
0
def graphDef():
    x = tf.placeholder(name='img_inputs', shape=[None, 112, 112, 3], dtype=tf.float32)
    w_init_method = tf.contrib.layers.xavier_initializer(uniform=False)
    w_init = w_init_method
    net = get_resnet(x, 100, type='ir', w_init=w_init_method, trainable=False, keep_rate=1.0)
    with tf.Session() as sess:
        graph_def = sess.graph.as_graph_def()
        with tf.gfile.FastGFile('./model/test.pb', 'wb') as f:
            f.write(graph_def.SerializeToString())
Exemple #2
0
                                     name='lr_schedule')
    #dp_rate = tf.train.piecewise_constant(global_step, boundaries=lr_steps, values=[0.4, 0.6, 0.8], name='dp_schedule')
    # 3.3 define the optimize method
    opt = tf.train.MomentumOptimizer(learning_rate=lr, momentum=args.momentum)

    # Calculate the gradients for each model tower.
    tower_grads = []
    tl.layers.set_name_reuse(True)
    loss_dict = {}
    drop_dict = {}
    loss_keys = []
    with tf.variable_scope(tf.get_variable_scope()):
      for i in range(args.num_gpus):
        with tf.device('/gpu:%d' % i):
          with tf.name_scope('%s_%d' % (args.tower_name, i)) as scope:
            net = get_resnet(images_s[i], args.net_depth, type='ir', w_init=w_init_method, trainable=trainable, keep_rate= args.dropout_rate)
            logit = arcface_loss(embedding=net.outputs, labels=labels_s[i], w_init=w_init_method, out_num=args.num_output)
            # Reuse variables for the next tower.
            tf.get_variable_scope().reuse_variables()
            # define the cross entropy
            inference_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logit, labels=labels_s[i]))
            # define weight deacy losses
            wd_loss = 0
            for weights in tl.layers.get_variables_with_name('W_conv2d', True, True):
                wd_loss += tf.contrib.layers.l2_regularizer(args.weight_deacy)(weights)
            for W in tl.layers.get_variables_with_name('resnet_v1_50/E_DenseLayer/W', True, True):
                wd_loss += tf.contrib.layers.l2_regularizer(args.weight_deacy)(W)
            for weights in tl.layers.get_variables_with_name('embedding_weights', True, True):
                wd_loss += tf.contrib.layers.l2_regularizer(args.weight_deacy)(weights)
            for gamma in tl.layers.get_variables_with_name('gamma', True, True):
                wd_loss += tf.contrib.layers.l2_regularizer(args.weight_deacy)(gamma)
Exemple #3
0
    # 3.3 define the optimize method
    opt = tf.train.MomentumOptimizer(learning_rate=lr, momentum=args.momentum)

    # Calculate the gradients for each model tower.
    tower_grads = []
    tl.layers.set_name_reuse(True)
    loss_dict = {}
    drop_dict = {}
    loss_keys = []
    with tf.variable_scope(tf.get_variable_scope()):
        for i in range(args.num_gpus):
            with tf.device('/gpu:%d' % i):
                with tf.name_scope('%s_%d' % (args.tower_name, i)) as scope:
                    net = get_resnet(images_s[i],
                                     args.net_depth,
                                     type='ir',
                                     w_init=w_init_method,
                                     trainable=trainable)
                    logit = arcface_loss(embedding=net.outputs,
                                         labels=labels_s[i],
                                         w_init=w_init_method,
                                         out_num=args.num_output)
                    # Reuse variables for the next tower.
                    tf.get_variable_scope().reuse_variables()
                    # define the cross entropy
                    inference_loss = tf.reduce_mean(
                        tf.nn.sparse_softmax_cross_entropy_with_logits(
                            logits=logit, labels=labels_s[i]))
                    # define weight deacy losses
                    wd_loss = 0
                    for weights in tl.layers.get_variables_with_name(
    # 3.3 define the optimize method
    opt = tf.train.MomentumOptimizer(learning_rate=lr, momentum=args.momentum)

    # Calculate the gradients for each model tower.
    tower_grads = []
    tl.layers.set_name_reuse(True)
    loss_dict = {}
    drop_dict = {}
    loss_keys = []
    with tf.variable_scope(tf.get_variable_scope()):
        for i in range(args.num_gpus):
            with tf.device('/cpu:0'):
                with tf.name_scope('%s_%d' % (args.tower_name, i)) as scope:
                    net = get_resnet(images_s[i],
                                     args.net_depth,
                                     type='ir',
                                     w_init=w_init_method,
                                     trainable=True,
                                     keep_rate=dropout_rate)
                    logit = arcface_loss(embedding=net.outputs,
                                         labels=labels_s[i],
                                         w_init=w_init_method,
                                         out_num=args.num_output)
                    # Reuse variables for the next tower.
                    tf.get_variable_scope().reuse_variables()
                    # define the cross entropy
                    inference_loss = tf.reduce_mean(
                        tf.nn.sparse_softmax_cross_entropy_with_logits(
                            logits=logit, labels=labels_s[i]))
                    # define weight deacy losses
                    wd_loss = 0
                    for weights in tl.layers.get_variables_with_name(
    lr = tf.train.piecewise_constant(global_step, boundaries=lr_steps, values=[0.001, 0.0005, 0.0003, 0.0001],
                                     name='lr_schedule')
    # 3.3 define the optimize method
    opt = tf.train.MomentumOptimizer(learning_rate=lr, momentum=args.momentum)

    # Calculate the gradients for each model tower.
    tower_grads = []
    tl.layers.set_name_reuse(True)
    loss_dict = {}
    drop_dict = {}
    loss_keys = []
    with tf.variable_scope(tf.get_variable_scope()):
      for i in range(args.num_gpus):
        with tf.device('/gpu:%d' % i):
          with tf.name_scope('%s_%d' % (args.tower_name, i)) as scope:
            net = get_resnet(images_s[i], args.net_depth, type='ir', w_init=w_init_method, trainable=True, keep_rate=dropout_rate)
            logit = arcface_loss(embedding=net.outputs, labels=labels_s[i], w_init=w_init_method, out_num=args.num_output)
            # Reuse variables for the next tower.
            tf.get_variable_scope().reuse_variables()
            # define the cross entropy
            inference_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logit, labels=labels_s[i]))
            # define weight deacy losses
            wd_loss = 0
            for weights in tl.layers.get_variables_with_name('W_conv2d', True, True):
                wd_loss += tf.contrib.layers.l2_regularizer(args.weight_deacy)(weights)
            for W in tl.layers.get_variables_with_name('resnet_v1_50/E_DenseLayer/W', True, True):
                wd_loss += tf.contrib.layers.l2_regularizer(args.weight_deacy)(W)
            for weights in tl.layers.get_variables_with_name('embedding_weights', True, True):
                wd_loss += tf.contrib.layers.l2_regularizer(args.weight_deacy)(weights)
            for gamma in tl.layers.get_variables_with_name('gamma', True, True):
                wd_loss += tf.contrib.layers.l2_regularizer(args.weight_deacy)(gamma)
Exemple #6
0
        data_set = load_bin(db, args.image_size, args)
        ver_list.append(data_set)
        ver_name_list.append(db)

    images = tf.placeholder(name='img_inputs',
                            shape=[None, *args.image_size, 3],
                            dtype=tf.float32)
    labels = tf.placeholder(name='img_labels', shape=[
        None,
    ], dtype=tf.int64)
    dropout_rate = tf.placeholder(name='dropout_rate', dtype=tf.float32)

    w_init_method = tf.contrib.layers.xavier_initializer(uniform=False)
    net = get_resnet(images,
                     args.net_depth,
                     type='ir',
                     w_init=w_init_method,
                     trainable=False,
                     keep_rate=0.5)
    embedding_tensor = net.outputs
    # mv_mean = tl.layers.get_variables_with_name('resnet_v1_50/bn0/moving_mean', False, True)[0]
    # 3.2 get arcface loss
    logit = arcface_loss(embedding=net.outputs,
                         labels=labels,
                         w_init=w_init_method,
                         out_num=args.num_output)

    sess = tf.Session()
    saver = tf.train.Saver()

    result_index = []
    for file_index in args.ckpt_index_list:
Exemple #7
0
        ver_name_list.append(db)

    images = tf.placeholder(name='img_inputs',
                            shape=[None, *args.image_size, 3],
                            dtype=tf.float32)  # (?, 112, 112, 3)
    labels = tf.placeholder(name='img_labels', shape=[
        None,
    ], dtype=tf.int64)  # (?, )
    dropout_rate = tf.placeholder(name='dropout_rate', dtype=tf.float32)

    w_init_method = tf.contrib.layers.xavier_initializer(
        uniform=False)  # 随机初始化权重先把空架子搭起来,后续再往里面restore train好的权重
    net = get_resnet(
        images,
        args.net_depth,
        type='ir',
        w_init=w_init_method,
        trainable=False,
        keep_rate=dropout_rate)  # L_Resnet_E_IR (?, 112,112,3)>(?, 512)
    embedding_tensor = net.outputs
    # mv_mean = tl.layers.get_variables_with_name('resnet_v1_50/bn0/moving_mean', False, True)[0]
    # 3.2 get arcface loss
    logit = arcface_loss(
        embedding=net.outputs,
        labels=labels,
        w_init=w_init_method,
        out_num=args.num_output)  # (?, 512),(?,),initializer ,85164

    gpu_config = tf.ConfigProto()
    gpu_config.gpu_options.allow_growth = True
    sess = tf.Session(config=gpu_config)
Exemple #8
0
 else:
     is_seblock = 'ir'
 # MGPU-start
 # Calculate the gradients for each model tower.
 tower_grads = []  # 保存来自不同GPU计算出的梯度、loss列表
 tl.layers.set_name_reuse(True)
 loss_dict = {}  # 保存来自不同GPU计算出的梯度、loss列表
 drop_dict = {}
 loss_keys = []
 with tf.variable_scope(tf.get_variable_scope()):
     for iter_gpus in num_gpus:
         with tf.device('/gpu:%d' % iter_gpus):
             with tf.name_scope('%s_%d' % (tower_name, iter_gpus)) as scope:
                 net = get_resnet(images_s[iter_gpus],
                                  net_depth,
                                  type=is_seblock,
                                  w_init=w_init_method,
                                  trainable=True,
                                  keep_rate=dropout_rate)
                 if is_arcloss == 1:
                     logit = arcface_loss(embedding=net.outputs,
                                          labels=labels[iter_gpus],
                                          w_init=w_init_method,
                                          out_num=num_output,
                                          s=loss_s,
                                          m=loss_m)
                 else:
                     logit = cosineface_losses(embedding=net.outputs,
                                               labels=labels_s[iter_gpus],
                                               w_init=w_init_method,
                                               out_num=num_output,
                                               s=loss_s,