<p>You can use weight maps (as proposed in the <a href="https://arxiv.org/abs/1505.04597" rel="nofollow noreferrer">U-Net paper</a>). In those weight maps, you can weight regions with more weight or less weight. Here is some pseudocode:</p>

<pre><code>loss = compute_categorical_crossentropy()
weighted_loss = loss * weight_map # using element-wise multiplication
</code></pre>
How to use weighted categorical crossentropy on FCN (U-Net) in Keras?
<p>I have built a Keras model for image segmentation (U-Net). However in my samples some misclassifications (areas) are not that important, while other are crucial, so I want to assign higher weight in loss function to them. To complicate things further, I would like some misclassifications (class 1 instead of 2) to have very high penalty while inverse (class 2 instead of 1) shouldn't be penalized that much.</p>

<p>The way I see it, I need to use a sum (across all of the pixels) of weighted categorical crossentropy, but the best I could find is <a href="https://github.com/fchollet/keras/issues/2115" rel="nofollow noreferrer">this</a>:</p>

<pre><code>def w_categorical_crossentropy(y_true, y_pred, weights):
    nb_cl = len(weights)
    final_mask = K.zeros_like(y_pred[:, 0])
    y_pred_max = K.max(y_pred, axis=1)
    y_pred_max = K.reshape(y_pred_max, (K.shape(y_pred)[0], 1))
    y_pred_max_mat = K.cast(K.equal(y_pred, y_pred_max), K.floatx())
    for c_p, c_t in product(range(nb_cl), range(nb_cl)):
        final_mask += (weights[c_t, c_p] * y_pred_max_mat[:, c_p] * y_true[:, c_t])
    return K.categorical_crossentropy(y_pred, y_true) * final_mask
</code></pre>

<p>However this code only works with a single prediction and my knowledge of Keras inner workings is lacking (and math side of it is not much better). Anyone know how I can adapt it, or even better, is there a ready-made loss function which would suit my case? </p>

<p>I would appreciate some pointers.</p>

<p>EDIT: my question is similar to <a href="https://stackoverflow.com/questions/43033436/how-to-do-point-wise-categorical-crossentropy-loss-in-keras">How to do point-wise categorical crossentropy loss in Keras?</a>, except that I would like to use <strong><em>weighted</em></strong> categorical crossentropy.</p>