语义分割Keras的交叉熵损失 [英] Cross Entropy Loss for Semantic Segmentation Keras
问题描述
我很确定这是一个愚蠢的问题,但我无法在其他任何地方找到它,所以我会在这里问它。
I'm pretty sure this is a silly question but I can't find it anywhere else so I'm going to ask it here.
我是使用带有7个标签的keras中的cnn(unet)进行语义图像分割。所以每个图像的标签是(7,n_rows,n_cols)使用theano后端。因此,对于每个像素,在7个图层中,它是单热编码的。在这种情况下,使用分类交叉熵是正确的误差函数吗?这似乎对我来说,但网络似乎更好地学习二进制交叉熵损失。有人可以解释为什么会这样,原则目标是什么?
I'm doing semantic image segmentation using a cnn (unet) in keras with 7 labels. So my label for each image is (7,n_rows,n_cols) using the theano backend. So across the 7 layers for each pixel, it's one-hot encoded. In this case, is the correct error function to use categorical cross-entropy? It seems that way to me but the network seems to learn better with binary cross-entropy loss. Can someone shed some light on why that would be and what the principled objective is?
推荐答案
应该使用二进制交叉熵损失在最后一层使用 sigmod
激活它会严重影响相反的预测。它没有考虑到输出是单热编码的,并且预测的总和应该是1.但是由于误预测严重惩罚模型,所以有些学会正确分类。
Binary cross-entropy loss should be used with sigmod
activation in the last layer and it severely penalizes opposite predictions. It does not take into account that the output is a one-hot coded and the sum of the predictions should be 1. But as mis-predictions are severely penalizing the model somewhat learns to classify properly.
现在强制执行单热代码的优先级是使用带分类交叉熵的 softmax
激活。这是你应该使用的。
Now to enforce the prior of one-hot code is to use softmax
activation with categorical cross-entropy. This is what you should use.
现在问题是在你的情况下使用 softmax
,因为Keras不支持每个像素上的softmax。
Now the problem is using the softmax
in your case as Keras don't support softmax on each pixel.
最简单的方法是使用 Permute
图层将尺寸置换为(n_rows,n_cols,7)然后使用 Reshape
图层将其重新整形为(n_rows * n_cols,7)。然后,您可以添加 softmax
激活层并使用crossentopy丢失。数据也应该相应地重新形成。
The easiest way to go about it is permute the dimensions to (n_rows,n_cols,7) using Permute
layer and then reshape it to (n_rows*n_cols,7) using Reshape
layer. Then you can added the softmax
activation layer and use crossentopy loss. The data should also be reshaped accordingly.
另一种方法是实现depth-softmax:
The other way of doing so will be to implement depth-softmax :
def depth_softmax(matrix):
sigmoid = lambda x: 1 / (1 + K.exp(-x))
sigmoided_matrix = sigmoid(matrix)
softmax_matrix = sigmoided_matrix / K.sum(sigmoided_matrix, axis=0)
return softmax_matrix
并将其用作lambda图层:
and use it as a lambda layer:
model.add(Deconvolution2D(7, 1, 1, border_mode='same', output_shape=(7,n_rows,n_cols)))
model.add(Permute(2,3,1))
model.add(BatchNormalization())
model.add(Lambda(depth_softmax))
如果 tf
<使用code> image_dim_ordering 然后您可以使用 Permute
图层。
If tf
image_dim_ordering
is used then you can do way with the Permute
layers.
如需更多参考,请查看此处。
For more reference check here.
这篇关于语义分割Keras的交叉熵损失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!