def parse_model(d, ch): # model_dict, input_channels(3)
# Parse a YOLOv5 model.yaml dictionary
LOGGER.info(f"\n{'':>3}{'from':>18}{'n':>3}{'params':>10} {'module':<40}{'arguments':<30}")
anchors, nc, gd, gw, act = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple'], d.get('activation') #输出模型开始时的参数
if act:
Conv.default_act = eval(act) # redefine default activation, i.e. Conv.default_act = nn.SiLU() 可以在yaml文件上更改,更换激活函数
LOGGER.info(f"{colorstr('activation:')} {act}") # print
na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors yaml文件中的anchors,框的长度除以2
no = na * (nc + 5) # number of outputs = anchors * (classes + 5)
layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out save代表之后用了哪些层,c2是输出通道数 ch默认为3,图片3通道
for i, (f, n, m, args) in enumerate(d['backbone'] + d['head']): # from, number重复的次数, module, args
m = eval(m) if isinstance(m, str) else m # eval strings 字符串参数化将'Conv'转换为参数
for j, a in enumerate(args):
with contextlib.suppress(NameError):
args[j] = eval(a) if isinstance(a, str) else a # eval strings
n = n_ = max(round(n * gd), 1) if n > 1 else n # depth gain 深度系数 n*gd
if m in {
Conv, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, MixConv2d, Focus, CrossConv,
BottleneckCSP, C3, C3TR, C3SPP, C3Ghost, nn.ConvTranspose2d, DWConvTranspose2d, C3x,CSMB,LKSP,Bottleneck2,CSMB_double}: #卷积块的名字
c1, c2 = ch[f], args[0] #args是[64,6,2,2],args[0]是64 ch[f]是从前一层输入的通道数
if c2 != no: # if not output
c2 = make_divisible(c2 * gw, 8) #宽度的转换,必须要整除8
args = [c1, c2, *args[1:]] #加个*号会一一分配参数 上一行对c2进行了宽度的处理
if m in {BottleneckCSP, C3, C3TR, C3Ghost, C3x, CSMB,CSMB_double}: #n的重复次数,填进去的话代表bottleneck重复n次,不填进去的话,BottleneckCSP重复n次
args.insert(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) #ch就是channel通道数
# TODO: channel, gw, gd
elif m in {Detect, Segment}:
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)
if m is Segment:
args[3] = make_divisible(args[3] * gw, 8)
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) # 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 打印参数
save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist
layers.append(m_)
if i == 0:
ch = []
ch.append(c2)
return nn.Sequential(*layers), sorted(save)
YOLOv5的parse_model解析
最新推荐文章于 2024-07-05 19:59:28 发布