PPYOLO模型参数配置理解

内容参考自:README_cn.md · PaddlePaddle/PaddleDetection - 码云 - 开源中国 (gitee.com)

说明:用于帮助自己理解参数,后续会更新,可能有错误的地方,请不吝赐教。

# YOLO系列模型参数配置教程

标签: 模型参数配置

++++++++++++++++++++++++++ppyolo_r18vd.yml++++++++++++++++++++++++++++++++architecture: YOLOv3 #模型的名称use_gpu: true #是否使用GPU

max_iters: 15500     #最大的迭代次数
log_iter: 10     #输出指定区间的平均结果,如10次的平均结果,也即打印log的间隔
save_dir: output
snapshot_iter: 1550
metric: COCO
pretrain_weights: https://paddle-imagenet-models-name.bj.bcebos.com/ResNet18_vd_pretrained.tar
weights: output/ppyolo_r18/model_final
num_classes: 9
use_fine_grained_loss: true
use_ema: true
ema_decay: 0.9998

YOLOv3:
  backbone: ResNet
  yolo_head: YOLOv3Head
  use_fine_grained_loss: true

ResNet:
  norm_type: sync_bn
  freeze_at: 0
  freeze_norm: false
  norm_decay: 0.
  depth: 18
  feature_maps: [4, 5]
  variant: d

YOLOv3Head:
  anchor_masks: [[3, 4, 5], [0, 1, 2]]
  anchors: [[10, 14], [23, 27], [37, 58],
            [81, 82], [135, 169], [344, 319]]
  norm_decay: 0.
  conv_block_num: 0
  scale_x_y: 1.05
  yolo_loss: YOLOv3Loss
  nms: MatrixNMS
  drop_block: true

YOLOv3Loss:
  ignore_thresh: 0.7
  scale_x_y: 1.05
  label_smooth: false
  use_fine_grained_loss: true
  iou_loss: IouLoss

IouLoss:
  loss_weight: 2.5
  max_height: 640
  max_ 640

MatrixNMS:
    background_label: -1
    keep_top_k: 100
    normalized: false
    score_threshold: 0.01
    post_threshold: 0.01

LearningRate:
  base_lr: 0.004 #学习率决定了权值更新的速度,学习率大,更新的就快,但太快容易越过最优值,而学习率太小又更新的慢,效率低,一般学习率随着训练的进行不断更改,先高一点,然后慢慢降低
  schedulers:
  - !PiecewiseDecay
    gamma: 0.1
    milestones:#学习率变动因子:如迭代到10000次时,学习率衰减十倍,15000次迭代时,学习率又会在前一个学习率的基础上衰减十倍
    - 10000
    - 15000
  - !LinearWarmup
    start_factor: 0. 
    steps: 4000  #学习率变动步长

OptimizerBuilder:
  optimizer:
    momentum: 0.9 #动量,影响梯度下降到最优的速度,一般默认0.9
    type: Momentum
  regularizer:
    factor: 0.0005  #权重衰减正则系数,防止过拟合
    type: L2

_READER_: 'ppyolo_reader.yml'
TrainReader:
  inputs_def:
    fields: ['image', 'gt_bbox', 'gt_class', 'gt_score']
    num_max_boxes: 50
  dataset:
    !COCODataSet
      image_dir: images
      anno_path: annotations/val.json
      dataset_dir: /home/aistudio/data/data101204/NGdet_v4-concat
      with_background: false
  sample_transforms:
    - !DecodeImage
      to_rgb: True
      with_mixup: True
    - !MixupImage
      alpha: 1.5
      beta: 1.5
    - !ColorDistort {}
    - !RandomExpand
      fill_value: [123.675, 116.28, 103.53]
    - !RandomCrop {}
    - !RandomFlipImage
      is_normalized: false
    - !NormalizeBox {}
    - !PadBox
      num_max_boxes: 50
    - !BboxXYXY2XYWH {}
  batch_transforms:
  - !RandomShape
    sizes: [ 352, 384, 416, 448, 480, 512, 544, 576, 608,640]
- !NormalizeImage mean: [0.485, 0.456, 0.406] std: [0.229, 0.224, 0.225] is_scale: True is_channel_first: false - !Permute to_bgr: false channel_first: True # Gt2YoloTarget is only used when use_fine_grained_loss set as true, # this operator will be deleted automatically if use_fine_grained_loss # is set as false - !Gt2YoloTarget anchor_masks: [[3, 4, 5], [0, 1, 2]]
  #anchors是可以事先通过cmd指令计算出来的,是和图片数量,width,height以及cluster(就是下面的num的值,即想要使用的anchors的数量)相关的预选框,可以手工挑选,也可以通过k-means算法从训练样本中学出 anchors: [[10, 14], [23, 27], [37, 58], [81, 82], [135, 169], [344, 319]]
downsample_ratios: [32, 16] batch_size: 8 shuffle: true mixup_epoch: 100 #大于最大epoch,表示训练过程一直使用mixup数据增广 drop_last: true worker_num: 8 bufsize: 8 use_process: true

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

以`ppyolo_r50vd_dcn_1x_coco.yml`为例,这个模型由五个子配置文件组成:

- 数据配置文件 `coco_detection.yml`

```yaml
# 数据评估类型
metric: COCO
# 数据集的类别数
num_classes: 80

# TrainDataset
TrainDataset:
!COCODataSet
# 图像数据路径,相对 dataset_dir 路径,os.path.join(dataset_dir, image_dir)
image_dir: train2017
# 标注文件路径,相对 dataset_dir 路径,os.path.join(dataset_dir, anno_path)
anno_path: annotations/instances_train2017.json
# 数据文件夹
dataset_dir: dataset/coco
# data_fields
data_fields: ['image', 'gt_bbox', 'gt_class', 'is_crowd']

EvalDataset:
!COCODataSet
# 图像数据路径,相对 dataset_dir 路径,os.path.join(dataset_dir, image_dir)
image_dir: val2017
# 标注文件路径,相对 dataset_dir 路径,os.path.join(dataset_dir, anno_path)
anno_path: annotations/instances_val2017.json
# 数据文件夹,os.path.join(dataset_dir, anno_path)
dataset_dir: dataset/coco

TestDataset:
!ImageFolder
# 标注文件路径,相对 dataset_dir 路径
anno_path: annotations/instances_val2017.json
```

- 优化器配置文件 `optimizer_1x.yml`

```yaml
# 总训练轮数
epoch: 405

# 学习率设置
LearningRate:
# 默认为8卡训学习率
base_lr: 0.01
# 学习率调整策略
schedulers:
- !PiecewiseDecay
gamma: 0.1
# 学习率变化位置(轮数)
milestones:
- 243
- 324
# Warmup
- !LinearWarmup
start_factor: 0.
steps: 4000

# 优化器
OptimizerBuilder:
# 优化器
optimizer:
momentum: 0.9
type: Momentum
# 正则化
regularizer:
factor: 0.0005
type: L2
```

- 数据读取配置文件 `ppyolo_reader.yml`

```yaml
# 每张GPU reader进程个数
worker_num: 2
# 训练数据
TrainReader:
inputs_def:
num_max_boxes: 50
# 训练数据transforms
sample_transforms:
- Decode: {}
- Mixup: {alpha: 1.5, beta: 1.5}
- RandomDistort: {}
- RandomExpand: {fill_value: [123.675, 116.28, 103.53]}
- RandomCrop: {}
- RandomFlip: {}
# batch_transforms
batch_transforms:
- BatchRandomResize: {target_size: [320, 352, 384, 416, 448, 480, 512, 544, 576, 608], random_size: True, random_interp: True, keep_ratio: False}
- NormalizeBox: {}
- PadBox: {num_max_boxes: 50}
- BboxXYXY2XYWH: {}
- NormalizeImage: {mean: [0.485, 0.456, 0.406], std: [0.229, 0.224, 0.225], is_scale: True}
- Permute: {}
- Gt2YoloTarget: {anchor_masks: [[6, 7, 8], [3, 4, 5], [0, 1, 2]], anchors: [[10, 13], [16, 30], [33, 23], [30, 61], [62, 45], [59, 119], [116, 90], [156, 198], [373, 326]], downsample_ratios: [32, 16, 8]}
# 训练时batch_size
batch_size: 24
# 读取数据是是否乱序
shuffle: true
# 是否丢弃最后不能完整组成batch的数据
drop_last: true
# mixup_epoch,大于最大epoch,表示训练过程一直使用mixup数据增广
mixup_epoch: 25000
# 是否通过共享内存进行数据读取加速,需要保证共享内存大小(如/dev/shm)满足大于1G
use_shared_memory: true

# 评估数据
EvalReader:
# 评估数据transforms
sample_transforms:
- Decode: {}
- Resize: {target_size: [608, 608], keep_ratio: False, interp: 2}
- NormalizeImage: {mean: [0.485, 0.456, 0.406], std: [0.229, 0.224, 0.225], is_scale: True}
- Permute: {}
# 评估时batch_size
batch_size: 8
# 是否丢弃没有标注的数据
drop_empty: false

# 测试数据
TestReader:
inputs_def:
image_shape: [3, 608, 608]
# 测试数据transforms
sample_transforms:
- Decode: {}
- Resize: {target_size: [608, 608], keep_ratio: False, interp: 2}
- NormalizeImage: {mean: [0.485, 0.456, 0.406], std: [0.229, 0.224, 0.225], is_scale: True}
- Permute: {}
# 测试时batch_size
batch_size: 1
```

- 模型配置文件 `ppyolo_r50vd_dcn.yml`

```yaml
# 模型结构类型
architecture: YOLOv3
# 预训练模型地址
pretrain_weights: https://paddledet.bj.bcebos.com/models/pretrained/ResNet50_vd_ssld_pretrained.pdparams
# norm_type
norm_type: sync_bn
# 是否使用ema
use_ema: true
# ema_decay
ema_decay: 0.9998

# YOLOv3
YOLOv3:
# backbone
backbone: ResNet
# neck
neck: PPYOLOFPN
# yolo_head
yolo_head: YOLOv3Head
# post_process
post_process: BBoxPostProcess


# backbone
ResNet:
# depth
depth: 50
# variant
variant: d
# return_idx, 0 represent res2
return_idx: [1, 2, 3]
# dcn_v2_stages
dcn_v2_stages: [3]
# freeze_at
freeze_at: -1
# freeze_norm
freeze_norm: false
# norm_decay
norm_decay: 0.

# PPYOLOFPN
PPYOLOFPN:
# 是否coord_conv
coord_conv: true
# 是否drop_block
drop_block: true
# block_size
block_size: 3
# keep_prob
keep_prob: 0.9
# 是否spp
spp: true

# YOLOv3Head
YOLOv3Head:
# anchors
anchors: [[10, 13], [16, 30], [33, 23],
[30, 61], [62, 45], [59, 119],
[116, 90], [156, 198], [373, 326]]
# anchor_masks
anchor_masks: [[6, 7, 8], [3, 4, 5], [0, 1, 2]]
# loss
loss: YOLOv3Loss
# 是否使用iou_aware
iou_aware: true
# iou_aware_factor
iou_aware_factor: 0.4

# YOLOv3Loss
YOLOv3Loss:
# ignore_thresh
ignore_thresh: 0.7
# downsample
downsample: [32, 16, 8]
# 是否label_smooth
label_smooth: false
# scale_x_y
scale_x_y: 1.05
# iou_loss
iou_loss: IouLoss
# iou_aware_loss
iou_aware_loss: IouAwareLoss

# IouLoss
IouLoss:
loss_weight: 2.5
loss_square: true

# IouAwareLoss
IouAwareLoss:
loss_weight: 1.0

# BBoxPostProcess
BBoxPostProcess:
decode:
name: YOLOBox
conf_thresh: 0.01
downsample_ratio: 32
clip_bbox: true
scale_x_y: 1.05
# nms 配置
nms:
name: MatrixNMS
keep_top_k: 100
score_threshold: 0.01
post_threshold: 0.01
nms_top_k: -1
background_label: -1

```

- 运行时置文件 `runtime.yml`

```yaml
# 是否使用gpu
use_gpu: true
# 日志打印间隔
log_iter: 20
# save_dir
save_dir: output
# 模型保存间隔时间
snapshot_epoch: 1
```

原文地址:https://www.cnblogs.com/Elvis123/p/15060489.html