跳到主要内容
YOLOv5 模型构建源码详解:yaml 配置与 parse_model 解析 | 极客日志
Python AI 算法
YOLOv5 模型构建源码详解:yaml 配置与 parse_model 解析 综述由AI生成 YOLOv5 模型基于 yaml 配置文件构建,涵盖 backbone 与 head 结构解析。parse_model 函数负责将 yaml 指令转换为 PyTorch 模块,处理卷积、C3、Concat 及 Detect 层的参数映射。训练流程通过 forward 方法调用_parse_once 进行单尺度推理,利用 save 列表管理特征层连接。源码展示了从模型初始化到检测头参数设置的完整构建逻辑。
星河入梦 发布于 2024/12/25 更新于 2026/6/5 21 浏览前言
本文记录 YOLOv5 如何通过模型文件 yaml 搭建模型,从解析 yaml 参数用途,到 parse_model 模型构建,最后到 YOLOv5 如何使用搭建模型实现模型训练过程。
一、YOLOv5 文件说明
model/yolo.py 文件:为模型构建文件,主要为模型集成类 class Model(nn.Module),模型 yaml 参数 (如:yolov5s.yaml) 构建 parse_model(d, ch)
model/common.py 文件:为模型模块 (或叫模型组装网络模块)
二、YOLOv5 调用模型构建位置
在 train.py 约 113 行位置,如下代码:
if pretrained:
with torch_distributed_zero_first(LOCAL_RANK):
weights = attempt_download(weights)
ckpt = torch.load(weights, map_location=device)
model = Model(cfg or ckpt['model' ].yaml, ch=3 , nc=nc, anchors=hyp.get('anchors' )).to(device)
exclude = ['anchor' ] if (cfg or hyp.get('anchors' )) and not resume else []
三、模型 yaml 文件解析
以 yolov5s.yaml 文件作为参考进行解析。
1. yaml 的 backbone 解读
backbone:
[[ -1 , 1 , Conv , [64 , 6 , 2 , 2 ]],
[-1 , 1 , Conv , [128 , 3 , 2 ]],
[-1 , , , [ ]],
[ , , , [ , , ]],
[ , , , [ ]],
[ , , , [ , , ]],
[ , , , [ ]],
[ , , , [ , , ]],
[ , , , [ ]],
[ , , , [ , ]]
]
3
C3
128
-1
1
Conv
256
3
2
-1
6
C3
256
-1
1
Conv
512
3
2
-1
9
C3
512
-1
1
Conv
1024
3
2
-1
3
C3
1024
-1
1
SPPF
1024
5
Conv 模块参数解读 backbone 的 [-1, 1, Conv, [128, 3, 2]] 行作为解读参考,在 parse_model(d, ch) 中表示 f, n, m, args = [-1, 1, Conv, [128, 3, 2]]。
f 为取 ch[f] 通道 (ch 保存通道,-1 取上次通道数);
m 为调用模块函数,通常在 common.py 中;
n 为网络深度 depth,使用 max[1, int(n*depth_multiple)] 赋值,即 m 结构循环次数;
args 对应 [128, 3, 2],表示通道数 args[0],该值会根据 math.ceil(args[0]/8)*8 调整,args[1] 表示 kernel 大小,args[2] 表示 stride,
args[-2:] 后 2 位为 m 模块传递参数;
C3 模块参数解读 backbone 的 [-1, 3, C3, [128]] 行作为解读参考,在 parse_model(d, ch) 中表示 f, n, m, args = [-1, 1, Conv, [128, 3, 2]]。
f 为取 ch[f] 通道 (ch 保存通道,-1 取上次通道数);
m 为调用模块函数,通常在 common.py 中;
n 为网络深度 depth,使用 max[1, int(n*depth_multiple)] 赋值,即 m 结构循环次数;
args 对应 [128],表示通道数 args[0] 为 c2,该值会根据 math.ceil(args[0]/8)*8 调整,决定当前层输出通道数量,而后在 parse_model 中被下面代码直接忽略,会被插值 n=1,在 C3 代码中表示循环次数,
顺便说下 args 对应 [128,False],在 C3 中的 False 表示是否需要 shortcut。
c1, c2 = ch[f], args[0 ] if c2 != no:
c2 = make_divisible(c2 * gw, 8 )
args = [c1, c2, *args[1 :]]
if m in [BottleneckCSP, C3, C3TR, C3Ghost]:
args.insert(2 , n)
n = 1
class C3 (nn.Module):
def __init__ (self, c1, c2, n=1 , shortcut=True , g=1 , e=0.5 ):
super ().__init__()
c_ = int (c2 * e)
self .cv1 = Conv(c1, c_, 1 , 1 )
self .cv2 = Conv(c1, c_, 1 , 1 )
self .cv3 = Conv(2 * c_, c2, 1 )
self .m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0 ) for _ in range (n)])
def forward (self, x ):
return self .cv3(torch.cat((self .m(self .cv1(x)), self .cv2(x)), dim=1 ))
2. yaml 的 head 解读 head: [[-1 , 1 , Conv , [512 , 1 , 1 ]],
[-1 , 1 , nn.Upsample , [None , 2 , 'nearest' ]],
[[-1 , 6 ], 1 , Concat , [1 ]],
[-1 , 3 , C3 , [512 , False ]],
[-1 , 1 , Conv , [256 , 1 , 1 ]],
[-1 , 1 , nn.Upsample , [None , 2 , 'nearest' ]],
[[-1 , 4 ], 1 , Concat , [1 ]],
[-1 , 3 , C3 , [256 , False ]],
[-1 , 1 , Conv , [256 , 3 , 2 ]],
[[-1 , 14 ], 1 , Concat , [1 ]],
[-1 , 3 , C3 , [512 , False ]],
[-1 , 1 , Conv , [512 , 3 , 2 ]],
[[-1 , 10 ], 1 , Concat , [1 ]],
[-1 , 3 , C3 , [1024 , False ]],
[[17 , 20 , 23 ], 1 , Detect , [nc , anchors ]],
]
Concat 模块参数解读 head 的 [[-1, 6], 1, Concat, [1]] 行作为解读参考,在 parse_model(d, ch) 中表示 f, n, m, args = [[-1, 6], 1, Concat, [1]]。
f 为取 ch[-1] 与 ch[6] 通道数和,且 6 会被保存到 save 列表中,在 forward 中该列表对应层模块输出会被保存;
m 为调用模块函数,通常在 common.py 中;
n 为网络深度 depth,使用 max[1, int(n*depth_multiple)] 赋值,即 m 结构循环次数,但这里必然为 1;
args 对应 [1],表示通 cat 维度,这里为 1,表示通道叠加;
Detect 模块参数解读 head 的 [[17, 20, 23], 1, Detect, [nc, anchors]] 行作为解读参考,在 parse_model(d, ch) 中表示 f, n, m, args = [[17, 20, 23], 1, Detect, [nc, anchors]]。
f 表示需要使用的层,并分别在 17,20,23 层获取对应通道,可通过 yaml 从 backbone 开始从 0 开始数的那一行,如 17 对应 [-1, 3, C3, [256, False]], # 17 (P3/8-small),
同时,17、20、23 也会被保存到 save 列表;
m 为调用模块函数,通常在 common.py 中;
n 为网络深度 depth,使用 max[1, int(n*depth_multiple)] 赋值,即 m 结构循环次数,但这里必然为 1;
args 对应 [nc, anchors],表示去 nc 数量与 anchor 三个列表,同时会将 f 找到的通道作为列表添加到 args 中,如下代码示意,
最终 args 大致为 [80, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [128, 256, 512]],
80 为类别 nc,[128, 256, 512] 为 f 对应的通道,其它为 anchor 值;
elif m is Detect:
args.append([ch[x] for x in f])
if isinstance (args[1 ], int ):
args[1 ] = [list (range (args[1 ] * 2 ))] * len (f)
四、模型构建整体解读 YOLOv5 模型集成网络代码如下,其重要解读已在代码中注释。
class Model (nn.Module):
def __init__ (self, cfg='yolov5s.yaml' , ch=3 , nc=None , anchors=None ):
super ().__init__()
if isinstance (cfg, dict ):
self .yaml = cfg
else :
import yaml
self .yaml_file = Path(cfg).name
with open (cfg, errors='ignore' ) as f:
self .yaml = yaml.safe_load(f)
ch = self .yaml['ch' ] = self .yaml.get('ch' , ch)
if nc and nc != self .yaml['nc' ]:
LOGGER.info(f"Overriding model.yaml nc={self.yaml['nc' ]} with nc={nc} " )
self .yaml['nc' ] = nc
if anchors:
LOGGER.info(f'Overriding model.yaml anchors with anchors={anchors} ' )
self .yaml['anchors' ] = round (anchors)
self .model, self .save = parse_model(deepcopy(self .yaml), ch=[ch])
self .names = [str (i) for i in range (self .yaml['nc' ])]
self .inplace = self .yaml.get('inplace' , True )
m = self .model[-1 ]
if isinstance (m, Detect):
s = 256
m.inplace = self .inplace
m.stride = torch.tensor([s / x.shape[-2 ] for x in self .forward(torch.zeros(1 , ch, s, s))])
m.anchors /= m.stride.view(-1 , 1 , 1 )
check_anchor_order(m)
self .stride = m.stride
self ._initialize_biases()
initialize_weights(self )
self .info()
LOGGER.info('' )
def forward (self, x, augment=False , profile=False , visualize=False ):
if augment:
return self ._forward_augment(x)
return self ._forward_once(x, profile, visualize)
以上代码最重要网络搭建模块,如下代码调用,我将在下一节解读。
self .model, self .save = parse_model(deepcopy(self .yaml), ch=[ch])
以上代码最重要网络运行 forward,如下代码调用,我将重点解读。
模型在训练时候,是调用下面模块,如下:
return self ._forward_once(x, profile, visualize)
m.f 和 m.i 已在上面 yaml 中介绍,实际需重点关注 y 保存 save 列表对应的特征层输出,若没有则保留为 none 占位,其代码解读已在有注释,详情如下:
def _forward_once (self, x, profile=False , visualize=False ):
y, dt = [], []
for m in self .model:
if m.f != -1 :
x = y[m.f] if isinstance (m.f, int ) else [x if j == -1 else y[j] for j in m.f]
if profile:
self ._profile_one_layer(m, x, dt)
x = m(x)
y.append(x if m.i in self .save else None )
if visualize:
feature_visualization(x, m.type , m.i, save_dir=visualize)
return x
五、构建模型 parse_model 源码解读 该部分是 YOLOv5 根据对应 yaml 模型文件搭建的网络,需结合对应 yaml 文件一起解读,我已在上面介绍了 yaml 文件,可自行查看。
同时,也需要重点关注 m_.i, m_.f, m_.type, m_.np = i, f, t, np,会在上面 _forward_once 函数中用到。
本模块代码解读,也已在代码注释中,请查看源码理解,代码如下:
def parse_model (d, ch ):
LOGGER.info('\n%3s%18s%3s%10s %-40s%-30s' % ('' , 'from' , 'n' , 'params' , 'module' , 'arguments' ))
anchors, nc, gd, gw = d['anchors' ], d['nc' ], d['depth_multiple' ], d['width_multiple' ]
na = (len (anchors[0 ]) // 2 ) if isinstance (anchors, list ) else anchors
no = na * (nc + 5 )
layers, save, c2 = [], [], ch[-1 ]
for i, (f, n, m, args) in enumerate (d['backbone' ] + d['head' ]):
m = eval (m) if isinstance (m, str ) else m
for j, a in enumerate (args):
try :
args[j] = eval (a) if isinstance (a, str ) else a
except NameError:
pass
n = n_ = max (round (n * gd), 1 ) if n > 1 else n
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:
c2 = make_divisible(c2 * gw, 8 )
args = [c1, c2, *args[1 :]]
if m in [BottleneckCSP, C3, C3TR, C3Ghost]:
args.insert(2 , n)
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 ):
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
else :
c2 = ch[f]
m_ = nn.Sequential(*[m(*args) for _ in range (n)]) if n > 1 else m(*args)
t = str (m)[8 :-2 ].replace('__main__.' , '' )
np = sum ([x.numel() for x in m_.parameters()])
m_.i, m_.f, m_.type , m_.np = i, f, t, np
LOGGER.info('%3s%18s%3s%10.0f %-40s%-30s' % (i, f, n_, np, t, args))
save.extend(x % i for x in ([f] if isinstance (f, int ) else f) if x != -1 )
layers.append(m_)
ch.append(c2)
return nn.Sequential(*layers), sorted (save)
相关免费在线工具 加密/解密文本 使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
RSA密钥对生成器 生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
Mermaid 预览与可视化编辑 基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
随机西班牙地址生成器 随机生成西班牙地址(支持马德里、加泰罗尼亚、安达卢西亚、瓦伦西亚筛选),支持数量快捷选择、显示全部与下载。 在线工具,随机西班牙地址生成器在线工具,online
Gemini 图片去水印 基于开源反向 Alpha 混合算法去除 Gemini/Nano Banana 图片水印,支持批量处理与下载。 在线工具,Gemini 图片去水印在线工具,online
curl 转代码 解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online