★★★ 本文源自AlStudio社区精品项目,【点击此处】查看更多精品内容 >>>

一、项目背景

飞浆框架中的PaddleDetection套件可以很好的帮助我们解决目标检测任务.

PP-YOLOE是基于PP-YOLOv2的卓越的单阶段Anchor-free模型,超越了多种流行的YOLO模型。PP-YOLOE有一系列的模型,即s/m/l/x,可以通过width multiplier和depth multiplier配置。PP-YOLOE避免了使用诸如Deformable Convolution或者Matrix NMS之类的特殊算子,以使其能轻松地部署在多种多样的硬件上,总结下来,PPYOLOE模型就是又快又好。

因此,本文采用PaddleDetection套件中的PP-YOLOE模型在钢铁缺陷检测数据集中进行训练与推理

根据PaddleDetection给出的云端模型性能对比,各模型结构和骨干网络的代表模型在COCO数据集上精度mAP和单卡Tesla V100上预测速度(FPS)对比图如下:

https://gitee.com/paddlepaddle/PaddleDetection/raw/develop/docs/images/fps_map.png

二、环境准备

2.1 数据准备

本次采用钢铁缺陷检测数据集COCO格式进行训练

# 解压数据集(仅运行一次)
!tar -xvf data/data206842/NEU-DET-coco.tar -C ./data/
# 组织数据目录
!mkdir MyDataset
!mkdir MyDataset/JPEGImages
!mkdir MyDataset/annotations
!mv data/NEU-DET-COCO/train/* MyDataset/JPEGImages/
!mv data/NEU-DET-COCO/test/* MyDataset/JPEGImages/
!mv data/NEU-DET-COCO/val/* MyDataset/JPEGImages/
!mv data/NEU-DET-COCO/annotations/* MyDataset/annotations/

2.2 安装PaddleDetection以及依赖

!git clone https://gitee.com/paddlepaddle/PaddleDetection.git -b release/2.5
正克隆到 'PaddleDetection'...
remote: Enumerating objects: 255205, done.[K
remote: Counting objects: 100% (202/202), done.[K
remote: Compressing objects: 100% (152/152), done.[K
remote: Total 255205 (delta 74), reused 156 (delta 50), pack-reused 255003[K
接收对象中: 100% (255205/255205), 412.30 MiB | 7.05 MiB/s, 完成.
处理 delta 中: 100% (209102/209102), 完成.
检查连接... 完成。
%cd PaddleDetection
/home/aistudio/PaddleDetection
# 安装依赖
!pip install -r requirements.txt --user
!python setup.py install --user

三、模型选型

PaddleDetection提供了多种模型进行选择,具体的模型选型可以根据自己数据集的情况选取精度与速度相适应的模型,本次采用ppyoloe_crn_l_300e_coco

3.1 找到配置文件

配置文件在PaddleDetection/configs/ppyoloe/ppyoloe_crn_l_300e_coco.yml,主要包含五个部分,为了方便更改,将其copy到根目录下

3.2 修改配置文件

找到配置文件后,需要对其进行修改,参考链接https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.6/docs/advanced_tutorials/customization/detection.md

在这里主要修改:

  • 预训练模型改为 pretrain_weights: https://paddledet.bj.bcebos.com/models/ppyoloe_crn_l_300e_coco.pdparams 加速模型收敛
  • coco_detection 文件修改数据集路径
  • 学习率训练轮数等超参数
# 训练配置文件覆盖
!cp ../ppyoloe_crn_l_300e_coco.yml configs/ppyoloe/ppyoloe_crn_l_300e_coco.yml
!cp ../coco_detection.yml configs/datasets/coco_detection.yml
!cp ../runtime.yml configs/runtime.yml
!cp ../optimizer_300e.yml configs/ppyoloe/_base_/optimizer_300e.yml
!cp ../ppyoloe_crn.yml configs/ppyoloe/_base_/ppyoloe_crn.yml
!cp ../ppyoloe_reader.yml configs/ppyoloe/_base_/ppyoloe_reader.yml

四、模型训练

训练100轮map为0.44

# 开始训练
# 恢复训练-r output/ppyoloe_crn_l_alpha_largesize_80e_visdrone/best_model 
!python tools/train.py -c configs/ppyoloe/ppyoloe_crn_l_300e_coco.yml --use_vdl=True --vdl_log_dir=./smalldet/visdrone/ --eval

4.1 训练20轮模型评估

4.2 可视化



五、预测推理



# 挑一张验证集的图片展示预测效果
!python tools/infer.py \
-c configs/ppyoloe/ppyoloe_crn_l_300e_coco.yml \
-o weights=output/ppyoloe_crn_l_300e_coco/best_model \
--infer_img=../MyDataset/JPEGImages/rolled-in_scale_139.jpg \
--save_results=True

六、导出部署模型

!python tools/export_model.py \
-c configs/ppyoloe/ppyoloe_crn_l_300e_coco.yml \
-o weights=output/ppyoloe_crn_l_300e_coco/best_model
[04/08 16:00:45] ppdet.utils.checkpoint INFO: Finish loading model weights: output/ppyoloe_crn_l_300e_coco/best_model.pdparams
[04/08 16:00:46] ppdet.data.source.category WARNING: anno_file '/home/aistudio/MyDataset/val.json' is None or not set or not exist, please recheck TrainDataset/EvalDataset/TestDataset.anno_path, otherwise the default categories will be used by metric_type.
[04/08 16:00:46] ppdet.data.source.category WARNING: metric_type: COCO, load default categories of COCO.
[04/08 16:00:46] ppdet.engine INFO: Export inference config file to output_inference/ppyoloe_crn_l_300e_coco/infer_cfg.yml
[04/08 16:00:52] ppdet.engine INFO: Export model and saved in output_inference/ppyoloe_crn_l_300e_cocon_l_300e_coco

此文章为搬运
原项目链接

Logo

学大模型,用大模型上飞桨星河社区!每天8点V100G算力免费领!免费领取ERNIE 4.0 100w Token >>>

更多推荐