我正在keras中进行语义分段,并试图修改categorical_crossentropy损失,以便损失是类加权的 .

这是我的代码:

def class_weighted_categorical_crossentropy(output, target, from_logits=False):
"""Categorical crossentropy between an output tensor and a target tensor.

parameter = TrainingParameters()
   # create ones array with shape of target tensor
   # multiply class weight array with inverse class_accuracies for each label
class_weights = tf.convert_to_tensor(parameter.class_weights, dtype=floatx())
   # weight targets with class weights and create pattern with which loss can be multiplied
class_weights_pattern = tf.multiply(target, class_weights)
class_weights_pattern = tf.reduce_sum(class_weights_pattern, reduction_indices=len(class_weights_pattern.get_shape())-1)#, keep_dims=True)
if not from_logits:
    # scale preds so that the class probas of each sample sum to 1
    output /= tf.reduce_sum(output,
                            reduction_indices=len(output.get_shape()) - 1,
                            keep_dims=True)
    # manual computation of crossentropy
    epsilon = _to_tensor(_EPSILON, output.dtype.base_dtype)
    output = tf.clip_by_value(output, epsilon, 1. - epsilon)
    loss = - tf.reduce_sum(target * tf.log(output), reduction_indices=len(output.get_shape()) - 1)
    return tf.multiply(loss, class_weights_pattern)
else:
    loss = tf.nn.softmax_cross_entropy_with_logits(labels=target, logits=output)
    return tf.multiply(loss, class_weights_pattern)

我最后只改变了损失乘以class_weights模式 . class_weights_pattern包含每个像素对应的类权重,因此应该对正常的categorical_crossentropy损失进行加权 . 但是,如果我使用修改后的损失训练我的模型,结果会比仅使用keras categorical_crossentropy损失更糟糕 . 即使我将所有类权重设置为1,因此我的class_weighted_categorical_crossentropy损失应该与来自keras的categorical_crossentropy损失完全相同,结果更糟 . 我已经用一些样本图像打印了这两种损失,损失完全相同 .

有谁能够帮我?为什么不起作用?提前致谢!