★★★ 本文源自AlStudio社区精品项目,【点击此处】查看更多精品内容 >>>
本项目基于yolov3实现老鼠检测从训练到部署的全流程,最终在验证集上的mAP达到0.906的效果。

一、项目背景


厨房的卫生问题对于餐饮行业是一个苦恼的问题,由于厨房白天的时候有人工作,老鼠一般于夜间出现,本项目基于yolov3解决该行业痛点,使得夜间能够发现老鼠的存在。
该项目数据集是在网上花费重金买来,非常的心痛,该数据集分为二个部分,JPEGImages和Annotations.JPEGImages文件夹中有1050+张各种场景的老鼠图像,共有1250+老鼠标注框。并对每张图片使用labelimg做了人工标注,标注对应的xml文件放在了Annotations文件夹中.

二、安装PaddleDetection


github上也可以下载,但国内gitee速度相对较快,实在不行,可以先下载再传PaddleDetection进来解压

#克隆PaddleDetection仓库
!git clone https://gitee.com/paddlepaddle/PaddleDetection.git

安装相关依赖

%cd PaddleDetection
!pip install -r requirements.txt

由于安装依赖时提示需要更新:因此用!pip install --upgrade pip进行更新

!pip install --upgrade pip

随后进入到相应的文件夹,如果不进入,后面的代码需要更改

%cd ..
/home/aistudio

三、数据预处理

解压上传的个人数据集到PaddleDetection/dataset/mouse中
mouse文件夹需自己新建

!unzip /home/aistudio/data/data200131/mouse.zip -d PaddleDetection/dataset/mouse

安装paddlex进行数据预处理,该包非常的好用,短短几条命令便可以进行对数据集的划分并生成txt文件(如果你的数据没有划分)

!pip install paddlex -q

用paddlex对PaddleDetection/dataset/mouse中的数据集进行划分,train数据集占80%,val数据集占20%,test数据集占0%

!paddlex --split_dataset --format VOC --dataset_dir PaddleDetection/dataset/mouse --val_value 0.2 --test_value 0.0

划分后的结果:

四、模型训练

先进入到PaddleDetection文件夹中,安装所需要的依赖:pycocotools,如果之前安装的时候已经存在就不需要安装,可以跳过

%cd PaddleDetection/
/home/aistudio/PaddleDetection
!pip install pycocotools

在这里之后我们需要更改我们的配置文件,我们进来到configs/yolov3中选择我们的backbone,我这里选择的是darknet53_270e_voc,我们点进去该模型的配置文件:可以看到

我们可以看到上面五个文件首先是第一个…/datasets/voc.yml,我们找到该文件,进入,更改我们需要更改的参数如:我们主要识别老鼠,所以num_classes设为1
TrainDataset、EvalDataset、TestDataset下的文件路径以及名字都需要改

回来看到我们的runtime.yml文件,这里存放我们的gpu的使用、保存训练后模型参数的文件夹等等参数
第三个文件为_base_/optimizer_270e.yml,我们可以看到这里有我们训练次数、学习率等等的参数值
如果有需要改的可以更改,下面用预训练模型的参数进行训练

此处还可以进行可视化操作:
–use_vdl True --vdl_log_dir 这边再加上自己的存放日志文件的地方进行可视化操作,
详情见PaddleDetection官方文档

!python -u tools/train.py -c configs/yolov3/yolov3_darknet53_270e_voc.yml \
                            -o pretrain_weights=https://paddledet.bj.bcebos.com/models/pretrained/DarkNet53_pretrained.pdparams \
                            --eval

我们来看以下训练出来的结果:
首先是刚训练出来的:


mAP在0.20左右
训练100次之后

最好的效果可以到达0.906

五、模型预测

配置我们的参数:需要预测的图片、权重、置信度等等,输出的图片保存在output文件夹中

!python tools/infer.py -c configs/yolov3/yolov3_darknet53_270e_voc.yml \
                        --infer_img=dataset/mouse/JPEGImages/mouse1906.jpg \
                        -o weights=output/yolov3_darknet53_270e_voc/best_model.pdparams \
                        --draw_threshold=0.5
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/setuptools/depends.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
Warning: Unable to use JDE/FairMOT/ByteTrack, please install lap, for example: `pip install lap`, see https://github.com/gatagat/lap
/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
Warning: Unable to use MOT metric, please install motmetrics, for example: `pip install motmetrics`, see https://github.com/longcw/py-motmetrics
Warning: Unable to use MCMOT metric, please install motmetrics, for example: `pip install motmetrics`, see https://github.com/longcw/py-motmetrics
Warning: import ppdet from source directory without installing, run 'python setup.py install' to install ppdet firstly
[03/18 14:42:38] ppdet.utils.checkpoint INFO: Finish loading model weights: output/yolov3_darknet53_270e_voc/best_model.pdparams
100%|█████████████████████████████████████████████| 1/1 [00:02<00:00,  2.47s/it]
[03/18 14:42:40] ppdet.engine INFO: Detection bbox results save in output/mouse1906.jpg

在最后一行的日志中可以看到检测结果保存在output/mouse1906.jpg
以下就是我的模型预测的一个结果

六、模型导出

!python tools/export_model.py -c configs/yolov3/yolov3_darknet53_270e_voc.yml \
                                --output_dir=./inference_model \
                                -o weights=output/yolov3_darknet53_270e_voc/best_model.pdparams
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/setuptools/depends.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
Warning: Unable to use JDE/FairMOT/ByteTrack, please install lap, for example: `pip install lap`, see https://github.com/gatagat/lap
/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
Warning: Unable to use MOT metric, please install motmetrics, for example: `pip install motmetrics`, see https://github.com/longcw/py-motmetrics
Warning: Unable to use MCMOT metric, please install motmetrics, for example: `pip install motmetrics`, see https://github.com/longcw/py-motmetrics
Warning: import ppdet from source directory without installing, run 'python setup.py install' to install ppdet firstly
[03/18 14:37:35] ppdet.utils.checkpoint INFO: Finish loading model weights: output/yolov3_darknet53_270e_voc/best_model.pdparams
[03/18 14:37:35] ppdet.engine INFO: Export inference config file to ./inference_model/yolov3_darknet53_270e_voc/infer_cfg.yml
[03/18 14:37:41] ppdet.engine INFO: Export model and saved in ./inference_model/yolov3_darknet53_270e_voc

model/yolov3_darknet53_270e_voc

由日志文件可以看到保存的相应位置Export model and saved in ./inference_model/yolov3_darknet53_270e_voc

七、最后总结

这里的mAP已经能达到0.906,取得了一个不错的效果,
以下是还可以提升的地方:
1.数据处理中还可以进行相关的数据增强操作
2.可以多试试其他的模型
3.可以进行模型剪枝等操作而后进行模型导出以及部署

八、作者介绍

作者(笔名):可喜可乐
指导老师:刘建建

此文章为转载
原文链接

Logo

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

更多推荐