YOLOv7训练自己的数据集(口罩检测)

前言

前提条件

实验环境

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

制作自己的数据集

<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
	           ......

YOLOv7训练自己的数据集(口罩检测)

训练自己的数据集

VOC格式数据集转换成YOLO格式数据集

在yolov7_train_mydatasets目录下,打开labelImg2yolo.py文件

# 修改成自己数据集的类别名
classes = ["with_mask","without_mask"] 

然后,运行labelImg2yolo.py

python labelImg2yolo.py

生成yolo格式的训练和验证数据集
YOLOv7训练自己的数据集(口罩检测)
YOLOv7训练自己的数据集(口罩检测)

修改cfg配置

新建一个myyolov7.yaml配置文件

YOLOv7训练自己的数据集(口罩检测)

myyolov7.yaml内容

YOLOv7训练自己的数据集(口罩检测)

# 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文件

YOLOv7训练自己的数据集(口罩检测)

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

YOLOv7训练自己的数据集(口罩检测)
YOLOv7训练自己的数据集(口罩检测)
训练完成,生成init.pt、best.pt和last.pt权重。
YOLOv7训练自己的数据集(口罩检测)
YOLOv7训练自己的数据集(口罩检测)

进行测试

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

YOLOv7训练自己的数据集(口罩检测)
YOLOv7训练自己的数据集(口罩检测)

进行预测

python detect.py --weights runs/train/myyolov7-train12/weights/best.pt --conf 0.25 --img-size 640 --source inference/images/face_mask.jpg

YOLOv7训练自己的数据集(口罩检测)
YOLOv7训练自己的数据集(口罩检测)

源码获取

获取链接 提取码: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.

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2023年3月10日
下一篇 2023年3月10日

相关推荐