- 本文是个人使用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.jpgfilename>
<path>path>
<source>
<database>Unknowndatabase>
source>
<size>
<width>1179width>
<height>710height>
<depth>3depth>
size>
<segmented>0segmented>
<object>
<name>with_maskname>
<pose>Unspecifiedpose>
<truncated>0truncated>
<difficult>0difficult>
<bndbox>
<xmin>89xmin>
<ymin>37ymin>
<xmax>492xmax>
<ymax>659ymax>
bndbox>
object>
<object>
<name>without_maskname>
<pose>Unspecifiedpose>
<truncated>0truncated>
<difficult>0difficult>
<bndbox>
<xmin>680xmin>
<ymin>31ymin>
<xmax>1071xmax>
<ymax>684ymax>
bndbox>
object>
annotation>
├─VOCdevkit
└─VOC2007
├─Annotations
│ 01.xml
│ ......
└─JPEGImages
01.jpg
......
在yolov7_train_mydatasets目录下,打开labelImg2yolo.py文件
# 修改成自己数据集的类别名
classes = ["with_mask","without_mask"]
然后,运行labelImg2yolo.py
python labelImg2yolo.py
生成yolo格式的训练和验证数据集
# 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)
]
# 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
[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.
- 更多精彩内容,可点击进入我的个人主页查看