[PP-YOLOV2保姆级教程]使用自定义数据集实现吸烟识别预测

[PP-YOLOV2保姆级教程]使用自定义数据集实现吸烟识别预测

PP-YOLOV2介绍

通过结合多项有效的改进,在COCO2017上将PP-YOLO的性能从45.9%mAP提升到49.5%mAP。称之为PP-YOLOv2。

在速度方面,PP-YOLOv2在640x640输入尺寸下以68.9FPS的速度运行。具有TensorRT,FP16精度和Batch=1的Paddle推理引擎进一步提高了PP-YOLOv2的推理速度,达到了106.5 FPS。这样的性能超过了具有大致相同数量的参数(即YOLOv4-CSP,YOLOv5l)的现有目标检测器。此外,带有ResNet101的PP-YOLOv2在COCO2017测试开发上达到了50.3%的mAP。

[PP-YOLOV2保姆级教程]使用自定义数据集实现吸烟识别预测_第1张图片

项目地址:[PP-YOLOV2保姆级教程]使用自定义数据集实现吸烟识别预测

手把手教你在AIStudio平台上使用PaddleDetection API训练自己的数据集

教程目的: 通过PaddleDetection API熟悉经典的目标检测框架,为下一步自己手写实现目标检测网络打基础

教程内容: 以PaddleDetection API中的特色模型为例,介绍网络结构,以及如何使用此框架训练自己的数据集

数据准备: 本教程程基于pp_fall数据集,为方便读者体验,已经上传至 data/data94796/pp_smoke.zip。

PaddelDetection: 为方便读者体验,存放在PaddleDetection,使用PP-YOLOV2。

其他说明: 本教程所有命令均在Notebook中执行。

检测效果

[PP-YOLOV2保姆级教程]使用自定义数据集实现吸烟识别预测_第2张图片

做这个项目的缘由:

看了许多PP-YOLOV2相关的项目,发现许多用来制作数据集都是COCO格式,这对于我这种习惯用VOC格式的人来说,就相对有点别扭,因此想自己弄一个VOC数据集格式的PP-YOLOV2,顺便熟悉一下PP-YOLOV2。

实验步骤:

一、解压数据集

!unzip data/data94796/pp_smoke.zip
  inflating: images/smoke_a970.jpg   

二、安装PaddleDetection包

最新的PaddleDetection包我已经放在了/home/aistudio/data/data98540/PaddleDetection-release-2.1.zip,解压后,安装相关的模块即可

!unzip -oq /home/aistudio/data/data98540/PaddleDetection-release-2.1.zip
!pip install -r /home/aistudio/PaddleDetection-release-2.1/requirements.txt
Looking in indexes: https://mirror.baidu.com/pypi/simple/
Requirement already satisfied: tqdm in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from -r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 1)) (4.36.1)
Collecting typeguard (from -r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 2))
  Downloading https://mirror.baidu.com/pypi/packages/a0/88/2a1613174e7d05540358b2f19881f369bfe6ba737f0a673177e69eb623df/typeguard-2.12.1-py3-none-any.whl
Requirement already satisfied: visualdl>=2.1.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from -r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (2.2.0)
Requirement already satisfied: opencv-python in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from -r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 4)) (4.1.1.26)
Requirement already satisfied: PyYAML in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from -r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 5)) (5.1.2)
Collecting shapely (from -r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 6))
[?25l  Downloading https://mirror.baidu.com/pypi/packages/98/f8/db4d3426a1aba9d5dfcc83ed5a3e2935d2b1deb73d350642931791a61c37/Shapely-1.7.1-cp37-cp37m-manylinux1_x86_64.whl (1.0MB)
[K     |████████████████████████████████| 1.0MB 17.2MB/s eta 0:00:01
[?25hRequirement already satisfied: scipy in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from -r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 7)) (1.6.3)
Collecting terminaltables (from -r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 8))
  Downloading https://mirror.baidu.com/pypi/packages/9b/c4/4a21174f32f8a7e1104798c445dacdc1d4df86f2f26722767034e4de4bff/terminaltables-3.1.0.tar.gz
Requirement already satisfied: Cython in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from -r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 9)) (0.29)
Collecting pycocotools (from -r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 10))
  Downloading https://mirror.baidu.com/pypi/packages/de/df/056875d697c45182ed6d2ae21f62015896fdb841906fe48e7268e791c467/pycocotools-2.0.2.tar.gz
Requirement already satisfied: setuptools>=42.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from -r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 12)) (56.2.0)
Collecting lap (from -r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 13))
[?25l  Downloading https://mirror.baidu.com/pypi/packages/bf/64/d9fb6a75b15e783952b2fec6970f033462e67db32dc43dfbb404c14e91c2/lap-0.4.0.tar.gz (1.5MB)
[K     |████████████████████████████████| 1.5MB 12.8MB/s eta 0:00:01
[?25hRequirement already satisfied: sklearn in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from -r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 14)) (0.0)
Collecting motmetrics (from -r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 15))
[?25l  Downloading https://mirror.baidu.com/pypi/packages/9c/28/9c3bc8e2a87f4c9e7b04ab72856ec7f9895a66681a65973ffaf9562ef879/motmetrics-1.2.0-py3-none-any.whl (151kB)
[K     |████████████████████████████████| 153kB 22.1MB/s eta 0:00:01
[?25hRequirement already satisfied: openpyxl in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from -r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 16)) (3.0.5)
Requirement already satisfied: numpy in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (1.20.3)
Requirement already satisfied: requests in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (2.22.0)
Requirement already satisfied: bce-python-sdk in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (0.8.53)
Requirement already satisfied: flake8>=3.7.9 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (3.8.2)
Requirement already satisfied: flask>=1.1.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (1.1.1)
Requirement already satisfied: shellcheck-py in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (0.7.1.1)
Requirement already satisfied: Flask-Babel>=1.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (1.0.0)
Requirement already satisfied: six>=1.14.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (1.15.0)
Requirement already satisfied: pre-commit in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (1.21.0)
Requirement already satisfied: matplotlib in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (2.2.3)
Requirement already satisfied: protobuf>=3.11.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (3.14.0)
Requirement already satisfied: pandas in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (1.1.5)
Requirement already satisfied: Pillow>=7.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (7.1.2)
Requirement already satisfied: scikit-learn in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from sklearn->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 14)) (0.24.2)
Collecting flake8-import-order (from motmetrics->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 15))
  Downloading https://mirror.baidu.com/pypi/packages/ab/52/cf2d6e2c505644ca06de2f6f3546f1e4f2b7be34246c9e0757c6048868f9/flake8_import_order-0.18.1-py2.py3-none-any.whl
Collecting pytest-benchmark (from motmetrics->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 15))
[?25l  Downloading https://mirror.baidu.com/pypi/packages/2c/60/423a63fb190a0483d049786a121bd3dfd7d93bb5ff1bb5b5cd13e5df99a7/pytest_benchmark-3.4.1-py2.py3-none-any.whl (50kB)
[K     |████████████████████████████████| 51kB 17.0MB/s eta 0:00:01
[?25hCollecting pytest (from motmetrics->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 15))
[?25l  Downloading https://mirror.baidu.com/pypi/packages/a1/59/6821e900592fbe261f19d67e4def0cb27e52ef8ed16d9922c144961cc1ee/pytest-6.2.4-py3-none-any.whl (280kB)
[K     |████████████████████████████████| 286kB 23.6MB/s eta 0:00:01
[?25hCollecting xmltodict>=0.12.0 (from motmetrics->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 15))
  Downloading https://mirror.baidu.com/pypi/packages/28/fd/30d5c1d3ac29ce229f6bdc40bbc20b28f716e8b363140c26eff19122d8a5/xmltodict-0.12.0-py2.py3-none-any.whl
Requirement already satisfied: et-xmlfile in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from openpyxl->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 16)) (1.0.1)
Requirement already satisfied: jdcal in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from openpyxl->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 16)) (1.4.1)
Requirement already satisfied: idna<2.9,>=2.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (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->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (1.25.6)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (2019.9.11)
Requirement already satisfied: pycryptodome>=3.8.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from bce-python-sdk->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (3.9.9)
Requirement already satisfied: future>=0.6.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from bce-python-sdk->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (0.18.0)
Requirement already satisfied: pyflakes<2.3.0,>=2.2.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8>=3.7.9->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (2.2.0)
Requirement already satisfied: mccabe<0.7.0,>=0.6.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8>=3.7.9->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (0.6.1)
Requirement already satisfied: importlib-metadata; python_version < "3.8" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8>=3.7.9->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (0.23)
Requirement already satisfied: pycodestyle<2.7.0,>=2.6.0a1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8>=3.7.9->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (2.6.0)
Requirement already satisfied: click>=5.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.1->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (7.0)
Requirement already satisfied: Jinja2>=2.10.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.1->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (2.10.1)
Requirement already satisfied: itsdangerous>=0.24 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.1->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (1.1.0)
Requirement already satisfied: Werkzeug>=0.15 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.1->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (0.16.0)
Requirement already satisfied: pytz in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Flask-Babel>=1.0.0->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (2019.3)
Requirement already satisfied: Babel>=2.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Flask-Babel>=1.0.0->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (2.8.0)
Requirement already satisfied: toml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (0.10.0)
Requirement already satisfied: virtualenv>=15.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (16.7.9)
Requirement already satisfied: aspy.yaml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (1.3.0)
Requirement already satisfied: identify>=1.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (1.4.10)
Requirement already satisfied: nodeenv>=0.11.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (1.3.4)
Requirement already satisfied: cfgv>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (2.0.1)
Requirement already satisfied: cycler>=0.10 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (0.10.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (2.4.2)
Requirement already satisfied: kiwisolver>=1.0.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (1.1.0)
Requirement already satisfied: python-dateutil>=2.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (2.8.0)
Requirement already satisfied: joblib>=0.11 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from scikit-learn->sklearn->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 14)) (0.14.1)
Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from scikit-learn->sklearn->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 14)) (2.1.0)
Collecting py-cpuinfo (from pytest-benchmark->motmetrics->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 15))
[?25l  Downloading https://mirror.baidu.com/pypi/packages/e6/ba/77120e44cbe9719152415b97d5bfb29f4053ee987d6cb63f55ce7d50fadc/py-cpuinfo-8.0.0.tar.gz (99kB)
[K     |████████████████████████████████| 102kB 24.6MB/s ta 0:00:01
[?25hRequirement already satisfied: packaging in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pytest->motmetrics->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 15)) (20.9)
Collecting py>=1.8.2 (from pytest->motmetrics->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 15))
[?25l  Downloading https://mirror.baidu.com/pypi/packages/67/32/6fe01cfc3d1a27c92fdbcdfc3f67856da8cbadf0dd9f2e18055202b2dc62/py-1.10.0-py2.py3-none-any.whl (97kB)
[K     |████████████████████████████████| 102kB 25.4MB/s ta 0:00:01
[?25hCollecting iniconfig (from pytest->motmetrics->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 15))
  Downloading https://mirror.baidu.com/pypi/packages/9b/dd/b3c12c6d707058fa947864b67f0c4e0c39ef8610988d7baea9578f3c48f3/iniconfig-1.1.1-py2.py3-none-any.whl
Requirement already satisfied: attrs>=19.2.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pytest->motmetrics->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 15)) (19.2.0)
Requirement already satisfied: pluggy<1.0.0a1,>=0.12 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pytest->motmetrics->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 15)) (0.13.1)
Requirement already satisfied: zipp>=0.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from importlib-metadata; python_version < "3.8"->flake8>=3.7.9->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (0.6.0)
Requirement already satisfied: MarkupSafe>=0.23 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Jinja2>=2.10.1->flask>=1.1.1->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (1.1.1)
Requirement already satisfied: more-itertools in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from zipp>=0.5->importlib-metadata; python_version < "3.8"->flake8>=3.7.9->visualdl>=2.1.0->-r /home/aistudio/PaddleDetection-release-2.1/requirements.txt (line 3)) (7.2.0)
Building wheels for collected packages: terminaltables, pycocotools, lap, py-cpuinfo
  Building wheel for terminaltables (setup.py) ... [?25ldone
[?25h  Created wheel for terminaltables: filename=terminaltables-3.1.0-cp37-none-any.whl size=15356 sha256=8d1370e8eb8b531bc33edafdc65c5ccaebdd743abd0eb89159a713ad3b69a481
  Stored in directory: /home/aistudio/.cache/pip/wheels/38/ce/e7/382f63c6888f05daac9bffbdea230dc620ceda20bedb449dce
  Building wheel for pycocotools (setup.py) ... [?25ldone
[?25h  Created wheel for pycocotools: filename=pycocotools-2.0.2-cp37-cp37m-linux_x86_64.whl size=278360 sha256=48cc30eaf411f16eee128cd5213e7db07be826eabc24348cad3a336733351d16
  Stored in directory: /home/aistudio/.cache/pip/wheels/fb/44/67/8baa69040569b1edbd7776ec6f82c387663e724908aaa60963
  Building wheel for lap (setup.py) ... [?25ldone
[?25h  Created wheel for lap: filename=lap-0.4.0-cp37-cp37m-linux_x86_64.whl size=1593860 sha256=778a02ceb62d87acc8171fc44e0f567c2e339ff2b39f4db29c31e03ec8601436
  Stored in directory: /home/aistudio/.cache/pip/wheels/83/50/a9/e3660736bfb1fb50598b822551bb8c7ff04f1a4ecf69c42277
  Building wheel for py-cpuinfo (setup.py) ... [?25ldone
[?25h  Created wheel for py-cpuinfo: filename=py_cpuinfo-8.0.0-cp37-none-any.whl size=22245 sha256=5df753682cec31c9aff9ed54f4c92fbf1fca15deb6f9036e63554e0deacf19e0
  Stored in directory: /home/aistudio/.cache/pip/wheels/a5/77/fe/bd2fe25844956ae1e2353d7b2ffc9a90ab8c3c1b4b0862e1e9
Successfully built terminaltables pycocotools lap py-cpuinfo
Installing collected packages: typeguard, shapely, terminaltables, pycocotools, lap, flake8-import-order, py-cpuinfo, py, iniconfig, pytest, pytest-benchmark, xmltodict, motmetrics
Successfully installed flake8-import-order-0.18.1 iniconfig-1.1.1 lap-0.4.0 motmetrics-1.2.0 py-1.10.0 py-cpuinfo-8.0.0 pycocotools-2.0.2 pytest-6.2.4 pytest-benchmark-3.4.1 shapely-1.7.1 terminaltables-3.1.0 typeguard-2.12.1 xmltodict-0.12.0

三、按比例划分数据集

ratio比例系数

import random
import os
#生成train.txt和val.txt
random.seed(2020)
xml_dir  = '/home/aistudio/Annotations'#标签文件地址
img_dir = '/home/aistudio/images'#图像文件地址
path_list = list()
for img in os.listdir(img_dir):
    img_path = os.path.join(img_dir,img)
    xml_path = os.path.join(xml_dir,img.replace('jpg', 'xml'))
    path_list.append((img_path, xml_path))
random.shuffle(path_list)
ratio = 0.9
train_f = open('/home/aistudio/work/train.txt','w') #生成训练文件
val_f = open('/home/aistudio/work/val.txt' ,'w')#生成验证文件

for i ,content in enumerate(path_list):
    img, xml = content
    text = img + ' ' + xml + '\n'
    if i < len(path_list) * ratio:
        train_f.write(text)
    else:
        val_f.write(text)
train_f.close()
val_f.close()

#生成标签文档
label = ['smoke']#设置你想检测的类别
with open('/home/aistudio/work/label_list.txt', 'w') as f:
    for text in label:
        f.write(text+'\n')

注意:

后面跑训练的时候,提示了这个错误,查明原因发现是有一张照片有问题,于是就删除掉,后面跑就成功了。要删除smoke_a187.jpg照片以及其对应的标注文件

%cd ~
import os
file_name = "Annotations/smoke_a187.xml"#/images//smoke_a187.jpg  /Annotations//smoke_a187.xml
if os.path.exists(file_name):
    os.remove(file_name)
    print('成功删除文件:', file_name)
else:
    print('未找到此文件:', file_name)
/home/aistudio
成功删除文件: Annotations/smoke_a187.xml

四、执行训练

此次使用的是 PaddleDetection-release-2.1/configs/ppyolo/ppyolov2_r50vd_dcn_voc.yml 文件,并开启vdl

修改部分:

只需要修改对应的红色部分

PaddleDetection-release-2.1/configs/datasets/voc.yml

[PP-YOLOV2保姆级教程]使用自定义数据集实现吸烟识别预测_第3张图片

如果爆内存就修改这两个文件的batch_size:

PaddleDetection-release-2.1/configs/ppyolo/base/ppyolov2_reader.yml

PaddleDetection-release-2.1/configs/ppyolo/ppyolov2_r50vd_dcn_voc.yml

%cd ~/PaddleDetection-release-2.1/
!python tools/train.py -c configs/ppyolo/ppyolov2_r50vd_dcn_voc.yml --eval --use_vdl=True --vdl_log_dir="./output"

[PP-YOLOV2保姆级教程]使用自定义数据集实现吸烟识别预测_第4张图片

五、模型评估

!python -u tools/eval.py -c configs/ppyolo/ppyolov2_r50vd_dcn_voc.yml  -o weights=output/ppyolov2_r50vd_dcn_voc/best_model.pdparams

[PP-YOLOV2保姆级教程]使用自定义数据集实现吸烟识别预测_第5张图片

打开可视化

步骤:

打开左侧可视化

[PP-YOLOV2保姆级教程]使用自定义数据集实现吸烟识别预测_第6张图片

设置logdir : 路径选择PaddleDetection-release-2.1/output

选择模型文件:路径选择PaddleDetection-release-2.1/output/ppyolov2_r50vd_dcn_voc/best_model.pdparams

然后按下面的打开VisualDL

即可看到如图

[PP-YOLOV2保姆级教程]使用自定义数据集实现吸烟识别预测_第7张图片

这是我训练了100epochs的结果,如果接下去训练mAP应该会更高。

还有下图的参数记录

[PP-YOLOV2保姆级教程]使用自定义数据集实现吸烟识别预测_第8张图片

六、模型预测

在执行tools/infer.py后,在output文件夹下会生成对应的预测结果

%cd ~/PaddleDetection-release-2.1/
!python tools/infer.py -c configs/ppyolo/ppyolov2_r50vd_dcn_voc.yml -o weights=output/ppyolov2_r50vd_dcn_voc/best_model.pdparams --infer_img=/home/aistudio/smoke1.png
/home/aistudio/PaddleDetection-release-2.1
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/__init__.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import MutableMapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Iterable, Mapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Sized
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py:125: 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:
W0707 10:35:11.390235  1627 device_context.cc:404] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W0707 10:35:11.394752  1627 device_context.cc:422] device: 0, cuDNN Version: 7.6.
[07/07 10:35:16] ppdet.utils.checkpoint INFO: Finish loading model weights: output/ppyolov2_r50vd_dcn_voc/best_model.pdparams
[07/07 10:35:17] ppdet.engine INFO: Detection bbox results save in output/smoke1.png

结果展示

原图

[PP-YOLOV2保姆级教程]使用自定义数据集实现吸烟识别预测_第9张图片

预测图

[PP-YOLOV2保姆级教程]使用自定义数据集实现吸烟识别预测_第10张图片

总结

由上图我们可以看到,使用PP-YOLOV2完成了吸烟的目标识别检测,并且mAP已经达到了86.74%。

优化方案

可以通过增加数据集、选择更优化模型,增加训练的次数。

后期应用

后期可以部署到商场、医院、疗养院等公共场合,进行禁烟管控。

[PP-YOLOV2保姆级教程]使用自定义数据集实现吸烟识别预测_第11张图片

关于更多关于PaddleDetection的信息请参考下面的链接地址。

PaddleDetection教程文档地址:https://github.com/PaddlePaddle/PaddleDetection

PaddleDetection Github地址:https://github.com/PaddlePaddle/PaddleDetection

关于作者

感兴趣的方向为:目标检测,分类任务等

AIstudio主页: 来互关呀~ 我在AI Studio上获得黄金等级,点亮8个徽章,来互关呀~ https://aistudio.baidu.com/aistudio/personalcenter/thirdview/474269

Github主页: https://github.com/Niki173

欢迎大家有问题留言交流学习,共同进步成长。

你可能感兴趣的:(PP-YOLOV2,PaddlePaddle,深度学习,pytorch,视觉检测,机器学习,神经网络)