-
Notifications
You must be signed in to change notification settings - Fork 15
/
loss_weighted_crossentropy.py
74 lines (69 loc) · 3.19 KB
/
loss_weighted_crossentropy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import tensorflow as tf
from keras import backend as K
import numpy as np
def weighted_categorical_crossentropy(weights):
# https://forums.fast.ai/t/unbalanced-classes-in-image-segmentation/18289
""" weighted_categorical_crossentropy
Args:
* weights<ktensor|nparray|list>: crossentropy weights
Returns:
* weighted categorical crossentropy function
"""
if isinstance(weights,list) or isinstance(np.ndarray):
weights=K.variable(weights)
def loss(target,output,from_logits=False):
if not from_logits:
output /= tf.reduce_sum(output,
len(output.get_shape()) - 1,
True)
_epsilon = tf.convert_to_tensor(K.epsilon(), dtype=output.dtype.base_dtype)
output = tf.clip_by_value(output, _epsilon, 1. - _epsilon)
losses = target * tf.log(output)
print(losses)
weighted_losses = target * tf.log(output) * weights
print(weighted_losses)
return - tf.reduce_sum(weighted_losses,len(output.get_shape()) - 1)
else:
raise ValueError('WeightedCategoricalCrossentropy: not valid with logits')
return loss
from keras.backend.common import epsilon
def ORIGINAL_categorical_crossentropy(target, output, from_logits=False, axis=-1):
"""Categorical crossentropy between an output tensor and a target tensor.
# Arguments
target: A tensor of the same shape as `output`.
output: A tensor resulting from a softmax
(unless `from_logits` is True, in which
case `output` is expected to be the logits).
from_logits: Boolean, whether `output` is the
result of a softmax, or is a tensor of logits.
axis: Int specifying the channels axis. `axis=-1`
corresponds to data format `channels_last`,
and `axis=1` corresponds to data format
`channels_first`.
# Returns
Output tensor.
# Raises
ValueError: if `axis` is neither -1 nor one of
the axes of `output`.
"""
output_dimensions = list(range(len(output.get_shape())))
if axis != -1 and axis not in output_dimensions:
raise ValueError(
'{}{}{}'.format(
'Unexpected channels axis {}. '.format(axis),
'Expected to be -1 or one of the axes of `output`, ',
'which has {} dimensions.'.format(len(output.get_shape()))))
# Note: tf.nn.softmax_cross_entropy_with_logits
# expects logits, Keras expects probabilities.
if not from_logits:
# scale preds so that the class probas of each sample sum to 1
output /= tf.reduce_sum(output, axis, True)
# manual computation of crossentropy
_epsilon = tf.convert_to_tensor(epsilon(), dtype=output.dtype.base_dtype)
output = tf.clip_by_value(output, _epsilon, 1. - _epsilon)
losses = target * tf.log(output)
#weighted_losses = target * tf.log(output) * weights
return - tf.reduce_sum(losses, axis)
else:
return tf.nn.softmax_cross_entropy_with_logits(labels=target,
logits=output)