【问题标题】:Categorical cross entropy with asymmetric error penalty具有不对称误差惩罚的分类交叉熵
【发布时间】:2021-10-02 11:55:46
【问题描述】:

我正在训练一个模型,该模型改编自 Matterport 的 implementation 的 Mask R-CNN,数据不平衡。我已经修改了一个损失函数,以使用真实类的单热表示将类权重应用于分类损失函数suggested previously,但我真正想做的是一个更一般的情况,我可以拥有权重矩阵为给定实际标签类别的预测类别提供不同的惩罚,如上一个答案的最后一段所述。

def mrcnn_class_loss_graph(target_class_ids, pred_class_logits,
                           active_class_ids):
    """Loss for the classifier head of Mask RCNN.
    batch = 1
    num_classes = 8  
    num_rois = variable
    target_class_ids: [batch, num_rois]. Integer class IDs. Uses zero padding to fill in the array.
    pred_class_logits: [batch, num_rois, num_classes]
    active_class_ids: [batch, num_classes]. Has a value of 1 for classes that are in the dataset of 
        the image, and 0 for classes that are not in the dataset.
    """
    # During model building, Keras calls this function with target_class_ids of type float32. 
    # Unclear why. Cast it to int to get around it.
    target_class_ids = tf.cast(target_class_ids, 'int64')

    # Find predictions of classes that are not in the dataset.
    pred_class_ids = tf.argmax(input=pred_class_logits, axis=2)
    pred_active = tf.gather(active_class_ids[0], pred_class_ids)

    # penalty_matrix = tf.constant([
    #         [0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
    #         [1.0, 0.0, 1.0, 0.6, 1.0, 0.2, 1.5, 1.0],
    #         [1.0, 1.0, 0.0, 0.2, 1.0, 0.2, 0.8, 1.0],
    #         [1.0, 0.4, 0.2, 0.0, 1.0, 0.2, 1.0, 1.0],
    #         [1.0, 1.2, 1.2, 1.2, 0.0, 1.5, 0.8, 0.8],
    #         [1.0, 0.5, 0.5, 0.5, 1.0, 0.0, 1.0, 1.0],
    #         [1.0, 2.0, 1.0, 1.0, 1.0, 0.5, 0.0, 1.0],
    #         [1.0, 1.0, 1.0, 1.0, 1.0, 0.8, 0.8, 0.0]
    #     ])

    class_weights = tf.constant([[1.0, 1.0, 0.5, 1.0, 2.0, 2.0, 2.0, 2.0]])

    one_hot = tf.one_hot(target_class_ids, depth = class_weights.shape[1], on_value=1.0, off_value=0.0)
    # deduce weights for batch samples based on their true label
    weights = tf.reduce_sum(class_weights * one_hot, axis=2)
    # compute your (unweighted) softmax cross entropy loss
    loss = tf.nn.softmax_cross_entropy_with_logits(labels=one_hot, logits=pred_class_logits)

    # apply the weights, relying on broadcasting of the multiplication
    loss = loss * weights

    # Erase losses of predictions of classes that are not in the active classes of the image.
    loss = loss * pred_active

    # Compute loss mean. Use only predictions that contribute to the loss to get a correct mean.
    loss = tf.reduce_sum(input_tensor=loss) / tf.reduce_sum(input_tensor=pred_active)

    return loss

在上面的代码中,我注释掉了一个潜在的 8x8 惩罚矩阵,其中每行 i 的条目 j 对应于错误识别类对象的惩罚i 作为类 j;对角线的零表示正确的分类。

我想知道如何根据真实类和预测类从这样的惩罚矩阵中正确(有效地)获取权重。我正在使用 TensorFlow 2.5 和 Python 3.8。

【问题讨论】:

    标签: python tensorflow deep-learning conv-neural-network


    【解决方案1】:

    PyTorch 的实现:

    import torch
    
    batch_size = 2
    num_classes = 3
    penalty_matrix = torch.tensor([[0, 2, 3], [1, 0, 2], [4, 5, 0]]).long()
    criterion = torch.nn.CrossEntropyLoss(reduction='none')  
    
    logits = torch.randn(batch_size, num_classes, requires_grad=True)
    target = torch.empty(batch_size, dtype=torch.long).random_(num_classes)
    
    output = criterion(logits, target)
    loss = output * penalty_matrix[logits.max(dim=-1)[1], target]
    
    loss.backward()
    

    【讨论】:

      猜你喜欢
      • 2017-10-23
      • 1970-01-01
      • 2020-01-03
      • 2019-07-13
      • 2021-07-17
      • 2019-10-13
      • 1970-01-01
      • 2021-03-19
      • 2018-11-27
      相关资源
      最近更新 更多