语义分割 Keras 的交叉熵损失 [英] Cross Entropy Loss for Semantic Segmentation Keras

查看:27
本文介绍了语义分割 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)进行语义图像分割.所以我使用 theano 后端为每个图像的标签是 (7,n_rows,n_cols).所以在每个像素的 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.

现在强制执行 one-hot 代码的先验是使用带有分类交叉熵的 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 激活层并使用交叉熵损失.数据也应该相应地重新调整.

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 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆