forked from MIA2I/Shape-preservingLoss
/
Loss.py
253 lines (192 loc) · 8.2 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
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
import sys
sys.path.append('/home/john/caffe-master/python/')
import caffe
import numpy as np
from skimage import filter
from skimage.morphology import dilation, disk
from numpy import linalg as LA
class WeightedSoftmaxLossLayer(caffe.Layer):
"""
Compute the Softmax Loss in the same manner but use the skeletal loss as weights
"""
def setup(self, bottom, top):
# check input pair
if len(bottom) != 2:
raise Exception("Need 2 inputs to compute distance.")
def reshape(self, bottom, top):
# check input dimensions match
if bottom[0].num != bottom[1].num:
raise Exception("Inputs must have the same dimension.")
# difference is shape of inputs
self.diff = np.zeros_like(bottom[0].data, dtype=np.float32)
# weights matrix
self.mask = np.zeros_like(bottom[0].data, dtype=np.float32)
# loss output is scalar
top[0].reshape(1)
# ratio for imbalanced data
self.ratio = 0.5
def forward(self, bottom, top):
# run softmax() layer
score = np.copy(bottom[0].data)
temp = np.maximum(score[0,0,:,:], score[0,1,:,:])
score[0,0,:,:] -= temp
score[0,1,:,:] -= temp
prob = np.exp(score)
temp = prob[0,0,:,:] + prob[0,1,:,:]
prob[0,0,:,:] /= temp
prob[0,1,:,:] /= temp
# generate weights matrix
label = np.copy(bottom[1].data)
# calcualte self.ratio
self.ratio = np.count_nonzero(label==1) * 1.0 / np.count_nonzero(label!=255)
temp = np.copy(label)
temp[...] = 0
temp[np.where(label==0)] = self.ratio
temp[np.where(label==255)] = 0
self.mask[0,0,:,:] = np.copy(temp)
temp[...] = 0
temp[np.where(label>0)] = 1.0 - self.ratio
temp[np.where(label==255)] = 0
self.mask[0,1,:,:] = np.copy(temp)
count = np.count_nonzero(self.mask)
weights = np.copy(self.mask)
# calculate loss
probs = np.copy(prob)
probs[np.where(probs<1.175494e-38)] = 1.175494e-38
logprob = -np.log(probs)
data_loss = np.sum(weights*logprob) *1.0 / count
self.diff[...] = np.copy(prob)
top[0].data[...] = np.copy(data_loss)
def backward(self, top, propagate_down, bottom):
delta = np.copy(self.diff[...])
count = np.count_nonzero(self.mask)
delta[np.where(self.mask>0)] -= 1
# generate pixel-wise matrix
label = np.copy(bottom[1].data)
weights = np.copy(bottom[0].data)
temp = np.copy(label)
temp[...] = 0
temp[np.where(label==0)] = self.ratio
temp[np.where(label>0)] = 1.0 - self.ratio
temp[np.where(label==255)] = 0
weights[0,0,:,:] = np.copy(temp)
weights[0,1,:,:] = np.copy(temp)
delta *= weights
bottom[0].diff[...] = delta * 1.0 / count
class ContourLossLayer(caffe.Layer):
"""
Compute the Softmax Loss in the same manner but use the skeletal loss as weights
"""
def setup(self, bottom, top):
# check input pair
if len(bottom) != 4:
raise Exception("Need 4 inputs to compute distance.")
def reshape(self, bottom, top):
# check input dimensions match
if bottom[0].num != bottom[1].num:
raise Exception("Inputs must have the same dimension.")
# difference is shape of inputs
self.diff = np.zeros_like(bottom[0].data, dtype=np.float32)
# weights matrix
self.mask = np.zeros_like(bottom[0].data, dtype=np.float32)
# similarity matrix
self.contourmask = np.ones_like(bottom[1].data, dtype=np.float32)
# weights matrix
self.Weights = np.ones_like(bottom[1].data, dtype=np.float32)
# loss output is scalar
top[0].reshape(1)
# ratio for imbalanced data
self.ratio = 0.5
def forward(self, bottom, top):
# run softmax() layer
score = np.copy(bottom[0].data)
temp = np.maximum(score[0,0,:,:], score[0,1,:,:])
score[0,0,:,:] -= temp
score[0,1,:,:] -= temp
prob = np.exp(score)
temp = prob[0,0,:,:] + prob[0,1,:,:]
prob[0,0,:,:] /= temp
prob[0,1,:,:] /= temp
# calcl skeletal loss
img = np.copy(prob[0,1,:,:])
img[np.where(img<0.5)] = 0
img[np.where(img>0)] = 1
label = np.copy(bottom[1].data[0,0,:,:])
IDMask = np.copy(bottom[2].data[0,0,:,:])
w = np.copy(bottom[3].data[0,0,:,:])
w[np.where(w>30)] = 0
self.Weights = 1.0 / (1.0 + np.exp(w - 15.0))
self.Weights[np.where(w==0)] = 0
self.contourmask[0,0,:,:] = 1.0 + 4.0 * (1.0 - self.ContourLoss(img, label, IDMask))
# generate pixel-wise matrix
label = np.copy(bottom[1].data)
Range = np.copy(bottom[2].data)
#label[np.where(Range==0)] = 0
# calcualte self.ratio
self.ratio = np.count_nonzero(label==1) * 1.0 / np.count_nonzero(label!=255)
temp = np.copy(label)
temp[...] = 0
temp[np.where(label==0)] = self.ratio
self.mask[0,0,:,:] = np.copy(temp)
temp[...] = 0
temp[np.where(label!=0)] = 1 - self.ratio
self.mask[0,1,:,:] = np.copy(temp)
count = np.count_nonzero(self.mask)
#weights: combination of self.mask and self.skelmask
weights = self.mask * self.contourmask * (1.0 + 6.0 * self.Weights)
# calculate loss
probs = np.copy(prob)
probs[np.where(probs<1.175494e-38)] = 1.175494e-38
logprob = -np.log(probs)
data_loss = np.sum(weights*logprob) * 1.0 / count
self.diff[...] = np.copy(prob)
top[0].data[...] = np.copy(data_loss)
def backward(self, top, propagate_down, bottom):
delta = np.copy(self.diff[...])
count = np.count_nonzero(self.mask)
delta[np.where(self.mask>0)] -= 1
# generate pixel-wise matrix
label = np.copy(bottom[1].data)
Range = np.copy(bottom[2].data)
#label[np.where(Range==0)] = 0
Weights = np.copy(bottom[3].data[0,0,:,:])
mask = np.copy(bottom[0].data)
temp = np.copy(label)
temp[...] = 0
temp[np.where(label==0)] = self.ratio
temp[np.where(label!=0)] = 1 - self.ratio
mask[0,0,:,:] = np.copy(temp)
mask[0,1,:,:] = np.copy(temp)
#weights: combination of self.mask and self.skelmask
weights = mask * self.contourmask * (1.0 + 6.0 * self.Weights)
delta *= weights
bottom[0].diff[...] = delta * 1.0 / count
def ContourLoss(self, img, label, IDMask):
contour = filter.canny(img)
selem = disk(5)
outlier = dilation(contour, selem)
outlier[np.where(np.absolute(IDMask)>0)] = 0
contour = contour.astype(float)
contour *= IDMask
Similarity = np.ones_like(IDMask, dtype=np.float)
Similarity[np.where(outlier>0)] = -0.5
if np.amax(IDMask) > 100000:
raise Exception("Wrong in IDMask!")
LocationsSrc = {index: np.where(np.absolute(contour)==index+1) for index in range(np.amax(IDMask))}
LocationsRef = {index: np.where(IDMask==-(index+1)) for index in range(np.amax(IDMask))}
for index in range(np.amax(IDMask)):
SrcX = LocationsSrc[index][0]
SrcY = LocationsSrc[index][1]
RefX = LocationsRef[index][0]
RefY = LocationsRef[index][1]
ContourSimilarity = 0
if np.size(SrcX) > 0.9 * np.size(RefX) and np.size(RefX) > 10 and np.size(SrcX) < 1.2 * np.size(RefX):
if np.size(np.unique(SrcX)) > np.size(np.unique(SrcY)):
VecSrc = np.polyfit(SrcX, SrcY, 3)[:3]
VecRef = np.polyfit(RefX, RefY, 3)[:3]
else:
VecSrc = np.polyfit(SrcY, SrcX, 3)[:3]
VecRef = np.polyfit(RefY, RefX, 3)[:3]
ContourSimilarity = np.absolute(np.inner(VecSrc, VecRef)) / (LA.norm(VecSrc) + 1e-10) / (LA.norm(VecRef) + 1e-10)
Similarity[np.where(np.absolute(IDMask)==index+1)] = ContourSimilarity
return Similarity