-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzk_loss.py
204 lines (154 loc) · 7.91 KB
/
zk_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
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
from __future__ import print_function
from keras import backend as K
from zk_config import *
import numpy as np
EPS = 2.2204e-16
def get_sum(input):
return K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.sum(input, axis=[1, 2])), shape_r_out, axis=1)), shape_c_out, axis=2)
def get_sum_3d(input):
return K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.sum(input, axis=[2, 3])), shape_r_out, axis=2)), shape_c_out, axis=3)
def get_max(input):
return K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.max(input, axis=[1, 2])), shape_r_out, axis=1)), shape_c_out, axis=2)
def get_max_3d(input):
return K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.max(input, axis=[2, 3])), shape_r_out, axis=2)), shape_c_out, axis=3)
def get_min(input):
return K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.min(input, axis=[1, 2])), shape_r_out, axis=1)), shape_c_out, axis=2)
def get_min_3d(input):
return K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.min(input, axis=[2, 3])), shape_r_out, axis=2)), shape_c_out, axis=3)
def get_mean(input):
return K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.mean(input, axis=[1, 2])), shape_r_out, axis=1)), shape_c_out, axis=2)
def get_mean_3d(input):
return K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.mean(input, axis=[2, 3])), shape_r_out, axis=2)), shape_c_out, axis=3)
def get_std(input):
return K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.std(input, axis=[1, 2])), shape_r_out, axis=1)), shape_c_out, axis=2)
def get_std_3d(input):
return K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.std(input, axis=[2, 3])), shape_r_out, axis=2)), shape_c_out, axis=3)
########################################################
# Loss function
# shape_r_out,shape_c_out = input.shape[1:3]
########################################################
# KL-Divergence Loss
def loss_kl(y_true, y_pred):
y_true.set_shape(y_pred.shape)
y_true /= (get_sum(y_true) + EPS)
y_pred /= (get_sum(y_pred) + EPS)
return K.sum(y_true * K.log((y_true / (y_pred + EPS)) + EPS), axis=[1,2])
def loss_kl_3d(y_true, y_pred):
y_true.set_shape(y_pred.shape)
y_true /= (get_sum_3d(y_true) + EPS)
y_pred /= (get_sum_3d(y_pred) + EPS)
return K.mean(K.sum(y_true * K.log((y_true / (y_pred + EPS)) + EPS), axis=[2,3]),axis=1)
# Correlation Coefficient Loss
def loss_cc(y_true, y_pred):
y_true.set_shape(y_pred.shape)
# y_true = (y_true - get_mean(y_true)) / get_std(y_true)
# y_pred = (y_pred - get_mean(y_pred)) / get_std(y_pred)
#
# y_true = y_true - get_mean(y_true)
# y_pred = y_pred - get_mean(y_pred)
# r1 = K.sum(y_true * y_pred,axis=[1,2])
# r2 = K.sqrt(K.sum(K.square(y_pred),axis=[1,2])*K.sum(K.square(y_true),axis=[1,2]))
# return -2 * r1 / r2
y_true /= (get_sum(y_true) + EPS)
y_pred /= (get_sum(y_pred) + EPS)
N = shape_r_out * shape_c_out
sum_prod = K.sum(y_true * y_pred, axis=[1,2])
sum_x = K.sum(y_true, axis=[1,2])
sum_y = K.sum(y_pred, axis=[1,2])
sum_x_square = K.sum(K.square(y_true), axis=[1,2])
sum_y_square = K.sum(K.square(y_pred), axis=[1,2])
num = sum_prod - ((sum_x * sum_y) / N)
den = K.sqrt((sum_x_square - K.square(sum_x) / N) * (sum_y_square - K.square(sum_y) / N))
return num / den
def loss_cc_3d(y_true, y_pred):
y_true.set_shape(y_pred.shape)
y_true /= (get_sum_3d(y_true) + EPS)
y_pred /= (get_sum_3d(y_pred) + EPS)
N = shape_r_out * shape_c_out
sum_prod = K.sum(y_true * y_pred, axis=[2,3])
sum_x = K.sum(y_true, axis=[2,3])
sum_y = K.sum(y_pred, axis=[2,3])
sum_x_square = K.sum(K.square(y_true), axis=[2,3])
sum_y_square = K.sum(K.square(y_pred), axis=[2,3])
num = sum_prod - ((sum_x * sum_y) / N)
den = K.sqrt((sum_x_square - K.square(sum_x) / N) * (sum_y_square - K.square(sum_y) / N))
return K.mean(num / den, axis=1)
# Normalized Scanpath Saliency Loss
def loss_nss(y_true, y_pred):
y_pred = (y_pred - get_mean(y_pred)) / (get_std(y_pred)+ EPS)
return K.sum(y_true * y_pred, axis=[1, 2]) / (K.sum(y_true, axis=[1, 2])+EPS)
def loss_nss_3d(y_true, y_pred):
y_pred = (y_pred - get_mean_3d(y_pred)) / (get_std_3d(y_pred)+ EPS)
return K.mean((K.sum(y_true * y_pred, axis=[2, 3]) / K.sum(y_true, axis=[2, 3])),axis=1)
def loss_sim_3d(y_true, y_pred):
y_true.set_shape(y_pred.shape)
y_true = (y_true - get_min_3d(y_true)) / (get_max_3d(y_true) - get_min_3d(y_true) + EPS)
y_pred = (y_pred - get_min_3d(y_pred)) / (get_max_3d(y_pred) - get_min_3d(y_pred) + EPS)
y_true /= (get_sum_3d(y_true) + EPS)
y_pred /= (get_sum_3d(y_pred) + EPS)
diff = K.minimum(y_true,y_pred)
score = K.mean(K.sum(diff, axis=[2, 3]), axis=1)
return score
def loss_sim(y_true, y_pred):
y_true.set_shape(y_pred.shape)
y_true = (y_true - get_min(y_true)) / (get_max(y_true) - get_min(y_true) + EPS)
y_pred = (y_pred - get_min(y_pred)) / (get_max(y_pred) - get_min(y_pred) + EPS)
y_true /= (get_sum(y_true) + EPS)
y_pred /= (get_sum(y_pred) + EPS)
diff = K.minimum(y_true,y_pred)
score = K.sum(diff,axis=[1,2])
return score
def loss_funet(y_true, y_pred):
y_true_map = y_true[:,:,:,0:1]
y_true_fix = y_true[:,:,:,1:2]
# kl_loss = loss_kl(y_true_map, y_pred)
# cc_loss = loss_cc(y_true_map, y_pred)
# nss_loss = loss_nss(y_true_fix, y_pred)
# return kl_loss + cc_loss + nss_loss
# for kl loss
y_true_map.set_shape(y_pred.shape)
norm_y_true_map = y_true_map / (get_sum(y_true_map) + EPS)
norm_y_pred = y_pred / (get_sum(y_pred) + EPS)
kl_loss = K.sum(norm_y_true_map * K.log((norm_y_true_map / (norm_y_pred + EPS)) + EPS), axis=[1, 2])
N = shape_r_out * shape_c_out
sum_prod = K.sum(norm_y_true_map * norm_y_pred, axis=[1,2])
sum_x = K.sum(norm_y_true_map, axis=[1,2])
sum_y = K.sum(norm_y_pred, axis=[1,2])
sum_x_square = K.sum(K.square(norm_y_true_map), axis=[1,2])
sum_y_square = K.sum(K.square(norm_y_pred), axis=[1,2])
num = sum_prod - ((sum_x * sum_y) / N)
den = K.sqrt((sum_x_square - K.square(sum_x) / N) * (sum_y_square - K.square(sum_y) / N))
cc_loss = num / den
# for nss loss
y_pred_sal = (y_pred - get_mean(y_pred)) / (get_std(y_pred)+ EPS)
nss_loss = K.sum(y_true_fix * y_pred_sal, axis=[1, 2]) / K.sum(y_true_fix, axis=[1, 2])
return 10 * kl_loss - 2 * cc_loss - nss_loss
def loss_funet_3d(y_true, y_pred):
y_true_map = y_true[:,:,:,:,0:1]
y_true_fix = y_true[:,:,:,:,1:2]
# kl_loss = loss_kl_3d(y_true_map, y_pred)
# cc_loss = loss_cc_3d(y_true_map, y_pred)
# nss_loss = loss_nss_3d(y_true_fix, y_pred)
# return kl_loss + cc_loss + nss_loss
# for kl loss
y_true_map.set_shape(y_pred.shape)
norm_y_true_map = y_true_map / (get_sum_3d(y_true_map) + EPS)
norm_y_pred = y_pred / (get_sum_3d(y_pred) + EPS)
kl_loss = K.mean(K.sum(norm_y_true_map * K.log((norm_y_true_map / (norm_y_pred + EPS)) + EPS), axis=[2, 3]), axis=1)
# for cc loss
# y_true_map.set_shape(y_pred_sal.shape)
# y_true_map /= (get_sum_3d(y_true_map) + EPS)
# y_pred_sal /= (get_sum_3d(y_pred_sal) + EPS)
N = shape_r_out * shape_c_out
sum_prod = K.sum(norm_y_true_map * norm_y_pred, axis=[2,3])
sum_x = K.sum(norm_y_true_map, axis=[2,3])
sum_y = K.sum(norm_y_pred, axis=[2,3])
sum_x_square = K.sum(K.square(norm_y_true_map), axis=[2,3])
sum_y_square = K.sum(K.square(norm_y_pred), axis=[2,3])
num = sum_prod - ((sum_x * sum_y) / N)
den = K.sqrt((sum_x_square - K.square(sum_x) / N) * (sum_y_square - K.square(sum_y) / N))
cc_loss = K.mean(num / den, axis=1)
# for nss loss
y_pred_sal = (y_pred - get_mean_3d(y_pred)) / (get_std_3d(y_pred) + EPS)
nss_loss = K.mean((K.sum(y_true_fix * y_pred_sal, axis=[2, 3]) / K.sum(y_true_fix, axis=[2, 3])), axis=1)
return 10 * kl_loss - 2 * cc_loss - nss_loss