data_pipeline.md 5.18 KB
Newer Older
zhangwenwei's avatar
Doc  
zhangwenwei committed
1
# Tutorial 3: Custom 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
8
9
10
11
12
13
14
15
16
17

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.
Since the data in object detection may not be the same size (image size, gt bbox size, etc.),
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).
zhangwenwei's avatar
Doc  
zhangwenwei committed
18
![pipeline figure](../../demo/data_pipeline.png)
zhangwenwei's avatar
zhangwenwei committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

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

Here is an pipeline example for Faster R-CNN.
```python
img_norm_cfg = dict(
    mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
train_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(type='LoadAnnotations', with_bbox=True),
    dict(type='Resize', img_scale=(1333, 800), keep_ratio=True),
    dict(type='RandomFlip', flip_ratio=0.5),
    dict(type='Normalize', **img_norm_cfg),
    dict(type='Pad', size_divisor=32),
    dict(type='DefaultFormatBundle'),
    dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']),
]
test_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(
        type='MultiScaleFlipAug',
        img_scale=(1333, 800),
41
        pts_scale_ratio=1.0,
zhangwenwei's avatar
zhangwenwei committed
42
        flip=False,
43
44
        pcd_horizontal_flip=False,
        pcd_vertical_flip=False,
zhangwenwei's avatar
zhangwenwei committed
45
        transforms=[
46
47
48
49
50
51
52
53
54
55
56
57
58
            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
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
        ])
]
```

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

### Data loading

`LoadImageFromFile`
- add: img, img_shape, ori_shape

`LoadAnnotations`
- add: gt_bboxes, gt_bboxes_ignore, gt_labels, gt_masks, gt_semantic_seg, bbox_fields, mask_fields

`LoadProposals`
- add: proposals

### Pre-processing

`Resize`
- add: scale, scale_idx, pad_shape, scale_factor, keep_ratio
- update: img, img_shape, *bbox_fields, *mask_fields, *seg_fields

`RandomFlip`
- add: flip
- update: img, *bbox_fields, *mask_fields, *seg_fields

`Pad`
- add: pad_fixed_size, pad_size_divisor
- update: img, pad_shape, *mask_fields, *seg_fields

`RandomCrop`
- update: img, pad_shape, gt_bboxes, gt_labels, gt_masks, *bbox_fields

`Normalize`
- add: img_norm_cfg
- update: img

`SegRescale`
- update: gt_semantic_seg

`PhotoMetricDistortion`
- update: img

`Expand`
- update: img, gt_bboxes

`MinIoURandomCrop`
- update: img, gt_bboxes, gt_labels

`Corrupt`
- update: img

### Formatting

`ToTensor`
- update: specified by `keys`.

`ImageToTensor`
- update: specified by `keys`.

`Transpose`
- update: specified by `keys`.

`ToDataContainer`
- update: specified by `fields`.

`DefaultFormatBundle`
- update: img, proposals, gt_bboxes, gt_bboxes_ignore, gt_labels, gt_masks, gt_semantic_seg

`Collect`
zhangwenwei's avatar
zhangwenwei committed
130
- add: img_metas (the keys of img_metas is specified by `meta_keys`)
zhangwenwei's avatar
zhangwenwei committed
131
132
133
134
- remove: all other keys except for those specified by `keys`

### Test time augmentation

135
136
`MultiScaleFlipAug3D`
- update: all the dict fields (update values to the collection of augmented data)
zhangwenwei's avatar
zhangwenwei committed
137

zhangwenwei's avatar
Doc  
zhangwenwei committed
138
## Extend and use custom pipelines
zhangwenwei's avatar
zhangwenwei committed
139

zhangwenwei's avatar
Doc  
zhangwenwei committed
140
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
141
142

    ```python
zhangwenwei's avatar
Doc  
zhangwenwei committed
143
    from mmdet.datasets import PIPELINES
zhangwenwei's avatar
zhangwenwei committed
144

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

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

zhangwenwei's avatar
Doc  
zhangwenwei committed
153
2. Import the new class.
zhangwenwei's avatar
zhangwenwei committed
154
155

    ```python
zhangwenwei's avatar
Doc  
zhangwenwei committed
156
    from .my_pipeline import MyTransform
zhangwenwei's avatar
zhangwenwei committed
157
158
    ```

zhangwenwei's avatar
Doc  
zhangwenwei committed
159
3. Use it in config files.
zhangwenwei's avatar
zhangwenwei committed
160
161

    ```python
zhangwenwei's avatar
Doc  
zhangwenwei committed
162
163
164
165
166
167
168
169
170
171
172
173
174
    img_norm_cfg = dict(
        mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
    train_pipeline = [
        dict(type='LoadImageFromFile'),
        dict(type='LoadAnnotations', with_bbox=True),
        dict(type='Resize', img_scale=(1333, 800), keep_ratio=True),
        dict(type='RandomFlip', flip_ratio=0.5),
        dict(type='Normalize', **img_norm_cfg),
        dict(type='Pad', size_divisor=32),
        dict(type='MyTransform'),
        dict(type='DefaultFormatBundle'),
        dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']),
    ]
zhangwenwei's avatar
zhangwenwei committed
175
    ```