Skip to content

Commit

Permalink
[Refactor] update data flow and ut (open-mmlab#1776)
Browse files Browse the repository at this point in the history
* update data flow and ut

* update ut

* update code

* fix mapping bug

* fix comments
  • Loading branch information
ZCMax authored Aug 31, 2022
1 parent c2c5abd commit c2d958a
Show file tree
Hide file tree
Showing 62 changed files with 819 additions and 712 deletions.
4 changes: 2 additions & 2 deletions configs/_base_/datasets/kitti-mono3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
dict(
type='Pack3DDetInputs',
keys=[
'img', 'gt_bboxes', 'gt_labels', 'gt_bboxes_3d', 'gt_labels_3d',
'centers_2d', 'depths'
'img', 'gt_bboxes', 'gt_bboxes_labels', 'gt_bboxes_3d',
'gt_labels_3d', 'centers_2d', 'depths'
]),
]
test_pipeline = [
Expand Down
4 changes: 2 additions & 2 deletions configs/_base_/datasets/nus-mono3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
dict(
type='Pack3DDetInputs',
keys=[
'img', 'gt_bboxes', 'gt_labels', 'attr_labels', 'gt_bboxes_3d',
'gt_labels_3d', 'centers_2d', 'depths'
'img', 'gt_bboxes', 'gt_bboxes_labels', 'attr_labels',
'gt_bboxes_3d', 'gt_labels_3d', 'centers_2d', 'depths'
]),
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
dict(
type='Pack3DDetInputs',
keys=[
'img', 'gt_bboxes', 'gt_labels', 'attr_labels', 'gt_bboxes_3d',
'gt_labels_3d', 'centers_2d', 'depths'
'img', 'gt_bboxes', 'gt_bboxes_labels', 'attr_labels',
'gt_bboxes_3d', 'gt_labels_3d', 'centers_2d', 'depths'
]),
]
test_pipeline = [
Expand Down
4 changes: 2 additions & 2 deletions configs/pgd/pgd_r101-caffe_fpn_head-gn_16xb2-1x_nus-mono3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
dict(
type='Collect3D',
keys=[
'img', 'gt_bboxes', 'gt_labels', 'attr_labels', 'gt_bboxes_3d',
'gt_labels_3d', 'centers2d', 'depths'
'img', 'gt_bboxes', 'gt_bboxes_labels', 'attr_labels',
'gt_bboxes_3d', 'gt_labels_3d', 'centers2d', 'depths'
]),
]
test_pipeline = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@
dict(
type='Pack3DDetInputs',
keys=[
'img', 'gt_bboxes', 'gt_labels', 'gt_bboxes_3d', 'gt_labels_3d',
'centers_2d', 'depths'
'img', 'gt_bboxes', 'gt_bboxes_labels', 'gt_bboxes_3d',
'gt_labels_3d', 'centers_2d', 'depths'
]),
]
test_pipeline = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
dict(
type='Pack3DDetInputs',
keys=[
'img', 'gt_bboxes', 'gt_labels', 'gt_bboxes_3d', 'gt_labels_3d',
'centers_2d', 'depths'
'img', 'gt_bboxes', 'gt_bboxes_labels', 'gt_bboxes_3d',
'gt_labels_3d', 'centers_2d', 'depths'
]),
]
test_pipeline = [
Expand Down
4 changes: 2 additions & 2 deletions mmdet3d/apis/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def inference_multi_modality_detector(model: nn.Module,
results = model.test_step(data)

for index in range(len(data)):
meta_info = data[index]['data_sample'].metainfo
meta_info = data[index]['data_samples'].metainfo
results[index].set_metainfo(meta_info)

if not is_batch:
Expand Down Expand Up @@ -320,7 +320,7 @@ def inference_mono_3d_detector(model: nn.Module,
results = model.test_step(data)

for index in range(len(data)):
meta_info = data[index]['data_sample'].metainfo
meta_info = data[index]['data_samples'].metainfo
results[index].set_metainfo(meta_info)

if not is_batch:
Expand Down
14 changes: 10 additions & 4 deletions mmdet3d/datasets/det3d_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,13 @@ def parse_ann_info(self, info: dict) -> Optional[dict]:
# in `transforms`
name_mapping = {
'bbox_label_3d': 'gt_labels_3d',
'bbox_label': 'gt_bboxes_labels',
'bbox': 'gt_bboxes',
'bbox_3d': 'gt_bboxes_3d',
'depth': 'depths',
'center_2d': 'centers_2d',
'attr_label': 'attr_labels'
}

instances = info['instances']
# empty gt
if len(instances) == 0:
Expand All @@ -203,13 +204,18 @@ def parse_ann_info(self, info: dict) -> Optional[dict]:
for ann_name in keys:
temp_anns = [item[ann_name] for item in instances]
# map the original dataset label to training label
if 'label' in ann_name:
if 'label' in ann_name and ann_name != 'attr_label':
temp_anns = [
self.label_mapping[item] for item in temp_anns
]
if ann_name in name_mapping:
ann_name = name_mapping[ann_name]
temp_anns = np.array(temp_anns)

if 'label' in ann_name:
temp_anns = np.array(temp_anns).astype(np.int64)
else:
temp_anns = np.array(temp_anns).astype(np.float32)

ann_info[ann_name] = temp_anns
ann_info['instances'] = info['instances']
return ann_info
Expand Down Expand Up @@ -310,7 +316,7 @@ def prepare_data(self, index):
# after pipeline drop the example with empty annotations
# return None to random another in `__getitem__`
if example is None or len(
example['data_sample'].gt_instances_3d.labels_3d) == 0:
example['data_samples'].gt_instances_3d.labels_3d) == 0:
return None
return example

Expand Down
10 changes: 5 additions & 5 deletions mmdet3d/datasets/transforms/formating.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ def transform(self, results: Union[dict,
- points
- img
- 'data_sample' (obj:`Det3DDataSample`): The annotation info of the
sample.
- 'data_samples' (obj:`Det3DDataSample`): The annotation info of
the sample.
"""
# augtest
if isinstance(results, list):
Expand Down Expand Up @@ -131,8 +131,8 @@ def pack_single_results(self, results):
- points
- img
- 'data_sample' (obj:`Det3DDataSample`): The annotation info of the
sample.
- 'data_samples' (obj:`Det3DDataSample`): The annotation info
of the sample.
"""
# Format 3D data
if 'points' in results:
Expand Down Expand Up @@ -213,7 +213,7 @@ def pack_single_results(self, results):
data_sample.eval_ann_info = None

packed_results = dict()
packed_results['data_sample'] = data_sample
packed_results['data_samples'] = data_sample
packed_results['inputs'] = inputs

return packed_results
Expand Down
23 changes: 2 additions & 21 deletions mmdet3d/datasets/transforms/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,17 +746,8 @@ def _load_bboxes(self, results: dict) -> None:
Returns:
dict: The dict contains loaded bounding box annotations.
"""
gt_bboxes = []
for instance in results['instances']:
gt_bboxes.append(instance['bbox'])
if len(gt_bboxes) == 0:
results['gt_bboxes'] = np.zeros((0, 4), dtype=np.float32)
else:
results['gt_bboxes'] = np.array(
gt_bboxes, dtype=np.float32).reshape((-1, 4))

if 'eval_ann_info' in results:
results['eval_ann_info']['gt_bboxes'] = results['gt_bboxes']
results['gt_bboxes'] = results['ann_info']['gt_bboxes']

def _load_labels(self, results: dict) -> None:
"""Private function to load label annotations.
Expand All @@ -767,17 +758,7 @@ def _load_labels(self, results: dict) -> None:
Returns:
dict: The dict contains loaded label annotations.
"""
gt_bboxes_labels = []
for instance in results['instances']:
gt_bboxes_labels.append(instance['bbox_label'])
if len(gt_bboxes_labels) == 0:
results['gt_bboxes_labels'] = np.zeros((0, ), dtype=np.int64)
else:
results['gt_bboxes_labels'] = np.array(
gt_bboxes_labels, dtype=np.int64)
if 'eval_ann_info' in results:
results['eval_ann_info']['gt_bboxes_labels'] = results[
'gt_bboxes_labels']
results['gt_bboxes_labels'] = results['ann_info']['gt_bboxes_labels']

def transform(self, results: dict) -> dict:
"""Function to load multiple types annotations.
Expand Down
36 changes: 14 additions & 22 deletions mmdet3d/evaluation/metrics/indoor_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,28 @@ def __init__(self,
prefix=prefix, collect_device=collect_device)
self.iou_thr = iou_thr

def process(self, data_batch: Sequence[dict],
predictions: Sequence[dict]) -> None:
def process(self, data_batch: dict, data_samples: Sequence[dict]) -> None:
"""Process one batch of data samples and predictions.
The processed results should be stored in ``self.results``,
which will be used to compute the metrics when all batches
have been processed.
Args:
data_batch (Sequence[dict]): A batch of data
from the dataloader.
predictions (Sequence[dict]): A batch of outputs from
data_batch (dict): A batch of data from the dataloader.
data_samples (Sequence[dict]): A batch of outputs from
the model.
"""
batch_eval_anns = [
item['data_sample']['eval_ann_info'] for item in data_batch
]
for eval_ann, pred_dict in zip(batch_eval_anns, predictions):
pred_3d = pred_dict['pred_instances_3d']
for data_sample in data_samples:
pred_3d = data_sample['pred_instances_3d']
eval_ann_info = data_sample['eval_ann_info']
cpu_pred_3d = dict()
for k, v in pred_3d.items():
if hasattr(v, 'to'):
cpu_pred_3d[k] = v.to('cpu')
else:
cpu_pred_3d[k] = v
self.results.append((eval_ann, cpu_pred_3d))
self.results.append((eval_ann_info, cpu_pred_3d))

def compute_metrics(self, results: list) -> Dict[str, float]:
"""Compute the metrics from processed results.
Expand Down Expand Up @@ -121,28 +117,24 @@ def __init__(self,
prefix=prefix, collect_device=collect_device)
self.iou_thr = iou_thr

def process(self, data_batch: Sequence[dict],
predictions: Sequence[dict]) -> None:
def process(self, data_batch: dict, data_samples: Sequence[dict]) -> None:
"""Process one batch of data samples and predictions.
The processed results should be stored in ``self.results``,
which will be used to compute the metrics when all batches
have been processed.
Args:
data_batch (Sequence[dict]): A batch of data
from the dataloader.
data_batch (dict): A batch of data from the dataloader.
predictions (Sequence[dict]): A batch of outputs from
the model.
"""
batch_eval_anns = [
item['data_sample']['eval_ann_info'] for item in data_batch
]
for eval_ann, pred_dict in zip(batch_eval_anns, predictions):
pred = pred_dict['pred_instances']
for data_sample in data_samples:
pred = data_sample['pred_instances']
eval_ann_info = data_sample['eval_ann_info']
ann = dict(
labels=eval_ann['gt_bboxes_labels'],
bboxes=eval_ann['gt_bboxes'])
labels=eval_ann_info['gt_bboxes_labels'],
bboxes=eval_ann_info['gt_bboxes'])

pred_bboxes = pred['bboxes'].cpu().numpy()
pred_scores = pred['scores'].cpu().numpy()
Expand Down
18 changes: 7 additions & 11 deletions mmdet3d/evaluation/metrics/instance_seg_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,28 @@ def __init__(self,
super(InstanceSegMetric, self).__init__(
prefix=prefix, collect_device=collect_device)

def process(self, data_batch: Sequence[dict],
predictions: Sequence[dict]) -> None:
def process(self, data_batch: dict, data_samples: Sequence[dict]) -> None:
"""Process one batch of data samples and predictions.
The processed results should be stored in ``self.results``,
which will be used to compute the metrics when all batches
have been processed.
Args:
data_batch (Sequence[dict]): A batch of data
from the dataloader.
predictions (Sequence[dict]): A batch of outputs from
data_batch (dict): A batch of data from the dataloader.
data_samples (Sequence[dict]): A batch of outputs from
the model.
"""
batch_eval_anns = [
item['data_sample']['eval_ann_info'] for item in data_batch
]
for eval_ann, pred_dict in zip(batch_eval_anns, predictions):
pred_3d = pred_dict['pred_pts_seg']
for data_sample in data_samples:
pred_3d = data_sample['pred_pts_seg']
eval_ann_info = data_sample['eval_ann_info']
cpu_pred_3d = dict()
for k, v in pred_3d.items():
if hasattr(v, 'to'):
cpu_pred_3d[k] = v.to('cpu')
else:
cpu_pred_3d[k] = v
self.results.append((eval_ann, cpu_pred_3d))
self.results.append((eval_ann_info, cpu_pred_3d))

def compute_metrics(self, results: list) -> Dict[str, float]:
"""Compute the metrics from processed results.
Expand Down
27 changes: 14 additions & 13 deletions mmdet3d/evaluation/metrics/kitti_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,29 +137,30 @@ def convert_annos_to_kitti_annos(
data_annos[i]['kitti_annos'] = kitti_annos
return data_annos

def process(self, data_batch: Sequence[dict],
predictions: Sequence[dict]) -> None:
def process(self, data_batch: dict, data_samples: Sequence[dict]) -> None:
"""Process one batch of data samples and predictions.
The processed results should be stored in ``self.results``,
which will be used to compute the metrics when all batches
have been processed.
Args:
data_batch (Sequence[dict]): A batch of data
from the dataloader.
predictions (Sequence[dict]): A batch of outputs from
data_batch (dict): A batch of data from the dataloader.
data_samples (Sequence[dict]): A batch of outputs from
the model.
"""
assert len(data_batch) == len(predictions)
for data, pred in zip(data_batch, predictions):

for data_sample in data_samples:
result = dict()
for pred_result in pred:
for attr_name in pred[pred_result]:
pred[pred_result][attr_name] = pred[pred_result][
attr_name].to(self.collect_device)
result[pred_result] = pred[pred_result]
sample_idx = data['data_sample']['sample_idx']
pred_3d = data_sample['pred_instances_3d']
pred_2d = data_sample['pred_instances']
for attr_name in pred_3d:
pred_3d[attr_name] = pred_3d[attr_name].to('cpu')
result['pred_instances_3d'] = pred_3d
for attr_name in pred_2d:
pred_2d[attr_name] = pred_2d[attr_name].to('cpu')
result['pred_instances'] = pred_2d
sample_idx = data_sample['sample_idx']
result['sample_idx'] = sample_idx
self.results.append(result)

Expand Down
Loading

0 comments on commit c2d958a

Please sign in to comment.