【YOLOV5】YOLOV5⽹络配置⽂件解读
注意注意此时的YOLOV5版本为6.0
相⽐5.0主要更新如下:
1. 将Focus()模块,⽤⼀个Conv(k=6,s=2,p=2)代替
2. 使⽤SPPF()替换 SPP()层以减少操作
3. 减少 P3 主⼲层 C3()从9到6重复以提⾼速度
4. 重新排序将 SPPF()放在主⼲的末尾
5. 在最后⼀个 C3()主⼲层中重新引⼊shortcut
6. 更新了mixup和copy-paste 的超参
下⾯是⽹络配置⽂件解读
配置⽂件路径:yolov5/models/yolov5l.yaml
# YOLOv5 by Ultralytics, GPL-3.0 licen
# Parameters
nc: 80# number of class
depth_multiple: 1.0# model depth multiple
惶恐的近义词width_multiple: 1.0# layer channel multiple
anchors:
韭菜盒子的做法- [10,13, 16,30, 33,23]# P3/8
- [30,61, 62,45, 59,119]# P4/16
- [116,90, 156,198, 373,326]# P5/32
# YOLOv5 v6.0 backbone
backbone:
# [from, number, module, args]
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
[-1, 3, C3, [128]],
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
[-1, 6, C3, [256]],
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
[-1, 9, C3, [512]],
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
[-1, 3, C3, [1024]],
[-1, 1, SPPF, [1024, 5]], # 9
]
# YOLOv5 v6.0 head
head:
[[-1, 1, Conv, [512, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 6], 1, Concat, [1]], # cat backbone P4
[-1, 3, C3, [512, Fal]], # 13
[-1, 1, Conv, [256, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 4], 1, Concat, [1]], # cat backbone P3
[-1, 3, C3, [256, Fal]], # 17 (P3/8-small)
[-1, 1, Conv, [256, 3, 2]],
[[-1, 14], 1, Concat, [1]], # cat head P4
[-1, 3, C3, [512, Fal]], # 20 (P4/16-medium)
[-1, 1, Conv, [512, 3, 2]],
[[-1, 10], 1, Concat, [1]], # cat head P5
[-1, 3, C3, [1024, Fal]], # 23 (P5/32-large)
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
]
Parameters解读
nc: 80# 训练的类别数,默认为coco的80类
depth_multiple: 1.0# ⽤来调整⽹络的深度
width_multiple: 1.0# ⽤来调整⽹络的宽度
anchors:
- [10,13, 16,30, 33,23]# FPN接主⼲⽹络下采样8倍后的anchor⼤⼩
- [30,61, 62,45, 59,119]# FPN接主⼲⽹络下采样4倍后的anchor⼤⼩
- [116,90, 156,198, 373,326]# FPN接主⼲⽹络下采样2倍后的anchor⼤⼩Backbone解读
backbone:
# [from, number, module, args]
游大海# from:来⾃哪⼀层
# number:与depth_multiple结合确定⽹络的深度,⽽且number数量要⼤于1 # module:与width_multiple结合确定⽹络的宽度,主要是改变卷积核的数量[[-1, 1, Conv, [64, 6, 2, 2]], # -1 代表来⾃上⼀层的输⼊
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
[-1, 3, C3, [128]],其他英语
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
[-1, 6, C3, [256]],
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
[-1, 9, C3, [512]],
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
58是什么意思[-1, 3, C3, [1024]],
[-1, 1, SPPF, [1024, 5]], # 9
]
Head
head:
[[-1, 1, Conv, [512, 1, 1]],
王维相思
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 6], 1, Concat, [1]], # cat backbone P4
[-1, 3, C3, [512, Fal]], # 13
[-1, 1, Conv, [256, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 4], 1, Concat, [1]], # cat backbone P3
[-1, 3, C3, [256, Fal]], # 17 (P3/8-small)
[-1, 1, Conv, [256, 3, 2]],
[[-1, 14], 1, Concat, [1]], # cat head P4
[-1, 3, C3, [512, Fal]], # 20 (P4/16-medium)
[-1, 1, Conv, [512, 3, 2]],
[[-1, 10], 1, Concat, [1]], # cat head P5
[-1, 3, C3, [1024, Fal]], # 23 (P5/32-large)
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)潜匿
]
⽹络结构
from n params module arguments
0 -1 17040 Conv [3, 64, 6, 2, 2]
1 -1 173984 Conv [64, 128, 3, 2]
2 -1 3156928 C
3 [128, 128, 3]
3 -1 129542
4 Conv [128, 256, 3, 2]
4 -1 61118208 C3 [256, 256, 6]
5 -1 11180672 Conv [256, 512, 3, 2]
6 -1 96433792 C3 [512, 512, 9]
7 -1 14720640 Conv [512, 1024, 3, 2]
8 -1 39971712 C3 [1024, 1024, 3]
9 -1 12624512 SPPF [1024, 1024, 5]
10 -1 1525312 Conv [1024, 512, 1, 1]
11 -1 10 dules.upsampling.Upsample [None, 2, 'nearest']
12[-1, 6]10 Concat [1]
13 -1 32757632 C3 [1024, 512, 3, Fal]
14 -1 1131584 Conv [512, 256, 1, 1]
15 -1 10 dules.upsampling.Upsample [None, 2, 'nearest']
16[-1, 4]10 Concat [1]
17 -1 3690688 C3 [512, 256, 3, Fal]
18 -1 1590336 Conv [256, 256, 3, 2]
19[-1, 14]10 Concat [1]
20 -1 32495488 C3 [512, 512, 3, Fal]
21 -1 12360320 Conv [512, 512, 3, 2]
22[-1, 10]10 Concat [1]
23 -1 39971712 C3 [1024, 1024, 3, Fal]
24[17, 20, 23]175390 lo.Detect [9, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [256, 512, 1024]]
解析代码
路径:yolov5/models/yolo.py
通过par_model⽅法,读取⽹络配置,返回⼀个Seq⽹络。
def par_model(d, ch):# model_dict, input_channels(3)
LOGGER.info(f"\n{'':>3}{'from':>18}{'n':>3}{'params':>10}{'module':<40}{'arguments':<30}")
anchors, nc, gd, gw = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple']
na =(len(anchors[0])//2)if isinstance(anchors,list)el anchors # number of anchors
no = na *(nc +5)# number of outputs = anchors * (class + 5)
layers, save, c2 =[],[], ch[-1]# layers, savelist, ch out
for i,(f, n, m, args)in enumerate(d['backbone']+ d['head']):# from, number, module, args
m =eval(m)if isinstance(m,str)el m # eval strings
for j, a in enumerate(args):
try:
args[j]=eval(a)if isinstance(a,str)el a # eval strings
except NameError:
pass
n = n_ =max(round(n * gd),1)if n >1el n # depth gain
if m in[Conv, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, MixConv2d, Focus, CrossConv, BottleneckCSP, C3, C3TR, C3SPP, C3Ghost]:
c1, c2 = ch[f], args[0]
if c2 != no:# if not output
c2 = make_divisible(c2 * gw,8)
args =[c1, c2,*args[1:]]
if m in[BottleneckCSP, C3, C3TR, C3Ghost]:
args.inrt(2, n)# number of repeats
n =1
elif m is nn.BatchNorm2d:
args =[ch[f]]
elif m is Concat:
c2 =sum(ch[x]for x in f)
elif m is Detect:
args.append([ch[x]for x in f])
if isinstance(args[1],int):# number of anchors
args[1]=[list(range(args[1]*2))]*len(f)
elif m is Contract:
c2 = ch[f]* args[0]**2
elif m is Expand:
c2 = ch[f]// args[0]**2
el:
c2 = ch[f]
m_ = nn.Sequential(*(m(*args)for _ in range(n)))if n >1el m(*args)# module
t =str(m)[8:-2].replace('__main__.','')# module type
np =sum(x.numel()for x in m_.parameters())# number params
m_.i, m_.f, m_.type, m_.np = i, f, t, np # attach index, 'from' index, type, number params
LOGGER.info(f'{i:>3}{str(f):>18}{n_:>3}{np:10.0f}{t:<40}{str(args):<30}')# print
layers.append(m_)
if i ==0:
ch =[]同学的可爱
ch.append(c2)
return nn.Sequential(*layers),sorted(save)