Keras Weighted Loss Graphic Card

I am trying to do multi-family semantic segmentation with unet . As with unet paper, I would like to make a loss function for overweight borders (page 5).

As such, I would like to create a custom loss map for each image, where the borders between the objects are overweight. I use categorical cross-entropy, where I smooth the image before the loss function like here . It would be nice for me to make a mask with pixel loss, but I wonder how, if possible, to lose a pixel mask many times.

+4
source share
2 answers

http://tf-unet.readthedocs.io/en/latest/_modules/tf_unet/unet.html keras . :

    def _get_cost(self, logits, cost_name, cost_kwargs):

    Optional arguments are: 
    class_weights: weights for the different classes in case of multi-class imbalance
    regularizer: power of the L2 regularizers added to the loss function

    flat_logits = tf.reshape(logits, [-1, self.n_class])
    flat_labels = tf.reshape(self.y, [-1, self.n_class])
    if cost_name == "cross_entropy":
        class_weights = cost_kwargs.pop("class_weights", None)

        if class_weights is not None:
            class_weights = tf.constant(np.array(class_weights, dtype=np.float32))

            weight_map = tf.multiply(flat_labels, class_weights)
            weight_map = tf.reduce_sum(weight_map, axis=1)

            loss_map = tf.nn.softmax_cross_entropy_with_logits(flat_logits, flat_labels)
            weighted_loss = tf.multiply(loss_map, weight_map)

            loss = tf.reduce_mean(weighted_loss)

        else:
            loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=flat_logits, 
                                                                          labels=flat_labels))}
0

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


All Articles