part_aggregation_roi_head.py 16.7 KB
Newer Older
dingchang's avatar
dingchang committed
1
# Copyright (c) OpenMMLab. All rights reserved.
2
from typing import Dict, List, Tuple
3

4
from mmdet.models.task_modules import AssignResult, SamplingResult
5
from mmengine import ConfigDict
6
from torch import Tensor
zhangwenwei's avatar
zhangwenwei committed
7
from torch.nn import functional as F
wuyuefeng's avatar
wuyuefeng committed
8

9
from mmdet3d.registry import MODELS
zhangshilong's avatar
zhangshilong committed
10
11
12
from mmdet3d.structures import bbox3d2roi
from mmdet3d.utils import InstanceList
from ...structures.det3d_data_sample import SampleList
wuyuefeng's avatar
wuyuefeng committed
13
14
15
from .base_3droi_head import Base3DRoIHead


16
@MODELS.register_module()
wuyuefeng's avatar
wuyuefeng committed
17
class PartAggregationROIHead(Base3DRoIHead):
zhangwenwei's avatar
zhangwenwei committed
18
    """Part aggregation roi head for PartA2.
wuyuefeng's avatar
wuyuefeng committed
19
20
21
22
23

    Args:
        semantic_head (ConfigDict): Config of semantic head.
        num_classes (int): The number of classes.
        seg_roi_extractor (ConfigDict): Config of seg_roi_extractor.
24
        bbox_roi_extractor (ConfigDict): Config of part_roi_extractor.
wuyuefeng's avatar
wuyuefeng committed
25
26
27
28
        bbox_head (ConfigDict): Config of bbox_head.
        train_cfg (ConfigDict): Training config.
        test_cfg (ConfigDict): Testing config.
    """
wuyuefeng's avatar
wuyuefeng committed
29
30

    def __init__(self,
31
32
33
34
35
36
37
38
                 semantic_head: dict,
                 num_classes: int = 3,
                 seg_roi_extractor: dict = None,
                 bbox_head: dict = None,
                 bbox_roi_extractor: dict = None,
                 train_cfg: dict = None,
                 test_cfg: dict = None,
                 init_cfg: dict = None) -> None:
wuyuefeng's avatar
wuyuefeng committed
39
        super(PartAggregationROIHead, self).__init__(
40
            bbox_head=bbox_head,
41
            bbox_roi_extractor=bbox_roi_extractor,
42
43
44
            train_cfg=train_cfg,
            test_cfg=test_cfg,
            init_cfg=init_cfg)
wuyuefeng's avatar
wuyuefeng committed
45
46
        self.num_classes = num_classes
        assert semantic_head is not None
47
48
49
50
51
        self.init_seg_head(seg_roi_extractor, semantic_head)

    def init_seg_head(self, seg_roi_extractor: dict,
                      semantic_head: dict) -> None:
        """Initialize semantic head and seg roi extractor.
wuyuefeng's avatar
wuyuefeng committed
52

53
54
55
56
57
58
59
        Args:
            seg_roi_extractor (dict): Config of seg
                roi extractor.
            semantic_head (dict): Config of semantic head.
        """
        self.semantic_head = MODELS.build(semantic_head)
        self.seg_roi_extractor = MODELS.build(seg_roi_extractor)
wuyuefeng's avatar
wuyuefeng committed
60
61
62

    @property
    def with_semantic(self):
zhangwenwei's avatar
zhangwenwei committed
63
        """bool: whether the head has semantic branch"""
wuyuefeng's avatar
wuyuefeng committed
64
65
66
        return hasattr(self,
                       'semantic_head') and self.semantic_head is not None

67
68
    def _bbox_forward_train(self, feats_dict: Dict, voxels_dict: Dict,
                            sampling_results: List[SamplingResult]) -> Dict:
zhangwenwei's avatar
zhangwenwei committed
69
70
71
        """Forward training function of roi_extractor and bbox_head.

        Args:
72
            feats_dict (dict): Contains features from the first stage.
zhangwenwei's avatar
zhangwenwei committed
73
74
75
76
77
78
79
            voxels_dict (dict): Contains information of voxels.
            sampling_results (:obj:`SamplingResult`): Sampled results used
                for training.

        Returns:
            dict: Forward results including losses and predictions.
        """
wuyuefeng's avatar
wuyuefeng committed
80
        rois = bbox3d2roi([res.bboxes for res in sampling_results])
81
        bbox_results = self._bbox_forward(feats_dict, voxels_dict, rois)
wuyuefeng's avatar
wuyuefeng committed
82
83
84
85
86
87
88
89
90
91

        bbox_targets = self.bbox_head.get_targets(sampling_results,
                                                  self.train_cfg)
        loss_bbox = self.bbox_head.loss(bbox_results['cls_score'],
                                        bbox_results['bbox_pred'], rois,
                                        *bbox_targets)

        bbox_results.update(loss_bbox=loss_bbox)
        return bbox_results

92
    def _assign_and_sample(
93
94
95
            self, rpn_results_list: InstanceList,
            batch_gt_instances_3d: InstanceList,
            batch_gt_instances_ignore: InstanceList) -> List[SamplingResult]:
zhangwenwei's avatar
zhangwenwei committed
96
        """Assign and sample proposals for training.
zhangwenwei's avatar
zhangwenwei committed
97
98

        Args:
99
100
            rpn_results_list (List[:obj:`InstanceData`]): Detection results
                of rpn head.
101
102
103
            batch_gt_instances_3d (list[:obj:`InstanceData`]): Batch of
                gt_instances. It usually includes ``bboxes_3d`` and
                ``labels_3d`` attributes.
104
            batch_gt_instances_ignore (list): Ignore instances of gt bboxes.
zhangwenwei's avatar
zhangwenwei committed
105
106
107
108
109

        Returns:
            list[:obj:`SamplingResult`]: Sampled results of each training
                sample.
        """
wuyuefeng's avatar
wuyuefeng committed
110
111
        sampling_results = []
        # bbox assign
112
113
        for batch_idx in range(len(rpn_results_list)):
            cur_proposal_list = rpn_results_list[batch_idx]
114
            cur_boxes = cur_proposal_list['bboxes_3d']
zhangwenwei's avatar
zhangwenwei committed
115
            cur_labels_3d = cur_proposal_list['labels_3d']
116
            cur_gt_instances_3d = batch_gt_instances_3d[batch_idx]
117
            cur_gt_instances_ignore = batch_gt_instances_ignore[batch_idx]
118
119
            cur_gt_instances_3d.bboxes_3d = cur_gt_instances_3d.\
                bboxes_3d.tensor
120
121
            cur_gt_bboxes = cur_gt_instances_3d.bboxes_3d.to(cur_boxes.device)
            cur_gt_labels = cur_gt_instances_3d.labels_3d
wuyuefeng's avatar
wuyuefeng committed
122
123

            batch_num_gts = 0
124
125
126
127
128
129
130
131
            # 0 is bg
            batch_gt_indis = cur_gt_labels.new_full((len(cur_boxes), ), 0)
            batch_max_overlaps = cur_boxes.tensor.new_zeros(len(cur_boxes))
            # -1 is bg
            batch_gt_labels = cur_gt_labels.new_full((len(cur_boxes), ), -1)

            # each class may have its own assigner
            if isinstance(self.bbox_assigner, list):
wuyuefeng's avatar
wuyuefeng committed
132
133
                for i, assigner in enumerate(self.bbox_assigner):
                    gt_per_cls = (cur_gt_labels == i)
zhangwenwei's avatar
zhangwenwei committed
134
                    pred_per_cls = (cur_labels_3d == i)
wuyuefeng's avatar
wuyuefeng committed
135
                    cur_assign_res = assigner.assign(
136
                        cur_proposal_list[pred_per_cls],
137
138
                        cur_gt_instances_3d[gt_per_cls],
                        cur_gt_instances_ignore)
wuyuefeng's avatar
wuyuefeng committed
139
140
141
                    # gather assign_results in different class into one result
                    batch_num_gts += cur_assign_res.num_gts
                    # gt inds (1-based)
Wenwei Zhang's avatar
Wenwei Zhang committed
142
143
                    gt_inds_arange_pad = gt_per_cls.nonzero(
                        as_tuple=False).view(-1) + 1
wuyuefeng's avatar
wuyuefeng committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
                    # pad 0 for indice unassigned
                    gt_inds_arange_pad = F.pad(
                        gt_inds_arange_pad, (1, 0), mode='constant', value=0)
                    # pad -1 for indice ignore
                    gt_inds_arange_pad = F.pad(
                        gt_inds_arange_pad, (1, 0), mode='constant', value=-1)
                    # convert to 0~gt_num+2 for indices
                    gt_inds_arange_pad += 1
                    # now 0 is bg, >1 is fg in batch_gt_indis
                    batch_gt_indis[pred_per_cls] = gt_inds_arange_pad[
                        cur_assign_res.gt_inds + 1] - 1
                    batch_max_overlaps[
                        pred_per_cls] = cur_assign_res.max_overlaps
                    batch_gt_labels[pred_per_cls] = cur_assign_res.labels

                assign_result = AssignResult(batch_num_gts, batch_gt_indis,
                                             batch_max_overlaps,
                                             batch_gt_labels)
            else:  # for single class
                assign_result = self.bbox_assigner.assign(
164
165
                    cur_proposal_list, cur_gt_instances_3d,
                    cur_gt_instances_ignore)
wuyuefeng's avatar
wuyuefeng committed
166
167
            # sample boxes
            sampling_result = self.bbox_sampler.sample(assign_result,
168
                                                       cur_boxes.tensor,
169
                                                       cur_gt_bboxes,
wuyuefeng's avatar
wuyuefeng committed
170
171
172
173
                                                       cur_gt_labels)
            sampling_results.append(sampling_result)
        return sampling_results

174
175
    def _semantic_forward_train(self, feats_dict: dict, voxel_dict: dict,
                                batch_gt_instances_3d: InstanceList) -> Dict:
zhangwenwei's avatar
zhangwenwei committed
176
        """Train semantic head.
zhangwenwei's avatar
zhangwenwei committed
177
178

        Args:
179
180
181
182
183
            feats_dict (dict): Contains features from the first stage.
            voxel_dict (dict): Contains information of voxels.
            batch_gt_instances_3d (list[:obj:`InstanceData`]): Batch of
                gt_instances. It usually includes ``bboxes_3d`` and
                ``labels_3d`` attributes.
zhangwenwei's avatar
zhangwenwei committed
184
185
186
187

        Returns:
            dict: Segmentation results including losses
        """
188
        semantic_results = self.semantic_head(feats_dict['seg_features'])
wuyuefeng's avatar
wuyuefeng committed
189
        semantic_targets = self.semantic_head.get_targets(
190
            voxel_dict, batch_gt_instances_3d)
wuyuefeng's avatar
wuyuefeng committed
191
192
193
194
        loss_semantic = self.semantic_head.loss(semantic_results,
                                                semantic_targets)
        semantic_results.update(loss_semantic=loss_semantic)
        return semantic_results
195
196
197
198
199
200
201
202
203
204
205
206

    def predict(self,
                feats_dict: Dict,
                rpn_results_list: InstanceList,
                batch_data_samples: SampleList,
                rescale: bool = False,
                **kwargs) -> InstanceList:
        """Perform forward propagation of the roi head and predict detection
        results on the features of the upstream network.

        Args:
            feats_dict (dict): Contains features from the first stage.
207
            rpn_results_list (List[:obj:`InstanceData`]): Detection results
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
                of rpn head.
            batch_data_samples (List[:obj:`Det3DDataSample`]): The Data
                samples. It usually includes information such as
                `gt_instance_3d`, `gt_panoptic_seg_3d` and `gt_sem_seg_3d`.
            rescale (bool): If True, return boxes in original image space.
                Defaults to False.

        Returns:
            list[:obj:`InstanceData`]: Detection results of each sample
            after the post process.
            Each item usually contains following keys.

            - scores_3d (Tensor): Classification scores, has a shape
              (num_instances, )
            - labels_3d (Tensor): Labels of bboxes, has a shape
              (num_instances, ).
            - bboxes_3d (BaseInstance3DBoxes): Prediction of bboxes,
              contains a tensor with shape (num_instances, C), where
              C >= 7.
        """
        assert self.with_bbox, 'Bbox head must be implemented in PartA2.'
        assert self.with_semantic, 'Semantic head must be implemented' \
                                   ' in PartA2.'

        batch_input_metas = [
            data_samples.metainfo for data_samples in batch_data_samples
        ]
        voxels_dict = feats_dict.pop('voxels_dict')
        # TODO: Split predict semantic and bbox
        results_list = self.predict_bbox(feats_dict, voxels_dict,
                                         batch_input_metas, rpn_results_list,
                                         self.test_cfg)
        return results_list

    def predict_bbox(self, feats_dict: Dict, voxel_dict: Dict,
                     batch_input_metas: List[dict],
                     rpn_results_list: InstanceList,
                     test_cfg: ConfigDict) -> InstanceList:
        """Perform forward propagation of the bbox head and predict detection
        results on the features of the upstream network.

        Args:
            feats_dict (dict): Contains features from the first stage.
            voxel_dict (dict): Contains information of voxels.
            batch_input_metas (list[dict], Optional): Batch image meta info.
                Defaults to None.
254
            rpn_results_list (List[:obj:`InstanceData`]): Detection results
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
                of rpn head.
            test_cfg (Config): Test config.

        Returns:
            list[:obj:`InstanceData`]: Detection results of each sample
            after the post process.
            Each item usually contains following keys.

            - scores_3d (Tensor): Classification scores, has a shape
              (num_instances, )
            - labels_3d (Tensor): Labels of bboxes, has a shape
              (num_instances, ).
            - bboxes_3d (BaseInstance3DBoxes): Prediction of bboxes,
              contains a tensor with shape (num_instances, C), where
              C >= 7.
        """
        semantic_results = self.semantic_head(feats_dict['seg_features'])
        feats_dict.update(semantic_results)
        rois = bbox3d2roi(
            [res['bboxes_3d'].tensor for res in rpn_results_list])
        labels_3d = [res['labels_3d'] for res in rpn_results_list]
        cls_preds = [res['cls_preds'] for res in rpn_results_list]
        bbox_results = self._bbox_forward(feats_dict, voxel_dict, rois)

        bbox_list = self.bbox_head.get_results(rois, bbox_results['cls_score'],
                                               bbox_results['bbox_pred'],
                                               labels_3d, cls_preds,
                                               batch_input_metas, test_cfg)
        return bbox_list

    def _bbox_forward(self, feats_dict: Dict, voxel_dict: Dict,
                      rois: Tensor) -> Dict:
        """Forward function of roi_extractor and bbox_head used in both
        training and testing.

        Args:
            feats_dict (dict): Contains features from the first stage.
            voxel_dict (dict): Contains information of voxels.
            rois (Tensor): Roi boxes.

        Returns:
            dict: Contains predictions of bbox_head and
                features of roi_extractor.
        """
        pooled_seg_feats = self.seg_roi_extractor(feats_dict['seg_features'],
                                                  voxel_dict['voxel_centers'],
                                                  voxel_dict['coors'][...,
                                                                      0], rois)
        pooled_part_feats = self.bbox_roi_extractor(
            feats_dict['part_feats'], voxel_dict['voxel_centers'],
            voxel_dict['coors'][..., 0], rois)
        cls_score, bbox_pred = self.bbox_head(pooled_seg_feats,
                                              pooled_part_feats)

        bbox_results = dict(
            cls_score=cls_score,
            bbox_pred=bbox_pred,
            pooled_seg_feats=pooled_seg_feats,
            pooled_part_feats=pooled_part_feats)
        return bbox_results

    def loss(self, feats_dict: Dict, rpn_results_list: InstanceList,
             batch_data_samples: SampleList, **kwargs) -> dict:
        """Perform forward propagation and loss calculation of the detection
        roi on the features of the upstream network.

        Args:
            feats_dict (dict): Contains features from the first stage.
323
            rpn_results_list (List[:obj:`InstanceData`]): Detection results
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
                of rpn head.
            batch_data_samples (List[:obj:`Det3DDataSample`]): The Data
                samples. It usually includes information such as
                `gt_instance_3d`, `gt_panoptic_seg_3d` and `gt_sem_seg_3d`.

        Returns:
            dict[str, Tensor]: A dictionary of loss components
        """
        assert len(rpn_results_list) == len(batch_data_samples)
        losses = dict()
        batch_gt_instances_3d = []
        batch_gt_instances_ignore = []
        voxels_dict = feats_dict.pop('voxels_dict')
        for data_sample in batch_data_samples:
            batch_gt_instances_3d.append(data_sample.gt_instances_3d)
            if 'ignored_instances' in data_sample:
                batch_gt_instances_ignore.append(data_sample.ignored_instances)
            else:
                batch_gt_instances_ignore.append(None)
        if self.with_semantic:
            semantic_results = self._semantic_forward_train(
                feats_dict, voxels_dict, batch_gt_instances_3d)
            losses.update(semantic_results.pop('loss_semantic'))

        sample_results = self._assign_and_sample(rpn_results_list,
349
350
                                                 batch_gt_instances_3d,
                                                 batch_gt_instances_ignore)
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
        if self.with_bbox:
            feats_dict.update(semantic_results)
            bbox_results = self._bbox_forward_train(feats_dict, voxels_dict,
                                                    sample_results)
            losses.update(bbox_results['loss_bbox'])

        return losses

    def _forward(self, feats_dict: dict,
                 rpn_results_list: InstanceList) -> Tuple:
        """Network forward process. Usually includes backbone, neck and head
        forward without any post-processing.

        Args:
            feats_dict (dict): Contains features from the first stage.
366
            rpn_results_list (List[:obj:`InstanceData`]): Detection results
367
368
369
370
371
372
373
374
                of rpn head.

        Returns:
            tuple: A tuple of results from roi head.
        """
        voxel_dict = feats_dict.pop('voxel_dict')
        semantic_results = self.semantic_head(feats_dict['seg_features'])
        feats_dict.update(semantic_results)
zhangshilong's avatar
zhangshilong committed
375
        rois = bbox3d2roi([res['bbox_3d'].tensor for res in rpn_results_list])
376
377
378
379
        bbox_results = self._bbox_forward(feats_dict, voxel_dict, rois)
        cls_score = bbox_results['cls_score']
        bbox_pred = bbox_results['bbox_pred']
        return cls_score, bbox_pred