Skip to content

Commit 7736c1c

Browse files
authored
Add quant and amp docs
1 parent e47cabf commit 7736c1c

File tree

3 files changed

+112
-6
lines changed

3 files changed

+112
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
_base_: '../_base_/kitti_mono.yml'
2+
3+
batch_size: 8
4+
iters: 70000
5+
6+
amp_cfg:
7+
enable: True
8+
level: O1
9+
scaler:
10+
init_loss_scaling: 1024.0
11+
custom_black_list: ['matmul_v2', 'elementwise_mul']
12+
13+
train_dataset:
14+
transforms:
15+
- type: LoadImage
16+
reader: pillow
17+
to_chw: False
18+
- type: Gt2SmokeTarget
19+
mode: train
20+
num_classes: 3
21+
input_size: [1280, 384]
22+
- type: Normalize
23+
mean: [0.485, 0.456, 0.406]
24+
std: [0.229, 0.224, 0.225]
25+
26+
val_dataset:
27+
transforms:
28+
- type: LoadImage
29+
reader: pillow
30+
to_chw: False
31+
- type: Gt2SmokeTarget
32+
mode: val
33+
num_classes: 3
34+
input_size: [1280, 384]
35+
- type: Normalize
36+
mean: [0.485, 0.456, 0.406]
37+
std: [0.229, 0.224, 0.225]
38+
39+
optimizer:
40+
type: Adam
41+
42+
lr_scheduler:
43+
type: MultiStepDecay
44+
milestones: [36000, 55000]
45+
learning_rate: 1.25e-4
46+
47+
model:
48+
type: SMOKE
49+
backbone:
50+
type: DLA34
51+
# This will automatically save to ~/.paddle3d/pretrained/dla34/dla34.pdparams
52+
pretrained: "https://bj.bcebos.com/paddle3d/pretrained/dla34.pdparams"
53+
head:
54+
type: SMOKEPredictor
55+
num_classes: 3
56+
reg_channels: [1, 2, 3, 2, 2]
57+
num_chanels: 256
58+
norm_type: "gn"
59+
in_channels: 64
60+
depth_ref: [28.01, 16.32]
61+
# dim_ref is the reference size mentioned in the paper, the order here is [l, h, w]
62+
dim_ref: [[3.88, 1.63, 1.53], [1.78, 1.70, 0.58], [0.88, 1.73, 0.67]]
63+
max_detection: 50
64+
pred_2d: True
65+
66+
export:
67+
transforms:
68+
- type: LoadImage
69+
reader: pillow
70+
to_chw: False
71+
to_rgb: True
72+
- type: Normalize
73+
mean: [0.485, 0.456, 0.406]
74+
std: [0.229, 0.224, 0.225]

docs/quickstart.md

+31
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export CUDA_VISIBLE_DEVICES=0,1,2,3
2727
fleetrun tools/train.py --config configs/smoke/smoke_dla34_no_dcn_kitti.yml --iters 100 --log_interval 10 --save_interval 20
2828
```
2929

30+
**混合精度训练**
31+
32+
如果想要启动混合精度训练,请参考[配置文件](../configs/smoke/smoke_dla34_no_dcn_kitti_amp.yml#L6-#L11)中添加amp的参数项,可用的参数可以参考 API **[paddle.amp.auto_cast](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/amp/auto_cast_cn.html#paddle.amp.auto_cast)**
33+
3034
**训练脚本参数介绍**
3135

3236
| 参数名 | 用途 | 是否必选项 | 默认值 |
@@ -43,6 +47,7 @@ fleetrun tools/train.py --config configs/smoke/smoke_dla34_no_dcn_kitti.yml --it
4347
| log_interval | 打印日志的间隔步数 || 10 |
4448
| resume | 是否从检查点中恢复训练状态 || None |
4549
| keep_checkpoint_max | 最多保存模型的数量 || 5 |
50+
| quant_config | 量化配置文件,一般放在[configs/quant](../configs/quant)目录下 || None |
4651
| seed | Paddle/numpy/random的全局随机种子值 || None |
4752

4853
*注意:使用一个 batch 数据对模型进行一次参数更新的过程称之为一步,iters 即为训练过程中的训练步数。完整遍历一次数据对模型进行训练的过程称之为一次迭代,epochs 即为训练过程中的训练迭代次数。一个epoch包含多个iter。*
@@ -63,6 +68,30 @@ visualdl --logdir output --host ${HOST_IP} --port {$PORT}
6368

6469
<br>
6570

71+
## 模型量化(可选)
72+
73+
为了导出量化的模型,我们可以对模型进行量化训练,量化后的模型可以使用TensorRT + int8进行推理,从而提升推理速度,使用如下命令启动量化训练。
74+
75+
以多卡训练为例子,使用如下命令启动多卡量化训练,同样只训练100个iter进行快速体验
76+
77+
```shell
78+
export CUDA_VISIBLE_DEVICES=0,1,2,3
79+
# 注意这是一次新的训练,需要指定加载已经训练好的模型参数进行微调
80+
# 并且指定新的模型保存路径
81+
fleetrun tools/train.py \
82+
--config configs/smoke/smoke_dla34_no_dcn_kitti.yml \
83+
--iters 100 \
84+
--log_interval 10 \
85+
--save_interval 20 \
86+
--quant_config configs/quant/smoke_kitti.yml \
87+
--model output/iter_100/model.pdparams \
88+
--save_dir output_smoke_quant
89+
```
90+
91+
*注意,不同的模型需要探索不同的量化训练配置(如重新训练的次数,学习率衰减等),我们提供了 **SMOKE****CenterPoint** 的配置文件供参考*
92+
93+
<br>
94+
6695
## 模型评估
6796

6897
**单卡评估**
@@ -82,6 +111,7 @@ python tools/evaluate.py --config configs/smoke/smoke_dla34_no_dcn_kitti.yml --m
82111
| config | 配置文件路径 || - |
83112
| model | 模型参数路径 || - |
84113
| num_workers | 用于异步读取数据的进程数量, 大于等于1时开启子进程读取数据 || 2 |
114+
| quant_config | 量化配置文件,一般放在[configs/quant](../configs/quant)目录下,如果模型使用量化训练,则在评估时同样需要指定量化配置文件 ||
85115

86116
<br>
87117

@@ -103,6 +133,7 @@ python tools/export.py --config configs/smoke/smoke_dla34_no_dcn_kitti.yml --mod
103133
| export_for_apollo | 是否用于Apollo部署,当打开该开关时,会同步生成用于Apollo部署的meta文件 || False |
104134
| save_dir | 推理模型文件的保存路径 || exported_model |
105135
| save_name | 推理模型文件的保存名字 || None(由各模型自定决定) |
136+
| quant_config | 量化配置文件,一般放在[configs/quant](../configs/quant)目录下,如果模型使用量化训练,则在模型导出时同样需要指定量化配置文件 ||
106137

107138
<br>
108139

tools/train.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,7 @@ def main(args):
140140
if not os.path.exists(args.cfg):
141141
raise RuntimeError("Config file `{}` does not exist!".format(args.cfg))
142142

143-
cfg = Config(
144-
path=args.cfg,
145-
learning_rate=args.learning_rate,
146-
iters=args.iters,
147-
epochs=args.epochs,
148-
batch_size=args.batch_size)
143+
cfg = Config(path=args.cfg)
149144

150145
if args.model is not None:
151146
load_pretrained_model(cfg.model, args.model)
@@ -155,6 +150,12 @@ def main(args):
155150
cfg.model.build_slim_model(quant_config['quant_config'])
156151
update_dic(cfg.dic, quant_config['finetune_config'])
157152

153+
cfg.update(
154+
learning_rate=args.learning_rate,
155+
batch_size=args.batch_size,
156+
iters=args.iters,
157+
epochs=args.epochs)
158+
158159
if cfg.train_dataset is None:
159160
raise RuntimeError(
160161
'The training dataset is not specified in the configuration file!')

0 commit comments

Comments
 (0)