|
1 |
| -# Add Transforms |
| 1 | +# 添加数据变换 |
| 2 | + |
| 3 | +在本教程中, 我们将介绍创建自定义转换的基本步骤。在学习创建自定义转换之前, 建议先了解文件 [transforms.md](transforms.md) 中转换的基本概念。 |
| 4 | + |
| 5 | +- [添加数据变换](#添加数据变换) |
| 6 | + - [管道概述](#管道概述) |
| 7 | + - [在管道中创建新转换](#在管道中创建新转换) |
| 8 | + - [步骤 1: 创建转换](#步骤-1-创建转换) |
| 9 | + - [步骤 2: 将新转换添加到 \_\_init\_\_py](#步骤-2-将新转换添加到-__init__py) |
| 10 | + - [步骤 3: 修改配置文件](#步骤-3-修改配置文件) |
| 11 | + |
| 12 | +## 管道概述 |
| 13 | + |
| 14 | +在 `Dataset` 中, `Pipeline` 是中的一个重要组件, 主要负责对图像应用一系列数据增强, 例如: `RandomResizedCrop`, `RandomFlip` 等操作。 |
| 15 | + |
| 16 | +以下代码是 `Pipeline` 用于 `SimCLR` 训练的配置示例: |
| 17 | + |
| 18 | +```python |
| 19 | +view_pipeline = [ |
| 20 | + dict(type='RandomResizedCrop', size=224, backend='pillow'), |
| 21 | + dict(type='RandomFlip', prob=0.5), |
| 22 | + dict( |
| 23 | + type='RandomApply', |
| 24 | + transforms=[ |
| 25 | + dict( |
| 26 | + type='ColorJitter', |
| 27 | + brightness=0.8, |
| 28 | + contrast=0.8, |
| 29 | + saturation=0.8, |
| 30 | + hue=0.2) |
| 31 | + ], |
| 32 | + prob=0.8), |
| 33 | + dict( |
| 34 | + type='RandomGrayscale', |
| 35 | + prob=0.2, |
| 36 | + keep_channels=True, |
| 37 | + channel_weights=(0.114, 0.587, 0.2989)), |
| 38 | + dict(type='RandomGaussianBlur', sigma_min=0.1, sigma_max=2.0, prob=0.5), |
| 39 | +] |
| 40 | + |
| 41 | +train_pipeline = [ |
| 42 | + dict(type='LoadImageFromFile', file_client_args=file_client_args), |
| 43 | + dict(type='MultiView', num_views=2, transforms=[view_pipeline]), |
| 44 | + dict(type='PackSelfSupInputs', meta_keys=['img_path']) |
| 45 | +] |
| 46 | +``` |
| 47 | + |
| 48 | +在这个 `Pipeline` 中, 每个数据增强接收一个 `dict` , 它们作为输入和输出时刻, 包含图像增强以及其他相关信息的 `dict` 。 |
| 49 | + |
| 50 | +## 在管道中创建新转换 |
| 51 | + |
| 52 | +以下是创建新转换的步骤。 |
| 53 | + |
| 54 | +### 步骤 1: 创建转换 |
| 55 | + |
| 56 | +在 [processing.py](https://github.com/open-mmlab/mmselfsup/tree/dev-1.x/mmselfsup/datasets/transforms/processing.py) 中编写一个新的转换类, 并在类中覆盖这个 `transform` 函数, 这个函数接收一个 `dict` 的对象, 并返回一个 `dict` 对象 |
| 57 | + |
| 58 | +```python |
| 59 | +@TRANSFORMS.register_module() |
| 60 | +class NewTransform(BaseTransform): |
| 61 | + """Docstring for transform. |
| 62 | + """ |
| 63 | + |
| 64 | + def transform(self, results: dict) -> dict: |
| 65 | + # apply transform |
| 66 | + return results |
| 67 | +``` |
| 68 | + |
| 69 | +**注意**: 对于这些转换的实现, 您可以应用 [mmcv](https://github.com/open-mmlab/mmcv/tree/2.x/mmcv/image) 中的函数。 |
| 70 | + |
| 71 | +### 步骤 2: 将新转换添加到 \_\_init\_\_py |
| 72 | + |
| 73 | +然后, 将转换添加到 [\_\_init\_\_.py](https://github.com/open-mmlab/mmselfsup/blob/dev-1.x/mmselfsup/datasets/transforms/__init__.py) 。 |
| 74 | + |
| 75 | +```python |
| 76 | +... |
| 77 | +from .processing import NewTransform, ... |
| 78 | + |
| 79 | +__all__ = [ |
| 80 | + ..., 'NewTransform' |
| 81 | +] |
| 82 | +``` |
| 83 | + |
| 84 | +### 步骤 3: 修改配置文件 |
| 85 | + |
| 86 | +要使用新添加的 `NewTransform`, 你可以按以下的方式修改配置文件: |
| 87 | + |
| 88 | +```python |
| 89 | +view_pipeline = [ |
| 90 | + dict(type='RandomResizedCrop', size=224, backend='pillow'), |
| 91 | + dict(type='RandomFlip', prob=0.5), |
| 92 | + # add `NewTransform` |
| 93 | + dict(type='NewTransform'), |
| 94 | + dict( |
| 95 | + type='RandomApply', |
| 96 | + transforms=[ |
| 97 | + dict( |
| 98 | + type='ColorJitter', |
| 99 | + brightness=0.8, |
| 100 | + contrast=0.8, |
| 101 | + saturation=0.8, |
| 102 | + hue=0.2) |
| 103 | + ], |
| 104 | + prob=0.8), |
| 105 | + dict( |
| 106 | + type='RandomGrayscale', |
| 107 | + prob=0.2, |
| 108 | + keep_channels=True, |
| 109 | + channel_weights=(0.114, 0.587, 0.2989)), |
| 110 | + dict(type='RandomGaussianBlur', sigma_min=0.1, sigma_max=2.0, prob=0.5), |
| 111 | +] |
| 112 | + |
| 113 | +train_pipeline = [ |
| 114 | + dict(type='LoadImageFromFile', file_client_args=file_client_args), |
| 115 | + dict(type='MultiView', num_views=2, transforms=[view_pipeline]), |
| 116 | + dict(type='PackSelfSupInputs', meta_keys=['img_path']) |
| 117 | +] |
| 118 | +``` |
0 commit comments