MCPcopy Create free account
hub / github.com/tensorpack/tensorpack / class_balanced_sigmoid_cross_entropy

Function class_balanced_sigmoid_cross_entropy

examples/HED/hed.py:20–43  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

18
19
20def 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)

Callers 1

build_graphMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…