forked from open-mmlab/OpenPCDet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
center_head.py
538 lines (447 loc) · 25.3 KB
/
center_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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
import copy
import numpy as np
import torch
import torch.nn as nn
from torch.nn.init import kaiming_normal_
from ..model_utils import model_nms_utils
from ..model_utils import centernet_utils
from ...utils import loss_utils
from typing import Dict, List
from functools import partial
from pcdet.ops.norm_funcs.res_aware_bnorm import ResAwareBatchNorm2d
from pcdet.ops.norm_funcs.fn_instance_norm import FnInstanceNorm
class SeparateHead(nn.Module):
def __init__(self, input_channels, sep_head_dict, init_bias=-2.19, use_bias=False, norm_func=None, enable_normalization=True):
super().__init__()
self.sep_head_dict = sep_head_dict
self.conv_names = tuple(sep_head_dict.keys())
for cur_name in self.sep_head_dict:
output_channels = self.sep_head_dict[cur_name]['out_channels']
num_conv = self.sep_head_dict[cur_name]['num_conv']
fc_list = []
for k in range(num_conv - 1):
inner_fc_list = [nn.Conv2d(input_channels, input_channels, kernel_size=3, stride=1, padding=1, bias=use_bias)]
if enable_normalization: #TODO I havent made an exception for hm, but its ok
inner_fc_list.append(nn.BatchNorm2d(input_channels) if norm_func is None else norm_func(input_channels))
inner_fc_list.append(nn.ReLU())
fc_list.append(nn.Sequential(*inner_fc_list))
fc_list.append(nn.Conv2d(input_channels, output_channels, kernel_size=3, stride=1, padding=1, bias=True))
fc = nn.Sequential(*fc_list)
if 'hm' in cur_name:
fc[-1].bias.data.fill_(init_bias)
else:
for m in fc.modules():
if isinstance(m, nn.Conv2d):
kaiming_normal_(m.weight.data)
if hasattr(m, "bias") and m.bias is not None:
nn.init.constant_(m.bias, 0)
self.__setattr__(cur_name, fc)
def forward_hm(self, x):
hm_out = self.__getattr__('hm')(x)
return {'hm': hm_out if self.training else hm_out.sigmoid()}
def forward_attr(self, x):
ret_dict = {}
for cur_name in self.sep_head_dict:
if cur_name != 'hm':
ret_dict[cur_name] = self.__getattr__(cur_name)(x)
return ret_dict
def forward(self, x : torch.Tensor) -> Dict[str, torch.Tensor]:
ret_dict = {}
for cur_name in self.conv_names:
ret_dict[cur_name] = self.__getattr__(cur_name)(x)
ret_dict['hm'] = ret_dict['hm'].sigmoid()
return ret_dict
class CenterHead(nn.Module):
def __init__(self, model_cfg, input_channels, num_class, class_names, grid_size, point_cloud_range, voxel_size,
predict_boxes_when_training=True):
super().__init__()
self.model_cfg = model_cfg
self.num_class = num_class
#self.grid_size = grid_size # not being used
self.point_cloud_range = point_cloud_range
self.voxel_size = voxel_size
self.initial_voxel_size = voxel_size.copy()
self.feature_map_stride = self.model_cfg.TARGET_ASSIGNER_CONFIG.get('FEATURE_MAP_STRIDE', None)
self.class_names = class_names
self.class_names_each_head = []
self.class_id_mapping_each_head = []
for cur_class_names in self.model_cfg.CLASS_NAMES_EACH_HEAD:
self.class_names_each_head.append([x for x in cur_class_names if x in class_names])
cur_class_id_mapping = torch.from_numpy(np.array(
[self.class_names.index(x) for x in cur_class_names if x in class_names]
)).cuda()
self.class_id_mapping_each_head.append(cur_class_id_mapping)
total_classes = sum([len(x) for x in self.class_names_each_head])
assert total_classes == len(self.class_names), f'class_names_each_head={self.class_names_each_head}'
self.cls_id_to_det_head_idx_map = torch.zeros((total_classes,), dtype=torch.int)
self.num_det_heads = len(self.class_id_mapping_each_head)
for i, cls_ids in enumerate(self.class_id_mapping_each_head):
for cls_id in cls_ids:
self.cls_id_to_det_head_idx_map[cls_id] = i
res_divs = model_cfg.get('RESOLUTION_DIV', [1.0])
norm_method = self.model_cfg.get('NORM_METHOD', 'Batch')
if norm_method == 'Batch':
norm_func = partial(nn.BatchNorm2d, eps=self.model_cfg.get('BN_EPS', 1e-5), momentum=self.model_cfg.get('BN_MOM', 0.1))
elif norm_method == 'ResAwareBatch':
norm_func = partial(ResAwareBatchNorm2d, num_resolutions=len(res_divs), \
eps=1e-3, momentum=0.01)
elif norm_method == 'Instance':
norm_func = partial(FnInstanceNorm, eps=self.model_cfg.get('BN_EPS', 1e-5), momentum=self.model_cfg.get('BN_MOM', 0.1))
self.shared_conv = nn.Sequential(
nn.Conv2d(
input_channels, self.model_cfg.SHARED_CONV_CHANNEL, 3, stride=1, padding=1,
bias=self.model_cfg.get('USE_BIAS_BEFORE_NORM', False)
),
norm_func(self.model_cfg.SHARED_CONV_CHANNEL),
nn.ReLU(),
)
self.heads_list = nn.ModuleList()
self.separate_head_cfg = self.model_cfg.SEPARATE_HEAD_CFG
for idx, cur_class_names in enumerate(self.class_names_each_head):
cur_head_dict = copy.deepcopy(self.separate_head_cfg.HEAD_DICT)
cur_head_dict['hm'] = dict(out_channels=len(cur_class_names), num_conv=self.model_cfg.NUM_HM_CONV)
self.heads_list.append(
SeparateHead(
input_channels=self.model_cfg.SHARED_CONV_CHANNEL,
sep_head_dict=cur_head_dict,
init_bias=-2.19,
use_bias=self.model_cfg.get('USE_BIAS_BEFORE_NORM', False),
norm_func=norm_func,
enable_normalization=self.model_cfg.get('ENABLE_NORM_IN_ATTR_LAYERS', True)
)
)
self.predict_boxes_when_training = predict_boxes_when_training
self.forward_ret_dict = {}
self.build_losses()
self.det_dict_copy = {
"pred_boxes": torch.zeros([0, 9], dtype=torch.float, device='cuda'),
"pred_scores": torch.zeros([0], dtype=torch.float,device='cuda'),
"pred_labels": torch.zeros([0], dtype=torch.int, device='cuda'),
}
def build_losses(self):
self.add_module('hm_loss_func', loss_utils.FocalLossCenterNet())
self.add_module('reg_loss_func', loss_utils.RegLossCenterNet())
def assign_target_of_single_head(
self, num_classes, gt_boxes, feature_map_size, feature_map_stride, num_max_objs=500,
gaussian_overlap=0.1, min_radius=2
):
"""
Args:
gt_boxes: (N, 8)
feature_map_size: (2), [x, y]
Returns:
"""
heatmap = gt_boxes.new_zeros(num_classes, feature_map_size[1], feature_map_size[0])
ret_boxes = gt_boxes.new_zeros((num_max_objs, gt_boxes.shape[-1] - 1 + 1))
inds = gt_boxes.new_zeros(num_max_objs).long()
mask = gt_boxes.new_zeros(num_max_objs).long()
ret_boxes_src = gt_boxes.new_zeros(num_max_objs, gt_boxes.shape[-1])
ret_boxes_src[:gt_boxes.shape[0]] = gt_boxes
x, y, z = gt_boxes[:, 0], gt_boxes[:, 1], gt_boxes[:, 2]
coord_x = (x - self.point_cloud_range[0]) / self.voxel_size[0] / feature_map_stride
coord_y = (y - self.point_cloud_range[1]) / self.voxel_size[1] / feature_map_stride
coord_x = torch.clamp(coord_x, min=0, max=feature_map_size[0] - 0.5) # bugfixed: 1e-6 does not work for center.int()
coord_y = torch.clamp(coord_y, min=0, max=feature_map_size[1] - 0.5) #
center = torch.cat((coord_x[:, None], coord_y[:, None]), dim=-1)
center_int = center.int()
center_int_float = center_int.float()
dx, dy, dz = gt_boxes[:, 3], gt_boxes[:, 4], gt_boxes[:, 5]
dx = dx / self.voxel_size[0] / feature_map_stride
dy = dy / self.voxel_size[1] / feature_map_stride
radius = centernet_utils.gaussian_radius(dx, dy, min_overlap=gaussian_overlap)
radius = torch.clamp_min(radius.int(), min=min_radius)
for k in range(min(num_max_objs, gt_boxes.shape[0])):
if dx[k] <= 0 or dy[k] <= 0:
continue
if not (0 <= center_int[k][0] <= feature_map_size[0] and 0 <= center_int[k][1] <= feature_map_size[1]):
continue
cur_class_id = (gt_boxes[k, -1] - 1).long()
centernet_utils.draw_gaussian_to_heatmap(heatmap[cur_class_id], center[k], radius[k].item())
inds[k] = center_int[k, 1] * feature_map_size[0] + center_int[k, 0]
mask[k] = 1
ret_boxes[k, 0:2] = center[k] - center_int_float[k].float()
ret_boxes[k, 2] = z[k]
ret_boxes[k, 3:6] = gt_boxes[k, 3:6].log()
ret_boxes[k, 6] = torch.cos(gt_boxes[k, 6])
ret_boxes[k, 7] = torch.sin(gt_boxes[k, 6])
if gt_boxes.shape[1] > 8:
ret_boxes[k, 8:] = gt_boxes[k, 7:-1]
return heatmap, ret_boxes, inds, mask, ret_boxes_src
def assign_targets(self, gt_boxes, feature_map_size=None, **kwargs):
"""
Args:
gt_boxes: (B, M, 8)
range_image_polar: (B, 3, H, W)
feature_map_size: (2) [H, W]
spatial_cartesian: (B, 4, H, W)
Returns:
"""
feature_map_size = feature_map_size[::-1] # [H, W] ==> [x, y]
target_assigner_cfg = self.model_cfg.TARGET_ASSIGNER_CONFIG
# feature_map_size = self.grid_size[:2] // target_assigner_cfg.FEATURE_MAP_STRIDE
batch_size = gt_boxes.shape[0]
ret_dict = {
'heatmaps': [],
'target_boxes': [],
'inds': [],
'masks': [],
'heatmap_masks': [],
'target_boxes_src': [],
}
all_names = np.array(['bg', *self.class_names])
for idx, cur_class_names in enumerate(self.class_names_each_head):
heatmap_list, target_boxes_list, inds_list, masks_list, target_boxes_src_list = [], [], [], [], []
for bs_idx in range(batch_size):
cur_gt_boxes = gt_boxes[bs_idx]
gt_class_names = all_names[cur_gt_boxes[:, -1].cpu().long().numpy()]
gt_boxes_single_head = []
for idx, name in enumerate(gt_class_names):
if name not in cur_class_names:
continue
temp_box = cur_gt_boxes[idx]
temp_box[-1] = cur_class_names.index(name) + 1
gt_boxes_single_head.append(temp_box[None, :])
if len(gt_boxes_single_head) == 0:
gt_boxes_single_head = cur_gt_boxes[:0, :]
else:
gt_boxes_single_head = torch.cat(gt_boxes_single_head, dim=0)
heatmap, ret_boxes, inds, mask, ret_boxes_src = self.assign_target_of_single_head(
num_classes=len(cur_class_names), gt_boxes=gt_boxes_single_head.cpu(),
feature_map_size=feature_map_size, feature_map_stride=target_assigner_cfg.FEATURE_MAP_STRIDE,
num_max_objs=target_assigner_cfg.NUM_MAX_OBJS,
gaussian_overlap=target_assigner_cfg.GAUSSIAN_OVERLAP,
min_radius=target_assigner_cfg.MIN_RADIUS,
)
heatmap_list.append(heatmap.to(gt_boxes_single_head.device))
target_boxes_list.append(ret_boxes.to(gt_boxes_single_head.device))
inds_list.append(inds.to(gt_boxes_single_head.device))
masks_list.append(mask.to(gt_boxes_single_head.device))
target_boxes_src_list.append(ret_boxes_src.to(gt_boxes_single_head.device))
ret_dict['heatmaps'].append(torch.stack(heatmap_list, dim=0))
ret_dict['target_boxes'].append(torch.stack(target_boxes_list, dim=0))
ret_dict['inds'].append(torch.stack(inds_list, dim=0))
ret_dict['masks'].append(torch.stack(masks_list, dim=0))
ret_dict['target_boxes_src'].append(torch.stack(target_boxes_src_list, dim=0))
return ret_dict
def sigmoid(self, x):
y = torch.clamp(x.sigmoid(), min=1e-4, max=1 - 1e-4)
return y
def get_loss(self):
pred_dicts = self.forward_ret_dict['pred_dicts']
target_dicts = self.forward_ret_dict['target_dicts']
tb_dict = {}
loss = 0
for idx, pred_dict in enumerate(pred_dicts):
pred_dict['hm'] = self.sigmoid(pred_dict['hm'])
hm_loss = self.hm_loss_func(pred_dict['hm'], target_dicts['heatmaps'][idx])
hm_loss *= self.model_cfg.LOSS_CONFIG.LOSS_WEIGHTS['cls_weight']
target_boxes = target_dicts['target_boxes'][idx]
pred_boxes = torch.cat([pred_dict[head_name] for head_name in self.separate_head_cfg.HEAD_ORDER], dim=1)
reg_loss = self.reg_loss_func(
pred_boxes, target_dicts['masks'][idx], target_dicts['inds'][idx], target_boxes
)
loc_loss = (reg_loss * reg_loss.new_tensor(self.model_cfg.LOSS_CONFIG.LOSS_WEIGHTS['code_weights'])).sum()
loc_loss = loc_loss * self.model_cfg.LOSS_CONFIG.LOSS_WEIGHTS['loc_weight']
loss += hm_loss + loc_loss
tb_dict['hm_loss_head_%d' % idx] = hm_loss.item()
tb_dict['loc_loss_head_%d' % idx] = loc_loss.item()
if 'iou' in pred_dict or self.model_cfg.get('IOU_REG_LOSS', False):
batch_box_preds = centernet_utils.decode_bbox_from_pred_dicts(
pred_dict=pred_dict,
point_cloud_range=self.point_cloud_range, voxel_size=self.voxel_size,
feature_map_stride=self.feature_map_stride
) # (B, H, W, 7 or 9)
if 'iou' in pred_dict:
batch_box_preds_for_iou = batch_box_preds.permute(0, 3, 1, 2) # (B, 7 or 9, H, W)
iou_loss = loss_utils.calculate_iou_loss_centerhead(
iou_preds=pred_dict['iou'],
batch_box_preds=batch_box_preds_for_iou.clone().detach(),
mask=target_dicts['masks'][idx],
ind=target_dicts['inds'][idx], gt_boxes=target_dicts['target_boxes_src'][idx]
)
loss += iou_loss
tb_dict['iou_loss_head_%d' % idx] = iou_loss.item()
if self.model_cfg.get('IOU_REG_LOSS', False):
iou_reg_loss = loss_utils.calculate_iou_reg_loss_centerhead(
batch_box_preds=batch_box_preds_for_iou,
mask=target_dicts['masks'][idx],
ind=target_dicts['inds'][idx], gt_boxes=target_dicts['target_boxes_src'][idx]
)
if target_dicts['masks'][idx].sum().item() != 0:
iou_reg_loss = iou_reg_loss * self.model_cfg.LOSS_CONFIG.LOSS_WEIGHTS['loc_weight']
loss += iou_reg_loss
tb_dict['iou_reg_loss_head_%d' % idx] = iou_reg_loss.item()
else:
loss += (batch_box_preds_for_iou * 0.).sum()
tb_dict['iou_reg_loss_head_%d' % idx] = (batch_box_preds_for_iou * 0.).sum()
tb_dict['rpn_loss'] = loss.item()
return loss, tb_dict
# NEW
def generate_predicted_boxes(self, batch_size, pred_dicts, topk_outputs=None, forecasted_dets=None):
post_process_cfg = self.model_cfg.POST_PROCESSING
post_center_limit_range = torch.tensor(post_process_cfg.POST_CENTER_LIMIT_RANGE).cuda().float()
ret_dict = [{
'pred_boxes': [],
'pred_scores': [],
'pred_labels': [],
} for k in range(batch_size)]
for idx, pred_dict in enumerate(pred_dicts):
batch_hm = pred_dict['hm'].sigmoid()
batch_center = pred_dict['center']
batch_center_z = pred_dict['center_z']
batch_dim = pred_dict['dim'].exp()
batch_rot_cos = pred_dict['rot'][:, 0].unsqueeze(dim=1)
batch_rot_sin = pred_dict['rot'][:, 1].unsqueeze(dim=1)
batch_vel = pred_dict['vel'] if 'vel' in self.separate_head_cfg.HEAD_ORDER else None
batch_iou = (pred_dict['iou'] + 1) * 0.5 if 'iou' in pred_dict else None
topk_outp = None if topk_outputs is None else topk_outputs[idx]
final_pred_dicts = centernet_utils.decode_bbox_from_heatmap(
heatmap=batch_hm, rot_cos=batch_rot_cos, rot_sin=batch_rot_sin,
center=batch_center, center_z=batch_center_z, dim=batch_dim, vel=batch_vel, iou=batch_iou,
point_cloud_range=self.point_cloud_range, voxel_size=self.voxel_size,
feature_map_stride=self.feature_map_stride,
K=post_process_cfg.MAX_OBJ_PER_SAMPLE,
circle_nms=(post_process_cfg.NMS_CONFIG.NMS_TYPE == 'circle_nms'),
score_thresh=post_process_cfg.SCORE_THRESH,
post_center_limit_range=post_center_limit_range,
topk_outp=topk_outp
)
for k, final_dict in enumerate(final_pred_dicts):
final_dict['pred_labels'] = self.class_id_mapping_each_head[idx][final_dict['pred_labels'].long()]
if post_process_cfg.get('USE_IOU_TO_RECTIFY_SCORE', False) and 'pred_iou' in final_dict:
pred_iou = torch.clamp(final_dict['pred_iou'], min=0, max=1.0)
IOU_RECTIFIER = final_dict['pred_scores'].new_tensor(post_process_cfg.IOU_RECTIFIER)
final_dict['pred_scores'] = torch.pow(final_dict['pred_scores'], 1 - IOU_RECTIFIER[final_dict['pred_labels']]) * torch.pow(pred_iou, IOU_RECTIFIER[final_dict['pred_labels']])
if post_process_cfg.NMS_CONFIG.NMS_TYPE not in ['circle_nms', 'multi_class_nms']:
if forecasted_dets is not None:
# get the forecasted_dets that match and cat them for NMS
for j in forecasted_dets[idx].keys():
final_dict[j] = torch.cat((final_dict[j], forecasted_dets[idx][j].cuda()), dim=0)
selected, selected_scores = model_nms_utils.class_agnostic_nms(
box_scores=final_dict['pred_scores'], box_preds=final_dict['pred_boxes'],
nms_config=post_process_cfg.NMS_CONFIG,
score_thresh=None
)
elif post_process_cfg.NMS_CONFIG.NMS_TYPE == 'multi_class_nms':
if forecasted_dets is not None:
# get the forecasted_dets that match and cat them for NMS
for j in forecasted_dets[idx].keys():
final_dict[j] = torch.cat((final_dict[j], forecasted_dets[idx][j].cuda()), dim=0)
selected, selected_scores = model_nms_utils.multi_classes_nms_mmdet(
box_scores=final_dict['pred_scores'], box_preds=final_dict['pred_boxes'],
box_labels=final_dict['pred_labels'], nms_config=post_process_cfg.NMS_CONFIG,
score_thresh=post_process_cfg.NMS_CONFIG.get('SCORE_THRESH', None)
)
final_dict['pred_boxes'] = final_dict['pred_boxes'][selected]
final_dict['pred_scores'] = selected_scores
final_dict['pred_labels'] = final_dict['pred_labels'][selected]
ret_dict[k]['pred_boxes'].append(final_dict['pred_boxes'])
ret_dict[k]['pred_scores'].append(final_dict['pred_scores'])
ret_dict[k]['pred_labels'].append(final_dict['pred_labels'])
for k in range(batch_size):
if not ret_dict[k]['pred_boxes']:
ret_dict[k] = self.get_empty_det_dict()
else:
ret_dict[k]['pred_boxes'] = torch.cat(ret_dict[k]['pred_boxes'], dim=0)
ret_dict[k]['pred_scores'] = torch.cat(ret_dict[k]['pred_scores'], dim=0)
ret_dict[k]['pred_labels'] = torch.cat(ret_dict[k]['pred_labels'], dim=0) + 1
return ret_dict
@staticmethod
def reorder_rois_for_refining(batch_size, pred_dicts):
num_max_rois = max([len(cur_dict['pred_boxes']) for cur_dict in pred_dicts])
num_max_rois = max(1, num_max_rois) # at least one faked rois to avoid error
pred_boxes = pred_dicts[0]['pred_boxes']
rois = pred_boxes.new_zeros((batch_size, num_max_rois, pred_boxes.shape[-1]))
roi_scores = pred_boxes.new_zeros((batch_size, num_max_rois))
roi_labels = pred_boxes.new_zeros((batch_size, num_max_rois)).long()
for bs_idx in range(batch_size):
num_boxes = len(pred_dicts[bs_idx]['pred_boxes'])
rois[bs_idx, :num_boxes, :] = pred_dicts[bs_idx]['pred_boxes']
roi_scores[bs_idx, :num_boxes] = pred_dicts[bs_idx]['pred_scores']
roi_labels[bs_idx, :num_boxes] = pred_dicts[bs_idx]['pred_labels']
return rois, roi_scores, roi_labels
def forward(self, batch_dict):
batch_dict = self.forward_pre(batch_dict)
batch_dict = self.forward_post(batch_dict)
batch_dict = self.forward_assign_targets(batch_dict)
batch_dict = self.forward_topk(batch_dict)
batch_dict = self.forward_genbox(batch_dict)
return batch_dict
def ordered_outp_names(self):
names = ['hm'] + list(self.separate_head_cfg.HEAD_ORDER)
if 'iou' in self.separate_head_cfg.HEAD_DICT:
names += ['iou']
return names
def adjust_voxel_size_wrt_resolution(self, res_div : float):
voxel_sz = torch.tensor([vs*res_div for vs in self.initial_voxel_size[:2]], dtype=torch.float)
voxel_sz = torch.round(voxel_sz, decimals=3)
self.voxel_size[0] = voxel_sz[0].item()
self.voxel_size[1] = voxel_sz[1].item()
# Alternative function for scripting
def forward_up_to_topk(self, spatial_features_2d : torch.Tensor) -> List[torch.Tensor]:
x = self.shared_conv(spatial_features_2d)
pred_dicts = [h.forward(x) for h in self.heads_list]
conv_order = self.ordered_outp_names()
out_tensors_ordered = [pd[conv_name] for pd in pred_dicts for conv_name in conv_order]
return out_tensors_ordered
def convert_out_to_batch_dict(self, out_tensors):
head_order = self.ordered_outp_names()
num_convs_per_head = len(out_tensors) // self.num_det_heads
pred_dicts = []
for i in range(self.num_det_heads):
ot = out_tensors[i*num_convs_per_head:(i+1)*num_convs_per_head]
pred_dicts.append({name : t for name, t in zip(head_order, ot)})
return pred_dicts
def forward_pre(self, batch_dict):
spatial_features_2d = batch_dict['spatial_features_2d']
x = self.shared_conv(spatial_features_2d)
batch_dict['pred_dicts'] = [head.forward_hm(x) for head in self.heads_list]
batch_dict['shared_conv_outp'] = x
return batch_dict
def forward_post(self, batch_dict):
x = batch_dict['shared_conv_outp']
for head, pd in zip(self.heads_list, batch_dict['pred_dicts']):
pd.update(head.forward_attr(x))
return batch_dict
def forward_assign_targets(self, batch_dict, feature_map_size=None):
if self.training:
if feature_map_size is None:
feature_map_size = batch_dict['spatial_features_2d'].size()[2:]
target_dict = self.assign_targets(
batch_dict['gt_boxes'], feature_map_size=feature_map_size,
feature_map_stride=batch_dict.get('spatial_features_2d_strides', None)
)
self.forward_ret_dict['target_dicts'] = target_dict
self.forward_ret_dict['pred_dicts'] = batch_dict['pred_dicts']
return batch_dict
def forward_topk(self, batch_dict):
if not self.training or self.predict_boxes_when_training:
topk_outputs = []
pred_dicts = batch_dict['pred_dicts']
post_process_cfg = self.model_cfg.POST_PROCESSING
for pd in pred_dicts:
scores, inds, class_ids, ys, xs = centernet_utils._topk(pd['hm'],
K=post_process_cfg.MAX_OBJ_PER_SAMPLE)
topk_outputs.append((scores, inds, class_ids, ys, xs))
batch_dict['topk_outputs'] = topk_outputs
return batch_dict
def forward_genbox(self, batch_dict):
if not self.training or self.predict_boxes_when_training:
pred_dicts = batch_dict['pred_dicts']
pred_dicts = self.generate_predicted_boxes( \
batch_dict['batch_size'], pred_dicts, batch_dict.get('topk_outputs', None), \
batch_dict.get('forecasted_dets', None))
if self.predict_boxes_when_training:
rois, roi_scores, roi_labels = self.reorder_rois_for_refining(batch_dict['batch_size'], pred_dicts)
batch_dict['rois'] = rois
batch_dict['roi_scores'] = roi_scores
batch_dict['roi_labels'] = roi_labels
batch_dict['has_class_labels'] = True
else:
batch_dict['final_box_dicts'] = pred_dicts
return batch_dict
def get_empty_det_dict(self):
det_dict = {}
for k,v in self.det_dict_copy.items():
det_dict[k] = v.clone().detach()
return det_dict