YOLOv7:第一篇之实践和推理
1. 下载源码
git clone https://github.com/WongKinYiu/yolov7.git
本文下载代码时间:2022.08.03
可能需要安装的库:
清华源加速安装:
-i https://pypi.tuna.tsinghua.edu.cn/simple
需要安装 yaml:
pip install pyyaml # 注意这里是 pyyaml,使用yaml 会安装失败找不到包。
2. 推理
python detect.py --weights yolov7.pt --conf 0.25 --img-size 640 --source inference/images/horses.jpg
首次使用会自动下载 yolov7.pt
文件到主目录。
输出信息:
Namespace(weights=['yolov7.pt'], source='inference/images/horses.jpg', img_size=640, conf_thres=0.25, iou_thres=0.45, device='', view_img=False, save_txt=False, save_conf=False, nosave=False, classes=None, agnostic_nms=False, augment=False, update=False, project='runs/detect', name='exp', exist_ok=False, no_trace=False)
YOLOR 🚀 v0.1-47-g4f6e390 torch 1.10.1+cu102 CPU
Downloading https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt to yolov7.pt...
100%|██████████████████████████████████████| 72.1M/72.1M [02:16<00:00, 553kB/s]
Fusing layers...
RepConv.fuse_repvgg_block
RepConv.fuse_repvgg_block
RepConv.fuse_repvgg_block
/home/xx/programs/miniconda3/envs/yolo/lib/python3.9/site-packages/torch/functional.py:445: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:2157.)
return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
Model Summary: 306 layers, 36905341 parameters, 6652669 gradients, 104.7 GFLOPS
Convert model to Traced-model...
traced_script_module saved!
model is traced!
The image with the result is saved in: runs/detect/exp/horses.jpg
Done. (0.359s)
需要注意的是上面的内容:
Model Summary: 306 layers, 36905341 parameters, 6652669 gradients, 104.7 GFLOPS
Convert model to Traced-model...
traced_script_module saved!
model is traced!
暂时不太清楚啥意思。
3. onnx导出
python export.py --weights yolov7.pt --grid --end2end --simplify \
--topk-all 100 --iou-thres 0.65 --conf-thres 0.35 --img-size 640 640 --max-wh 640
这里我们主要关注得到的 yolov7.onnx
,我们使用 netron 进行可视化,以结合源码查看网络架构。
4. 网络配置yaml 文件解读
文件路径:cfg/training/yolov7.yaml
点击查看代码
# parameters
nc: 80 # 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]], # 2
[-1, 1, Conv, [128, 3, 2]], # 3-P2/4 # -2
[-1, 1, Conv, [64, 1, 1]], # 4 # -6
[-2, 1, Conv, [64, 1, 1]], # 5 # -5
[-1, 1, Conv, [64, 3, 1]], # 6
[-1, 1, Conv, [64, 3, 1]], # 7 # -3
[-1, 1, Conv, [64, 3, 1]], # 8
[-1, 1, Conv, [64, 3, 1]], # 9 # -1
[[-1, -3, -5, -6], 1, Concat, [1]], # 10
[-1, 1, Conv, [256, 1, 1]], # 11
[-1, 1, MP, []], # 12
[-1, 1, Conv, [128, 1, 1]], # 13 # -3
[-3, 1, Conv, [128, 1, 1]], # 14
[-1, 1, Conv, [128, 3, 2]], # 15 # -1
[[-1, -3], 1, Concat, [1]], # 16-P3/8 # -2
[-1, 1, Conv, [128, 1, 1]], # 17 # -6
[-2, 1, Conv, [128, 1, 1]], # 18 # -5
[-1, 1, Conv, [128, 3, 1]], # 19
[-1, 1, Conv, [128, 3, 1]], # 20 # -3
[-1, 1, Conv, [128, 3, 1]], # 21
[-1, 1, Conv, [128, 3, 1]], # 22 # -1
[[-1, -3, -5, -6], 1, Concat, [1]], # 23
[-1, 1, Conv, [512, 1, 1]], # 24 # -3
[-1, 1, MP, []], # 25
[-1, 1, Conv, [256, 1, 1]], # 26 # -3
[-3, 1, Conv, [256, 1, 1]], # 27
[-1, 1, Conv, [256, 3, 2]], # 28 # -1
[[-1, -3], 1, Concat, [1]], # 29-P4/16 # -2
[-1, 1, Conv, [256, 1, 1]], # 30 # -6
[-2, 1, Conv, [256, 1, 1]], # 31 # -5
[-1, 1, Conv, [256, 3, 1]], # 32
[-1, 1, Conv, [256, 3, 1]], # 33 # -3
[-1, 1, Conv, [256, 3, 1]], # 34
[-1, 1, Conv, [256, 3, 1]], # 35 # -1
[[-1, -3, -5, -6], 1, Concat, [1]], # 36
[-1, 1, Conv, [1024, 1, 1]], # 37 # -3
[-1, 1, MP, []], # 38
[-1, 1, Conv, [512, 1, 1]], # 39 # -3
[-3, 1, Conv, [512, 1, 1]], # 40
[-1, 1, Conv, [512, 3, 2]], # 41 # -1
[[-1, -3], 1, Concat, [1]], # 42-P5/32 # -2
[-1, 1, Conv, [256, 1, 1]], # 43 # -6
[-2, 1, Conv, [256, 1, 1]], # 44 # -5
[-1, 1, Conv, [256, 3, 1]], # 45
[-1, 1, Conv, [256, 3, 1]], # 46 # -3
[-1, 1, Conv, [256, 3, 1]], # 47
[-1, 1, Conv, [256, 3, 1]], # 48 # -1
[[-1, -3, -5, -6], 1, Concat, [1]], # 49
[-1, 1, Conv, [1024, 1, 1]], # 50
]
# yolov7 head
head:
[[-1, 1, SPPCSPC, [512]], # 51
[-1, 1, Conv, [256, 1, 1]], # 52
[-1, 1, nn.Upsample, [None, 2, 'nearest']], # 53 # -2
[37, 1, Conv, [256, 1, 1]], # route backbone P4 # 54 # -1
[[-1, -2], 1, Concat, [1]], # 55 # -2
[-1, 1, Conv, [256, 1, 1]], # 56 # -6
[-2, 1, Conv, [256, 1, 1]], # 57 # -5
[-1, 1, Conv, [128, 3, 1]], # 58 # -4
[-1, 1, Conv, [128, 3, 1]], # 59 # -3
[-1, 1, Conv, [128, 3, 1]], # 60 # -2
[-1, 1, Conv, [128, 3, 1]], # 61 # -1
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]], # 62
[-1, 1, Conv, [256, 1, 1]], # 63
[-1, 1, Conv, [128, 1, 1]], # 64
[-1, 1, nn.Upsample, [None, 2, 'nearest']], # 65 # -2
[24, 1, Conv, [128, 1, 1]], # route backbone P3 # 66
[[-1, -2], 1, Concat, [1]], # 67 # -2
[-1, 1, Conv, [128, 1, 1]], # 68 # -6
[-2, 1, Conv, [128, 1, 1]], # 69 # -5
[-1, 1, Conv, [64, 3, 1]], # 70 # -4
[-1, 1, Conv, [64, 3, 1]], # 71 # -3
[-1, 1, Conv, [64, 3, 1]], # 72 # -2
[-1, 1, Conv, [64, 3, 1]], # 73 # -1
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]], # 74
[-1, 1, Conv, [128, 1, 1]], # 75
[-1, 1, MP, []], # 76
[-1, 1, Conv, [128, 1, 1]], # 77 # -3
[-3, 1, Conv, [128, 1, 1]], # 78
[-1, 1, Conv, [128, 3, 2]], # 79 # -1
[[-1, -3, 63], 1, Concat, [1]], # 80 # -2
[-1, 1, Conv, [256, 1, 1]], # 81
[-2, 1, Conv, [256, 1, 1]], # 82
[-1, 1, Conv, [128, 3, 1]], # 83
[-1, 1, Conv, [128, 3, 1]], # 84
[-1, 1, Conv, [128, 3, 1]], # 85
[-1, 1, Conv, [128, 3, 1]], # 86
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]], # 87
[-1, 1, Conv, [256, 1, 1]], # 88 # -3
[-1, 1, MP, []], # 89
[-1, 1, Conv, [256, 1, 1]], # 90 # -3
[-3, 1, Conv, [256, 1, 1]], # 91
[-1, 1, Conv, [256, 3, 2]], # 92 # -1
[[-1, -3, 51], 1, Concat, [1]], # 93 # -2
[-1, 1, Conv, [512, 1, 1]], # 94
[-2, 1, Conv, [512, 1, 1]], # 95
[-1, 1, Conv, [256, 3, 1]], # 96
[-1, 1, Conv, [256, 3, 1]], # 97
[-1, 1, Conv, [256, 3, 1]], # 98
[-1, 1, Conv, [256, 3, 1]], # 99
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]], # 100
[-1, 1, Conv, [512, 1, 1]], # 101
[75, 1, RepConv, [256, 3, 1]], # 102
[88, 1, RepConv, [512, 3, 1]], # 103
[101, 1, RepConv, [1024, 3, 1]], # 104
[[102,103,104], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5) # 105 # IDetect 训练和部署不一样
]
已完成:
- 翻译下面的论文:
https://arxiv.org/pdf/2101.03697.pdf
【论文阅读】RepVGG: Making VGG-style ConvNets Great Again
待完成:
-
理解YOLOv7 中的 RepConv啥意思。
-
画出 YOLOv7的网络架构图
-
有时间的话,将YOLOv7 部分代码模块化,显然现在的YOLOv7的模型搭建还不够模块化。
5.推理参数
使用 detect.py 来检测:
python .\detect.py --source 0
# source:0 调用笔记本摄像头
# 'xx.jpg' 直接检测图片
# '传入文件夹' 检测文件夹的目录
# '传入视频文件' 直接检测视频文件