1 Star 9 Fork 4

Xinchen Cai / YoloV4_Insulators

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

YoloV4_Insulators

一、项目简介

Yolo_Insulators是一个基于YoloV4的绝缘子目标检测程序,人工智能课程设计作业。

依赖

  • Python3.6
  • Pytorch1.2.0
  • CUDA10.0

二、Yolo算法简介

“You Only Look Once”或“YOLO”是一个对象检测算法的名字,这是Redmon等人在2016年的一篇研究论文中命名的。YOLO实现了自动驾驶汽车等前沿技术中使用的实时对象检测。让我们看看是什么使该算法如此受欢迎,并概述其工作原理。

对象检测示例

1、背景

实时的重要性

人们看到图像以后,可以立即识别其中的对象、它们的位置和相对位置。这使得我们能够在几乎无意识的情况下完成复杂的任务,比如开车。因此,对汽车进行自动驾驶训练需要类似水平的反应能力和准确性。在其最基本的形式中,这样的系统必须能够分析实时视频中的道路,并能够在继续确定路径之前检测各种类型的对象及其在现实世界中的位置,所有这些都必须是实时的。

在YOLO之前

先前的检测系统使用分类器对测试图像的不同切片进行评估。例如,Deformable Parts Model (DPM)涉及到在图像中均匀间隔的位置上滑动窗口并在这些部件上运行分类器。R-CNN(Region-based Convolutional Neural Networks)是另一种模型,它运行一种分割算法将一幅图像分割成一个个小块,然后在这些小块上运行一个分类器。但是,速度慢、优化困难一直困扰着这种YOLO之前的系统。

2、YOLO算法

YOLO将对象检测重新定义为一个回归问题。它将单个卷积神经网络(CNN)应用于整个图像,将图像分成网格,并预测每个网格的类概率和边界框。例如,以一个100x100的图像为例。我们把它分成网格,比如7x7。

img

然后,对于每个网格,网络都会预测一个边界框和与每个类别(汽车,行人,交通信号灯等)相对应的概率。

img

每个边界框可以使用四个描述符进行描述:

边界框的中心高度宽度值映射到对象所属的类

此外,该算法还可以预测边界框中存在对象的概率。如果一个对象的中心落在一个网格单元中,则该网格单元负责检测该对象。每个网格中将有多个边界框。在训练时,我们希望每个对象只有一个边界框。因此,我们根据哪个Box与ground truth box的重叠度最高,从而分配一个Box来负责预测对象。

最后,我们对每个类的对象应用一个称为“非最大抑制(Non Max Suppression)”的方法来过滤出“置信度”小于阈值的边界框。这为我们提供了图像预测。

img

3、重要性

YOLO非常快。由于检测问题是一个回归问题,所以不需要复杂的管道。它比“R-CNN”快1000倍,比“Fast R-CNN”快100倍。它能够处理实时视频流,延迟小于25毫秒。它的精度是以前实时系统的两倍多。同样重要的是,YOLO遵循的是“端到端深度学习”的实践。

三、代码说明

1、文件结构

.
│  predict.py				# 对图片进行预测
│  train.py					# 训练模型
│  voc_annotation.py		# 对VOC数据集处理导出索引
│  yolo.py					# 预测程序的子程序

├─img						# 存放预测后的图像
├─logs						# 存放训练的模型文件
├─model_data				# 存放预训练模型
│      new_classes.txt		# 类别的名称
│      yolo_anchors.txt		# 先验框的大小

├─nets						# 网络结构
│      CSPdarknet.py		# CSPdarkNet53主干特征网络
│      yolo4.py				# FPN、SPP等网络
│      yolo_training.py		# 模型训练子程序

├─utils						# 数据加载、NMS等
│      dataloader.py		# 数据加载
│      utils.py				# 数据处理、增强等

└─VOCdevkit					# VOC数据集
    └─VOC2007
        │  voc2yolo4.py		# 数据集转换

        ├─Annotations		# 标注XML文件
        ├─ImageSets
        │  └─Main
        └─JPEGImages		# 数据集图片

2、基本原理

YoloV4基本网络结构如下:

20200512144007178

YoloV4整个网络主要分为CSPDarknet53、SPP、PANet和Yolo Head四个部分。

CSPDarknet53:主干特征提取网络,主要利用深度卷积提取图像特征,便于后续网络使用。代码主要在nets\CSPdarknet.py下,以下为CSPDarknet53网络结构的类。

class CSPDarkNet(nn.Module)

forward部分可以看到输入图像经过一次普通卷积和五次残差卷积,最后将倒数三层结果输出,供给后面网络使用。这样可以提取不同尺度的特征信息,方便后续的特征融合及提取。值得注意的是

SPP:加强特征提取网络的一部分,主要是使用不同池化核进行最大池化,再进行多重感受野融合。以下为SPP网络部分的类。

class SpatialPyramidPooling(nn.Module)

采用三种不同的池化核对输入特征层池化,得到不同感受野的特征层,最后融合所有输出层及输入层实现多重感受野的融合。

PANet+Yolo Head:加强特征提取网络的一部分和网络输出,主要对上两个网络输出的不同尺度的特征进行上下采样特征融合,最后在三种不同的尺度上对预测的结果输出,以下为PANet和YoloHead的类。

class YoloBody(nn.Module)

输入的x2,x1,x0CSPDarknet53SPP网络的输出,分别代表着三个不同尺度的特征层,在PANet中将这三个不同尺度的特征通过上下采样,使其在大小上具有相同的尺度,再进行特征融合,依次在三个不同尺度下进行,最后通过卷积将结果输出,值得注意的是yolo的输出既包含回归也包含分类,其中在不同物体识别上是采用分类的方式,在预测物体所在位置时采用回归的方式。

四,如何使用

1、数据集

数据集采用网上开源的绝缘子数据集,共600张图片。数据集格式使用VOC2007,标注文件为xml。

你可以通过百度网盘来下载绝缘子数据集-提取码:djuf ,以下是部分数据集图片。

image-20201214193009500

若需要扩增自己的数据,可以使用labelimg来标注新的数据,注意标签为insulator。

如何制作数据集

将数据集图片存放至VOCdevkit/VOC2007/JPEGImages目录,再将标注文件放至VOCdevkit/VOC2007/Anootations目录。

执行

python VOCdevkit/VOC2007/voc2yolo4.py
python voc_annotation.py

运行成功后会在VOCdevkit/VOC2007/ImageSets/Main目录生成训练需要的文件。

2、训练模型

由于数据集数量较小,直接训练模型收敛效果可能不佳,达不到高识别率。绝缘子识别是目标检测的一个子应用,其模型的很多参数与其他目标检测的参数相似,因此可以通过一个在完备的数据集上训练好的模型通过迁移学习应用到绝缘子识别上,可以在数据集较小的情况下使模型快速收敛,实现更高的准确率。

迁移学习策略:先冻结CSPDarknet53网络, 只训练FPN部分,后期再将CSPDarknet53解冻,在全网络上训练模型。

train.py中可以通过设置Cosine_lrmosaicsmoooth_label来设置是否采用余弦退火策略、mosaic数据增强和标签平滑等。训练集和验证集默认比例为9:1,可在train.py文件中修改val_split参数来调整比例。同时也可以调整参数lrBatch_sizeEpoch来修改学习率、批大小及迭代次数。

训练模型只需运行

python train.py

训练好的模型会存在log文件下。

你可以通过百度网盘来下载我已经训练好的模型-提取码:t9ct,以下是训练模型loss的变化,

image-20201214203019767

3、测试模型

若使用自己训练的模型,需要在根目录的yolo.py中修改model_path的路径。不过也可以使用这里训练好的模型-提取码:t9ct,下载模型后将模型文件放入logs文件夹即可。在predict.py中修改imgPath为需要预测的图片路径,运行

python predict.py

即可弹出预测成功的窗口,并将预测的结果存放至img文件夹中。

以下为我训练的模型的部分测试结果。

00034 00037
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

YoloV4算法检测绝缘子-人工智能课程设计 展开 收起
Python
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/Onicc/YoloV4_Insulators.git
git@gitee.com:Onicc/YoloV4_Insulators.git
Onicc
YoloV4_Insulators
YoloV4_Insulators
main

搜索帮助

344bd9b3 5694891 D2dac590 5694891