# 数据变换迁移 ## 简介 MMOCR 0.x 版本中,我们在 `mmocr/datasets/pipelines/xxx_transforms.py` 中实现了一系列的数据变换(Data Transforms)方法。然而,这些模块分散在各处,且缺乏规范统一的设计。因此,我们在 MMOCR 1.x 版本中对所有的数据增强模块进行了重构,并依照任务类型分别存放在 `mmocr/datasets/transforms` 目录下的 `ocr_transforms.py`,`textdet_transforms.py` 及 `textrecog_transforms.py` 中。其中,`ocr_transforms.py` 中实现了 OCR 相关任务通用的数据增强模块,而 `textdet_transforms.py` 和 `textrecog_transforms.py` 则分别实现了文本检测任务与文本识别任务相关的数据增强模组。 由于在重构过程中我们对部分模块进行了重命名、合并或拆分,使得新的调用接口与默认参数可能与旧版本存在不一致。因此,本文档将详细介绍如何对数据增强模块进行迁移,即,如何配置现有的数据变换来达到与旧版一致的行为。 ## 配置迁移指南 ### 数据格式化相关数据变换 1. `Collect` + `CustomFormatBundle` -> [`PackTextDetInputs`](mmocr.datasets.transforms.PackTextDetInputs)/[`PackTextRecogInputs`](mmocr.datasets.transforms.PackTextRecogInputs) `PackxxxInputs` 同时囊括了 `Collect` 和 `CustomFormatBundle` 两个功能,且不再有 `key` 参数,而训练目标 target 的生成现在被转移至在 `loss` 中完成。
MMOCR 0.x 配置 | MMOCR 1.x 配置 |
---|---|
```python dict( type='CustomFormatBundle', keys=['gt_shrink', 'gt_shrink_mask', 'gt_thr', 'gt_thr_mask'], meta_keys=['img_path', 'ori_shape', 'img_shape'], visualize=dict(flag=False, boundary_key='gt_shrink')), dict( type='Collect', keys=['img', 'gt_shrink', 'gt_shrink_mask', 'gt_thr', 'gt_thr_mask']) ``` | ```python dict( type='PackTextDetInputs', meta_keys=('img_path', 'ori_shape', 'img_shape')) ``` |
MMOCR 0.x 配置 | MMOCR 1.x 配置 |
---|---|
```python dict( type='ResizeOCR', height=32, min_width=100, max_width=100, keep_aspect_ratio=False) ``` | ```python dict( type='Resize', scale=(100, 32), keep_ratio=False) ``` |
MMOCR 0.x 配置 | MMOCR 1.x 配置 |
---|---|
```python dict( type='ResizeOCR', height=32, min_width=32, max_width=None, width_downsample_ratio = 1.0 / 16 keep_aspect_ratio=True) ``` | ```python dict( type='RescaleToHeight', height=32, min_width=32, max_width=None, width_divisor=16), ``` |
MMOCR 0.x 配置 | MMOCR 1.x 配置 |
---|---|
```python dict( type='ResizeOCR', height=32, min_width=32, max_width=100, width_downsample_ratio = 1.0 / 16, keep_aspect_ratio=True) ``` | ```python dict( type='RescaleToHeight', height=32, min_width=32, max_width=100, width_divisor=16), dict( type='PadToWidth', width=100) ``` |
MMOCR 0.x 配置 | MMOCR 1.x 配置 |
---|---|
```python dict(type='RandomRotateTextDet') ``` | ```python dict(type='RandomRotate', max_angle=10) ``` |
MMOCR 0.x 配置 | MMOCR 1.x 配置 |
---|---|
```python dict( type='RandomRotatePolyInstances', rotate_ratio=0.5, # 指定概率为0.5 max_angle=60, pad_with_fixed_color=False) ``` | ```python # 用 RandomApply 对数据变换进行包装,并指定执行概率 dict( type='RandomApply', transforms=[ dict(type='RandomRotate', max_angle=60, pad_with_fixed_color=False, use_canvas=True)], prob=0.5) # 设置执行概率为 0.5 ``` |
MMOCR 0.x 配置 | MMOCR 1.x 配置 |
---|---|
```python dict( type='RandomCropPolyInstances', instance_key='gt_masks', crop_ratio=0.8, # 指定概率为 0.8 min_side_ratio=0.3) ``` | ```python # 用 RandomApply 对数据变换进行包装,并指定执行概率 dict( type='RandomApply', transforms=[dict(type='RandomCrop', min_side_ratio=0.3)], prob=0.8) # 设置执行概率为 0.8 ``` |
MMOCR 0.x 配置 | MMOCR 1.x 配置 |
---|---|
```python dict( type='RandomCropInstances', target_size=(800,800), instance_key='gt_kernels') ``` | ```python dict( type='TextDetRandomCrop', target_size=(800,800)) ``` |
MMOCR 0.x 配置 | MMOCR 1.x 配置 |
---|---|
```python dict( type='EastRandomCrop', max_tries=10, min_crop_side_ratio=0.1, target_size=(640, 640)) ``` | ```python dict(type='RandomCrop', min_side_ratio=0.1), dict(type='Resize', scale=(640,640), keep_ratio=True), dict(type='Pad', size=(640,640)) ``` |
MMOCR 0.x 配置 | MMOCR 1.x 配置 |
---|---|
```python dict( type='RandomScaling', size=800, scale=(0.75, 2.5)) ``` | ```python dict( type='RandomResize', scale=(800, 800), ratio_range=(0.75, 2.5), keep_ratio=True) ``` |
MMOCR 0.x 配置 | MMOCR 1.x 配置 |
---|---|
```python dict( type='SquareResizePad', target_size=800, pad_ratio=0.6) ``` | ```python dict( type='RandomChoice', transforms=[ [ dict( type='Resize', scale=800, keep_ratio=True), dict( type='SourceImagePad', target_scale=800) ], [ dict( type='Resize', scale=800, keep_ratio=False) ] ], prob=[0.4, 0.6]), # 两种组合的选用概率 ``` |
MMOCR 0.x 配置 | MMOCR 1.x 配置 |
---|---|
```python dict( type='RandomWrapper', p=0.25, transforms=[ dict(type='PyramidRescale'), ]) ``` | ```python dict( type='RandomApply', prob=0.25, transforms=[ dict(type='PyramidRescale'), ]) ``` |
MMOCR 0.x 配置 | MMOCR 1.x 配置 |
---|---|
```python dict( type='ScaleAspectJitter', img_scale=None, keep_ratio=False, resize_type='indep_sample_in_range', scale_range=(640, 2560)) ``` | ```python dict( type='RandomResize', scale=(640, 640), ratio_range=(1.0, 4.125), resize_type='Resize', keep_ratio=True)) ``` |
MMOCR 0.x 配置 | MMOCR 1.x 配置 |
---|---|
```python dict( type='ScaleAspectJitter', img_scale=[(3000, 736)], # Unused ratio_range=(0.7, 1.3), aspect_ratio_range=(0.9, 1.1), multiscale_mode='value', long_size_bound=800, short_size_bound=480, resize_type='long_short_bound', keep_ratio=False) ``` | ```python dict( type='BoundedScaleAspectJitter', long_size_bound=800, short_size_bound=480, ratio_range=(0.7, 1.3), aspect_ratio_range=(0.9, 1.1)) ``` |
MMOCR 0.x 配置 | MMOCR 1.x 配置 |
---|---|
```python dict( type='ScaleAspectJitter', img_scale=[(3000, 640)], ratio_range=(0.7, 1.3), aspect_ratio_range=(0.9, 1.1), multiscale_mode='value', keep_ratio=False) ``` | ```python dict( type='ShortScaleAspectJitter', short_size=640, ratio_range=(0.7, 1.3), aspect_ratio_range=(0.9, 1.1), scale_divisor=32), ``` |