-
Notifications
You must be signed in to change notification settings - Fork 0
/
layers.py
370 lines (322 loc) · 14.8 KB
/
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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import numpy as np
import torch
from torch import nn
import math
class PostRes2d(nn.Module):
def __init__(self, n_in, n_out, stride = 1):
super(PostRes2d, self).__init__()
self.conv1 = nn.Conv2d(n_in, n_out, kernel_size = 3, stride = stride, padding = 1)
self.bn1 = nn.BatchNorm2d(n_out)
self.relu = nn.ReLU(inplace = True)
self.conv2 = nn.Conv2d(n_out, n_out, kernel_size = 3, padding = 1)
self.bn2 = nn.BatchNorm2d(n_out)
if stride != 1 or n_out != n_in:
self.shortcut = nn.Sequential(
nn.Conv2d(n_in, n_out, kernel_size = 1, stride = stride),
nn.BatchNorm2d(n_out))
else:
self.shortcut = None
def forward(self, x):
residual = x
if self.shortcut is not None:
residual = self.shortcut(x)
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out += residual
out = self.relu(out)
return out
class PostRes(nn.Module):
def __init__(self, n_in, n_out, stride = 1):
super(PostRes, self).__init__()
self.conv1 = nn.Conv3d(n_in, n_out, kernel_size = 3, stride = stride, padding = 1)
self.bn1 = nn.BatchNorm3d(n_out)
self.relu = nn.ReLU(inplace = True)
self.conv2 = nn.Conv3d(n_out, n_out, kernel_size = 3, padding = 1)
self.bn2 = nn.BatchNorm3d(n_out)
if stride != 1 or n_out != n_in:
self.shortcut = nn.Sequential(
nn.Conv3d(n_in, n_out, kernel_size = 1, stride = stride),
nn.BatchNorm3d(n_out))
else:
self.shortcut = None
def forward(self, x):
residual = x
if self.shortcut is not None:
residual = self.shortcut(x)
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out += residual
out = self.relu(out)
return out
class Rec3(nn.Module):
def __init__(self, n0, n1, n2, n3, p = 0.0, integrate = True):
super(Rec3, self).__init__()
self.block01 = nn.Sequential(
nn.Conv3d(n0, n1, kernel_size = 3, stride = 2, padding = 1),
nn.BatchNorm3d(n1),
nn.ReLU(inplace = True),
nn.Conv3d(n1, n1, kernel_size = 3, padding = 1),
nn.BatchNorm3d(n1))
self.block11 = nn.Sequential(
nn.Conv3d(n1, n1, kernel_size = 3, padding = 1),
nn.BatchNorm3d(n1),
nn.ReLU(inplace = True),
nn.Conv3d(n1, n1, kernel_size = 3, padding = 1),
nn.BatchNorm3d(n1))
self.block21 = nn.Sequential(
nn.ConvTranspose3d(n2, n1, kernel_size = 2, stride = 2),
nn.BatchNorm3d(n1),
nn.ReLU(inplace = True),
nn.Conv3d(n1, n1, kernel_size = 3, padding = 1),
nn.BatchNorm3d(n1))
self.block12 = nn.Sequential(
nn.Conv3d(n1, n2, kernel_size = 3, stride = 2, padding = 1),
nn.BatchNorm3d(n2),
nn.ReLU(inplace = True),
nn.Conv3d(n2, n2, kernel_size = 3, padding = 1),
nn.BatchNorm3d(n2))
self.block22 = nn.Sequential(
nn.Conv3d(n2, n2, kernel_size = 3, padding = 1),
nn.BatchNorm3d(n2),
nn.ReLU(inplace = True),
nn.Conv3d(n2, n2, kernel_size = 3, padding = 1),
nn.BatchNorm3d(n2))
self.block32 = nn.Sequential(
nn.ConvTranspose3d(n3, n2, kernel_size = 2, stride = 2),
nn.BatchNorm3d(n2),
nn.ReLU(inplace = True),
nn.Conv3d(n2, n2, kernel_size = 3, padding = 1),
nn.BatchNorm3d(n2))
self.block23 = nn.Sequential(
nn.Conv3d(n2, n3, kernel_size = 3, stride = 2, padding = 1),
nn.BatchNorm3d(n3),
nn.ReLU(inplace = True),
nn.Conv3d(n3, n3, kernel_size = 3, padding = 1),
nn.BatchNorm3d(n3))
self.block33 = nn.Sequential(
nn.Conv3d(n3, n3, kernel_size = 3, padding = 1),
nn.BatchNorm3d(n3),
nn.ReLU(inplace = True),
nn.Conv3d(n3, n3, kernel_size = 3, padding = 1),
nn.BatchNorm3d(n3))
self.relu = nn.ReLU(inplace = True)
self.p = p
self.integrate = integrate
def forward(self, x0, x1, x2, x3):
if self.p > 0 and self.training:
coef = torch.bernoulli((1.0 - self.p) * torch.ones(8))
out1 = coef[0] * self.block01(x0) + coef[1] * self.block11(x1) + coef[2] * self.block21(x2)
out2 = coef[3] * self.block12(x1) + coef[4] * self.block22(x2) + coef[5] * self.block32(x3)
out3 = coef[6] * self.block23(x2) + coef[7] * self.block33(x3)
else:
out1 = (1 - self.p) * (self.block01(x0) + self.block11(x1) + self.block21(x2))
out2 = (1 - self.p) * (self.block12(x1) + self.block22(x2) + self.block32(x3))
out3 = (1 - self.p) * (self.block23(x2) + self.block33(x3))
if self.integrate:
out1 += x1
out2 += x2
out3 += x3
return x0, self.relu(out1), self.relu(out2), self.relu(out3)
def hard_mining(neg_output, neg_labels, num_hard):
_, idcs = torch.topk(neg_output, min(num_hard, len(neg_output)))
neg_output = torch.index_select(neg_output, 0, idcs)
neg_labels = torch.index_select(neg_labels, 0, idcs)
return neg_output, neg_labels
class Loss(nn.Module):
def __init__(self, num_hard = 0):
super(Loss, self).__init__()
self.sigmoid = nn.Sigmoid()
self.classify_loss = nn.BCELoss()
self.regress_loss = nn.SmoothL1Loss()
self.num_hard = num_hard#2
def forward(self, output, labels, train = True):
batch_size = labels.size(0)
# print('loss',output.shape,labels.shape)#torch.Size([5, 24, 24, 24, 3, 5]) torch.Size([5, 24, 24, 24, 3, 5])
output = output.view(-1, 5)#将输出维度调整,以anchor为第二维度
labels = labels.view(-1, 5)
# print('loss',output.shape,labels.shape)#torch.Size([207360, 5]) torch.Size([207360, 5])
pos_idcs = labels[:, 0] > 0.5#对标签进行筛选,输出为索引,示例[1,2,5],If an anchor overlaps a ground truth bounding box with the intersection over union (IoU) higher than 0.5, we consider it as a positive anchor
pos_idcs = pos_idcs.unsqueeze(1).expand(pos_idcs.size(0), 5)#对索引维度扩展,重复5次,示例[[1,1,1,1,1],[2,2,2,2,2],[5,5,5,5,5]]
pos_output = output[pos_idcs].view(-1, 5)#筛选出与正标签对应的输出
pos_labels = labels[pos_idcs].view(-1, 5)#筛选出正标签
neg_idcs = labels[:, 0] < -0.5#同上,筛选负标签索引,此处为负值
neg_output = output[:, 0][neg_idcs]#注意,此处与上面不同,负标签只考虑置信度即可,因为位置及直径不计入损失,没有意义
neg_labels = labels[:, 0][neg_idcs]
if self.num_hard > 0 and train:
neg_output, neg_labels = hard_mining(neg_output, neg_labels, self.num_hard * batch_size)#只选择置信度较高的负样本作计算,对于易于分类的负样本,都是虾兵蟹将,不足虑
neg_prob = self.sigmoid(neg_output)#对负样本输出进行sigmoid处理,生成0~1之间的值,符合置信度的范围,可能大家要问输出不就是0~1吗,这里网络最后没有用sigmoid激活函数,所以最后输出应该是没有范围的,
#classify_loss = self.classify_loss(
# torch.cat((pos_prob, neg_prob), 0),
# torch.cat((pos_labels[:, 0], neg_labels + 1), 0))
if len(pos_output)>0:
pos_prob = self.sigmoid(pos_output[:, 0])#对正样本进行sigmoid处理
pz, ph, pw, pd = pos_output[:, 1], pos_output[:, 2], pos_output[:, 3], pos_output[:, 4]#依次输出z,h,w,d以便与标签结合求损失
lz, lh, lw, ld = pos_labels[:, 1], pos_labels[:, 2], pos_labels[:, 3], pos_labels[:, 4]#依次输出z,h,w,d以便与输出结合求损失
# print('0',pos_output.shape)
# print('0',pz, lz)
# print('0',ph, lh)
# print('0',pw, lw)
# print('0',pd, ld)
# print('0',stop)
regress_losses = [#回归损失
self.regress_loss(pz, lz),
self.regress_loss(ph, lh),
self.regress_loss(pw, lw),
self.regress_loss(pd, ld)]
# regress_losses_data = [l.data[0] for l in regress_losses]#torch0.3-0.1
regress_losses_data = [l.item() for l in regress_losses]#torch1.0
classify_loss = 0.5 * self.classify_loss(#对正样本和负样本分别求分类损失
pos_prob, pos_labels[:, 0]) + 0.5 * self.classify_loss(
neg_prob, neg_labels + 1)
pos_correct = (pos_prob.data >= 0.5).sum()#那些输出确实大于0.5的正样本是正确预测的正样本
pos_total = len(pos_prob)#正样本总数
else:#如果没有正标签,由于负标签又不用计算回归损失,于是回归损失就置零了,分类损失只计算负标签的分类损失
regress_losses = [0,0,0,0]
classify_loss = 0.5 * self.classify_loss(
neg_prob, neg_labels + 1)
pos_correct = 0 #此时没有正样本或正标签
pos_total = 0#总数也为0
regress_losses_data = [0,0,0,0]
# classify_loss_data = classify_loss.data[0]#torch0.1-0.3
classify_loss_data = classify_loss.item()#torch1.0
loss = classify_loss
for regress_loss in regress_losses:#将回归损失与分类损失相加,求出总损失(标量)
loss += regress_loss
neg_correct = (neg_prob.data < 0.5).sum()#那些输出确实低于0.5的负样本是正确预测的负样本
neg_total = len(neg_prob)
return [loss, classify_loss_data] + regress_losses_data + [pos_correct, pos_total, neg_correct, neg_total]
class GetPBB(object):
def __init__(self, config):
self.stride = config['stride']
self.anchors = np.asarray(config['anchors'])
def __call__(self, output,thresh = -3, ismask=False):
stride = self.stride#4
anchors = self.anchors#5,10,20
output = np.copy(output)
# print(stride, (float(stride) - 1) / 2)#4,1.5
offset = (float(stride) - 1) / 2#1.5
output_size = output.shape
oz = np.arange(offset, offset + stride * (output_size[0] - 1) + 1, stride)
oh = np.arange(offset, offset + stride * (output_size[1] - 1) + 1, stride)
ow = np.arange(offset, offset + stride * (output_size[2] - 1) + 1, stride)
# print('PBB-output',output.shape,oz.shape)
# print(oz.reshape((-1, 1, 1, 1)).shape, (output[:, :, :, :, 1] * anchors.reshape((1, 1, 1, -1))).shape)#(80, 1, 1, 1) (80, 80, 80, 3) (1, 1, 1, 3)
output[:, :, :, :, 1] = oz.reshape((-1, 1, 1, 1)) + output[:, :, :, :, 1] * anchors.reshape((1, 1, 1, -1))
output[:, :, :, :, 2] = oh.reshape((1, -1, 1, 1)) + output[:, :, :, :, 2] * anchors.reshape((1, 1, 1, -1))
output[:, :, :, :, 3] = ow.reshape((1, 1, -1, 1)) + output[:, :, :, :, 3] * anchors.reshape((1, 1, 1, -1))
output[:, :, :, :, 4] = np.exp(output[:, :, :, :, 4]) * anchors.reshape((1, 1, 1, -1))
mask = output[..., 0] > thresh
xx,yy,zz,aa = np.where(mask)
output = output[xx,yy,zz,aa]
if ismask:
return output,[xx,yy,zz,aa]
else:
return output
#output = output[output[:, 0] >= self.conf_th]
#bboxes = nms(output, self.nms_th)
def nms(output, nms_th):
if len(output) == 0:
return output
output = output[np.argsort(-output[:, 0])]
bboxes = [output[0]]
for i in np.arange(1, len(output)):
bbox = output[i]
flag = 1
for j in range(len(bboxes)):
if iou(bbox[1:5], bboxes[j][1:5]) >= nms_th:
flag = -1
break
if flag == 1:
bboxes.append(bbox)
bboxes = np.asarray(bboxes, np.float32)
return bboxes
def iou(box0, box1):
# print('box0//',box0)
r0 = box0[3] / 2
s0 = box0[:3] - r0
e0 = box0[:3] + r0
r1 = box1[3] / 2
s1 = box1[:3] - r1
e1 = box1[:3] + r1
overlap = []
for i in range(len(s0)):
overlap.append(max(0, min(e0[i], e1[i]) - max(s0[i], s1[i])))
intersection = overlap[0] * overlap[1] * overlap[2]
union = box0[3] * box0[3] * box0[3] + box1[3] * box1[3] * box1[3] - intersection
return intersection / union
def acc(pbb, lbb, conf_th, nms_th, detect_th):
pbb = pbb[pbb[:, 0] >= conf_th]
pbb = nms(pbb, nms_th)
tp = []
fp = []
fn = []
l_flag = np.zeros((len(lbb),), np.int32)
for p in pbb:
flag = 0
bestscore = 0
for i, l in enumerate(lbb):
score = iou(p[1:5], l)
if score>bestscore:
bestscore = score
besti = i
if bestscore > detect_th:
flag = 1
if l_flag[besti] == 0:
l_flag[besti] = 1
tp.append(np.concatenate([p,[bestscore]],0))
else:
fp.append(np.concatenate([p,[bestscore]],0))
if flag == 0:
fp.append(np.concatenate([p,[bestscore]],0))
for i,l in enumerate(lbb):
if l_flag[i]==0:
score = []
for p in pbb:
score.append(iou(p[1:5],l))
if len(score)!=0:
bestscore = np.max(score)
else:
bestscore = 0
if bestscore<detect_th:
fn.append(np.concatenate([l,[bestscore]],0))
return tp, fp, fn, len(lbb)
def topkpbb(pbb,lbb,nms_th,detect_th,topk=30):
conf_th = 0
fp = []
tp = []
while len(tp)+len(fp)<topk:
conf_th = conf_th-0.2
tp, fp, fn, _ = acc(pbb, lbb, conf_th, nms_th, detect_th)
if conf_th<-3:
break
tp = np.array(tp).reshape([len(tp),6])
fp = np.array(fp).reshape([len(fp),6])
fn = np.array(fn).reshape([len(fn),5])
allp = np.concatenate([tp,fp],0)
sorting = np.argsort(allp[:,0])[::-1]
n_tp = len(tp)
topk = np.min([topk,len(allp)])
tp_in_topk = np.array([i for i in range(n_tp) if i in sorting[:topk]])
fp_in_topk = np.array([i for i in range(topk) if sorting[i] not in range(n_tp)])
# print(fp_in_topk)
fn_i = np.array([i for i in range(n_tp) if i not in sorting[:topk]])
newallp = allp[:topk]
if len(fn_i)>0:
fn = np.concatenate([fn,tp[fn_i,:5]])
else:
fn = fn
if len(tp_in_topk)>0:
tp = tp[tp_in_topk]
else:
tp = []
if len(fp_in_topk)>0:
fp = newallp[fp_in_topk]
else:
fp = []
return tp, fp , fn