Пример #1
0
def training(batch_sample, net_type, loss_layers):
    """
    Build training architecture.
    :param batch_sample: batch samples
    :param net_type: network type, e.g., half_googlenet, googlenet, vgg, etc.
    :param loss_layers: names of loss layers
    :return:
        tower_*: Feature towers
        loss_*: Loss of multiple stages.
        accuracy*: Accuracy for stages.
    """
    fn = get_net_func(net_type)
    spec = helper.get_data_spec(model_class=fn)
    input_sample = tf.concat(batch_sample[0:3], axis=0)
    input_mask = tf.cast(batch_sample[3], tf.float32)
    input_sample.set_shape([
        spec.batch_size * 3, spec.input_size[0], spec.input_size[1],
        spec.channels
    ])

    with tf.name_scope('feature_tower'):  # pylint: disable=not-context-manager
        feature_tower = fn({'data': input_sample},
                           is_training=True,
                           reuse=False)

    def _create_loss_stage(feature_tower, loss_weight, mask, feat_name,
                           stage_name):
        feat_stage = feature_tower.get_output_by_name(feat_name)
        feat_anc = tf.slice(feat_stage, [0, 0, 0, 0],
                            [spec.batch_size, -1, -1, -1])
        feat_easy = tf.slice(feat_stage, [spec.batch_size, 0, 0, 0],
                             [spec.batch_size, -1, -1, -1])
        feat_hard = tf.slice(feat_stage, [2 * spec.batch_size, 0, 0, 0],
                             [spec.batch_size, -1, -1, -1])

        # use hinge_loss_layer or softmin_loss_layer
        loss_stage, accuracy = softmin_loss_layer(feat_anc,
                                                  feat_easy,
                                                  feat_hard,
                                                  loss_weight,
                                                  mask,
                                                  name=stage_name)
        return loss_stage, accuracy

    loss_stages = []
    accuracys = []
    loss_num = len(loss_layers)
    for i in range(len(loss_layers)):
        if i == loss_num - 1:
            loss_weight = 1.0
        else:
            loss_weight = 0.5
        loss_stage, accuracy = _create_loss_stage(feature_tower, loss_weight,
                                                  input_mask, loss_layers[i],
                                                  'loss_stage' + str(i))
        loss_stages.append(loss_stage)
        accuracys.append(accuracy)

    _activation_summaries(tf.group(feature_tower.get_output()))
    return feature_tower, loss_stages, accuracys
Пример #2
0
def get_triplet_input_tensors(dataset_root, train_list, global_img_list, sess,
                              net_type):
    """
    Get input data tensor from triplet list and global image list
    :param triplet_list: each line contains three comma-separated index
    :param global_img_list: global image paths corresponding to the triplet indexes.
    :return: three data tensors
    """
    fn = get_net_func(net_type)
    spec = helper.get_data_spec(model_class=fn)
    iterator = _training_data_queue(dataset_root, spec, train_list,
                                    global_img_list, sess)
    return iterator
Пример #3
0
def training_google_landmark(batch_sample, net_type, loss_layers):
    fn = get_net_func(net_type)
    spec = helper.get_data_spec(model_class=fn)
    input_sample = tf.concat(batch_sample[:3], axis=0)
    input_sample.set_shape([
        spec.batch_size * 3, spec.input_size[0], spec.input_size[1],
        spec.channels
    ])

    with tf.name_scope('feature_tower'):  # pylint: disable=not-context-manager
        feature_tower = fn({'data': input_sample},
                           is_training=True,
                           reuse=False)

    def _create_loss_stage(feature_tower, loss_weight, mask, feat_name,
                           stage_name):
        feat_stage = feature_tower.get_output_by_name(feat_name)
        feat_anc = tf.slice(feat_stage, [0, 0, 0, 0],
                            [spec.batch_size, -1, -1, -1])
        feat_easy = tf.slice(feat_stage, [spec.batch_size, 0, 0, 0],
                             [spec.batch_size, -1, -1, -1])
        feat_hard = tf.slice(feat_stage, [2 * spec.batch_size, 0, 0, 0],
                             [spec.batch_size, -1, -1, -1])

        # use hinge_loss_layer or softmin_loss_layer
        loss_stage, accuracy = hinge_loss_layer(feat_anc,
                                                feat_easy,
                                                feat_hard,
                                                loss_weight,
                                                mask,
                                                name=stage_name)
        return loss_stage, accuracy

    loss_stages = []
    accuracys = []
    loss_num = len(loss_layers)
    for i in range(len(loss_layers)):
        if i == loss_num - 1:
            loss_weight = 1.0
        else:
            loss_weight = 0.5
        loss_stage, accuracy = _create_loss_stage(feature_tower, loss_weight,
                                                  None, loss_layers[i],
                                                  'loss_stage' + str(i))
        loss_stages.append(loss_stage)
        accuracys.append(accuracy)

    _activation_summaries(tf.group(feature_tower.get_output()))
    return feature_tower, loss_stages, accuracys
Пример #4
0
def inference(sess,
              img_list,
              net_type,
              rmac_step,
              output_tensor_name,
              reduce_method='L2'):
    """Model for feature inference.
    Args:
        img_list: a string list of image path.
    Returns:
        net: net graph definition.
        batch_size: batch size.
    """
    fn = get_net_func(net_type)
    spec = helper.get_data_spec(model_class=fn)

    # make image_list length to be multiples of batch_size
    image_num = len(img_list)
    if FLAGS.img_size is not None:  # each batch has spec.batch_size images
        last_batch_add = spec.batch_size - (image_num % spec.batch_size)
        if image_num % spec.batch_size != 0:
            for i in range(last_batch_add):
                img_list.append(img_list[image_num - 1])

    batch_img, batch_size = _testing_data_queue(spec, img_list)
    data_iterator = batch_img.get_next()
    image = tf.concat(data_iterator[1], axis=0)
    image.set_shape(
        [spec.batch_size, FLAGS.img_size, FLAGS.img_size, spec.channels])

    net = fn({'data': image}, is_training=False, reuse=False, fcn=True)
    feat_map = net.get_output_by_name(output_tensor_name)
    rvec = _rmac(feat_map, rmac_step, reduce_method, deploy=False)
    if FLAGS.multi_scale:
        rvec = multi_scale_feature(rvec,
                                   fn,
                                   image,
                                   output_tensor_name,
                                   rmac_step,
                                   reduce_method,
                                   deploy=False)
    sess.run(batch_img.initializer)
    return net, rvec, batch_size, data_iterator[0]
Пример #5
0
def deploy(net_type, rmac_step, output_tensor_name, reduce_method='MAX'):
    fn = get_net_func(net_type)
    spec = helper.get_data_spec(model_class=fn)
    image = tf.placeholder(tf.float32,
                           shape=(None, FLAGS.img_size, FLAGS.img_size,
                                  spec.channels),
                           name='input')
    net = fn({'data': image}, is_training=False, reuse=False, fcn=True)
    feat_map = net.get_output_by_name(output_tensor_name)
    rvec = _rmac(feat_map, rmac_step, reduce_method, deploy=True)
    if FLAGS.multi_scale:
        rvec = multi_scale_feature(rvec,
                                   fn,
                                   image,
                                   output_tensor_name,
                                   rmac_step,
                                   reduce_method,
                                   deploy=True)
    return net, rvec