-
Notifications
You must be signed in to change notification settings - Fork 2
/
loss_layers.py
151 lines (126 loc) · 4.77 KB
/
loss_layers.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
147
148
149
150
151
import mxnet as mx
import numpy as np
class VerfiLoss(mx.operator.CustomOp):
'''
Verfication Loss Layer
'''
def __init__(self, grad_scale, threshd):
self.grad_scale = grad_scale
self.threshd = threshd
self.eps = 1e-5
def forward(self, is_train, req, in_data, out_data, aux):
# print "forward"
x = in_data[0]
label = in_data[1]
n = x.shape[0]
ctx = x.context
# y = out_data[0]
# y[:] = 0
# print y.shape
y = np.zeros((x.shape[0], ))
#y = mx.nd.array((n, ), ctx=ctx)
for i in range(x.shape[0]):
mask = np.zeros((n, ))
mask[np.where(label == label[i])] = 1
pos = np.sum(mask)
mask = mx.nd.array(mask, ctx=ctx)
diff = x[i] - x
d = mx.nd.sqrt(mx.nd.sum(diff * diff, axis=1))
d1 = mx.nd.maximum(0, self.threshd - d)
z = mx.nd.sum(mask * d * d) / (pos + self.eps) \
+ mx.nd.sum((1 - mask) * d1 * d1) / (n - pos + self.eps)
y[i] = z.asnumpy()[0]
# y /= x.shape[0]
self.assign(out_data[0], req[0], mx.nd.array(y, ctx=ctx))
def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
# print "backward"
x = in_data[0]
label = in_data[1]
n = x.shape[0]
ctx = x.context
grad = in_grad[0]
grad[:] = 0
for i in range(x.shape[0]):
mask = np.zeros((1, n))
mask[np.where(label == label[i])] = 1
pos = np.sum(mask)
mask = mx.nd.array(mask, ctx=ctx)
diff = x[i] - x
d = mx.nd.sqrt(mx.nd.sum(diff * diff, axis=1))
g1 = mx.nd.minimum(0, (d - self.threshd) / (d + self.eps))
z = mx.nd.dot((1 - mask) * g1.reshape([1, n]), diff)[0]
# print grad[i].shape, z.shape
# grad[i] = z
# print "z"
grad[i] = mx.nd.dot(mask, diff)[0] / (pos + self.eps)\
+ mx.nd.dot((1 - mask) * g1.reshape([1, n]), diff)[0] / (n - pos + self.eps)
grad *= self.grad_scale
@mx.operator.register("verifiLoss")
class VerifiLossProp(mx.operator.CustomOpProp):
def __init__(self, grad_scale=1.0, threshd=0.5):
super(VerifiLossProp, self).__init__(need_top_grad=False)
self.grad_scale = float(grad_scale)
self.threshd = float(threshd)
def list_arguments(self):
return ['data', 'label']
def list_outputs(self):
return ['output']
def infer_shape(self, in_shape):
data_shape = in_shape[0]
label_shape = (in_shape[0][0], )
output_shape = (in_shape[0][0], )
return [data_shape, label_shape], [output_shape]
def create_operator(self, ctx, shapes, dtypes):
return VerfiLoss(self.grad_scale, self.threshd)
class TripletLoss(mx.operator.CustomOp):
'''
Triplet loss layer
'''
def __init__(self, grad_scale=1.0, threshd=0.5):
self.grad_scale = grad_scale
self.threshd = threshd
def forward(self, is_train, req, in_data, out_data, aux):
x = in_data[0]
y = np.zeros((x.shape[0], ))
ctx = x.context
for i in range(x.shape[0] / 2):
pid = i + 1 if i % 2 == 0 else i - 1
nid = i + int(x.shape[0] / 2)
pdiff = x[i] - x[pid]
ndiff = x[i] - x[nid]
y[i] = mx.nd.sum(pdiff * pdiff).asnumpy()[0] -\
mx.nd.sum(ndiff * ndiff).asnumpy()[0] + self.threshd
if y[i] < 0:
y[i] = 0
# y /= x.shape[0]
self.assign(out_data[0], req[0], mx.nd.array(y, ctx=ctx))
def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
x = in_data[0]
y = out_data[0]
grad = in_grad[0]
grad[:] = 0
for i in range(x.shape[0] / 2):
pid = i + 1 if i % 2 == 0 else i - 1
nid = i + int(x.shape[0] / 2)
if y[i] > 0:
grad[i] += x[nid] - x[pid]
grad[pid] += x[pid] - x[i]
grad[nid] += x[i] - x[nid]
grad *= self.grad_scale
@mx.operator.register("tripletLoss")
class TripletLossProp(mx.operator.CustomOpProp):
def __init__(self, grad_scale=1.0, threshd=0.5):
super(TripletLossProp, self).__init__(need_top_grad=False)
self.grad_scale = float(grad_scale)
self.threshd = float(threshd)
def list_arguments(self):
return ['data']
def list_outputs(self):
return ['output']
def infer_shape(self, in_shape):
data_shape = in_shape[0]
# label_shape = (in_shape[0][0], )
output_shape = (in_shape[0][0], )
return [data_shape], [output_shape]
def create_operator(self, ctx, shapes, dtypes):
return TripletLoss(self.grad_scale, self.threshd)