Exemple #1
0
def inference_to_loss(image_batch, label_batch, is_training=True):
    with arg_scope(
            resnet_arg_scope_gn(weight_decay=FLAGS.weight_decay,
                                group_norm_num=32)):
        ########################### Define Network Structure ############################
        net, end_points = resnet_v1_50(image_batch,
                                       num_classes=FLAGS.num_classes,
                                       is_training=is_training,
                                       global_pool=True,
                                       rates=(1, 1, 1, 1),
                                       scope='resnet_v1_50_gn')

        logits = tf.squeeze(net, axis=[1, 2])
        loss = softmax_with_logits(logits,
                                   label_batch,
                                   ignore_labels=[],
                                   loss_collections=tf.GraphKeys.LOSSES,
                                   reduce_method='nonzero_mean')

        # add accuracy
        predictions = tf.argmax(logits, axis=-1, output_type=label_batch.dtype)
        label_batch = tf.squeeze(label_batch, axis=-1)

        accuracy = tf.reduce_mean(
            tf.cast(tf.equal(predictions, label_batch), tf.float32))
        metrics_loss, _ = tf.metrics.mean(
            loss, updates_collections=LOCAL_VARIABLE_UPDATE_OPS)
        metrics_acc, _ = tf.metrics.mean(
            accuracy, updates_collections=LOCAL_VARIABLE_UPDATE_OPS)

        with tf.name_scope('summary_input_output'):
            tf.summary.scalar('classfication_loss', metrics_loss)
            tf.summary.scalar('classfication_acc', metrics_acc)
    return loss, accuracy
def inference_to_loss(gpu_id):
    image_batch = image[gpu_id*FLAGS.batch_size:(gpu_id+1)*FLAGS.batch_size, ...]
    label_batch = label[gpu_id*FLAGS.batch_size:(gpu_id+1)*FLAGS.batch_size, ...]
    with arg_scope(xception_arg_scope_gn(weight_decay=FLAGS.weight_decay)):
        net, end_points = xception_65(image_batch,
                                      global_pool=True,
                                      num_classes=FLAGS.num_classes,
                                      is_training=True,
                                      output_stride=32,
                                      keep_prob=0.5)
        logits = tf.squeeze(net, axis=[1, 2])
        loss = softmax_with_logits(logits, label_batch,
                                   ignore_labels=[],
                                   loss_collections=tf.GraphKeys.LOSSES,
                                   reduce_method='nonzero_mean')

        # add accuracy
        predictions = tf.argmax(logits, axis=-1, output_type=label_batch.dtype)
        label_batch = tf.squeeze(label_batch, axis=-1)

        accuracy = tf.reduce_mean(tf.cast(tf.equal(predictions, label_batch), tf.float32))
        metrics_loss, _ = tf.metrics.mean(loss, updates_collections=LOCAL_VARIABLE_UPDATE_OPS)
        metrics_acc, _ = tf.metrics.mean(accuracy, updates_collections=LOCAL_VARIABLE_UPDATE_OPS)

        # add summaries
        with tf.name_scope('summary_input_output'):
            # tf.summary.image('image_batch', image_batch, max_outputs=3)
            # tf.summary.histogram('label_batch', label_batch)
            tf.summary.scalar('classfication_loss', metrics_loss)
            tf.summary.scalar('classfication_acc', metrics_acc)
    return loss, accuracy
Exemple #3
0
def _layer_loss(locations,
                scores,
                encode_locations,
                encode_labels,
                encode_ious,
                pos_th,
                neg_th,
                neg_ratio,
                background_label=0):
    """
    Calculate loss for one layer,
    encode_labels corresponds to the GT box with highest iou, but this iou can be less than neg_th!
    so need to process and create new labels !
    :param locations: predicted locations [1, H, W, K, 4 ]
    :param scores: predicted scores [1, H, W, K, 21]
    :param encode_locations: [H, W, K, 4]
    :param encode_labels: [H, W, K]
    :param encode_ious: [H, W, K]
    :return:
    """
    positive_mask = encode_ious > pos_th
    positive_num = tf.reduce_sum(tf.cast(positive_mask, tf.int32))

    # need to redefine the labels, those assgined to some class with iou < neg_th, should be assgined to background
    negative_mask = tf.logical_and(encode_ious < neg_th,
                                   tf.logical_not(positive_mask))
    negative_labels = tf.where(
        negative_mask, background_label * tf.cast(negative_mask, tf.int32),
        encode_labels)

    # calculate background scores
    neg_scores = tf.nn.softmax(scores, axis=-1)[:, :, :, background_label]
    neg_scores = tf.where(
        negative_mask,
        neg_scores,
        # set positive ones's negative score to be 1, so that it won't be count in top_k
        1.0 - tf.cast(negative_mask, tf.float32))
    # solve # negative, add one so that neg_values has more than one value
    max_negative_num = 1 + tf.reduce_sum(tf.cast(negative_mask, tf.int32))
    negative_num = tf.maximum(neg_ratio * positive_num,
                              tf.shape(encode_locations)[0] * 4)
    negative_num = tf.minimum(negative_num, max_negative_num)

    # Hard Negative Mining :
    # find those with lower background scores, but are indeed background!
    neg_values, _ = tf.nn.top_k(tf.reshape(-1.0 * neg_scores, [-1]),
                                k=negative_num)
    negative_mask = tf.logical_and(negative_mask, neg_scores < -neg_values[-1])

    positive_mask = tf.cast(positive_mask, tf.float32)
    negative_mask = tf.cast(negative_mask, tf.float32)

    with tf.name_scope('cross_entropy_loss'):
        with tf.name_scope('positive'):
            pos_loss = softmax_with_logits(predictions=scores,
                                           labels=encode_labels,
                                           ignore_labels=[],
                                           weights=tf.reshape(
                                               positive_mask, [-1]),
                                           loss_collections=LOSS_COLLECTIONS)
        with tf.name_scope('negative'):
            neg_loss = softmax_with_logits(predictions=scores,
                                           labels=negative_labels,
                                           ignore_labels=[],
                                           weights=tf.reshape(
                                               negative_mask, [-1]),
                                           loss_collections=LOSS_COLLECTIONS)

    with tf.name_scope('bbox_regression_loss'):
        bbox_loss = smooth_l1(locations - encode_locations)
        bbox_loss = tf.reduce_sum(bbox_loss, axis=-1)

        # [H*W*K]
        bbox_loss = tf.reduce_mean(bbox_loss * positive_mask,
                                   name='bbox_regression_loss')
        tf.add_to_collection(LOSS_COLLECTIONS, bbox_loss)

    return pos_loss, neg_loss, bbox_loss