火灾烟雾检测

背景介绍

火灾初期通常会产生大量烟雾,在很大程度上降低了火焰检测的有效性,通过计算机视觉的方法对烟雾进行监控,可以实现早期火灾预警。计算能力的提高、存储设备的发展,使得传统视觉技术中存在的问题逐渐得到改善或解决。

项目概述

下面我们采用paddleDetection(如果大家喜欢记得给detection项目点star)套件为大家实现一个火灾烟雾检测。
对于数据的标注,我们主要标注了火场的烟和火。对于检测属于一个二分类的问题。

开始项目

Step1:安装环境

  • 使用时候请先务必检查一下自己的环境,本文采用paddle-gpu最新稳定版本,其他版本操作基本类似。如果环境满足,则可以直接跳过环境安装.
  • 如果大家是CPU环境,则安装paddle相应的版本即可
!python -m pip install paddlepaddle-gpu==2.2.0.post101 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Looking in links: https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html
Requirement already satisfied: paddlepaddle-gpu==2.2.0.post101 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (2.2.0.post101)
Requirement already satisfied: astor in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlepaddle-gpu==2.2.0.post101) (0.8.1)
Requirement already satisfied: numpy>=1.13; python_version >= "3.5" and platform_system != "Windows" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlepaddle-gpu==2.2.0.post101) (1.20.3)
Requirement already satisfied: protobuf>=3.1.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlepaddle-gpu==2.2.0.post101) (3.14.0)
Requirement already satisfied: requests>=2.20.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlepaddle-gpu==2.2.0.post101) (2.22.0)
Requirement already satisfied: decorator in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlepaddle-gpu==2.2.0.post101) (4.4.2)
Requirement already satisfied: Pillow in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlepaddle-gpu==2.2.0.post101) (7.1.2)
Requirement already satisfied: six in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlepaddle-gpu==2.2.0.post101) (1.15.0)
Requirement already satisfied: idna<2.9,>=2.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests>=2.20.0->paddlepaddle-gpu==2.2.0.post101) (2.8)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests>=2.20.0->paddlepaddle-gpu==2.2.0.post101) (1.25.6)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests>=2.20.0->paddlepaddle-gpu==2.2.0.post101) (2019.9.11)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests>=2.20.0->paddlepaddle-gpu==2.2.0.post101) (3.0.4)

Step2:安装PaddleDetection

  • 下载PaddleDetection并安装至Notebook
  • 安装PaddleDetection的相关依赖

进入工作目录,下载相关代码。如果大家从github上clone代码较慢,可以切换至gitee上下载。

!cd   /home/aistudio/work&&git clone https://gitee.com/PaddlePaddle/PaddleDetection.git -b develop
Cloning into 'PaddleDetection'...
remote: Enumerating objects: 760, done.[K
remote: Counting objects: 100% (760/760), done.[K
remote: Compressing objects: 100% (418/418), done.[K
remote: Total 20290 (delta 493), reused 508 (delta 342), pack-reused 19530[K
Receiving objects: 100% (20290/20290), 201.01 MiB | 29.73 MiB/s, done.
Resolving deltas: 100% (15042/15042), done.
Checking connectivity... done.
%cd   /home/aistudio/work/PaddleDetection/
/home/aistudio/work/PaddleDetection
! pip install -r requirements.txt    #安装项目必要依赖

Step3:上传数据集并解压

  • 本案例使用数据集包含MIT协议互联网下载图片和MIT协议公开数据集,共有6675张图片,分别检测烟雾和火灾,示例图片如下图所示:

数据集图片格式是VOC数据格式,VOC数据是每个图像文件对应一个同名的xml文件,xml文件内包含对应图片的基本信息,比如文件名、来源、图像尺寸以及图像中包含的物体区域信息和类别信息等。

本文采用的数据集来源于开源数据集,感兴趣的也可以本地下载进行训练。

%cd /home/aistudio/data/data118365/
/home/aistudio/data/data118365

解压data目录下的数据集

! unzip fire_smoke.zip
inflating: __MACOSX/fire_smoke/annotations/._smoke_01167.xml   267c8-0c0a-4244-8fcc-d2a7e9774387.xml

Step4:配置训练文件

  • PP-YOLOv2

相较20年发布的PP-YOLO,PP-YOLOv2版本在COCO 2017 test-dev上的精度提升了3.6个百分点,由45.9%提升到了49.5%;在640*640的输入尺寸下,FPS达到68.9FPS。 主要改进点:

  1. Path Aggregation Network
  2. Mish Activation Function
  3. Larger Input Size
  4. IoU Aware Branch
  • 配置ppyolov2_r50vd_dcn_voc.yml文件

路径 /home/aistudio/work/PaddleDetection/configs/ppyolo/ppyolov2r50vddcnvoc.yml
其中置文件中的epoch可以根据实际情况调整,如果原文件的设置,训练时间一般较长。

另外大家可以选用较小的网络比如mobilenetv3-small等。
本文配置为了演示,将下面的总epoch数目设置为10,训练时长大概3个小时左右。 如果大家想要提高检测精度可以根据实际情况增加epoch数目。

BASE_: [
  '../datasets/voc.yml',
  '../runtime.yml',
  './_base_/ppyolov2_r50vd_dcn.yml',
  './_base_/optimizer_365e.yml',
  './_base_/ppyolov2_reader.yml',
]

snapshot_epoch: 2    # 保存模型epoch间隔
weights: output/ppyolov2_r50vd_dcn_voc/model_final    # 模型权重路径配置

TrainReader:
  mixup_epoch: 6   #数据增强的epoch数目,根据实际情况调整
  batch_size: 12   # 批次数目也可以适当进行调整


EvalReader:
  collate_batch: false # 表示reader是否对gt进行组batch的操作,在rcnn系列算法中设置为false,得到的gt格式为list[Tensor]

epoch: 10  #总epoch数目,上面的eponch不能超过此处设置数目

LearningRate:
  base_lr: 0.00333
  schedulers:
  - !PiecewiseDecay
    gamma: 0.1
    milestones:
    - 466
    - 516
  - !LinearWarmup
    start_factor: 0.
    steps: 4000

OptimizerBuilder:
  optimizer:
    momentum: 0.9
    type: Momentum
  regularizer:
    factor: 0.0005
    type: L2
  • 配置voc.yml文件

配置数据读取路径 /home/aistudio/work/PaddleDetection/configs/datasets/voc.yml
比如:

metric: VOC
map_type: 11point
num_classes: 2

TrainDataset:
  !VOCDataSet
    dataset_dir: /home/aistudio/data/data118365/fire_smoke/   # 数据集所在的文件夹
    anno_path: train_list.txt    # 训练集文件名
    label_list: labels.txt   # 标签文件名
    data_fields: ['image', 'gt_bbox', 'gt_class', 'difficult']

EvalDataset:
  !VOCDataSet
    dataset_dir: /home/aistudio/data/data118365/fire_smoke/
    anno_path: val_list.txt   # 评估数据集文件名
    label_list: labels.txt
    data_fields: ['image', 'gt_bbox', 'gt_class', 'difficult']

Step5:模型训练

  • 进入相应路径
  • 启动单卡训练
  • 选择训练配置并执行训练命令
config_path = '/home/aistudio/work/PaddleDetection/configs/ppyolo/ppyolov2_r50vd_dcn_voc.yml'  # 配置文件设置
!export CUDA_VISIBLE_DEVICES=0   # 配置单卡,如果大家环境有多卡可以配置为 0,1,2,3

Tips: 上述两个命令也可以通过脚本来直接执行,配置完成后开始训练

!python /home/aistudio/work/PaddleDetection/tools/train.py -c $config_path   # 启动训练

由于输出内容较长本文仅截取最后结果作为展示:

javascript烟雾检测代码怎么写 烟雾检测电路_计算机视觉

Step6:模型评估

  • 设置模型路径
weight_path = '/home/aistudio/data/data118365/output/ppyolov2_r50vd_dcn_voc/model_final.pdparams'

Step7:模型预测

通过-c选项指定待分析模型的配置文件的相对路径

通过-o weights指定模型的权重,可以指定url或本地文件系统的路径

!python /home/aistudio/work/PaddleDetection/tools/eval.py -c $config_path -o weights=$weight_path
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py:130: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. 
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  if data.dtype == np.object:
Warning: import ppdet from source directory without installing, run 'python setup.py install' to install ppdet firstly
W1128 19:19:02.993319  5384 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W1128 19:19:02.998888  5384 device_context.cc:465] device: 0, cuDNN Version: 7.6.
[11/28 19:19:07] ppdet.utils.checkpoint INFO: Finish loading model weights: /home/aistudio/data/data118365/output/ppyolov2_r50vd_dcn_voc/model_final.pdparams
[11/28 19:19:08] ppdet.engine INFO: Eval iter: 0
[11/28 19:19:32] ppdet.metrics.metrics INFO: Accumulating evaluatation results...
[11/28 19:19:32] ppdet.metrics.metrics INFO: mAP(0.50, 11point) = 14.56%
[11/28 19:19:32] ppdet.engine INFO: Total sample number: 667, averge FPS: 26.75302856086791

设置预测图片路径

  • 可以自定义图片路径

由于模型评估后精度不高(当前只是简单训练,训练epoch较低),这里预测模型设置置信度在0.1以上的火灾烟雾检测

img_path = '/home/aistudio/work/images/fire_work.png'   # 待预测图片路径
thresh = 0.2  # 设置预测的阈值,可以根据实际情况进行调整,设置过大,可能预测结果为空,即图片没有任何的检测结果

模型预测

!python /home/aistudio/work/PaddleDetection/tools/infer.py -c $config_path -o weights=$weight_path -o use_gpu=True --draw_threshold=$thresh --infer_img=$img_path
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py:130: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. 
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  if data.dtype == np.object:
Warning: import ppdet from source directory without installing, run 'python setup.py install' to install ppdet firstly
W1128 19:21:40.539165  5722 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W1128 19:21:40.544663  5722 device_context.cc:465] device: 0, cuDNN Version: 7.6.
[11/28 19:21:44] ppdet.utils.checkpoint INFO: Finish loading model weights: output/ppyolov2_r50vd_dcn_voc/model_final.pdparams
[11/28 19:21:44] ppdet.engine INFO: Detection bbox results save in output/fire_work.png

最后可以打开预测结果,类似于这样

javascript烟雾检测代码怎么写 烟雾检测电路_paddlepaddle_02

总结

本案例只是简单的向大家展示了如何构建一个火灾烟雾检测检测的案例。案例中使用的backbone为resnet-50,如果是为了部署,可以采用一些轻量级的网络。另外也可以做一些数据增强的效果,丰富数据,也可以提高检测精度。大家如果有疑问可以一起交流探讨。