在我之前的文章中也写过很多关于生产质检相关的实践文章,一直觉得这块是比较有意思的应用方向,做出来的模型能够以一种更加直观贴切的形式展现出来,瓷砖缺陷问题检测识别也是一个比较老的话题了,今天还是想拿出来具体实践做一下,瓷砖里面的缺陷问题有很多种,名称比较专业化,这里我也不是很懂,直接先看下效果:
![](https://i-blog.csdnimg.cn/blog_migrate/3de5ca5a4f9674e02195414f46557c02.gif)
包含的缺陷类型如下:
光圈瑕疵
浅色块瑕疵
深色点块瑕疵
白色点瑕疵
角异常
边异常
模型这里使用的是yolov5s系列的模型,如下所示:
#Parameters
nc: 6 # number of classes
depth_multiple: 0.33 # model depth multiple
width_multiple: 0.50 # layer channel multiple
anchors:
- [10,13, 16,30, 33,23] # P3/8
- [30,61, 62,45, 59,119] # P4/16
- [116,90, 156,198, 373,326] # P5/32
#Backbone
backbone:
# [from, number, module, args]
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
[-1, 3, C3, [128]],
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
[-1, 6, C3, [256]],
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
[-1, 9, C3, [512]],
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
[-1, 3, C3, [1024]],
[-1, 1, SPPF, [1024, 5]], # 9
]
#Head
head:
[[-1, 1, Conv, [512, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 6], 1, Concat, [1]], # cat backbone P4
[-1, 3, C3, [512, False]], # 13
[-1, 1, Conv, [256, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 4], 1, Concat, [1]], # cat backbone P3
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
[-1, 1, Conv, [256, 3, 2]],
[[-1, 14], 1, Concat, [1]], # cat head P4
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
[-1, 1, Conv, [512, 3, 2]],
[[-1, 10], 1, Concat, [1]], # cat head P5
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
]
训练数据配置文件如下:
# Dataset
path: ./dataset
train:
- images/train
val:
- images/test
test:
- images/test
# Classes
names:
0: GQXC
1: QSKXC
2: SSDXC
3: BSDXC
4: JYC
5: BYC
一共包含6种不同类型的问题缺陷。
简单看下数据集:
![](https://i-blog.csdnimg.cn/blog_migrate/ef2b9e48bcf77ef24f392ec6d114ac66.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a133e2af84b92d8a211d6cfbeb33b7db.png)
YOLO格式标注数据:
![](https://i-blog.csdnimg.cn/blog_migrate/9d2708b97ebfd04b5c74a905599f6efc.png)
实例标注数据如下:
2 0.21875 0.678711 0.011719 0.009766
2 0.403809 0.63916 0.010742 0.010742
VOC格式标注数据如下:
![](https://i-blog.csdnimg.cn/blog_migrate/ae2ea5d0b9d06456f436c195dab928f6.png)
实例标注数据如下:
<annotation>
<folder>CIZHUAN</folder>
<filename>JPEGImages/3412_4914_5733_1024_1024_0_8192_6000.jpg</filename>
<source>
<database>The CIZHUAN Database</database>
<annotation>CIZHUAN</annotation>
<image>CIZHUAN</image>
</source>
<owner>
<name>CGB</name>
</owner>
<size>
<width>1024</width>
<height>1024</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>BSDXC</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>924</xmin>
<ymin>476</ymin>
<xmax>939</xmax>
<ymax>491</ymax>
</bndbox>
</object>
<object>
<name>JYC</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>916</xmin>
<ymin>478</ymin>
<xmax>1008</xmax>
<ymax>503</ymax>
</bndbox>
</object>
</annotation>
修改train.py,如下所示:
![](https://i-blog.csdnimg.cn/blog_migrate/d5138b50c5885042c3269e0b71bc0042.png)
默认执行100次epoch的迭代计算,日志输出如下所示:
![](https://i-blog.csdnimg.cn/blog_migrate/ac8b3404d12674bcde5d1739da80ce55.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b4eb3be33d6bb6999fadd44e6c2dd6b5.png)
结果数据目录如下所示:
![](https://i-blog.csdnimg.cn/blog_migrate/a89c7c4e86a6daec730bb798e69719e7.png)
训练过程loss等指标评估可视化如下:
![](https://i-blog.csdnimg.cn/blog_migrate/780d48fd26e7c145869953538ad88591.png)
分类识别的混淆矩阵如下:
![](https://i-blog.csdnimg.cn/blog_migrate/69ac1b7f14efedec9e80ae5cea594624.png)
标签数据可视化如下:
![](https://i-blog.csdnimg.cn/blog_migrate/56757509214b1d90abf7a1e86aca72c8.png)
F1值和PR曲线:
![](https://i-blog.csdnimg.cn/blog_migrate/202dbae45635860fd6258ef0a77e2bbb.png)
batch实例:
![](https://i-blog.csdnimg.cn/blog_migrate/d92e480664b77ef571f30a6dfce42ecb.jpeg)
基于界面模块实现推理可视化如下:
![](https://i-blog.csdnimg.cn/blog_migrate/4e960e5edcf5c61061f182ebcbb12dc3.png)
上传图像:
![](https://i-blog.csdnimg.cn/blog_migrate/8268a92503332e52497eb387c312552e.png)
检测推理:
![](https://i-blog.csdnimg.cn/blog_migrate/d2bf9fa001e8abd2a020718c2fac6141.png)
![](https://i-blog.csdnimg.cn/blog_migrate/aa8f9ba12f5640aab278ab40e4adf26c.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7e137bbf3ca9137f1fb15e316e8c4f77.png)