-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathloss.py
146 lines (114 loc) · 4.34 KB
/
loss.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import numpy as np
from keras import backend as K
import tensorflow as tf
def symmetric_cross_entropy(alpha, beta):
"""
Symmetric Cross Entropy:
ICCV2019 "Symmetric Cross Entropy for Robust Learning with Noisy Labels"
https://arxiv.org/abs/1908.06112
"""
def loss(y_true, y_pred):
y_true_1 = y_true
y_pred_1 = y_pred
y_true_2 = y_true
y_pred_2 = y_pred
y_pred_1 = tf.clip_by_value(y_pred_1, 1e-7, 1.0)
y_true_2 = tf.clip_by_value(y_true_2, 1e-4, 1.0)
return alpha*tf.reduce_mean(-tf.reduce_sum(y_true_1 * tf.log(y_pred_1), axis = -1)) + beta*tf.reduce_mean(-tf.reduce_sum(y_pred_2 * tf.log(y_true_2), axis = -1))
return loss
def cross_entropy(y_true, y_pred):
return K.categorical_crossentropy(y_true, y_pred)
def boot_soft(y_true, y_pred):
"""
2015 - iclrws - Training deep neural networks on noisy labels with bootstrapping.
https://arxiv.org/abs/1412.6596
:param y_true:
:param y_pred:
:return:
"""
beta = 0.95
y_pred /= K.sum(y_pred, axis=-1, keepdims=True)
y_pred = K.clip(y_pred, K.epsilon(), 1.0 - K.epsilon())
return -K.sum((beta * y_true + (1. - beta) * y_pred) *
K.log(y_pred), axis=-1)
def boot_hard(y_true, y_pred):
"""
2015 - iclrws - Training deep neural networks on noisy labels with bootstrapping.
https://arxiv.org/abs/1412.6596
:param y_true:
:param y_pred:
:return:
"""
beta = 0.8
y_pred /= K.sum(y_pred, axis=-1, keepdims=True)
y_pred = K.clip(y_pred, K.epsilon(), 1.0 - K.epsilon())
pred_labels = K.one_hot(K.argmax(y_pred, 1), num_classes=K.shape(y_true)[1])
return -K.sum((beta * y_true + (1. - beta) * pred_labels) *
K.log(y_pred), axis=-1)
def forward(P):
"""
Making Deep Neural Networks Robust to Label Noise: a Loss Correction Approach
CVPR17 https://arxiv.org/abs/1609.03683
:param P: noise model, a noisy label transition probability matrix
:return:
"""
P = K.constant(P)
def loss(y_true, y_pred):
y_pred /= K.sum(y_pred, axis=-1, keepdims=True)
y_pred = K.clip(y_pred, K.epsilon(), 1.0 - K.epsilon())
return -K.sum(y_true * K.log(K.dot(y_pred, P)), axis=-1)
return loss
def backward(P):
"""
Making Deep Neural Networks Robust to Label Noise: a Loss Correction Approach
CVPR17 https://arxiv.org/abs/1609.03683
:param P: noise model, a noisy label transition probability matrix
:return:
"""
P_inv = K.constant(np.linalg.inv(P))
def loss(y_true, y_pred):
y_pred /= K.sum(y_pred, axis=-1, keepdims=True)
y_pred = K.clip(y_pred, K.epsilon(), 1.0 - K.epsilon())
return -K.sum(K.dot(y_true, P_inv) * K.log(y_pred), axis=-1)
return loss
def lid(logits, k=20):
"""
Calculate LID for each data point in the array.
:param logits:
:param k:
:return:
"""
batch_size = tf.shape(logits)[0]
# n_samples = logits.get_shape().as_list()
# calculate pairwise distance
r = tf.reduce_sum(logits * logits, 1)
# turn r into column vector
r1 = tf.reshape(r, [-1, 1])
D = r1 - 2 * tf.matmul(logits, tf.transpose(logits)) + tf.transpose(r1) + \
tf.ones([batch_size, batch_size])
# find the k nearest neighbor
D1 = -tf.sqrt(D)
D2, _ = tf.nn.top_k(D1, k=k, sorted=True)
D3 = -D2[:, 1:] # skip the x-to-x distance 0 by using [,1:]
m = tf.transpose(tf.multiply(tf.transpose(D3), 1.0 / D3[:, -1]))
v_log = tf.reduce_sum(tf.log(m + K.epsilon()), axis=1) # to avoid nan
lids = -k / v_log
return lids
def lid_paced_loss(alpha=1.0, beta1=0.1, beta2=1.0):
"""TO_DO
Class wise lid pace learning, targeting classwise asymetric label noise.
Args:
alpha: lid based adjustment paramter: this needs real-time update.
Returns:
Loss tensor of type float.
"""
if alpha == 1.0:
return symmetric_cross_entropy(alpha=beta1, beta=beta2)
else:
def loss(y_true, y_pred):
pred_labels = K.one_hot(K.argmax(y_pred, 1), num_classes=K.shape(y_true)[1])
y_new = alpha * y_true + (1. - alpha) * pred_labels
y_pred /= K.sum(y_pred, axis=-1, keepdims=True)
y_pred = K.clip(y_pred, K.epsilon(), 1.0 - K.epsilon())
return -K.sum(y_new * K.log(y_pred), axis=-1)
return loss