Spaces:
Sleeping
Sleeping
File size: 14,394 Bytes
14c9181 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 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 254 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 323 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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 |
# 数据变换迁移
## 简介
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` 中完成。
<table class="docutils">
<thead>
<tr>
<th>MMOCR 0.x 配置</th>
<th>MMOCR 1.x 配置</th>
</tr>
<tbody><tr>
<td valign="top">
```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'])
```
</td><td>
```python
dict(
type='PackTextDetInputs',
meta_keys=('img_path', 'ori_shape', 'img_shape'))
```
</td></tr>
</thead>
</table>
### 数据增强相关数据变换
1. `ResizeOCR` -> [`Resize`](mmocr.datasets.transforms.Resize), [`RescaleToHeight`](mmocr.datasets.transforms.RescaleToHeight), [`PadToWidth`](mmocr.datasets.transforms.PadToWidth)
原有的 `ResizeOCR` 现在被拆分为三个独立的数据增强模块。
`keep_aspect_ratio=False` 时,等价为 1.x 版本中的 `Resize`,其配置可按如下方式修改。
<table class="docutils">
<thead>
<tr>
<th>MMOCR 0.x 配置</th>
<th>MMOCR 1.x 配置</th>
</tr>
<tbody><tr>
<td valign="top">
```python
dict(
type='ResizeOCR',
height=32,
min_width=100,
max_width=100,
keep_aspect_ratio=False)
```
</td><td>
```python
dict(
type='Resize',
scale=(100, 32),
keep_ratio=False)
```
</td></tr>
</thead>
</table>
`keep_aspect_ratio=True`,且 `max_width=None` 时。将图片的高缩放至固定值,并等比例缩放图像的宽。
<table class="docutils">
<thead>
<tr>
<th>MMOCR 0.x 配置</th>
<th>MMOCR 1.x 配置</th>
</tr>
<tbody><tr>
<td valign="top">
```python
dict(
type='ResizeOCR',
height=32,
min_width=32,
max_width=None,
width_downsample_ratio = 1.0 / 16
keep_aspect_ratio=True)
```
</td><td>
```python
dict(
type='RescaleToHeight',
height=32,
min_width=32,
max_width=None,
width_divisor=16),
```
</td></tr>
</thead>
</table>
`keep_aspect_ratio=True`,且 `max_width` 为固定值时。将图片的高缩放至固定值,并等比例缩放图像的宽。若缩放后的图像宽小于 `max_width`, 则将其填充至 `max_width`, 反之则将其裁剪至 `max_width`。即,输出图像的尺寸固定为 `(height, max_width)`。
<table class="docutils">
<thead>
<tr>
<th>MMOCR 0.x 配置</th>
<th>MMOCR 1.x 配置</th>
</tr>
<tbody><tr>
<td valign="top">
```python
dict(
type='ResizeOCR',
height=32,
min_width=32,
max_width=100,
width_downsample_ratio = 1.0 / 16,
keep_aspect_ratio=True)
```
</td><td>
```python
dict(
type='RescaleToHeight',
height=32,
min_width=32,
max_width=100,
width_divisor=16),
dict(
type='PadToWidth',
width=100)
```
</td></tr>
</thead>
</table>
2. `RandomRotateTextDet` & `RandomRotatePolyInstances` -> [`RandomRotate`](mmocr.datasets.transforms.RandomRotate)
随机旋转数据增强策略已被整合至 `RanomRotate`。该方法的默认行为与 0.x 版本中的 `RandomRotateTextDet` 保持一致。此时仅需指定最大旋转角度 `max_angle` 即可。
```{note}
新旧版本 "max_angle" 的默认值不同,因此需要重新进行指定。
```
<table class="docutils">
<thead>
<tr>
<th>MMOCR 0.x 配置</th>
<th>MMOCR 1.x 配置</th>
</tr>
<tbody><tr>
<td valign="top">
```python
dict(type='RandomRotateTextDet')
```
</td><td>
```python
dict(type='RandomRotate', max_angle=10)
```
</td></tr>
</thead>
</table>
对于 `RandomRotatePolyInstances`,则需要指定参数 `use_canvas=True`。
<table class="docutils">
<thead>
<tr>
<th>MMOCR 0.x 配置</th>
<th>MMOCR 1.x 配置</th>
</tr>
<tbody><tr>
<td valign="top">
```python
dict(
type='RandomRotatePolyInstances',
rotate_ratio=0.5, # 指定概率为0.5
max_angle=60,
pad_with_fixed_color=False)
```
</td><td>
```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
```
</td></tr>
</thead>
</table>
```{note}
在 0.x 版本中,部分数据增强方法通过定义一个内部变量 "xxx_ratio" 来指定执行概率,如 "rotate_ratio", "crop_ratio" 等。在 1.x 版本中,这些参数已被统一删除。现在,我们可以通过 "RandomApply" 来对不同的数据变换方法进行包装,并指定其执行概率。
```
3. `RandomCropFlip` -> [`TextDetRandomCropFlip`](mmocr.datasets.transforms.TextDetRandomCropFlip)
目前仅对方法名进行了更改,其他参数保持一致。
4. `RandomCropPolyInstances` -> [`RandomCrop`](mmocr.datasets.transforms.RandomCrop)
新版本移除了 `crop_ratio` 以及 `instance_key`,并统一使用 `gt_polygons` 为目标进行裁剪。
<table class="docutils">
<thead>
<tr>
<th>MMOCR 0.x 配置</th>
<th>MMOCR 1.x 配置</th>
</tr>
<tbody><tr>
<td valign="top">
```python
dict(
type='RandomCropPolyInstances',
instance_key='gt_masks',
crop_ratio=0.8, # 指定概率为 0.8
min_side_ratio=0.3)
```
</td><td>
```python
# 用 RandomApply 对数据变换进行包装,并指定执行概率
dict(
type='RandomApply',
transforms=[dict(type='RandomCrop', min_side_ratio=0.3)],
prob=0.8) # 设置执行概率为 0.8
```
</td></tr>
</thead>
</table>
5. `RandomCropInstances` -> [`TextDetRandomCrop`](mmocr.datasets.transforms.TextDetRandomCrop)
新版本移除了 `instance_key` 和 `mask_type`,并统一使用 `gt_polygons` 为目标进行裁剪。
<table class="docutils">
<thead>
<tr>
<th>MMOCR 0.x 配置</th>
<th>MMOCR 1.x 配置</th>
</tr>
<tbody><tr>
<td valign="top">
```python
dict(
type='RandomCropInstances',
target_size=(800,800),
instance_key='gt_kernels')
```
</td><td>
```python
dict(
type='TextDetRandomCrop',
target_size=(800,800))
```
</td></tr>
</thead>
</table>
6. `EastRandomCrop` -> [`RandomCrop`](mmocr.datasets.transforms.RandomCrop) + [`Resize`](mmocr.datasets.transforms.Resize) + [`mmengine.Pad`](mmcv.transforms.Pad)
原有的 `EastRandomCrop` 内同时对图像进行了剪裁、缩放以及填充。在新版本中,我们可以通过组合三种数据增强策略来达到相同的效果。
<table class="docutils">
<thead>
<tr>
<th>MMOCR 0.x 配置</th>
<th>MMOCR 1.x 配置</th>
</tr>
<tbody><tr>
<td valign="top">
```python
dict(
type='EastRandomCrop',
max_tries=10,
min_crop_side_ratio=0.1,
target_size=(640, 640))
```
</td><td>
```python
dict(type='RandomCrop', min_side_ratio=0.1),
dict(type='Resize', scale=(640,640), keep_ratio=True),
dict(type='Pad', size=(640,640))
```
</td></tr>
</thead>
</table>
7. `RandomScaling` -> [`mmengine.RandomResize`](mmcv.transforms.RandomResize)
在新版本中,我们直接使用 MMEngine 中实现的 `RandomResize` 来代替原有的实现。
<table class="docutils">
<thead>
<tr>
<th>MMOCR 0.x 配置</th>
<th>MMOCR 1.x 配置</th>
</tr>
<tbody><tr>
<td valign="top">
```python
dict(
type='RandomScaling',
size=800,
scale=(0.75, 2.5))
```
</td><td>
```python
dict(
type='RandomResize',
scale=(800, 800),
ratio_range=(0.75, 2.5),
keep_ratio=True)
```
</td></tr>
</thead>
</table>
```{note}
默认地,数据流水线会从当前 *scope* 的注册器中搜索对应的数据变换,如果不存在该数据变换,则将继续在上游库,如 MMCV 及 MMEngine 中进行搜索。例如,MMOCR 中并未实现 `RandomResize` 方法,但我们仍然可以在配置中直接引用该数据增强方法,因为程序将自动从上游的 MMCV 中搜索该方法。此外,用户也可以通过添加前缀的形式来指定 *scope*。例如,`mmengine.RandomResize` 将强制指定使用 MMCV 库中实现的 `RandomResize`,当上下游库中存在同名方法时,则可以通过这种形式强制使用特定的版本。另外需要注意的是,MMCV 中所有的数据变换方法都被注册至 MMEngine 中,因此我们使用 `mmengine.RandomResize` 而不是 `mmcv.RandomResize`。
```
8. `SquareResizePad` -> [`Resize`](mmocr.datasets.transforms.Resize) + [`SourceImagePad`](mmocr.datasets.transforms.SourceImagePad)
原有的 `SquareResizePad` 内部实现了两个分支,并依据概率 `pad_ratio` 随机使用其中的一个分支进行数据增强。具体而言,一个分支先对图像缩放再填充;另一个分支则直接对图像进行缩放。为增强不同模块的复用性,我们在 1.x 版本中将该方法拆分成了 `Resize` + `SourceImagePad` 的组合形式,并通过 MMCV 中的 `RandomChoice` 来控制分支。
<table class="docutils">
<thead>
<tr>
<th>MMOCR 0.x 配置</th>
<th>MMOCR 1.x 配置</th>
</tr>
<tbody><tr>
<td valign="top">
```python
dict(
type='SquareResizePad',
target_size=800,
pad_ratio=0.6)
```
</td><td>
```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]), # 两种组合的选用概率
```
</td></tr>
</thead>
</table>
```{note}
在 1.x 版本中,随机选择包装器 "RandomChoice" 代替了 "OneOfWrapper",可以从一系列数据变换组合中随机抽取一组并应用。
```
9. `RandomWrapper` -> [`mmegnine.RandomApply`](mmcv.transforms.RandomApply)
在 1.x 版本中,`RandomWrapper` 包装器被替换为由 MMCV 实现的 `RandomApply`,用以指定数据变换的执行概率。其中概率 `p` 现在被命名为 `prob`。
<table class="docutils">
<thead>
<tr>
<th>MMOCR 0.x 配置</th>
<th>MMOCR 1.x 配置</th>
</tr>
<tbody><tr>
<td valign="top">
```python
dict(
type='RandomWrapper',
p=0.25,
transforms=[
dict(type='PyramidRescale'),
])
```
</td><td>
```python
dict(
type='RandomApply',
prob=0.25,
transforms=[
dict(type='PyramidRescale'),
])
```
</td></tr>
</thead>
</table>
10. `OneOfWrapper` -> [`mmegnine.RandomChoice`](mmcv.transforms.RandomChoice)
随机选择包装器现在被重命名为 `RandomChoice`,并且使用方法和原来完全一致。
11. `ScaleAspectJitter` -> [`ShortScaleAspectJitter`](mmocr.datasets.transforms.ShortScaleAspectJitter), [`BoundedScaleAspectJitter`](mmocr.datasets.transforms.BoundedScaleAspectJitter)
原有的 `ScaleAspectJitter` 实现了多种不同的图像尺寸抖动数据增强策略,在新版本中,我们将其拆分为数个逻辑更加清晰的独立数据变化方法。
`resize_type='indep_sample_in_range'` 时,其等价于图像在指定范围内的随机缩放。
<table class="docutils">
<thead>
<tr>
<th>MMOCR 0.x 配置</th>
<th>MMOCR 1.x 配置</th>
</tr>
<tbody><tr>
<td valign="top">
```python
dict(
type='ScaleAspectJitter',
img_scale=None,
keep_ratio=False,
resize_type='indep_sample_in_range',
scale_range=(640, 2560))
```
</td><td>
```python
dict(
type='RandomResize',
scale=(640, 640),
ratio_range=(1.0, 4.125),
resize_type='Resize',
keep_ratio=True))
```
</td></tr>
</thead>
</table>
`resize_type='long_short_bound'` 时,将图像缩放至指定大小,再对其长宽比进行抖动。这一逻辑现在由新的数据变换类 `BoundedScaleAspectJitter` 实现。
<table class="docutils">
<thead>
<tr>
<th>MMOCR 0.x 配置</th>
<th>MMOCR 1.x 配置</th>
</tr>
<tbody><tr>
<td valign="top">
```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)
```
</td><td>
```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))
```
</td></tr>
</thead>
</table>
`resize_type='around_min_img_scale'` (默认参数)时,将图像的短边缩放至指定大小,再在指定范围内对长宽比进行抖动。最后,确保其边长能被 `scale_divisor` 整除。这一逻辑由新的数据变换类 `ShortScaleAspectJitter` 实现。
<table class="docutils">
<thead>
<tr>
<th>MMOCR 0.x 配置</th>
<th>MMOCR 1.x 配置</th>
</tr>
<tbody><tr>
<td valign="top">
```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)
```
</td><td>
```python
dict(
type='ShortScaleAspectJitter',
short_size=640,
ratio_range=(0.7, 1.3),
aspect_ratio_range=(0.9, 1.1),
scale_divisor=32),
```
</td></tr>
</thead>
</table>
|