python - Tensorflow:针对单个图像中不平衡类的加权稀疏_softmax_cross_entropy

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

我正在研究一项二进制语义分割任务,其中一个类的分布在任何输入图像中都非常小,因此只有几个像素被标记。当使用sparse_softmax_cross_entropy时 忽略此类时,总体错误很容易减少。现在,我正在寻找一种通过系数对类别进行加权的方法,该系数对特定类别的错误分类惩罚比其他类别更高。

损失函数的文档指出:

weights acts as a coefficient for the loss. If a scalar is provided, then the loss is simply scaled by the given value. If weights is a tensor of shape [batch_size], then the loss weights apply to each corresponding sample.

如果我理解正确的话,它表示批处理中的特定样本与其他样本相比,其加权不同。但这实际上不是我想要的。有谁知道如何实现此损失函数的加权版本,其中权重衡量特定类别而不是样本的重要性?

最佳答案

回答我自己的问题:

U-Net paper 的作者使用预先计算的权重图来处理不平衡的类别。

苏黎世联邦理工学院天文学研究所提供了Tensorflow-based U-Net package其中包含 Softmax 函数的加权版本(不是稀疏的,但它们首先展平其标签和 logits):

class_weights = tf.constant(np.array(class_weights, dtype=np.float32))
weight_map = tf.multiply(flat_labels, class_weights)
weight_map = tf.reduce_sum(weight_map, axis=1)
loss_map = tf.nn.softmax_cross_entropy_with_logits_v2(logits=flat_logits, labels=flat_labels)
weighted_loss = tf.multiply(loss_map, weight_map)    
loss = tf.reduce_mean(weighted_loss)

关于python - Tensorflow:针对单个图像中不平衡类的加权稀疏_softmax_cross_entropy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55663783/

相关文章:

python-3.x - WRN 安装 python-prctl 以便可以保证清理进程

python - 如何在 TensorFlow 数字识别中使用自己的手绘图像

python - 如何使用pytest生成测试报告?

python - Django celery worker 向前端发送实时状态和结果消息

java - 有多少数据可以存储到 Google App Engine 应用程序中?

python - 安装具有 SYCL 支持的 Tensorflow

python - Pandas 字符串提取所有匹配项

python - ValueError : Dimension 2 in both shapes must be equal, 但是是 512 和 511。形状是 [?,384,512] 和 [?,384,511]

python - ValueError : Dimensions must be equal,,但对于 'loss/output_1_loss/mul' 来说是 3 和 3072 (op : 'Mul' ) with input shapes: [? ,3], [?,3072]

python - Keras:如何为每个时代改变数据