前言
- 本文是个人使用YOLOv7训练自己的VOC数据集的应用案例,由于水平有限,难免出现错漏,敬请批评改正。
- YOLOv7代码结构与YOLOv5很相似,要求的数据集格式也一致,熟悉YOLOv5,可以快速入手YOLOv7。
- 更多精彩内容,可点击进入我的个人主页查看
前提条件
- 熟悉Python
实验环境
matplotlib>=3.2.2
numpy>=1.18.5
opencv-python>=4.1.1
Pillow>=7.1.2
PyYAML>=5.3.1
requests>=2.23.0
scipy>=1.4.1
torch>=1.7.0
torchvision>=0.8.1
tqdm>=4.41.0
protobuf<4.21.3
项目结构
yolov7_train_mydatasets
├─cfg
├─data
├─deploy
├─figure
├─inference
│ └─images
├─models
├─paper
├─scripts
├─tools
├─utils
├─VOCdevkit
└─VOC2007
├─Annotations
└─JPEGImages
│ .gitignore
│ detect.py
│ export.py
│ hubconf.py
│ labelImg2yolo.py
│ LICENSE.md
│ README.md
│ requirements.txt
│ test.py
│ train.py
│ train_aux.py
│ yolov7.pt
制作自己的数据集
- LabelImg是一款功能相当实用且被广泛使用的图像标注工具,为开发人员提供一个可以自定义制作和创建数据集的平台,所以我们这里使用LabelImg图像标注工具,来制作自己的数据集。
- LabelImg下载地址 提取码:sjbz
- 图像标注完成后,LabelImg 图像标注工具会生成.xml格式的文件,.xml格式的文件中包含标注图像的路径、大小以及标注图像中的目标的类别信息和目标的真实框在图像中的位置信息等。
- 使用LabelImg 标注工具进行数据标注示例如下图所示。
- 使用LabelImg 图像标注工具会生成.xml格式的文件及其文件内容示例如下图所示。
<annotation>
<folder></folder>
<filename>01.jpg</filename>
<path></path>
<source>
<database>Unknown</database>
</source>
<size>
<width>1179</width>
<height>710</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>with_mask</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>89</xmin>
<ymin>37</ymin>
<xmax>492</xmax>
<ymax>659</ymax>
</bndbox>
</object>
<object>
<name>without_mask</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>680</xmin>
<ymin>31</ymin>
<xmax>1071</xmax>
<ymax>684</ymax>
</bndbox>
</object>
</annotation>
数据集目录结构
├─VOCdevkit
└─VOC2007
├─Annotations
│ 01.xml
│ ......
└─JPEGImages
01.jpg
......
训练自己的数据集
VOC格式数据集转换成YOLO格式数据集
在yolov7_train_mydatasets目录下,打开labelImg2yolo.py文件
# 修改成自己数据集的类别名
classes = ["with_mask","without_mask"]
然后,运行labelImg2yolo.py
python labelImg2yolo.py
生成yolo格式的训练和验证数据集
修改cfg配置
新建一个myyolov7.yaml配置文件
myyolov7.yaml内容
# parameters
nc: 2 # number of classes
depth_multiple: 1.0 # model depth multiple
width_multiple: 1.0 # layer channel multiple
# anchors
anchors:
- [12,16, 19,36, 40,28] # P3/8
- [36,75, 76,55, 72,146] # P4/16
- [142,110, 192,243, 459,401] # P5/32
# yolov7 backbone
backbone:
# [from, number, module, args]
[[-1, 1, Conv, [32, 3, 1]], # 0
[-1, 1, Conv, [64, 3, 2]], # 1-P1/2
[-1, 1, Conv, [64, 3, 1]],
[-1, 1, Conv, [128, 3, 2]], # 3-P2/4
[-1, 1, Conv, [64, 1, 1]],
[-2, 1, Conv, [64, 1, 1]],
[-1, 1, Conv, [64, 3, 1]],
[-1, 1, Conv, [64, 3, 1]],
[-1, 1, Conv, [64, 3, 1]],
[-1, 1, Conv, [64, 3, 1]],
[[-1, -3, -5, -6], 1, Concat, [1]],
[-1, 1, Conv, [256, 1, 1]], # 11
[-1, 1, MP, []],
[-1, 1, Conv, [128, 1, 1]],
[-3, 1, Conv, [128, 1, 1]],
[-1, 1, Conv, [128, 3, 2]],
[[-1, -3], 1, Concat, [1]], # 16-P3/8
[-1, 1, Conv, [128, 1, 1]],
[-2, 1, Conv, [128, 1, 1]],
[-1, 1, Conv, [128, 3, 1]],
[-1, 1, Conv, [128, 3, 1]],
[-1, 1, Conv, [128, 3, 1]],
[-1, 1, Conv, [128, 3, 1]],
[[-1, -3, -5, -6], 1, Concat, [1]],
[-1, 1, Conv, [512, 1, 1]], # 24
[-1, 1, MP, []],
[-1, 1, Conv, [256, 1, 1]],
[-3, 1, Conv, [256, 1, 1]],
[-1, 1, Conv, [256, 3, 2]],
[[-1, -3], 1, Concat, [1]], # 29-P4/16
[-1, 1, Conv, [256, 1, 1]],
[-2, 1, Conv, [256, 1, 1]],
[-1, 1, Conv, [256, 3, 1]],
[-1, 1, Conv, [256, 3, 1]],
[-1, 1, Conv, [256, 3, 1]],
[-1, 1, Conv, [256, 3, 1]],
[[-1, -3, -5, -6], 1, Concat, [1]],
[-1, 1, Conv, [1024, 1, 1]], # 37
[-1, 1, MP, []],
[-1, 1, Conv, [512, 1, 1]],
[-3, 1, Conv, [512, 1, 1]],
[-1, 1, Conv, [512, 3, 2]],
[[-1, -3], 1, Concat, [1]], # 42-P5/32
[-1, 1, Conv, [256, 1, 1]],
[-2, 1, Conv, [256, 1, 1]],
[-1, 1, Conv, [256, 3, 1]],
[-1, 1, Conv, [256, 3, 1]],
[-1, 1, Conv, [256, 3, 1]],
[-1, 1, Conv, [256, 3, 1]],
[[-1, -3, -5, -6], 1, Concat, [1]],
[-1, 1, Conv, [1024, 1, 1]], # 50
]
# yolov7 head
head:
[[-1, 1, SPPCSPC, [512]], # 51
[-1, 1, Conv, [256, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[37, 1, Conv, [256, 1, 1]], # route backbone P4
[[-1, -2], 1, Concat, [1]],
[-1, 1, Conv, [256, 1, 1]],
[-2, 1, Conv, [256, 1, 1]],
[-1, 1, Conv, [128, 3, 1]],
[-1, 1, Conv, [128, 3, 1]],
[-1, 1, Conv, [128, 3, 1]],
[-1, 1, Conv, [128, 3, 1]],
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
[-1, 1, Conv, [256, 1, 1]], # 63
[-1, 1, Conv, [128, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[24, 1, Conv, [128, 1, 1]], # route backbone P3
[[-1, -2], 1, Concat, [1]],
[-1, 1, Conv, [128, 1, 1]],
[-2, 1, Conv, [128, 1, 1]],
[-1, 1, Conv, [64, 3, 1]],
[-1, 1, Conv, [64, 3, 1]],
[-1, 1, Conv, [64, 3, 1]],
[-1, 1, Conv, [64, 3, 1]],
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
[-1, 1, Conv, [128, 1, 1]], # 75
[-1, 1, MP, []],
[-1, 1, Conv, [128, 1, 1]],
[-3, 1, Conv, [128, 1, 1]],
[-1, 1, Conv, [128, 3, 2]],
[[-1, -3, 63], 1, Concat, [1]],
[-1, 1, Conv, [256, 1, 1]],
[-2, 1, Conv, [256, 1, 1]],
[-1, 1, Conv, [128, 3, 1]],
[-1, 1, Conv, [128, 3, 1]],
[-1, 1, Conv, [128, 3, 1]],
[-1, 1, Conv, [128, 3, 1]],
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
[-1, 1, Conv, [256, 1, 1]], # 88
[-1, 1, MP, []],
[-1, 1, Conv, [256, 1, 1]],
[-3, 1, Conv, [256, 1, 1]],
[-1, 1, Conv, [256, 3, 2]],
[[-1, -3, 51], 1, Concat, [1]],
[-1, 1, Conv, [512, 1, 1]],
[-2, 1, Conv, [512, 1, 1]],
[-1, 1, Conv, [256, 3, 1]],
[-1, 1, Conv, [256, 3, 1]],
[-1, 1, Conv, [256, 3, 1]],
[-1, 1, Conv, [256, 3, 1]],
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
[-1, 1, Conv, [512, 1, 1]], # 101
[75, 1, RepConv, [256, 3, 1]],
[88, 1, RepConv, [512, 3, 1]],
[101, 1, RepConv, [1024, 3, 1]],
[[102,103,104], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5)
]
创建自己数据集的yaml文件
新建mydata.yaml文件
mydata.yaml文件内容
# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
train: ./VOCdevkit
val: ./VOCdevkit
# number of classes
nc: 2
# class names
names: ["with_mask","without_mask"]
进行训练
python train.py --workers 8 --batch-size 4 --data data/mydata.yaml --img 640 640 --cfg cfg/training/myyolov7.yaml --weights 'yolov7.pt' --name myyolov7-train --hyp data/hyp.scratch.p5.yaml
训练完成,生成init.pt、best.pt和last.pt权重。
进行测试
python test.py --data data/mydata.yaml --img 640 --batch 32 --conf 0.001 --iou 0.65 --weights runs/train/myyolov7-train12/weights/best.pt --name myyolov7-train
进行预测
python detect.py --weights runs/train/myyolov7-train12/weights/best.pt --conf 0.25 --img-size 640 --source inference/images/face_mask.jpg
源码获取
获取链接 提取码:kzjc文章来源:https://www.toymoban.com/news/detail-447986.html
参考文献
[1] https://github.com/WongKinYiu/yolov7
[2] Chien-Yao Wang, Alexey Bochkovskiy, Hong-Yuan Mark Liao. YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors,2022.文章来源地址https://www.toymoban.com/news/detail-447986.html
- 更多精彩内容,可点击进入我的个人主页查看
到了这里,关于YOLOv7训练自己的数据集(口罩检测)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!