How to make an accurate definitive loss of crossenropy in Keras?

I have a network that creates a 4D output tensor where the value at each position in spatial dimensions (~ pixel) should be interpreted as the class probabilities for that position. In other words, the output signal (num_batches, height, width, num_classes). I have tags of the same size where the real class is encoded as hot. I would like to calculate the losses categorical-crossentropyusing this.

Problem # 1: The function K.softmaxexpects a tensor2D (num_batches, num_classes)

Problem number 2 . I am not sure how to combine losses from each position. Is this right for the reshapetensor on (num_batches * height * width, num_classes), and then call K.categorical_crossentropyon that? Or rather, call K.categorical_crossentropy(num_batches, num_classes)height * width times and average the results?

+6
source share
3 answers

Just smooth out the output into a 2D tensor of size (num_batches, height * width * num_classes). You can do this with a layer Flatten. Make sure yours are ysmoothed the same way (usually y = y.reshape((num_batches, height * width * num_classes))enough for a call ).

For your second question, using categorical cross-entropy in all predictions width*heightessentially coincides with averaging the categorical cross-entropy for each prediction width*height(by the definition of categorical cross-entropy).

+2
source

Found this question to confirm my intuition.

: softmax . 3D-, , (, timedimension, numclasses) softmax . 4D-.

:

reshaped_output = Reshape((height*width, num_classes))(output_tensor)

softmax

new_output = Activation('softmax')(reshaped_output) 

2D, (, , num_classes).

, , timedistributed (Activation ( "softmax" )). , ...

, :-)

+2

You can also not do reshapeanything and determine both softmax, and lossyourself. Here softmax, which applies to the last input size (e.g. in the tfbackend):

def image_softmax(input):
    label_dim = -1
    d = K.exp(input - K.max(input, axis=label_dim, keepdims=True))
    return d / K.sum(d, axis=label_dim, keepdims=True)

and here you have it loss(no need to rename anything):

__EPS = 1e-5
def image_categorical_crossentropy(y_true, y_pred):
    y_pred = K.clip(y_pred, __EPS, 1 - __EPS)
    return -K.mean(y_true * K.log(y_pred) + (1 - y_true) * K.log(1 - y_pred))

No further changes are required.

+2
source

Source: https://habr.com/ru/post/1016060/


All Articles