Skip to content

Commit c951bb6

Browse files
[Docs] (CodeCamp #76) translate add_transforms.md and conventions.md (#674)
* translate add_transforms.md and conventions.md * translate add_transforms.md and conventions.md * update add_translate.md format * update add_translate.md format * update Co-authored-by: fangyixiao18 <[email protected]>
1 parent 386f66a commit c951bb6

File tree

3 files changed

+181
-4
lines changed

3 files changed

+181
-4
lines changed

docs/en/advanced_guides/add_transforms.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ class NewTransform(BaseTransform):
6666
return results
6767
```
6868

69-
**Note:** For the implementation of transforms, you could apply functions in [mmcv](https://github.com/open-mmlab/mmcv/tree/dev-2.x/mmcv/image).
69+
**Note:** For the implementation of transforms, you could apply functions in [mmcv](https://github.com/open-mmlab/mmcv/tree/2.x/mmcv/image).
7070

7171
### Step 2: Add NewTransform to \_\_init\_\_py
7272

73-
Then, add the transform to [\_\_init\_\_.py](https://github.com/open-mmlab/mmselfsup/blob/1.x/mmselfsup/datasets/transforms/__init__.py).
73+
Then, add the transform to [\_\_init\_\_.py](https://github.com/open-mmlab/mmselfsup/blob/dev-1.x/mmselfsup/datasets/transforms/__init__.py).
7474

7575
```python
7676
...
Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,118 @@
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+
```
Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,61 @@
1-
# Conventions
1+
# 约定
2+
3+
- [约定](#约定)
4+
- [损失](#损失)
5+
6+
如果您想将 MMSelfSup 修改为您自己的项目, 请检查以下约定。
7+
8+
## 损失
9+
10+
当算法实现时, 函数 `loss` 返回的损失应该是 `dict` 类型。
11+
12+
举个 `MAE` 的例子:
13+
14+
```python
15+
class MAE(BaseModel):
16+
"""MAE.
17+
18+
Implementation of `Masked Autoencoders Are Scalable Vision Learners
19+
<https://arxiv.org/abs/2111.06377>`_.
20+
"""
21+
22+
def extract_feat(self, inputs: List[torch.Tensor],
23+
**kwarg) -> Tuple[torch.Tensor]:
24+
...
25+
26+
def loss(self, inputs: List[torch.Tensor],
27+
data_samples: List[SelfSupDataSample],
28+
**kwargs) -> Dict[str, torch.Tensor]:
29+
"""The forward function in training.
30+
31+
Args:
32+
inputs (List[torch.Tensor]): The input images.
33+
data_samples (List[SelfSupDataSample]): All elements required
34+
during the forward function.
35+
36+
Returns:
37+
Dict[str, torch.Tensor]: A dictionary of loss components.
38+
"""
39+
# ids_restore: the same as that in original repo, which is used
40+
# to recover the original order of tokens in decoder.
41+
latent, mask, ids_restore = self.backbone(inputs[0])
42+
pred = self.neck(latent, ids_restore)
43+
loss = self.head(pred, inputs[0], mask)
44+
losses = dict(loss=loss)
45+
return losses
46+
47+
```
48+
49+
`MAE` 模型正向传播期间, 这个 `MAE.loss()` 函数将被调用用于计算损失并返回这个损失值。
50+
51+
默认情况下, 只有 `dict` 中的键包含的 `loss` 值时, 才会进行反向传播, 如果你的算法需要多个损失值, 你可以用多个键打包损失字典。
52+
53+
```python
54+
class YourAlgorithm(BaseModel):
55+
56+
def loss():
57+
...
58+
59+
losses['loss_1'] = loss_1
60+
losses['loss_2'] = loss_2
61+
```

0 commit comments

Comments
 (0)