基于PaddleDetection的铝片表面缺陷检测
转自AI Studio,原文链接:基于PaddleDetection的铝片表面缺陷检测 - 飞桨AI Studio一、作品背景随着深度学习等一系列人工智能技术的发展,神经网络模型在机器视觉领域大放异彩。目标检测是指计算机从图片或视频中确定目标物体的位置并识别其种类的过程。作为计算机视觉领域中重要的基础问题,目标检测是其他图像处理任务(如目标追踪、图像分割、行为识别等)的前置步骤,具有重要的基础研究
转自AI Studio,原文链接:
基于PaddleDetection的铝片表面缺陷检测 - 飞桨AI Studio
一、作品背景
随着深度学习等一系列人工智能技术的发展,神经网络模型在机器视觉领域大放异彩。目标检测是指计算机从图片或视频中确定目标物体的位置并识别其种类的过程。作为计算机视觉领域中重要的基础问题,目标检测是其他图像处理任务(如目标追踪、图像分割、行为识别等)的前置步骤,具有重要的基础研究价值。随着其精度和速度不断提升,服务于目标检测需求的神经网络模型也越来越多,众多的网络模型为工厂车间内的工件检测提供了新的可能。 目标检测模型在现实生产生活中有着广泛的应用,现已经被广泛应用到安防监控、人脸识别、车牌检测、工业生产线监控、公共场所视频监控、无人机遥感和自动驾驶等等领域。具有重要的基础研究价值。
二、模型介绍
Liu W等人在2016年提出了一种one-stage的SSD算法,在检测速度和准确率上均优于传统检测算法。SSD以VGG-16为基础提取图像特征,额外添加6个不同尺度的卷积层,分别进行多尺度分类和目标框回归。同时设置不同尺寸的先验框,提高了小尺寸目标的识别准确率。解决了同时期YOLO算法存在的定位精度低、难以检测小目标等问题。SSD算法有以下改进:
- 采用CNN来直接进行检测,避免了YOLO方法在全连接之后再进行检测的操作;
- 提出尺度不同的特征图来对物体进行检测,通过快速检测不同尺度特征图的准确度,明确区分预测;
- 结合Faster R-CNN算法中的Anchor机制,通过大小不同的先验框在特征图中获取候选区域,提高了召回率。下图为SSD算法示意图。
三、数据集介绍
通过海康工业相机采集铝片表面工业缺陷检测数据集。该数据集标注为COCO格式,包含了针孔、脏污、褶皱、划伤4个类别的缺陷目标,超过400张标注图片,整个数据集中的缺陷数量达到1000个以上。
In [1]
#解压数据集
!unzip -oq /home/aistudio/data/data135640/aluminum.zip -d /home/aistudio/data/
In [4]
# 数据可视化
import os
import matplotlib.pyplot as plt
import cv2
#matplotlib inline
image_path = '/home/aistudio/data/aluminum/images'
#返回指定路径下的文件和文件夹列表
image_path_list = sorted(os.listdir(image_path))
#获取每一张图片的路径
image_path_list = [os.path.join(image_path, path) for path in image_path_list]
sample_image_path_list = ['/home/aistudio/data/aluminum/images/80.jpg',\
'/home/aistudio/data/aluminum/images/82.jpg', '/home/aistudio/data/aluminum/images/401.jpg']
plt.figure(figsize=(12, 3))
for i in range(len(sample_image_path_list)):
#subplot(行数,列数,第几行第几列的第几幅图)
plt.subplot(1,len(sample_image_path_list), i+1)
pic = cv2.imread(sample_image_path_list[i])
plt.imshow(pic)
plt.tight_layout()
plt.show()
print("图片总共有{}张".format(len(image_path_list)))
<Figure size 864x216 with 3 Axes>
图片总共有400张
四、下载安装环境
In [ ]
# gitee 国内下载比较快
!git clone https://gitee.com/paddlepaddle/PaddleDetection.git -b release/2.0-beta
In [ ]
cd PaddleDetection
/home/aistudio/PaddleDetection
In [ ]
# %env PYTHONPATH=.:$PYTHONPATH
# %env CUDA_VISIBLE_DEVICES=0
! pip install pycocotools
# 安装其他依赖
!pip install -r requirements.txt
# 编译安装paddledet
!python setup.py install
五、模型训练
In [ ]
! python tools/train.py -c configs/ssd/ssdlite_mobilenet_v3_large_fpn.yml
In [ ]
# 断点续训
!python -u tools/train.py -c configs/ssd/ssdlite_mobilenet_v3_large_fpn.yml -r /home/aistudio/PaddleDetection/output/ssdlite_mobilenet_v3_large_fpn/model_final.pdparams --eval
In [ ]
# 断点续训
!python -u tools/train.py -c configs/ssd/ssdlite_mobilenet_v3_large_fpn.yml -r /home/aistudio/PaddleDetection/output/ssdlite_mobilenet_v3_large_fpn/8575.pdparams --eval
In [ ]
# 断点续训
!python -u tools/train.py -c configs/ssd/ssdlite_mobilenet_v3_large_fpn.yml -r /home/aistudio/PaddleDetection/output/ssdlite_mobilenet_v3_large_fpn/13375.pdparams --eval
In [38]
# 断点续训
!python -u tools/train.py -c configs/ssd/ssdlite_mobilenet_v3_large_fpn.yml -r /home/aistudio/PaddleDetection/output/ssdlite_mobilenet_v3_large_fpn/model_final.pdparams --eval
In [ ]
# 断点续训
!python -u tools/train.py -c configs/ssd/ssdlite_mobilenet_v3_large_fpn.yml -r /home/aistudio/PaddleDetection/output/ssdlite_mobilenet_v3_large_fpn/8225.pdparams --eval
loss曲线
六、模型测试
In [39]
!python -u tools/eval.py -c configs/ssd/ssdlite_mobilenet_v3_large_fpn.yml \
-o weights=output/ssdlite_mobilenet_v3_large_fpn/best_model.pdparams
loading annotations into memory... Done (t=0.00s) creating index... index created! W0406 20:28:49.379544 14452 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1 W0406 20:28:49.384357 14452 device_context.cc:465] device: 0, cuDNN Version: 7.6. /home/aistudio/PaddleDetection/ppdet/data/reader.py:89: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working if isinstance(item, collections.Sequence) and len(item) == 0: 2022-04-06 20:28:54,322-INFO: Test iter 0 2022-04-06 20:28:56,871-INFO: Test iter 100 2022-04-06 20:28:58,955-INFO: Test iter 200 2022-04-06 20:29:01,055-INFO: Test iter 300 2022-04-06 20:29:02,286-INFO: Test finish iter 354 2022-04-06 20:29:02,286-INFO: Total number of images: 354, inference time: 42.90856389884784 fps. loading annotations into memory... Done (t=0.01s) creating index... index created! 2022-04-06 20:29:02,335-INFO: Start evaluate... Loading and preparing results... DONE (t=0.01s) creating index... index created! Running per image evaluation... Evaluate annotation type *bbox* DONE (t=0.48s). Accumulating evaluation results... DONE (t=0.11s). Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.604 Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.978 Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.662 Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.350 Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.556 Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.698 Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.516 Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.663 Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.663 Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.460 Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.614 Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.746
七、模型推理
In [40]
#模型推理
!python tools/infer.py -c configs/ssd/ssdlite_mobilenet_v3_large_fpn.yml -o weights=output/ssdlite_mobilenet_v3_large_fpn/best_model.pdparams --infer_dir=/home/aistudio/data/aluminum/images/
八、模型转换
In [41]
!python tools/export_model.py -c configs/ssd/ssdlite_mobilenet_v3_large_fpn.yml --output_dir=./inference_model_2 \
-o weights=output/ssdlite_mobilenet_v3_large_fpn/best_model.pdparams
2022-04-06 20:34:24,080-INFO: Load categories from /home/aistudio/data/aluminum/annotations/valid.json loading annotations into memory... Done (t=0.01s) creating index... index created! 2022-04-06 20:34:24,091-INFO: Export inference config file to ./inference_model_2/ssdlite_mobilenet_v3_large_fpn/infer_cfg.yml 2022-04-06 20:34:24,309-INFO: save_inference_model pruned unused feed variables im_id 2022-04-06 20:34:24,309-INFO: save_inference_model pruned unused feed variables im_shape 2022-04-06 20:34:24,309-INFO: Export inference model to ./inference_model_2/ssdlite_mobilenet_v3_large_fpn, input: ['image'], output: ['detection_output_0.tmp_0']...
九、前端部署
In [ ]
# 安装 Paddle2ONNX
!pip install paddle2onnx
In [ ]
# 模型转换
!paddle2onnx \
--model_dir ./infer_model \
--model_filename model.pdmodel \
--params_filename params.pdiparams \
--save_file ./infer_model/model.onnx \
--opset_version 12
快速部署,在Git Bash中运行
1. 克隆 WebAI.js-Examples 项目
$ git clone https://github.com/AgentMaker/WebAI.js-Examples
2. 切换至示例目录
$ cd ./ppdet
3.目录结构
- public # 服务器公开目录
- blazeface_1000e # 模型文件目录
- configs.json # 配置文件
- model.onnx # 模型文件
- index.html # 网页
- main.js # 功能代码
- package.json # 项目配置文件
4. 安装依赖
$ npm install
5. 开发网页
$ npm run dev --host
6.效果预览
十、个人兴趣
本人燕山大学2021级硕士研究生在读,研究方向为智能制造,寻求数控加工与深度学习的交叉融合
欢迎大家fork喜欢评论三连,感兴趣的朋友也可互相关注一下啊
更多推荐
所有评论(0)