【发布时间】: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