PaddleDetection项目代码结构简介
ppdet(PaddleDetection)是一个相当方便的实验框架,将神经网络的各部分模块化,非常适合对神经网络架构进行改进优化。
1. 项目目录
挑几个重点的目录介绍
- configs 存放网络配置文件
- ppdet 存放网络架构、backbone、head等文件
- tools 存放训练、预测、验证脚本
- dataset 数据集存放目录
- output 当执行完训练之后,会生成output文件夹并存放训练好的可训练参数
2. 神经网络的相关代码结构关系
举例说明
假如想要了解mask-rcnn(backbone为resnet-50+FPN),可查看configs目录下的mask_rcnn_r50_fpn_1x.yml配置文件。
2.1 配置文件
# architecture 说明这个网络使用的是哪个架构
# 架构目录为 ./ppdet/modeling/architectures
architecture: MaskRCNN
use_gpu: true
# 最大迭代次数
max_iters: 180000
# 每隔10000次保存一次权重
snapshot_iter: 10000
# 每20个iter打印一次loss
log_smooth_window: 20
# 训练参数保存目录
save_dir: output
# 预训练参数下载地址
pretrain_weights: https://paddle-imagenet-models-name.bj.bcebos.com/ResNet50_cos_pretrained.tar
metric: COCO
# 加载权重路径
weights: output/mask_rcnn_r50_fpn_1x/model_final
# 分类数量(类目+背景)
num_classes: 81
# 架构名称
MaskRCNN:
# 以下对应项目对象的参数,MaskRCNN的构造函数可看下面的图一
backbone: ResNet
fpn: FPN
rpn_head: FPNRPNHead
roi_extractor: FPNRoIAlign
bbox_head: BBoxHead
bbox_assigner: BBoxAssigner
# 以下为对应架构的输入,输入均为已注册的模块,如ResNet为./ppdet/modeling下的一个注册类
ResNet:
depth: 50
feature_maps: [2, 3, 4, 5]
freeze_at: 2
norm_type: bn
FPN:
max_level: 6
min_level: 2
num_chan: 256
spatial_scale: [0.03125, 0.0625, 0.125, 0.25]
FPNRPNHead:
anchor_generator:
aspect_ratios: [0.5, 1.0, 2.0]
variance: [1.0, 1.0, 1.0, 1.0]
anchor_start_size: 32
max_level: 6
min_level: 2
num_chan: 256
rpn_target_assign:
rpn_batch_size_per_im: 256
rpn_fg_fraction: 0.5
rpn_negative_overlap: 0.3
rpn_positive_overlap: 0.7
rpn_straddle_thresh: 0.0
train_proposal:
min_size: 0.0
nms_thresh: 0.7
pre_nms_top_n: 2000
post_nms_top_n: 2000
test_proposal:
min_size: 0.0
nms_thresh: 0.7
pre_nms_top_n: 1000
post_nms_top_n: 1000
FPNRoIAlign:
canconical_level: 4
canonical_size: 224
max_level: 5
min_level: 2
sampling_ratio: 2
box_resolution: 7
mask_resolution: 14
MaskHead:
dilation: 1
conv_dim: 256
num_convs: 4
resolution: 28
BBoxAssigner:
batch_size_per_im: 512
bbox_reg_weights: [0.1, 0.1, 0.2, 0.2]
bg_thresh_hi: 0.5
bg_thresh_lo: 0.0
fg_fraction: 0.25
fg_thresh: 0.5
MaskAssigner:
resolution: 28
BBoxHead:
head: TwoFCHead
nms:
keep_top_k: 100
nms_threshold: 0.5
score_threshold: 0.05
TwoFCHead:
mlp_dim: 1024
# 学习率配置
LearningRate:
# 初始学习率
base_lr: 0.01
# 学习率自动调整部分
schedulers:
- !PiecewiseDecay
gamma: 0.1
# 当前总iter为180000,当iter等于120000和160000时,当前学习率乘以gamma
# 设初始学习率为0.01,到120000时学习率为0.001,到160000是为0.0001
milestones: [120000, 160000]
- !LinearWarmup
# 设置LinearWarmup后,初始学习率为0.3333333333333333
# 经过500个iter后,学习率恢复到0.1
# 学习率优化策略-线性学习率热身
# 论文:Bag of Tricks for Image Classification with Convolutional Neural Networks
# paper:https://arxiv.org/abs/1812.01187
start_factor: 0.3333333333333333
steps: 500
OptimizerBuilder:
optimizer:
momentum: 0.9
type: Momentum
regularizer:
factor: 0.0001
type: L2
# reader配置文件
_READER_: 'mask_fpn_reader.yml'
图一、 MaskRCNN构造函数
2.2 reader配置文件
从上一步知道当前网络的reader文件为mask_fpn_reader.yml,该文件一般与网络配置文件同层。reader代表为数据集的读取器。
# 注意:当检查dataset/coco下没有对应的数据集时,就会自动下载对应的数据集,如果已有现成的数据集,可通过修改dataset_dir、anno_path、image_dir参数
# annotations文件路径为:dataset_dir + '/' + anno_path
# 即: ./dataset/coco/annotations/instances_train2017.json
# 图片目录则为:dataset_dir + '/' + image_dir
# 即:./dataset/coco/train2017
# 执行训练脚本时的reader
TrainReader:
inputs_def:
fields: ['image', 'im_info', 'im_id', 'gt_bbox', 'gt_class', 'is_crowd', 'gt_mask']
dataset:
!COCODataSet
image_dir: train2017
anno_path: annotations/instances_train2017.json
dataset_dir: dataset/coco
sample_transforms:
- !DecodeImage
to_rgb: true
- !RandomFlipImage
prob: 0.5
is_mask_flip: true
- !NormalizeImage
is_channel_first: false
is_scale: true
mean: [0.485,0.456,0.406]
std: [0.229, 0.224,0.225]
- !ResizeImage
target_size: 800
max_size: 1333
interp: 1
use_cv2: true
- !Permute
to_bgr: false
channel_first: true
batch_transforms:
- !PadBatch
pad_to_stride: 32
use_padded_im_info: false
batch_size: 1
shuffle: true
worker_num: 2
drop_last: false
use_process: false
# 执行验证脚本时的reader
EvalReader:
inputs_def:
fields: ['image', 'im_info', 'im_id', 'im_shape']
# for voc
#fields: ['image', 'im_info', 'im_id', 'gt_bbox', 'gt_class', 'is_difficult']
dataset:
!COCODataSet
image_dir: val2017
anno_path: annotations/instances_val2017.json
dataset_dir: dataset/coco
sample_transforms:
- !DecodeImage
to_rgb: true
- !NormalizeImage
is_channel_first: false
is_scale: true
mean: [0.485,0.456,0.406]
std: [0.229, 0.224,0.225]
- !ResizeImage
interp: 1
max_size: 1333
target_size: 800
use_cv2: true
- !Permute
channel_first: true
to_bgr: false
batch_transforms:
- !PadBatch
pad_to_stride: 32
use_padded_im_info: true
batch_size: 1
shuffle: false
drop_last: false
drop_empty: false
worker_num: 2
# 执行测试脚本时的reader
TestReader:
inputs_def:
fields: ['image', 'im_info', 'im_id', 'im_shape']
dataset:
!ImageFolder
# 预测需要该json提供id与label的对应关系
anno_path: annotations/instances_val2017.json
sample_transforms:
- !DecodeImage
to_rgb: true
with_mixup: false
- !NormalizeImage
is_channel_first: false
is_scale: true
mean: [0.485,0.456,0.406]
std: [0.229, 0.224,0.225]
- !ResizeImage
interp: 1
max_size: 1333
target_size: 800
use_cv2: true
- !Permute
channel_first: true
to_bgr: false
batch_transforms:
- !PadBatch
pad_to_stride: 32
use_padded_im_info: true
batch_size: 1
shuffle: false
drop_last: false
2.3 结构代码
结构代码均存放在./ppdet/modeling下,MaskRCNN的架构与子结构都能在里面找到对应的类。
图二、项目结构代码