1 项目背景

一架无人机穿梭在电力线路和杆塔之间,对输电线路进行高精度拍照,拍摄完成的照片利用算法进行高倍率压缩,并通过无人机自带的无线通讯能力,结合杆塔的复合光纤架空地线,快速传输到后端机房设备中,解压缩后,恢复的图片与拍摄完成的原始图片几乎无异,通过人工智能对图片进行分析,快速生成诊断结果发送给工作人员。这样的电力巡检模式不但更有效率,也很好地避免了人工巡检过程中存在的各种安全隐患,这也成为许多国家在电力行业数字化转型中努力实现的目标。

2 目标检测

浓烟火灾500张安全帽500张仪表500张

2.1 数据集简介

数据集来自电力巡检场景,包括火灾浓烟、安全帽佩戴和电力仪表,共计1500个样本。

# 数据集解压缩:已将数据集放置于work/下,可解压数据集到实际项目路径
!unzip work/cocome_1500.zip -d ./work/

2.2 目标检测训练环境

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

PP-YOLOE+_l在COCO test-dev2017达到了53.3的mAP, 同时其速度在Tesla V100上达到了78.1 FPS。本项目会在PP-YOLOE+_s模型上进行电力巡检场景数据集的端到端应用。

# 可直接使用work/中的detection套件
!unzip work/PaddleDetection-release-2.5.zip -d ./work/
# 如何需要更新套件版本,可在代码仓库克隆代码
!cd work && git clone https://gitee.com/paddlepaddle/PaddleDetection.git
# 更改项目文件名称
! mv work/PaddleDetection-release-2.5 work/PaddleDetection
# 把coco格式数据集移动到项目路径
! mv work/cocome_1500 work/PaddleDetection/coco

2.3 模型训练

注意:PP-YOLOE模型训练过程中使用8 GPUs进行混合精度训练,如果GPU卡数或者batch size发生了改变,你需要按照公式 lrnew = lrdefault * (batch_sizenew * GPU_numbernew) / (batch_sizedefault * GPU_numberdefault) 调整学习率。

# 安装依赖
! cd work/PaddleDetection && pip install -r requirements.txt
# 下载预训练模型前创建文件夹
! cd work/PaddleDetection && mkdir pretrained
# 下载预训练模型
! cd work/PaddleDetection/pretrained && wget https://bj.bcebos.com/v1/paddledet/models/pretrained/ppyoloe_crn_s_obj365_pretrained.pdparams

配置文件.yml在work/下,具体内容如下:

_BASE_: [
'../datasets/coco_detection.yml',
'../runtime.yml',
'./_base_/optimizer_80e.yml',
'./_base_/ppyoloe_plus_crn.yml',
'./_base_/ppyoloe_plus_reader.yml',
]

log_iter: 100
snapshot_epoch: 5
weights: output/ppyoloe_plus_crn_s_80e_coco_1500/model_final

metric: COCO
num_classes: 8 # 8个labels

LearningRate:
base_lr: 0.00025  # 8卡变1卡,除以8 0.001/8=0.000125,batch_size增大为16,再乘以2

#pretrain_weights: https://bj.bcebos.com/v1/paddledet/models/pretrained/ppyoloe_crn_s_obj365_pretrained.pdparams
pretrain_weights: pretrained/ppyoloe_crn_s_obj365_pretrained.pdparams
depth_mult: 0.33
width_mult: 0.50

TrainReader:
batch_size: 16  # 由8改为16

EvalReader:
batch_size: 1

TrainDataset:
!COCODataSet
image_dir: train
anno_path: annotations/voc_train.json
dataset_dir: coco
data_fields: ['image', 'gt_bbox', 'gt_class', 'is_crowd']

EvalDataset:
!COCODataSet
image_dir: val
anno_path: annotations/voc_val.json
dataset_dir: coco

TestDataset:
!ImageFolder
anno_path: annotations/voc_val.json # also support txt (like VOC's label_list.txt)
dataset_dir: coco # if set, anno_path will be 'dataset_dir/anno_path'
# 开始训练
! cd work/PaddleDetection && CUDA_VISIBLE_DEVICES=0 python tools/train.py -c configs/ppyoloe/ppyoloe_plus_crn_s_80e_coco_1500.yml --use_vdl=true --vdl_log_dir=vdl_dir/ppyoloe_plus_crn_s_80e_coco_1500 --eval

2.4 模型评估与预测

# 模型评估
%cd work/PaddleDetection
! python tools/eval.py -c configs/ppyoloe/ppyoloe_plus_crn_s_80e_coco_1500.yml -o use_gpu=true
/home/aistudio/work/PaddleDetection
Warning: import ppdet from source directory without installing, run 'python setup.py install' to install ppdet firstly
W1206 23:06:37.394459 28091 gpu_resources.cc:61] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 11.2
W1206 23:06:37.399641 28091 gpu_resources.cc:91] device: 0, cuDNN Version: 8.2.
loading annotations into memory...
Done (t=0.00s)
creating index...
index created!
[12/06 23:06:40] ppdet.utils.checkpoint INFO: Finish loading model weights: output/ppyoloe_plus_crn_s_80e_coco_1500/model_final.pdparams
[12/06 23:06:41] ppdet.engine INFO: Eval iter: 0
[12/06 23:06:44] ppdet.engine INFO: Eval iter: 100
[12/06 23:06:48] ppdet.data.transform.operators WARNING: The actual image height: 663 is not equal to the height: 0.0 in annotation, and update sample['h'] by actual image height.
[12/06 23:06:48] ppdet.data.transform.operators WARNING: The actual image width: 949 is not equal to the width: 0.0 in annotation, and update sample['w'] by actual image width.
[12/06 23:06:48] ppdet.engine INFO: Eval iter: 200
[12/06 23:06:53] ppdet.metrics.metrics INFO: The bbox result is saved to bbox.json.
loading annotations into memory...
Done (t=0.00s)
creating index...
index created!
[12/06 23:06:53] ppdet.metrics.coco_utils INFO: Start evaluate...
Loading and preparing results...
DONE (t=1.05s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type *bbox*
DONE (t=3.29s).
Accumulating evaluation results...
DONE (t=0.67s).
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.713
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.926
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.790
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.430
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.696
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.793
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.492
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.768
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.795
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.606
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.770
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.865
[12/06 23:06:58] ppdet.engine INFO: Total sample number: 299, averge FPS: 24.662211738423768
# 模型预测
! python tools/infer.py -c configs/ppyoloe/ppyoloe_plus_crn_s_80e_coco_1500.yml -o use_gpu=true --infer_dir=test/
Warning: import ppdet from source directory without installing, run 'python setup.py install' to install ppdet firstly
W1206 23:07:37.652858 28323 gpu_resources.cc:61] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 11.2
W1206 23:07:37.658515 28323 gpu_resources.cc:91] device: 0, cuDNN Version: 8.2.
[12/06 23:07:39] ppdet.utils.checkpoint INFO: Finish loading model weights: output/ppyoloe_plus_crn_s_80e_coco_1500/model_final.pdparams
[12/06 23:07:39] train INFO: Found 6 inference images in total.
loading annotations into memory...
Done (t=0.00s)
creating index...
index created!
100%|█████████████████████████████████████████████| 6/6 [00:01<00:00,  3.55it/s]
[12/06 23:07:41] ppdet.engine INFO: Detection bbox results save in output/2_fire_298.jpg
[12/06 23:07:41] ppdet.engine INFO: Detection bbox results save in output/03.png
[12/06 23:07:41] ppdet.engine INFO: Detection bbox results save in output/04.png
[12/06 23:07:42] ppdet.engine INFO: Detection bbox results save in output/4_meter_00415.jpg
[12/06 23:07:42] ppdet.engine INFO: Detection bbox results save in output/06.png
[12/06 23:07:42] ppdet.engine INFO: Detection bbox results save in output/02.png

2.5 训练结果可视化

训练时开启了visualDL,所以可以很方便的查看训练过程中的各个曲线。

2.6 模型导出

模型训练得到的.pdparams权重文件包括了模型的参数数据,实际部署只需要前向推理参数,这里执行模型导出。
注意:如果没有指定–output_dir,那么导出的模型将默认存储在output_inference/路径下。

# 导出模型
! python tools/export_model.py -c configs/ppyoloe/ppyoloe_plus_crn_s_80e_coco_1500.yml -o weights=output/ppyoloe_plus_crn_s_80e_coco_1500/best_model.pdparams
Warning: import ppdet from source directory without installing, run 'python setup.py install' to install ppdet firstly
[12/06 23:20:06] ppdet.utils.checkpoint INFO: Finish loading model weights: output/ppyoloe_plus_crn_s_80e_coco_1500/best_model.pdparams
loading annotations into memory...
Done (t=0.00s)
creating index...
index created!
[12/06 23:20:06] ppdet.engine INFO: Export inference config file to output_inference/ppyoloe_plus_crn_s_80e_coco_1500/infer_cfg.yml
[12/06 23:20:11] ppdet.engine INFO: Export model and saved in output_inference/ppyoloe_plus_crn_s_80e_coco_1500

3 推理部署

# 推理部署
! python deploy/python/infer.py --model_dir=output_inference/ppyoloe_plus_crn_s_80e_coco_1500 --image_dir=test/ --run_mode=paddle --device=gpu
-----------  Running Arguments -----------
action_file: None
batch_size: 1
camera_id: -1
combine_method: nms
cpu_threads: 1
device: gpu
enable_mkldnn: False
enable_mkldnn_bfloat16: False
image_dir: test/
image_file: None
match_metric: ios
match_threshold: 0.6
model_dir: output_inference/ppyoloe_plus_crn_s_80e_coco_1500
output_dir: output
overlap_ratio: [0.25, 0.25]
random_pad: False
reid_batch_size: 50
reid_model_dir: None
run_benchmark: False
run_mode: paddle
save_images: True
save_mot_txt_per_img: False
save_mot_txts: False
save_results: False
scaled: False
slice_infer: False
slice_size: [640, 640]
threshold: 0.5
tracker_config: None
trt_calib_mode: False
trt_max_shape: 1280
trt_min_shape: 1
trt_opt_shape: 640
use_coco_category: False
use_dark: True
use_gpu: False
video_file: None
window_size: 50
------------------------------------------
-----------  Model Configuration -----------
Model Arch: YOLO
Transform Order: 
--transform op: Resize
--transform op: NormalizeImage
--transform op: Permute
--------------------------------------------
Found 6 inference images in total.
class_id:6, confidence:0.8285, left_top:[230.61,44.29],right_bottom:[416.27,231.86]
class_id:6, confidence:0.6354, left_top:[523.24,41.23],right_bottom:[641.42,231.07]
save result to: output/04.png
Test iter 0
class_id:6, confidence:0.9672, left_top:[168.01,332.01],right_bottom:[304.75,537.80]
class_id:6, confidence:0.8858, left_top:[200.03,58.23],right_bottom:[284.54,142.17]
save result to: output/4_meter_00415.jpg
Test iter 1
class_id:0, confidence:0.7039, left_top:[372.23,181.50],right_bottom:[465.63,240.07]
save result to: output/02.png
Test iter 2
class_id:1, confidence:0.8960, left_top:[315.53,261.07],right_bottom:[429.25,387.93]
class_id:1, confidence:0.8838, left_top:[569.86,248.37],right_bottom:[677.83,373.89]
save result to: output/03.png
Test iter 3
class_id:1, confidence:0.6965, left_top:[46.36,84.20],right_bottom:[160.41,208.57]
class_id:1, confidence:0.6853, left_top:[173.51,121.14],right_bottom:[255.31,226.07]
class_id:2, confidence:0.8218, left_top:[292.84,125.40],right_bottom:[351.82,199.10]
class_id:2, confidence:0.6286, left_top:[376.79,49.61],right_bottom:[483.15,162.08]
save result to: output/06.png
Test iter 4
class_id:0, confidence:0.7223, left_top:[354.05,501.98],right_bottom:[562.75,699.07]
save result to: output/2_fire_298.jpg
Test iter 5
------------------ Inference Time Info ----------------------
total_time(ms): 1458.3999999999999, img_num: 6
average latency time(ms): 243.07, QPS: 4.114098
preprocess_time(ms): 18.40, inference_time(ms): 224.60, postprocess_time(ms): 0.10

average latency time(ms): 243.07, QPS: 4.114098
preprocess_time(ms): 18.40, inference_time(ms): 224.60, postprocess_time(ms): 0.10

推理结果:

浓烟火灾安全帽仪表

4 项目小结

本项目基于PaddleDetection提供的PPYOLOE+目标检测算法对电力巡检场景中的火灾烟雾、安全帽和仪表进行检测,介绍了一种端到端训练和部署方案,可实现该场景的落地应用。

关于作者

项目作者: 姓名:袁靖 AI Studio昵称:JimYuan336 个人主页

飞桨导师: 姓名:韩磊 AI Studio昵称:ninetailskim 个人主页

此文章为搬运
原项目链接

Logo

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

更多推荐