Cross Entropy Loss for Keras Semantic Segmentation

I am pretty sure this is a stupid question, but I cannot find it anywhere, so I will ask it here.

I'm doing semantic image segmentation using cnn (unet) in keras with 7 labels. Therefore, my shortcut for each image (7, n_rows, n_cols) is used with theano. Thus, in 7 layers for each pixel it is encoded one hot. In this case, the correct error function to use categorical cross-entropy? It seems that for me, but for the network, it seems better to learn with the binary loss of cross-entropy. Can someone shed light on why this would be and what is the principal goal?

+14
image-processing deep-learning image-segmentation keras
source share
3 answers

The binary cross-entropy loss should be used with sigmod activation in the last layer, and this severely punishes the opposite predictions. He does not take into account that the output is one hot code, and the sum of the predictions should be 1. But since erroneous predictions are severely punished, the model is somewhat learning to classify correctly.

Now, to force the use of the previous hot code, softmax activation with categorical cross-entropy is used. This is what you should use.

Now the problem is using softmax in your case, since Keras does not support softmax on every pixel.

The easiest way is to resize (n_rows, n_cols, 7) using the Permute layer, and then change it to (n_rows * n_cols, 7) using the Reshape layer. Then you can add a softmax activation softmax and use crossentopia. Data must also be modified accordingly.

Another way to do this would 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 

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)) 

If tf image_dim_ordering , you can do this using Permute layers.

See here for more details.

+14
source share

I checked the @indraforyou solution and I think that the proposed method has some errors. Since the comments section does not take into account the correct code segments, I think that here is a fixed version:

 def depth_softmax(matrix): from keras import backend as K exp_matrix = K.exp(matrix) softmax_matrix = exp_matrix / K.expand_dims(K.sum(exp_matrix, axis=-1), axis=-1) return softmax_matrix 

This method will expect that the order of the matrix will be (height, width, channels).

0
source share

Since my reputation is below 50, I can not add conmment to indraforyou answer. I think there is something important to notice. The reshape function in numpy can indicate the order in which the form changes (i.e., first the row or column first) if you use the form change for the groundtruth label and the output of the layer. I think you will notice the order if you do not use the same method to change their shape, or both. In other words, the shape-changing operation must be consistent. I hope this is not superfluous advice. Ok let me know.

0
source share

All Articles