The class-balanced cross entropy loss, as in `Holistically-Nested Edge Detection `_. Args: logits: of shape (b, ...). label: of the same shape. the ground truth in {0,1}. Returns: class-balanced cross entropy loss.
(logits, label, name='cross_entropy_loss')
| 18 | |
| 19 | |
| 20 | def class_balanced_sigmoid_cross_entropy(logits, label, name='cross_entropy_loss'): |
| 21 | """ |
| 22 | The class-balanced cross entropy loss, |
| 23 | as in `Holistically-Nested Edge Detection |
| 24 | <http://arxiv.org/abs/1504.06375>`_. |
| 25 | |
| 26 | Args: |
| 27 | logits: of shape (b, ...). |
| 28 | label: of the same shape. the ground truth in {0,1}. |
| 29 | Returns: |
| 30 | class-balanced cross entropy loss. |
| 31 | """ |
| 32 | with tf.name_scope('class_balanced_sigmoid_cross_entropy'): |
| 33 | y = tf.cast(label, tf.float32) |
| 34 | |
| 35 | count_neg = tf.reduce_sum(1. - y) |
| 36 | count_pos = tf.reduce_sum(y) |
| 37 | beta = count_neg / (count_neg + count_pos) |
| 38 | |
| 39 | pos_weight = beta / (1 - beta) |
| 40 | cost = tf.nn.weighted_cross_entropy_with_logits(logits=logits, targets=y, pos_weight=pos_weight) |
| 41 | cost = tf.reduce_mean(cost * (1 - beta)) |
| 42 | zero = tf.equal(count_pos, 0.0) |
| 43 | return tf.where(zero, 0.0, cost, name=name) |
| 44 | |
| 45 | |
| 46 | @layer_register(log_shape=True) |
no outgoing calls
no test coverage detected
searching dependent graphs…