-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlosses.py
340 lines (252 loc) · 10.7 KB
/
losses.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"""
For some part of code below:
Thanks to voxelmorph: Learning-Based Image Registration, https://github.com/voxelmorph/voxelmorph for this code.
If you use this code, please cite the respective papers in their repo.
"""
# Third party inports
import tensorflow as tf
import keras.backend as K
import numpy as np
import sys
class IR():
"""
image matching term
"""
def __init__(self, image_sigma, prior_lambda, flow_vol_shape=None):
self.image_sigma = image_sigma
self.prior_lambda = prior_lambda
self.D = None
self.flow_vol_shape = flow_vol_shape
def _adj_filt(self, ndims):
"""
compute an adjacency filter that, for each feature independently,
has a '1' in the immediate neighbor, and 0 elsewehre.
so for each filter, the filter has 2^ndims 1s.
the filter is then setup such that feature i outputs only to feature i
"""
# inner filter, that is 3x3x...
filt_inner = np.zeros([3] * ndims)
for j in range(ndims):
o = [[1]] * ndims
o[j] = [0, 2]
filt_inner[np.ix_(*o)] = 1
# full filter, that makes sure the inner filter is applied
# ith feature to ith feature
filt = np.zeros([3] * ndims + [ndims, ndims])
for i in range(ndims):
filt[..., i, i] = filt_inner
return filt
def _degree_matrix(self, vol_shape):
# get shape stats
ndims = len(vol_shape)
sz = [*vol_shape, ndims]
# prepare conv kernel
conv_fn = getattr(tf.nn, 'conv%dd' % ndims)
# prepare tf filter
z = K.ones([1] + sz)
filt_tf = tf.convert_to_tensor(self._adj_filt(ndims), dtype=tf.float32)
strides = [1] * (ndims + 2)
return conv_fn(z, filt_tf, strides, "SAME")
def prec_loss(self, y_pred):
"""
a more manual implementation of the precision matrix term
mu * P * mu where P = D - A
where D is the degree matrix and A is the adjacency matrix
mu * P * mu = 0.5 * sum_i mu_i sum_j (mu_i - mu_j) = 0.5 * sum_i,j (mu_i - mu_j) ^ 2
where j are neighbors of i
Note: could probably do with a difference filter,
but the edges would be complicated unless tensorflow allowed for edge copying
"""
vol_shape = y_pred.get_shape().as_list()[1:-1]
ndims = len(vol_shape)
sm = 0
for i in range(ndims):
d = i + 1
# permute dimensions to put the ith dimension first
r = [d, *range(d), *range(d + 1, ndims + 2)]
y = K.permute_dimensions(y_pred, r)
df = y[1:, ...] - y[:-1, ...]
sm += K.mean(df * df)
return 0.5 * sm / ndims
def kl_loss(self, y_true, y_pred):
"""
KL loss
y_pred is assumed to be D*2 channels: first D for mean, next D for logsigma
D (number of dimensions) should be 1, 2 or 3
y_true is only used to get the shape
"""
# prepare inputs
ndims = len(y_pred.get_shape()) - 2
mean = y_pred[..., 0:ndims]
log_sigma = y_pred[..., ndims:]
if self.flow_vol_shape is None:
# Note: this might not work in multi_gpu mode if vol_shape is not apriori passed in
self.flow_vol_shape = y_true.get_shape().as_list()[1:-1]
# compute the degree matrix (only needs to be done once)
# we usually can't compute this until we know the ndims,
# which is a function of the data
if self.D is None:
self.D = self._degree_matrix(self.flow_vol_shape)
# sigma terms
sigma_term = self.prior_lambda * self.D * tf.exp(log_sigma) - log_sigma
sigma_term = K.mean(sigma_term)
# precision terms
# note needs 0.5 twice, one here (inside self.prec_loss), one below
prec_term = self.prior_lambda * self.prec_loss(mean)
# combine terms
return 0.5 * ndims * (sigma_term + prec_term) # ndims because we averaged over dimensions as well
def recon_loss(self, y_true, y_pred):
""" reconstruction loss """
#return 1. / (self.image_sigma ** 2) * K.mean(K.square(y_true - y_pred))
#return K.mean(K.square(y_true - y_pred),axis=-1)
return K.mean(K.abs(y_true - y_pred))
class Smoothness():
def __init__(self, penalty):
self.penalty = penalty
def loss(self, y_true, y_pred):
dx = tf.abs(y_pred[:, 1:, :, :, :] - y_pred[:, :-1, :, :, :])
dy = tf.abs(y_pred[:, :, 1:, :, :] - y_pred[:, :, :-1, :, :])
dz = tf.abs(y_pred[:, :, :, 1:, :] - y_pred[:, :, :, :-1, :])
if (self.penalty == 'l2'):
dy = dy * dy
dx = dx * dx
dz = dz * dz
d = tf.reduce_mean(dx) + tf.reduce_mean(dy) + tf.reduce_mean(dz)
return d / 3.0
class Foldings():
def loss(self, y_true, y_pred):
'''
Penalizing locations where Jacobian has negative determinants
'''
jac = Get_Ja_2d(y_pred)
Neg_Jac = 0.5 * (tf.abs(jac) - jac)
return tf.reduce_sum(Neg_Jac)
def Get_Ja(displacement):
'''
Calculate the Jacobian value at each point of the displacement map having
size of b*h*w*d*3 and in the cubic volumn of [-1, 1]^3
'''
D_x = (displacement[:, 1:, :-1, :-1, :] - displacement[:, :-1, :-1, :-1, :])
D_y = (displacement[:, :-1, 1:, :-1, :] - displacement[:, :-1, :-1, :-1, :])
D_z = (displacement[:, :-1, :-1, 1:, :] - displacement[:, :-1, :-1, :-1, :])
D1 = (D_x[..., 0] + 1) * ((D_y[..., 1] + 1) * (D_z[..., 2] + 1) - D_z[..., 1] * D_y[..., 2])
D2 = (D_x[..., 1]) * (D_y[..., 0] * (D_z[..., 2] + 1) - D_y[..., 2] * D_x[..., 0])
D3 = (D_x[..., 2]) * (D_y[..., 0] * D_z[..., 1] - (D_y[..., 1] + 1) * D_z[..., 0])
return D1 - D2 + D3
def Get_Ja_2d(displacement):
'''
Calculate the Jacobian value at each point of the displacement map having
size of b*h*w*d*3 and in the cubic volumn of [-1, 1]^3
'''
D_x = (displacement[:, 1:, :-1, :] - displacement[:, :-1, :-1, :])
D_y = (displacement[:, :-1, 1:, :] - displacement[:, :-1, :-1, :])
return D_x[..., 0] * D_y[..., 1] - D_y[..., 0] * D_x[..., 1]
class NGF():
def __init__(self, epsilon):
self.epsilon = epsilon
def ngf_loss_3d(self, warped_image):
dx = (warped_image[:, 1:, 1:, 1:,:] - warped_image[:, :-1, 1:, 1:,:])
dy = (warped_image[:, 1:, 1:, 1:,:] - warped_image[:, 1:, :-1, 1:,:])
dz = (warped_image[:, 1:, 1:, 1:,:] - warped_image[:, 1:, 1:, :-1,:])
norm = tf.sqrt(tf.math.pow(dx,2) + tf.math.pow(dy,2) + tf.math.pow(dz,2) + self.epsilon ** 2)
#return tf.pad(tf.concat((dx, dy, dz), axis=4), paddings=([0,0],[0,1],[0,1],[0,1],[0,0]),mode='CONSTANT') / norm
return tf.concat((dx, dy, dz), axis=4) / norm
def loss(self, y_true, y_pred):
ng_target_image = self.ngf_loss_3d(y_true)
ng_warped_image = self.ngf_loss_3d(y_pred)
value = 0
for dim in range(3):
value = value + ng_warped_image[:,:,:,:,dim] * ng_target_image[:,:,:,:,dim]
value = 0.5 * tf.math.pow(-value, 2)
return tf.reduce_sum(value)
class NCC():
"""
local (over window) normalized cross correlation
"""
def __init__(self, win=None, eps=1e-5):
self.win = win
self.eps = eps
def ncc(self, I, J):
# get dimension of volume
# assumes I, J are sized [batch_size, *vol_shape, nb_feats]
ndims = len(I.get_shape().as_list()) - 2
assert ndims in [1, 2, 3], "volumes should be 1 to 3 dimensions. found: %d" % ndims
# set window size
if self.win is None:
self.win = [3] * ndims
# get convolution function
conv_fn = getattr(tf.nn, 'conv%dd' % ndims)
# compute CC squares
I2 = I*I
J2 = J*J
IJ = I*J
# compute filters
sum_filt = tf.ones([*self.win, 1, 1])
strides = 1
if ndims > 1:
strides = [1] * (ndims + 2)
padding = 'SAME'
# compute local sums via convolution
I_sum = conv_fn(I, sum_filt, strides, padding)
J_sum = conv_fn(J, sum_filt, strides, padding)
I2_sum = conv_fn(I2, sum_filt, strides, padding)
J2_sum = conv_fn(J2, sum_filt, strides, padding)
IJ_sum = conv_fn(IJ, sum_filt, strides, padding)
# compute cross correlation
win_size = np.prod(self.win)
u_I = I_sum/win_size
u_J = J_sum/win_size
cross = IJ_sum - u_J*I_sum - u_I*J_sum + u_I*u_J*win_size
I_var = I2_sum - 2 * u_I * I_sum + u_I*u_I*win_size
J_var = J2_sum - 2 * u_J * J_sum + u_J*u_J*win_size
cc = cross*cross / (I_var*J_var + self.eps)
# return negative cc.
# return negative cc.
return tf.reduce_mean(cc)
def loss(self, I, J):
return - self.ncc(I, J)
class Grad:
"""
N-D gradient loss.
loss_mult can be used to scale the loss value - this is recommended if
the gradient is computed on a downsampled vector field (where loss_mult
is equal to the downsample factor).
"""
def __init__(self, penalty='l1', loss_mult=None, vox_weight=None):
self.penalty = penalty
self.loss_mult = loss_mult
self.vox_weight = vox_weight
def _diffs(self, y):
vol_shape = y.get_shape().as_list()[1:-1]
ndims = len(vol_shape)
df = [None] * ndims
for i in range(ndims):
d = i + 1
# permute dimensions to put the ith dimension first
r = [d, *range(d), *range(d + 1, ndims + 2)]
yp = K.permute_dimensions(y, r)
dfi = yp[1:, ...] - yp[:-1, ...]
if self.vox_weight is not None:
w = K.permute_dimensions(self.vox_weight, r)
# TODO: Need to add square root, since for non-0/1 weights this is bad.
dfi = w[1:, ...] * dfi
# permute back
# note: this might not be necessary for this loss specifically,
# since the results are just summed over anyway.
r = [*range(1, d + 1), 0, *range(d + 1, ndims + 2)]
df[i] = K.permute_dimensions(dfi, r)
return df
def loss(self, _, y_pred):
"""
returns Tensor of size [bs]
"""
if self.penalty == 'l1':
dif = [tf.abs(f) for f in self._diffs(y_pred)]
else:
assert self.penalty == 'l2', 'penalty can only be l1 or l2. Got: %s' % self.penalty
dif = [f * f for f in self._diffs(y_pred)]
df = [tf.reduce_mean(K.batch_flatten(f), axis=-1) for f in dif]
grad = tf.add_n(df) / len(df)
if self.loss_mult is not None:
grad *= self.loss_mult
return grad