Skip to content

Commit 1ea3975

Browse files
authored
[Doc] Refine training tricks documentation (open-mmlab#2755)
1 parent 1896344 commit 1ea3975

File tree

2 files changed

+28
-64
lines changed

2 files changed

+28
-64
lines changed

docs/en/advanced_guides/training_tricks.md

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# \[WIP\] Training Tricks
1+
# Training Tricks
22

33
MMSegmentation support following training tricks out of box.
44

@@ -9,18 +9,19 @@ In semantic segmentation, some methods make the LR of heads larger than backbone
99
In MMSegmentation, you may add following lines to config to make the LR of heads 10 times of backbone.
1010

1111
```python
12-
optimizer=dict(
12+
optim_wrapper=dict(
1313
paramwise_cfg = dict(
1414
custom_keys={
1515
'head': dict(lr_mult=10.)}))
1616
```
1717

1818
With this modification, the LR of any parameter group with `'head'` in name will be multiplied by 10.
19-
You may refer to [MMCV doc](https://mmcv.readthedocs.io/en/latest/api.html#mmcv.runner.DefaultOptimizerConstructor) for further details.
19+
You may refer to [MMEngine documentation](https://mmengine.readthedocs.io/en/latest/tutorials/optim_wrapper.html#advanced-usages) for further details.
2020

2121
## Online Hard Example Mining (OHEM)
2222

23-
We implement pixel sampler [here](https://github.com/open-mmlab/mmsegmentation/tree/master/mmseg/core/seg/sampler) for training sampling.
23+
We implement pixel sampler for training sampling, like OHEM (Online Hard Example Mining),
24+
which is used for remove the "easy" examples for model training.
2425
Here is an example config of training PSPNet with OHEM enabled.
2526

2627
```python
@@ -58,33 +59,17 @@ For loss calculation, we support multiple losses training concurrently. Here is
5859
```python
5960
_base_ = './fcn_unet_s5-d16_64x64_40k_drive.py'
6061
model = dict(
61-
decode_head=dict(loss_decode=[dict(type='CrossEntropyLoss', loss_name='loss_ce', loss_weight=1.0),
62-
dict(type='DiceLoss', loss_name='loss_dice', loss_weight=3.0)]),
63-
auxiliary_head=dict(loss_decode=[dict(type='CrossEntropyLoss', loss_name='loss_ce',loss_weight=1.0),
64-
dict(type='DiceLoss', loss_name='loss_dice', loss_weight=3.0)]),
65-
)
62+
decode_head=dict(loss_decode=[
63+
dict(type='CrossEntropyLoss', loss_name='loss_ce', loss_weight=1.0),
64+
dict(type='DiceLoss', loss_name='loss_dice', loss_weight=3.0)
65+
]),
66+
auxiliary_head=dict(loss_decode=[
67+
dict(type='CrossEntropyLoss', loss_name='loss_ce', loss_weight=1.0),
68+
dict(type='DiceLoss', loss_name='loss_dice', loss_weight=3.0)
69+
]),
70+
)
6671
```
6772

6873
In this way, `loss_weight` and `loss_name` will be weight and name in training log of corresponding loss, respectively.
6974

7075
Note: If you want this loss item to be included into the backward graph, `loss_` must be the prefix of the name.
71-
72-
## Ignore specified label index in loss calculation
73-
74-
In default setting, `avg_non_ignore=False` which means each pixel counts for loss calculation although some of them belong to ignore-index labels.
75-
76-
For loss calculation, we support ignore index of certain label by `avg_non_ignore` and `ignore_index`. In this way, the average loss would only be calculated in non-ignored labels which may achieve better performance, and here is the [reference](https://github.com/open-mmlab/mmsegmentation/pull/1409). Here is an example config of training `unet` on `Cityscapes` dataset: in loss calculation it would ignore label 0 which is background and loss average is only calculated on non-ignore labels:
77-
78-
```python
79-
_base_ = './unet-s5-d16_fcn_4xb4-160k_cityscapes-512x1024.py'
80-
model = dict(
81-
decode_head=dict(
82-
ignore_index=0,
83-
loss_decode=dict(
84-
type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0, avg_non_ignore=True),
85-
auxiliary_head=dict(
86-
ignore_index=0,
87-
loss_decode=dict(
88-
type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0, avg_non_ignore=True)),
89-
))
90-
```
Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 训练技巧(待更新)
1+
# 训练技巧
22

33
MMSegmentation 支持如下训练技巧:
44

@@ -9,17 +9,17 @@ MMSegmentation 支持如下训练技巧:
99
在 MMSegmentation 里面,您也可以在配置文件里添加如下行来让解码头组件的学习率是主干组件的10倍。
1010

1111
```python
12-
optimizer=dict(
12+
optim_wrapper=dict(
1313
paramwise_cfg = dict(
1414
custom_keys={
1515
'head': dict(lr_mult=10.)}))
1616
```
1717

18-
通过这种修改,任何被分组到 `'head'` 的参数的学习率都将乘以10。您也可以参照 [MMCV 文档](https://mmcv.readthedocs.io/en/latest/api.html#mmcv.runner.DefaultOptimizerConstructor) 获取更详细的信息。
18+
通过这种修改,任何被分组到 `'head'` 的参数的学习率都将乘以10。您也可以参照 [MMEngine 文档](https://mmengine.readthedocs.io/zh_CN/latest/tutorials/optim_wrapper.html#id6) 获取更详细的信息。
1919

2020
## 在线难样本挖掘 (Online Hard Example Mining, OHEM)
2121

22-
对于训练时采样,我们在 [这里](https://github.com/open-mmlab/mmsegmentation/tree/master/mmseg/core/seg/sampler) 做了像素采样器。
22+
MMSegmentation 中实现了像素采样器,训练时可以对特定像素进行采样,例如 OHEM(Online Hard Example Mining),可以解决样本不平衡问题,
2323
如下例子是使用 PSPNet 训练并采用 OHEM 策略的配置:
2424

2525
```python
@@ -58,38 +58,17 @@ model=dict(
5858
```python
5959
_base_ = './fcn_unet_s5-d16_64x64_40k_drive.py'
6060
model = dict(
61-
decode_head=dict(loss_decode=[dict(type='CrossEntropyLoss', loss_name='loss_ce', loss_weight=1.0),
62-
dict(type='DiceLoss', loss_name='loss_dice', loss_weight=3.0)]),
63-
auxiliary_head=dict(loss_decode=[dict(type='CrossEntropyLoss', loss_name='loss_ce',loss_weight=1.0),
64-
dict(type='DiceLoss', loss_name='loss_dice', loss_weight=3.0)]),
65-
)
61+
decode_head=dict(loss_decode=[
62+
dict(type='CrossEntropyLoss', loss_name='loss_ce', loss_weight=1.0),
63+
dict(type='DiceLoss', loss_name='loss_dice', loss_weight=3.0)
64+
]),
65+
auxiliary_head=dict(loss_decode=[
66+
dict(type='CrossEntropyLoss', loss_name='loss_ce', loss_weight=1.0),
67+
dict(type='DiceLoss', loss_name='loss_dice', loss_weight=3.0)
68+
]),
69+
)
6670
```
6771

6872
通过这种方式,确定训练过程中损失函数的权重 `loss_weight` 和在训练日志里的名字 `loss_name`
6973

70-
注意: `loss_name` 的名字必须带有 `loss_` 前缀,这样它才能被包括在反传的图里。
71-
72-
## 在损失函数中忽略特定的 label 类别
73-
74-
默认设置 `avg_non_ignore=False`, 即每个像素都用来计算损失函数。尽管其中的一些像素属于需要被忽略的类别。
75-
76-
对于训练时损失函数的计算,我们目前支持使用 `avg_non_ignore``ignore_index` 来忽略 label 特定的类别。 这样损失函数将只在非忽略类别像素中求平均值,会获得更好的表现。这里是[相关 PR](https://github.com/open-mmlab/mmsegmentation/pull/1409)。以 `unet` 使用 `Cityscapes` 数据集训练为例,
77-
在计算损失函数时,忽略 label 为0的背景,并且仅在不被忽略的像素上计算均值。配置文件写为:
78-
79-
```python
80-
_base_ = './fcn_unet_s5-d16_4x4_512x1024_160k_cityscapes.py'
81-
model = dict(
82-
decode_head=dict(
83-
ignore_index=0,
84-
loss_decode=dict(
85-
type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0, avg_non_ignore=True),
86-
auxiliary_head=dict(
87-
ignore_index=0,
88-
loss_decode=dict(
89-
type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0, avg_non_ignore=True)),
90-
))
91-
```
92-
93-
通过这种方式,确定训练过程中损失函数的权重 `loss_weight` 和在训练日志里的名字 `loss_name`
94-
95-
注意: `loss_name` 的名字必须带有 `loss_` 前缀,这样它才能被包括在反传的图里。
74+
注意: `loss_name` 的名字必须带有 `loss_` 前缀,这样它才能被包括在计算图里。

0 commit comments

Comments
 (0)