data_pipeline.md 6.02 KB
Newer Older
twang's avatar
twang committed
1
# Tutorial 3: Customize Data Pipelines
zhangwenwei's avatar
zhangwenwei committed
2

zhangwenwei's avatar
Doc  
zhangwenwei committed
3
## Design of Data pipelines
zhangwenwei's avatar
zhangwenwei committed
4
5
6
7

Following typical conventions, we use `Dataset` and `DataLoader` for data loading
with multiple workers. `Dataset` returns a dict of data items corresponding
the arguments of models' forward method.
8
Since the data in object detection may not be the same size (point number, gt bbox size, etc.),
zhangwenwei's avatar
zhangwenwei committed
9
10
11
12
13
14
15
16
17
we introduce a new `DataContainer` type in MMCV to help collect and distribute
data of different size.
See [here](https://github.com/open-mmlab/mmcv/blob/master/mmcv/parallel/data_container.py) for more details.

The data preparation pipeline and the dataset is decomposed. Usually a dataset
defines how to process the annotations and a data pipeline defines all the steps to prepare a data dict.
A pipeline consists of a sequence of operations. Each operation takes a dict as input and also output a dict for the next transform.

We present a classical pipeline in the following figure. The blue blocks are pipeline operations. With the pipeline going on, each operator can add new keys (marked as green) to the result dict or update the existing keys (marked as orange).
18
![](https://github.com/open-mmlab/mmdetection3d/blob/master/resources/data_pipeline.png)
zhangwenwei's avatar
zhangwenwei committed
19
20
21

The operations are categorized into data loading, pre-processing, formatting and test-time augmentation.

wangtai's avatar
wangtai committed
22
23
Here is an pipeline example for PointPillars.

zhangwenwei's avatar
zhangwenwei committed
24
25
```python
train_pipeline = [
wangtai's avatar
wangtai committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    dict(
        type='LoadPointsFromFile',
        load_dim=5,
        use_dim=5,
        file_client_args=file_client_args),
    dict(
        type='LoadPointsFromMultiSweeps',
        sweeps_num=10,
        file_client_args=file_client_args),
    dict(type='LoadAnnotations3D', with_bbox_3d=True, with_label_3d=True),
    dict(
        type='GlobalRotScaleTrans',
        rot_range=[-0.3925, 0.3925],
        scale_ratio_range=[0.95, 1.05],
        translation_std=[0, 0, 0]),
    dict(type='RandomFlip3D', flip_ratio_bev_horizontal=0.5),
    dict(type='PointsRangeFilter', point_cloud_range=point_cloud_range),
    dict(type='ObjectRangeFilter', point_cloud_range=point_cloud_range),
    dict(type='ObjectNameFilter', classes=class_names),
    dict(type='PointShuffle'),
    dict(type='DefaultFormatBundle3D', class_names=class_names),
    dict(type='Collect3D', keys=['points', 'gt_bboxes_3d', 'gt_labels_3d'])
zhangwenwei's avatar
zhangwenwei committed
48
49
]
test_pipeline = [
wangtai's avatar
wangtai committed
50
51
52
53
54
55
56
57
58
    dict(
        type='LoadPointsFromFile',
        load_dim=5,
        use_dim=5,
        file_client_args=file_client_args),
    dict(
        type='LoadPointsFromMultiSweeps',
        sweeps_num=10,
        file_client_args=file_client_args),
zhangwenwei's avatar
zhangwenwei committed
59
60
61
    dict(
        type='MultiScaleFlipAug',
        img_scale=(1333, 800),
wangtai's avatar
wangtai committed
62
        pts_scale_ratio=1.0,
zhangwenwei's avatar
zhangwenwei committed
63
        flip=False,
wangtai's avatar
wangtai committed
64
65
        pcd_horizontal_flip=False,
        pcd_vertical_flip=False,
zhangwenwei's avatar
zhangwenwei committed
66
        transforms=[
wangtai's avatar
wangtai committed
67
68
69
70
71
72
73
74
75
76
77
78
79
            dict(
                type='GlobalRotScaleTrans',
                rot_range=[0, 0],
                scale_ratio_range=[1., 1.],
                translation_std=[0, 0, 0]),
            dict(type='RandomFlip3D'),
            dict(
                type='PointsRangeFilter', point_cloud_range=point_cloud_range),
            dict(
                type='DefaultFormatBundle3D',
                class_names=class_names,
                with_label=False),
            dict(type='Collect3D', keys=['points'])
zhangwenwei's avatar
zhangwenwei committed
80
81
82
83
84
85
86
87
        ])
]
```

For each operation, we list the related dict fields that are added/updated/removed.

### Data loading

wangtai's avatar
wangtai committed
88
89
`LoadPointsFromFile`
- add: points
zhangwenwei's avatar
zhangwenwei committed
90

wangtai's avatar
wangtai committed
91
92
`LoadPointsFromMultiSweeps`
- update: points
zhangwenwei's avatar
zhangwenwei committed
93

94
95
`LoadAnnotations3D`
- add: gt_bboxes_3d, gt_labels_3d, gt_bboxes, gt_labels, pts_instance_mask, pts_semantic_mask, bbox3d_fields, pts_mask_fields, pts_seg_fields
zhangwenwei's avatar
zhangwenwei committed
96
97
98

### Pre-processing

wangtai's avatar
wangtai committed
99
100
101
`GlobalRotScaleTrans`
- add: pcd_trans, pcd_rotation, pcd_scale_factor
- update: points, *bbox3d_fields
zhangwenwei's avatar
zhangwenwei committed
102

wangtai's avatar
wangtai committed
103
104
105
`RandomFlip3D`
- add: flip, pcd_horizontal_flip, pcd_vertical_flip
- update: points, *bbox3d_fields
zhangwenwei's avatar
zhangwenwei committed
106

wangtai's avatar
wangtai committed
107
108
`PointsRangeFilter`
- update: points
zhangwenwei's avatar
zhangwenwei committed
109

wangtai's avatar
wangtai committed
110
111
`ObjectRangeFilter`
- update: gt_bboxes_3d, gt_labels_3d
zhangwenwei's avatar
zhangwenwei committed
112

wangtai's avatar
wangtai committed
113
114
`ObjectNameFilter`
- update: gt_bboxes_3d, gt_labels_3d
zhangwenwei's avatar
zhangwenwei committed
115

wangtai's avatar
wangtai committed
116
117
`PointShuffle`
- update: points
zhangwenwei's avatar
zhangwenwei committed
118

wangtai's avatar
wangtai committed
119
120
`PointsRangeFilter`
- update: points
zhangwenwei's avatar
zhangwenwei committed
121
122
123

### Formatting

wangtai's avatar
wangtai committed
124
`DefaultFormatBundle3D`
125
- update: points, gt_bboxes_3d, gt_labels_3d, gt_bboxes, gt_labels
zhangwenwei's avatar
zhangwenwei committed
126

wangtai's avatar
wangtai committed
127
`Collect3D`
zhangwenwei's avatar
zhangwenwei committed
128
- add: img_meta (the keys of img_meta is specified by `meta_keys`)
zhangwenwei's avatar
zhangwenwei committed
129
130
131
132
- remove: all other keys except for those specified by `keys`

### Test time augmentation

zhangwenwei's avatar
zhangwenwei committed
133
`MultiScaleFlipAug`
wangtai's avatar
wangtai committed
134
- update: scale, pcd_scale_factor, flip, flip_direction, pcd_horizontal_flip, pcd_vertical_flip with list of augmented data with these specific parameters
zhangwenwei's avatar
zhangwenwei committed
135

zhangwenwei's avatar
Doc  
zhangwenwei committed
136
## Extend and use custom pipelines
zhangwenwei's avatar
zhangwenwei committed
137

zhangwenwei's avatar
Doc  
zhangwenwei committed
138
1. Write a new pipeline in any file, e.g., `my_pipeline.py`. It takes a dict as input and return a dict.
zhangwenwei's avatar
zhangwenwei committed
139
140

    ```python
zhangwenwei's avatar
Doc  
zhangwenwei committed
141
    from mmdet.datasets import PIPELINES
zhangwenwei's avatar
zhangwenwei committed
142

zhangwenwei's avatar
Doc  
zhangwenwei committed
143
144
    @PIPELINES.register_module()
    class MyTransform:
zhangwenwei's avatar
zhangwenwei committed
145

zhangwenwei's avatar
Doc  
zhangwenwei committed
146
147
148
        def __call__(self, results):
            results['dummy'] = True
            return results
zhangwenwei's avatar
zhangwenwei committed
149
150
    ```

zhangwenwei's avatar
Doc  
zhangwenwei committed
151
2. Import the new class.
zhangwenwei's avatar
zhangwenwei committed
152
153

    ```python
zhangwenwei's avatar
Doc  
zhangwenwei committed
154
    from .my_pipeline import MyTransform
zhangwenwei's avatar
zhangwenwei committed
155
156
    ```

zhangwenwei's avatar
Doc  
zhangwenwei committed
157
3. Use it in config files.
zhangwenwei's avatar
zhangwenwei committed
158
159

    ```python
zhangwenwei's avatar
Doc  
zhangwenwei committed
160
    train_pipeline = [
wangtai's avatar
wangtai committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
        dict(
            type='LoadPointsFromFile',
            load_dim=5,
            use_dim=5,
            file_client_args=file_client_args),
        dict(
            type='LoadPointsFromMultiSweeps',
            sweeps_num=10,
            file_client_args=file_client_args),
        dict(type='LoadAnnotations3D', with_bbox_3d=True, with_label_3d=True),
        dict(
            type='GlobalRotScaleTrans',
            rot_range=[-0.3925, 0.3925],
            scale_ratio_range=[0.95, 1.05],
            translation_std=[0, 0, 0]),
        dict(type='RandomFlip3D', flip_ratio_bev_horizontal=0.5),
        dict(type='PointsRangeFilter', point_cloud_range=point_cloud_range),
        dict(type='ObjectRangeFilter', point_cloud_range=point_cloud_range),
        dict(type='ObjectNameFilter', classes=class_names),
zhangwenwei's avatar
Doc  
zhangwenwei committed
180
        dict(type='MyTransform'),
wangtai's avatar
wangtai committed
181
182
183
        dict(type='PointShuffle'),
        dict(type='DefaultFormatBundle3D', class_names=class_names),
        dict(type='Collect3D', keys=['points', 'gt_bboxes_3d', 'gt_labels_3d'])
zhangwenwei's avatar
Doc  
zhangwenwei committed
184
    ]
zhangwenwei's avatar
zhangwenwei committed
185
    ```