-
Notifications
You must be signed in to change notification settings - Fork 36
/
fcaf3d_neck_with_head.py
466 lines (416 loc) · 19.1 KB
/
fcaf3d_neck_with_head.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import torch
from torch import nn
import MinkowskiEngine as ME
from mmdet.core import BaseAssigner, reduce_mean, build_assigner
from mmdet.models.builder import HEADS, build_loss
from mmdet.core.bbox.builder import BBOX_ASSIGNERS
from mmcv.cnn import Scale, bias_init_with_prob
from mmdet3d.core.bbox.structures import rotation_3d_in_axis
from mmdet3d.ops.pcdet_nms import pcdet_nms_gpu, pcdet_nms_normal_gpu
@HEADS.register_module()
class Fcaf3DNeckWithHead(nn.Module):
def __init__(self,
n_classes,
in_channels,
out_channels,
n_reg_outs,
voxel_size,
pts_threshold,
assigner,
yaw_parametrization='fcaf3d',
loss_centerness=dict(
type='CrossEntropyLoss',
use_sigmoid=True,
loss_weight=1.0),
loss_bbox=dict(type='IoU3DLoss', loss_weight=1.0),
loss_cls=dict(
type='FocalLoss',
use_sigmoid=True,
gamma=2.0,
alpha=0.25,
loss_weight=1.0),
train_cfg=None,
test_cfg=None):
super(Fcaf3DNeckWithHead, self).__init__()
self.voxel_size = voxel_size
self.yaw_parametrization = yaw_parametrization
self.assigner = build_assigner(assigner)
self.loss_centerness = build_loss(loss_centerness)
self.loss_bbox = build_loss(loss_bbox)
self.loss_cls = build_loss(loss_cls)
self.train_cfg = train_cfg
self.test_cfg = test_cfg
self.pts_threshold = pts_threshold
self._init_layers(in_channels, out_channels, n_reg_outs, n_classes)
@staticmethod
def _make_block(in_channels, out_channels):
return nn.Sequential(
ME.MinkowskiConvolution(in_channels, out_channels, kernel_size=3, dimension=3),
ME.MinkowskiBatchNorm(out_channels),
ME.MinkowskiELU()
)
@staticmethod
def _make_up_block(in_channels, out_channels):
return nn.Sequential(
ME.MinkowskiGenerativeConvolutionTranspose(
in_channels,
out_channels,
kernel_size=2,
stride=2,
dimension=3,
),
ME.MinkowskiBatchNorm(out_channels),
ME.MinkowskiELU(),
ME.MinkowskiConvolution(out_channels, out_channels, kernel_size=3, dimension=3),
ME.MinkowskiBatchNorm(out_channels),
ME.MinkowskiELU()
)
def _init_layers(self, in_channels, out_channels, n_reg_outs, n_classes):
# neck layers
self.pruning = ME.MinkowskiPruning()
for i in range(len(in_channels)):
if i > 0:
self.__setattr__(f'up_block_{i}', self._make_up_block(in_channels[i], in_channels[i - 1]))
self.__setattr__(f'out_block_{i}', self._make_block(in_channels[i], out_channels))
# head layers
self.centerness_conv = ME.MinkowskiConvolution(out_channels, 1, kernel_size=1, dimension=3)
self.reg_conv = ME.MinkowskiConvolution(out_channels, n_reg_outs, kernel_size=1, dimension=3)
self.cls_conv = ME.MinkowskiConvolution(out_channels, n_classes, kernel_size=1, bias=True, dimension=3)
self.scales = nn.ModuleList([Scale(1.) for _ in range(len(in_channels))])
def init_weights(self):
nn.init.normal_(self.centerness_conv.kernel, std=.01)
nn.init.normal_(self.reg_conv.kernel, std=.01)
nn.init.normal_(self.cls_conv.kernel, std=.01)
nn.init.constant_(self.cls_conv.bias, bias_init_with_prob(.01))
def forward(self, x):
outs = []
inputs = x
x = inputs[-1]
for i in range(len(inputs) - 1, -1, -1):
if i < len(inputs) - 1:
x = self.__getattr__(f'up_block_{i + 1}')(x)
x = inputs[i] + x
x = self._prune(x, scores)
out = self.__getattr__(f'out_block_{i}')(x)
out = self.forward_single(out, self.scales[i])
scores = out[-1]
outs.append(out[:-1])
return zip(*outs[::-1])
def _prune(self, x, scores):
if self.pts_threshold < 0:
return x
with torch.no_grad():
coordinates = x.C.float()
interpolated_scores = scores.features_at_coordinates(coordinates)
prune_mask = interpolated_scores.new_zeros((len(interpolated_scores)), dtype=torch.bool)
for permutation in x.decomposition_permutations:
score = interpolated_scores[permutation]
mask = score.new_zeros((len(score)), dtype=torch.bool)
topk = min(len(score), self.pts_threshold)
ids = torch.topk(score.squeeze(1), topk, sorted=False).indices
mask[ids] = True
prune_mask[permutation[mask]] = True
x = self.pruning(x, prune_mask)
return x
def loss(self,
centernesses,
bbox_preds,
cls_scores,
points,
gt_bboxes,
gt_labels,
img_metas):
assert len(centernesses[0]) == len(bbox_preds[0]) == len(cls_scores[0]) \
== len(points[0]) == len(img_metas) == len(gt_bboxes) == len(gt_labels)
loss_centerness, loss_bbox, loss_cls = [], [], []
for i in range(len(img_metas)):
img_loss_centerness, img_loss_bbox, img_loss_cls = self._loss_single(
centernesses=[x[i] for x in centernesses],
bbox_preds=[x[i] for x in bbox_preds],
cls_scores=[x[i] for x in cls_scores],
points=[x[i] for x in points],
img_meta=img_metas[i],
gt_bboxes=gt_bboxes[i],
gt_labels=gt_labels[i]
)
loss_centerness.append(img_loss_centerness)
loss_bbox.append(img_loss_bbox)
loss_cls.append(img_loss_cls)
return dict(
loss_centerness=torch.mean(torch.stack(loss_centerness)),
loss_bbox=torch.mean(torch.stack(loss_bbox)),
loss_cls=torch.mean(torch.stack(loss_cls))
)
# per image
def _loss_single(self,
centernesses,
bbox_preds,
cls_scores,
points,
gt_bboxes,
gt_labels,
img_meta):
with torch.no_grad():
centerness_targets, bbox_targets, labels = self.assigner.assign(points, gt_bboxes, gt_labels)
centerness = torch.cat(centernesses)
bbox_preds = torch.cat(bbox_preds)
cls_scores = torch.cat(cls_scores)
points = torch.cat(points)
# skip background
pos_inds = torch.nonzero(labels >= 0).squeeze(1)
n_pos = torch.tensor(len(pos_inds), dtype=torch.float, device=centerness.device)
n_pos = max(reduce_mean(n_pos), 1.)
loss_cls = self.loss_cls(cls_scores, labels, avg_factor=n_pos)
pos_centerness = centerness[pos_inds]
pos_bbox_preds = bbox_preds[pos_inds]
pos_centerness_targets = centerness_targets[pos_inds].unsqueeze(1)
pos_bbox_targets = bbox_targets[pos_inds]
# centerness weighted iou loss
centerness_denorm = max(
reduce_mean(pos_centerness_targets.sum().detach()), 1e-6)
if len(pos_inds) > 0:
pos_points = points[pos_inds]
loss_centerness = self.loss_centerness(
pos_centerness, pos_centerness_targets, avg_factor=n_pos
)
loss_bbox = self.loss_bbox(
self._bbox_pred_to_bbox(pos_points, pos_bbox_preds),
pos_bbox_targets,
weight=pos_centerness_targets.squeeze(1),
avg_factor=centerness_denorm
)
else:
loss_centerness = pos_centerness.sum()
loss_bbox = pos_bbox_preds.sum()
return loss_centerness, loss_bbox, loss_cls
def get_bboxes(self,
centernesses,
bbox_preds,
cls_scores,
points,
img_metas,
rescale=False):
assert len(centernesses[0]) == len(bbox_preds[0]) == len(cls_scores[0]) \
== len(points[0]) == len(img_metas)
results = []
for i in range(len(img_metas)):
result = self._get_bboxes_single(
centernesses=[x[i] for x in centernesses],
bbox_preds=[x[i] for x in bbox_preds],
cls_scores=[x[i] for x in cls_scores],
points=[x[i] for x in points],
img_meta=img_metas[i]
)
results.append(result)
return results
# per image
def _get_bboxes_single(self,
centernesses,
bbox_preds,
cls_scores,
points,
img_meta):
mlvl_bboxes, mlvl_scores = [], []
for centerness, bbox_pred, cls_score, point in zip(
centernesses, bbox_preds, cls_scores, points
):
scores = cls_score.sigmoid() * centerness.sigmoid()
max_scores, _ = scores.max(dim=1)
if len(scores) > self.test_cfg.nms_pre > 0:
_, ids = max_scores.topk(self.test_cfg.nms_pre)
bbox_pred = bbox_pred[ids]
scores = scores[ids]
point = point[ids]
bboxes = self._bbox_pred_to_bbox(point, bbox_pred)
mlvl_bboxes.append(bboxes)
mlvl_scores.append(scores)
bboxes = torch.cat(mlvl_bboxes)
scores = torch.cat(mlvl_scores)
bboxes, scores, labels = self._nms(bboxes, scores, img_meta)
return bboxes, scores, labels
# per scale
def forward_single(self, x, scale):
centerness = self.centerness_conv(x).features
scores = self.cls_conv(x)
cls_score = scores.features
prune_scores = ME.SparseTensor(
scores.features.max(dim=1, keepdim=True).values,
coordinate_map_key=scores.coordinate_map_key,
coordinate_manager=scores.coordinate_manager)
reg_final = self.reg_conv(x).features
reg_distance = torch.exp(scale(reg_final[:, :6]))
reg_angle = reg_final[:, 6:]
bbox_pred = torch.cat((reg_distance, reg_angle), dim=1)
centernesses, bbox_preds, cls_scores, points = [], [], [], []
for permutation in x.decomposition_permutations:
centernesses.append(centerness[permutation])
bbox_preds.append(bbox_pred[permutation])
cls_scores.append(cls_score[permutation])
points = x.decomposed_coordinates
for i in range(len(points)):
points[i] = points[i] * self.voxel_size
return centernesses, bbox_preds, cls_scores, points, prune_scores
def _bbox_pred_to_bbox(self, points, bbox_pred):
if bbox_pred.shape[0] == 0:
return bbox_pred
x_center = points[:, 0] + (bbox_pred[:, 1] - bbox_pred[:, 0]) / 2
y_center = points[:, 1] + (bbox_pred[:, 3] - bbox_pred[:, 2]) / 2
z_center = points[:, 2] + (bbox_pred[:, 5] - bbox_pred[:, 4]) / 2
# dx_min, dx_max, dy_min, dy_max, dz_min, dz_max -> x, y, z, w, l, h
base_bbox = torch.stack([
x_center,
y_center,
z_center,
bbox_pred[:, 0] + bbox_pred[:, 1],
bbox_pred[:, 2] + bbox_pred[:, 3],
bbox_pred[:, 4] + bbox_pred[:, 5],
], -1)
if bbox_pred.shape[1] == 6:
return base_bbox
if self.yaw_parametrization == 'naive':
# ..., alpha
return torch.cat((
base_bbox,
bbox_pred[:, 6:7]
), -1)
elif self.yaw_parametrization == 'sin-cos':
# ..., sin(a), cos(a)
norm = torch.pow(torch.pow(bbox_pred[:, 6:7], 2) + torch.pow(bbox_pred[:, 7:8], 2), 0.5)
sin = bbox_pred[:, 6:7] / norm
cos = bbox_pred[:, 7:8] / norm
return torch.cat((
base_bbox,
torch.atan2(sin, cos)
), -1)
else: # self.yaw_parametrization == 'fcaf3d'
# ..., sin(2a)ln(q), cos(2a)ln(q)
scale = bbox_pred[:, 0] + bbox_pred[:, 1] + bbox_pred[:, 2] + bbox_pred[:, 3]
q = torch.exp(torch.sqrt(torch.pow(bbox_pred[:, 6], 2) + torch.pow(bbox_pred[:, 7], 2)))
alpha = 0.5 * torch.atan2(bbox_pred[:, 6], bbox_pred[:, 7])
return torch.stack((
x_center,
y_center,
z_center,
scale / (1 + q),
scale / (1 + q) * q,
bbox_pred[:, 5] + bbox_pred[:, 4],
alpha
), dim=-1)
def _nms(self, bboxes, scores, img_meta):
n_classes = scores.shape[1]
yaw_flag = bboxes.shape[1] == 7
nms_bboxes, nms_scores, nms_labels = [], [], []
for i in range(n_classes):
ids = scores[:, i] > self.test_cfg.score_thr
if not ids.any():
continue
class_scores = scores[ids, i]
class_bboxes = bboxes[ids]
if yaw_flag:
nms_function = pcdet_nms_gpu
else:
class_bboxes = torch.cat((
class_bboxes, torch.zeros_like(class_bboxes[:, :1])), dim=1)
nms_function = pcdet_nms_normal_gpu
nms_ids, _ = nms_function(class_bboxes, class_scores, self.test_cfg.iou_thr)
nms_bboxes.append(class_bboxes[nms_ids])
nms_scores.append(class_scores[nms_ids])
nms_labels.append(bboxes.new_full(class_scores[nms_ids].shape, i, dtype=torch.long))
if len(nms_bboxes):
nms_bboxes = torch.cat(nms_bboxes, dim=0)
nms_scores = torch.cat(nms_scores, dim=0)
nms_labels = torch.cat(nms_labels, dim=0)
else:
nms_bboxes = bboxes.new_zeros((0, bboxes.shape[1]))
nms_scores = bboxes.new_zeros((0,))
nms_labels = bboxes.new_zeros((0,))
if yaw_flag:
box_dim = 7
with_yaw = True
else:
box_dim = 6
with_yaw = False
nms_bboxes = nms_bboxes[:, :6]
nms_bboxes = img_meta['box_type_3d'](
nms_bboxes, box_dim=box_dim, with_yaw=with_yaw, origin=(.5, .5, .5))
return nms_bboxes, nms_scores, nms_labels
def compute_centerness(bbox_targets):
x_dims = bbox_targets[..., [0, 1]]
y_dims = bbox_targets[..., [2, 3]]
z_dims = bbox_targets[..., [4, 5]]
centerness_targets = x_dims.min(dim=-1)[0] / x_dims.max(dim=-1)[0] * \
y_dims.min(dim=-1)[0] / y_dims.max(dim=-1)[0] * \
z_dims.min(dim=-1)[0] / z_dims.max(dim=-1)[0]
return torch.sqrt(centerness_targets)
@BBOX_ASSIGNERS.register_module()
class Fcaf3DAssigner(BaseAssigner):
def __init__(self, limit, topk, n_scales):
self.limit = limit
self.topk = topk
self.n_scales = n_scales
def assign(self, points, gt_bboxes, gt_labels):
float_max = 1e8
# expand scales to align with points
expanded_scales = [
points[i].new_tensor(i).expand(len(points[i]))
for i in range(len(points))
]
points = torch.cat(points, dim=0)
scales = torch.cat(expanded_scales, dim=0)
# below is based on FCOSHead._get_target_single
n_points = len(points)
n_boxes = len(gt_bboxes)
volumes = gt_bboxes.volume.to(points.device)
volumes = volumes.expand(n_points, n_boxes).contiguous()
gt_bboxes = torch.cat((gt_bboxes.gravity_center, gt_bboxes.tensor[:, 3:]), dim=1)
gt_bboxes = gt_bboxes.to(points.device).expand(n_points, n_boxes, 7)
expanded_points = points.unsqueeze(1).expand(n_points, n_boxes, 3)
shift = torch.stack((
expanded_points[..., 0] - gt_bboxes[..., 0],
expanded_points[..., 1] - gt_bboxes[..., 1],
expanded_points[..., 2] - gt_bboxes[..., 2]
), dim=-1).permute(1, 0, 2)
shift = rotation_3d_in_axis(shift, -gt_bboxes[0, :, 6], axis=2).permute(1, 0, 2)
centers = gt_bboxes[..., :3] + shift
dx_min = centers[..., 0] - gt_bboxes[..., 0] + gt_bboxes[..., 3] / 2
dx_max = gt_bboxes[..., 0] + gt_bboxes[..., 3] / 2 - centers[..., 0]
dy_min = centers[..., 1] - gt_bboxes[..., 1] + gt_bboxes[..., 4] / 2
dy_max = gt_bboxes[..., 1] + gt_bboxes[..., 4] / 2 - centers[..., 1]
dz_min = centers[..., 2] - gt_bboxes[..., 2] + gt_bboxes[..., 5] / 2
dz_max = gt_bboxes[..., 2] + gt_bboxes[..., 5] / 2 - centers[..., 2]
bbox_targets = torch.stack((dx_min, dx_max, dy_min, dy_max, dz_min, dz_max, gt_bboxes[..., 6]), dim=-1)
# condition1: inside a gt bbox
inside_gt_bbox_mask = bbox_targets[..., :6].min(-1)[0] > 0 # skip angle
# condition2: positive points per scale >= limit
# calculate positive points per scale
n_pos_points_per_scale = []
for i in range(self.n_scales):
n_pos_points_per_scale.append(torch.sum(inside_gt_bbox_mask[scales == i], dim=0))
# find best scale
n_pos_points_per_scale = torch.stack(n_pos_points_per_scale, dim=0)
lower_limit_mask = n_pos_points_per_scale < self.limit
lower_index = torch.argmax(lower_limit_mask.int(), dim=0) - 1
lower_index = torch.where(lower_index < 0, 0, lower_index)
all_upper_limit_mask = torch.all(torch.logical_not(lower_limit_mask), dim=0)
best_scale = torch.where(all_upper_limit_mask, self.n_scales - 1, lower_index)
# keep only points with best scale
best_scale = torch.unsqueeze(best_scale, 0).expand(n_points, n_boxes)
scales = torch.unsqueeze(scales, 1).expand(n_points, n_boxes)
inside_best_scale_mask = best_scale == scales
# condition3: limit topk locations per box by centerness
centerness = compute_centerness(bbox_targets)
centerness = torch.where(inside_gt_bbox_mask, centerness, torch.ones_like(centerness) * -1)
centerness = torch.where(inside_best_scale_mask, centerness, torch.ones_like(centerness) * -1)
top_centerness = torch.topk(centerness, min(self.topk + 1, len(centerness)), dim=0).values[-1]
inside_top_centerness_mask = centerness > top_centerness.unsqueeze(0)
# if there are still more than one objects for a location,
# we choose the one with minimal area
volumes = torch.where(inside_gt_bbox_mask, volumes, torch.ones_like(volumes) * float_max)
volumes = torch.where(inside_best_scale_mask, volumes, torch.ones_like(volumes) * float_max)
volumes = torch.where(inside_top_centerness_mask, volumes, torch.ones_like(volumes) * float_max)
min_area, min_area_inds = volumes.min(dim=1)
labels = gt_labels[min_area_inds]
labels = torch.where(min_area == float_max, -1, labels)
bbox_targets = bbox_targets[range(n_points), min_area_inds]
centerness_targets = compute_centerness(bbox_targets)
return centerness_targets, gt_bboxes[range(n_points), min_area_inds], labels