卷积神经网络深度学习的木材计数系统(部署教程和源码)
由于图床过期,图片无法正常显示,有图阅览请移步以下Gitee/Github网址,文末获取【源码和部署教程】或者通过以下Gitee/Github的文末邮件获取
Gitee(推荐国内访问): https://gitee.com/qunmasj/projects
Github(推荐国外访问): https://github.com/qunshansj?tab=repositories
1.研究背景与意义
随着科技的不断发展,计算机视觉技术在各个领域中得到了广泛的应用。其中,卷积神经网络(Convolutional Neural Network,CNN)作为一种强大的深度学习模型,已经在图像识别、目标检测、人脸识别等领域取得了重大突破。然而,在木材行业中,目前仍然存在着一个重要的问题,即如何高效准确地对木材进行计数。
传统的木材计数方法主要依赖于人工操作,这种方法存在着计数速度慢、容易出错等问题。而且,随着木材行业的不断发展,生产规模不断扩大,传统的计数方法已经无法满足需求。因此,开发一种基于卷积神经网络的木材计数系统具有重要的实际意义。
卷积神经网络是一种能够自动学习特征的深度学习模型,其具有良好的图像处理能力。通过对大量的木材图像进行训练,卷积神经网络可以学习到木材的特征,从而实现对木材的自动计数。相比传统的计数方法,基于卷积神经网络的木材计数系统具有以下几个优势:
首先,卷积神经网络可以实现高效准确的计数。通过对大量的木材图像进行训练,卷积神经网络可以学习到木材的特征,从而能够在短时间内对大量的木材进行准确计数,大大提高了计数的效率。
其次,基于卷积神经网络的木材计数系统具有较高的鲁棒性。由于木材的形状、颜色等特征具有一定的变化性,传统的计数方法往往对这些变化较为敏感,容易出现计数错误的情况。而卷积神经网络通过学习大量的样本,可以对这些变化进行较好的适应,从而提高了计数的准确性。
此外,基于卷积神经网络的木材计数系统还具有较强的泛化能力。一旦训练好的模型可以应用于不同的木材图像,无论是形状、颜色还是大小等方面的差异,都可以通过卷积神经网络进行有效的计数。
综上所述,基于卷积神经网络的木材计数系统具有高效准确、鲁棒性强、泛化能力强等优势,可以为木材行业提供一种高效、准确的计数方法。本研究旨在开发一种基于卷积神经网络的木材计数系统,并提供相应的部署教程和源码,以帮助木材行业实现自动化计数,提高生产效率。
2.图片演示
3.视频演示
卷积神经网络深度学习的木材计数系统(部署教程和源码)_哔哩哔哩_bilibili
4.木材数量智能识别发展现状
我国对方形木材数量检测的主要方式还是采取人工检测。国外对木材进行检测使用光学检测器、激光、感应器等工具,此等设备不但价格较高,而且使用其中的光学仪器,对识别的环境有一定的要求。Herbon[5]等人提出将高斯混合模型以及图像分割等技术应用于原木计数上,对图像使用分割算法以及高斯混合模型初步分离出感兴趣区域,迭代发现和验证新的目标,直到不再发现新目标,每次迭代都会更新高斯混合模型,最终实现原木数量识别。2019年,Wu[6]等人首次提出一种深度学习的图像分割算法实现对棒状物体计数的检测,与之前算法的不同之处是没有提取候选框以及初始化锚框的操作。该算法在钢筋计数上效果较好。国内外许多团队[7,8]开始研究目标检测计数,使用卷积神经网络或者图像处理技术实现对目标数量的统计,但针对的都是棒状物体计数。可以看出,智能识别计数算法主要都应用于原木计数,棒状物体计数上,尚未关注加工后的方形木材。因此本文对方形木材数量智能识别算法进行研究,后续本文所说的木材均为方形木材。
5.核心代码讲解
5.1 Interface.py
1 |
|
该程序文件名为Interface.py,主要功能是加载YOLOv5模型并进行目标检测。
程序首先导入了必要的库和模块,包括os、sys、pathlib、cv2、torch等。
然后定义了一些常量和全局变量,包括文件路径、根目录、设备等。
接下来导入了一些自定义的模块和函数,包括DetectMultiBackend、letterbox、LoadImages、LoadStreams等。
load_model函数用于加载模型,参数包括权重文件路径、数据集文件路径、设备类型、是否使用FP16等。
run函数用于运行模型进行目标检测,参数包括模型、输入图像、步长、图像尺寸、置信度阈值、NMS IOU阈值等。
detect函数用于调用load_model和run函数进行目标检测,并在图像上绘制检测结果。
最后调用detect函数进行目标检测。
整个程序的功能是加载YOLOv5模型并对指定图像进行目标检测,并在图像上绘制检测结果。
5.2 torch_utils.py
1 |
|
这个程序文件是一个PyTorch工具文件,主要包含了一些常用的函数和类。以下是文件中的主要部分:
torch_distributed_zero_first
:一个上下文管理器,用于在分布式训练中使所有进程等待每个本地主进程执行某些操作。date_modified
:返回文件的人类可读的修改日期。git_describe
:返回人类可读的git描述。select_device
:选择设备(CPU或GPU)。time_sync
:返回PyTorch准确的时间。profile
:用于测量模型的速度、内存和FLOPs。is_parallel
:检查模型是否是并行模型。de_parallel
:将模型转换为单GPU模型。intersect_dicts
:返回
5.3 ui.py
1 |
|
这个程序文件是一个使用YOLOv5模型进行目标检测的图形用户界面(GUI)应用程序。它使用PyQt5库创建了一个窗口,其中包含一个标签、一个显示图像的标签、一个文本浏览器和三个按钮。用户可以通过点击按钮选择要检测的图像文件,然后点击另一个按钮开始检测。检测结果将显示在图像标签中,并在文本浏览器中显示木头数量。用户还可以点击退出按钮退出程序。
该程序还导入了其他一些库,包括argparse、platform、shutil、time、numpy、os、sys、pathlib、cv2、torch和torchvision等。它还包含了一些辅助函数和类,用于加载模型、运行检测、绘制检测结果等。
整个程序的逻辑是,当用户点击开始检测按钮时,程序会调用det_yolov7函数,该函数会加载模型并使用模型对选择的图像进行检测。检测结果将显示在图像标签中,并在文本浏览器中显示木头数量。
程序还使用了多线程来处理检测过程,以避免界面卡顿。
5.4 models\common.py
1 |
|
这个程序文件是YOLOv7的一个实现,是一个用于目标检测的深度学习模型。该文件定义了一些常用的模块和函数,包括卷积层、残差块、注意力机制等。其中,Conv类和DWConv类分别表示标准卷积和深度可分离卷积;TransformerLayer类和TransformerBlock类分别表示Transformer模块的一个层和多个层;Bottleneck类和BottleneckCSP类分别表示标准的残差块和CSP残差块;C3类表示具有3个卷积层的CSP残差块;SPP类和SPPF类分别表示空间金字塔池化层的两种实现;Focus类表示将宽高信息聚合到通道维度的模块;GhostConv类表示Ghost卷积层。
该文件还导入了其他一些模块和函数,包括一些用于数据处理和可视化的工具函数,以及一些用于模型训练和推理的工具函数。
5.5 models\experimental.py
1 |
|
这个程序文件是YOLOv5的实验模块。它包含了一些实验性的网络模块和辅助函数。
主要的模块包括:
CrossConv
:交叉卷积下采样模块。Sum
:多个层的加权和模块。MixConv2d
:混合深度卷积模块。Ensemble
:模型集合模块。
此外,还有一些辅助函数和加载模型的函数。
整个文件的功能是定义了YOLOv5的实验模块,并提供了加载模型的函数。
5.6 models\tf.py
1 | class YOLOv5: |
这是一个使用TensorFlow和Keras实现的YOLOv5模型。它包含了YOLOv5的各个组件,如C3、SPP、Bottleneck等。该模型可以用于目标检测任务,并支持导出为SavedModel、PB、TFLite和TFJS格式。
该模型的主要组件包括:
- TFBN:TensorFlow的BatchNormalization封装
- TFPad:TensorFlow的填充层
- TFConv:标准卷积层
- TFFocus:将宽高信息聚合到通道维度的层
- TFBottleneck:标准的瓶颈块
- TFConv2d:替代PyTorch的nn.Conv2D的层
- TFBottleneckCSP:CSP瓶颈块
- TFC3:带有3个卷积层的CSP瓶颈块
- TFSPP:YOLOv3-SPP中使用的空间金字塔池化层
- TFSPPF:空间金字塔池化-Fast层
- TFDetect:检测层,用于目标检测任务
该模型还包含一些辅助函数和工具类,用于实现YOLOv5的各个功能。
该模型的使用方法和导出方法在代码中有详细的注释说明。
6.系统整体结构
整体功能和构架概述:
该项目是一个基于YOLOv5的目标检测工程,主要包括模型训练、模型推理和图形用户界面(GUI)应用程序。它使用PyTorch和TensorFlow作为深度学习框架,提供了一系列的模块和工具函数,用于加载模型、数据处理、训练、推理和可视化等任务。
下面是每个文件的功能概述:
文件路径 | 功能概述 |
---|---|
Interface.py | 加载YOLOv5模型并进行目标检测 |
torch_utils.py | 包含一些PyTorch工具函数和类,如设备选择、时间同步、模型性能测量等 |
train.py | 模型训练脚本,用于训练YOLOv5模型 |
ui.py | 图形用户界面(GUI)应用程序,用于选择图像并进行目标检测 |
models\common.py | 定义了一些常用的模块和函数,用于构建YOLOv5模型 |
models\experimental.py | 包含一些实验性的网络模块和辅助函数 |
models\tf.py | 使用TensorFlow和Keras实现的YOLOv5模型 |
models\yolo.py | 定义了YOLOv5模型的主要组件和功能 |
models_init_.py | 模型模块的初始化文件 |
tools\activations.py | 包含一些激活函数的实现 |
tools\augmentations.py | 包含一些数据增强的函数和类 |
tools\autoanchor.py | 包含自动锚框生成的函数和类 |
tools\autobatch.py | 包含自动批量大小调整的函数和类 |
tools\callbacks.py | 包含一些回调函数的实现 |
tools\datasets.py | 包含数据集加载和处理的函数和类 |
tools\downloads.py | 包含一些下载数据和模型的函数 |
tools\general.py | 包含一些通用的工具函数 |
tools\loss.py | 包含一些损失函数的实现 |
tools\metrics.py | 包含一些评估指标的实现 |
tools\plots.py | 包含一些绘图函数的实现 |
tools\torch_utils.py | 包含一些PyTorch的工具函数和类 |
tools_init_.py | 工具模块的初始化文件 |
tools\aws\resume.py | 包含AWS训练恢复的函数 |
tools\aws_init_.py | AWS模块的初始化文件 |
tools\flask_rest_api\example_request.py | 包含一个示例请求的脚本 |
tools\flask_rest_api\restapi.py | 包含一个Flask REST API的实现 |
tools\loggers_init_.py | 日志记录器模块的初始化文件 |
tools\loggers\wandb\log_dataset.py | 包含将数据集日志记录到WandB的函数 |
tools\loggers\wandb\sweep.py | 包含WandB超参数优化的函数 |
tools\loggers\wandb\wandb_utils.py | 包含一些与WandB相关的工具函数 |
tools\loggers\wandb_init_.py | WandB模块的初始化文件 |
utils\activations.py | 包含一些激活函数的实现 |
utils\augmentations.py | 包含一些数据增强的函数和类 |
utils\autoanchor.py | 包含自动锚框生成的函数和类 |
utils\autobatch.py | 包含自动批量大小调整的函数和类 |
utils\callbacks.py | 包含一些回调函数的实现 |
utils\datasets.py | 包含数据集加载和处理的函数和类 |
utils\downloads.py | 包含一些下载数据和模型的函数 |
utils\general.py | 包含一些通用的工具函数 |
utils\loss.py | 包含一些损失函数的实现 |
utils\metrics.py | 包含一些评估指标的实现 |
utils\plots.py | 包含一些绘图函数的实现 |
utils\torch_utils.py | 包含一些PyTorch的工具函数和类 |
utils_init_.py | 工具模块的初始化文件 |
utils\aws\resume.py | 包含AWS训练恢复的函数 |
utils\aws_init_.py | AWS模块的初始化文件 |
utils\flask_rest_api\example_request.py | 包含一个示例请求的脚本 |
utils\flask_rest_api\restapi.py | 包含一个Flask REST API的实现 |
utils\loggers_init_.py | 日志记录器模块的初始化文件 |
utils\loggers\wandb\log_dataset.py | 包含将数据集日志记录到WandB的函数 |
utils\loggers\wandb\sweep.py | 包含WandB超参数优化的函数 |
utils\loggers\wandb\wandb_utils.py | 包含一些与WandB相关的工具函数 |
utils\loggers\wandb_init_.py | WandB模块的初始化文件 |
以上是对每个文件的功能进行了简要的概述。具体的实现细节和功能可能需要查看每个文件的代码来了解。
7.传统目标检测发展现状
2001年 Viola-Joinest[9]算法大幅提升了人脸检测的速度,传统目标检测步骤如下:1.生成候选框,主要使用滑动窗口算法实现,滑动窗口通过小窗口的滑动,确定目标所处的位置,生成相应的候选框。算法有Selective Search[10]。
2.特征提取,通过相应的特征提取算法提取候选框中目标的特征。算法有Harl[2]和 HOG[3]。
3.通过分类器对特征分类,判断目标的所属类别并且计算出对应目标的坐标,代表算法有DPM [4]算法,AdaBoost15]算法以及SVM16算法。
传统目标算法大多采取滑动窗口算法进行特征提取,通过不同长宽比的窗口遍历整张图片,确定目标所处的候选框。不足之处是造成了大量的窗口冗余。由于大量的遍历图片操作和窗口冗余,算法的运行速度和性能受到了很大的影响。并且特征提取时的特征设置几乎都为手工设置,需要一定的先验知识,对于目标的变化例如遮挡等情况下识别效果不佳,鲁棒性不好。
8.原始木材数量识别存在的缺陷
该博客提供的代码使用YOLOv5对方形木材数量智能识别的结果如图(a)所示,在边缘的方形木材并未得到检测,出现大量的漏检。如图(b)所示,由于方形木材图片中方形木材较为密集,当方形木材之间的边缘存在遮挡的情况下,也会出现部分漏检。究其原因,是由于感受野大小不足,神经网络并未很好地考虑到方形木材周围的信息,因此产生漏检。从模型上看,YOLOv5网络通过深层网络提取出的特征经过SPP模块处理后与浅层特征进入特征融合模块,进行多尺度检测。多尺度检测时特征对应的感受野大小不足,无法考虑方形木材周围的信息,需要增大感受野。YOLO网络增大感受野可以使用增加卷积层、空洞卷积以及池化等操作。由于池化操作会导致信息的部分遗漏,卷积层增大感受野的速度不及空洞卷积,最终本文选取了空洞卷积对YOLOv5改进。从损失函数上看,YOLOv5在位置损失上采用了CIOU损失取代了之前的均方误差,主流的目标检测在位置损失上使用较多的损失函数为GIOU损失、DIOU损失以及CIOU损失,本文将对损失函数进行对比研究,提出较好的位置损失函数。
9.空洞卷积
空洞卷积于2016年被该博客提出,空洞卷积可以很好的增大感受野。卷积神经网络通过卷积、池化来提取图像特征,通过较多的卷积层和池化层后,图片的尺寸逐渐减少,感受野逐渐扩大。在图像分割领域中,最后通过上采样的方式来扩大图片尺寸,直到恢复原图尺寸。由于图像分割处理的是图像的像素级元素,但是在上述过程中,图像尺寸由大变小再变大,部分细节信息不可避免的遗漏,丢失。最终图像分割的效果必定会受到影响。空洞卷积解决了这个问题,在不使用池化操作,不改变图片尺寸,不增加参数量的情况下扩大感受野。
空洞卷积引入了空洞的概念,空洞卷积中除了普通卷积中的长、宽、步长、填充之外还有一个空洞率r。空洞卷积通过向提取的点中插入r-1个0进行卷积,不增加参数数目,不使用池化操作,实现感受野的扩大。如图4-2[9]所示,分别是空洞率为1、2、4的33空洞卷积,三个空洞卷积依次连接,组成卷积神经网络的一部分,其中红色的点对应空洞卷积提取出来的原图上的点,绿色模块表示当前提取的点对应的感受野大小。(a)表示未加空洞的普通的卷积层。(b)中表示空洞率为2的卷积,其中提取的点相隔1,其中红点的感受野大小为33,原本33的卷积核在使用空洞卷积后,感受野扩大为77,与此同时,参数数量并没有发生改变。(c)中表示空洞率为4的卷积,由于前一层每个点的感受野大小为77,所以最终的感受野大小为1515。设卷积核大小为k*k,当前层数为l,普通卷积的感受野计算公式如式4-1所示,可见普通卷积的感受野增长是线性增长。相比于原来普通卷积的线性增长,空洞卷积的感受野大小呈现指数增长,随着网络越深,二者的感受野差距越大。未引入空洞卷积的概念时,卷积的感受野计算如式4-1所示。
10.实验结果
其中左方为DYOLOD识别结果,右边为原YOLOv5识别结果。可见 DYOLOD在边缘和存在部分遮挡的情况下仍然能识别出目标。在过度曝光的情况下,原YOLOv5在遮挡和边缘部分存在漏检,并且在左侧出现误检,而DYOLOD能识别出目标,在左侧也未发生误检。并且,对肉眼较难识别的图片进行了测试,如图所示,在图片左下角的方形木材由于拍摄条件过差,人眼无法准确判断左下角方形木材的具体数量,DYOLOD相比于原YOLOV5模型仍能准确地识别出较多的方形木材。因此在肉眼较难识别的情况下,本文模型仍然有一定的识别能力。
11.系统整合
参考博客《卷积神经网络深度学习的木材计数系统(部署教程和源码)》
12.参考博客
[1]于文吉,余养伦.我国木、竹重组材产业发展的现状与前景[J].木材工业.2013,(1).DOI:10.3969/j.issn.1001-8654.2013.01.001.
[2]刘治国.几种软件体系结构风格分析[J].天津职业大学学报.2003,(5).DOI:10.3969/j.issn.1008-8415.2003.05.013.
[3]谢海桢.复杂场景下的棒材计数算法研究与应用[D].2020.
[4]刘莹.我国进口北美木材现状与发展趋势研究[J].中国林业科学研究院.2015.
[5]Liu, Jun,Chang, Huihong,Forrest, Jeffrey Yi-Lin,等.Influence of artificial intelligence on technological innovation: Evidence from the panel data of china’s manufacturing sectors[J].Technological forecasting and social change.2020,158(Sep.).120142.1-120142.11.DOI:10.1016/j.techfore.2020.120142.
[6]Zhanchao Huang,Jianlin Wang,Xuesong Fu,等.DC-SPP-YOLO: Dense connection and spatial pyramid pooling based YOLO for object detection[J].Information Sciences.2020.522241-258.
[7]Jihong Fan,Ru-Ze Liang.Stochastic learning of multi-instance dictionary for earth mover’s distance-based histogram comparison[J].Neural computing & applications.2018,29(10).733–743.DOI:10.1007/s00521-016-2603-2.
[8]Russakovsky, Olga,Deng, Jia,Su, Hao,等.ImageNet Large Scale Visual Recognition Challenge[J].International Journal of Computer Vision.2015,115(3).211-252.DOI:10.1007/s11263-015-0816-y.
[9]He, Kaiming,Zhang, Xiangyu,Ren, Shaoqing,等.Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition[J].IEEE Transactions on Pattern Analysis & Machine Intelligence.2015,37(9).1904-1916.
[10]Uijlings, J.R.R.,Van De Sande, K.E.A.,Smeulders, A.W.M.,等.Selective search for object recognition[J].International Journal of Computer Vision.2013,104(2).154-171.