本来打算速通一下v1到v8,不过发现到v5的源码是真的多,太菜了,研究了好几天!!
目录
- YOLOv5结构
- YOLOv5网络结构
- 1.C3模块:
- 2.SPPF模块
- 3.使用siLU激活函数
- YOLOv5代码解读
- 1.YOLOv5l.yaml配置文件
- 2.yolo.py文件
- 1.parse_model函数
- 2.从配置文件中提取backbone 和head,循环列表,创建网络结构
- 3.Detect类
- 4.model类
- 3.train文件
YOLOv5结构
YOLOv5网络结构
2021年10月12日,yolov5 发布了 V6.0 版本,就直接学这个版本的结构了。
整体网络结构还是蛮简单的,neck部分就是更加级联的特征融合,和FPN相似。
1.C3模块:
在backbone和neck两个部分中,C3中的BottleNeck1和2在残差部分有所区别。
2.SPPF模块
这部分就是一个串联拼接的spp,使用1*1的CBL和maxpooling,进行特征提取和融合。
3.使用siLU激活函数
替换LeakyRelu变为了siLU
YOLOv5代码解读
1.YOLOv5l.yaml配置文件
yaml文件中,depth_multiple: 1.0来控制C3模块实际重复次数,width_multiple: 1.0来控制输出的维度。
**[from, number, module, args]**来实现model搭建时所需的参数,
from为输入;
number为模块重复次数;
module为模块;
args里有输出维度,卷积核,步长,padding的信息。
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
# Parameters
nc: 80 # number of classes
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-C1/2
[-1, 1, Conv, [128, 3, 2]], # 1-C2/4
[-1, 3, C3, [128]], #2
[-1, 1, Conv, [256, 3, 2]], # 3-C3/8
[-1, 6, C3, [256]], #4
[-1, 1, Conv, [512, 3, 2]], # 5-C4/16
[-1, 9, C3, [512]], #6
[-1, 1, Conv, [1024, 3, 2]], # 7-C5/32
[-1, 3, C3, [1024]], #8
[-1, 1, SPPF, [1024, 5]], #9
]
# YOLOv5 v6.0 head
head:
[[-1, 1, Conv, [512, 1, 1]], #10
[-1, 1, nn.Upsample, [None, 2, 'nearest']], #11
[[-1, 6], 1, Concat, [1]], # cat backbone P4 #12
[-1, 3, C3, [512, False]], # 13
[-1, 1, Conv, [256, 1, 1]], #14
[-1, 1, nn.Upsample, [None, 2, 'nearest']], #15
[[-1, 4], 1, Concat, [1]], # cat backbone P3 #16
[-1, 3, C3, [256, False]], # 17 (P3/8-small) #17
[-1, 1, Conv, [256, 3, 2]], #18
[[-1, 14], 1, Concat, [1]], # cat head P4 #19
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)#20
[-1, 1, Conv, [512, 3, 2]], #21
[[-1, 10], 1, Concat, [1]], # cat head P5 #22
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)#23
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) #24
]
2.yolo.py文件
这个model文件中,parse_model比较有意思,通过解析配置文件信息来搭建网络结构。
1.parse_model函数
解析得到:anchor(这个函数中只是用来计算na,也就是锚框数量)、nc(数据集类别数)、gd(depth_multiple: 1.0)、gw(width_multiple: 1.0)。
2.从配置文件中提取backbone 和head,循环列表,创建网络结构
通过函数eval()将str类型转换成了class类型,通过全局变量搜索找到class类,参考文献:此处eval()的详细解释
将结构中,输入不是-1的f,保存到save中,主要是后面为了那几个concat部件和detect部件的feature
def parse_model(d, ch): # model_dict, input_channels(3)
LOGGER.info(f"\n{'':>3}{'from':>18}{'n':>3}{'params':>10} {'module':<40}{'arguments':<30}")
#d:配置文件解析之后的字典
anchors, nc, gd, gw = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple']
#na = 传入的anchor的x,y的一半
na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors
#与yolov3类似,head之后featuremap的通道数,每个anchors:(4+1+nc类别数)
no = na * (nc + 5) # number of outputs = anchors * (classes + 5) 255 网络输出的维度个数
layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out
#从配置文件中提取backbone 和head,循环列表,创建网络结构
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
for j, a in enumerate(args):
try:
args[j] = eval(a) if isinstance(a, str) else a # eval strings
except NameError:
pass
#根据配置文件中的depth_multiple变量,计算要构建的网络的深度,也就是C3模块实际重复次数
n = n_ = max(round(n * gd), 1) if n > 1 else n # depth gain
if m in (Conv, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, MixConv2d, Focus, CrossConv,
BottleneckCSP, C3, C3TR, C3SPP, C3Ghost):
#确定每一次操作的输入通道数c1和输出通道数c2,其中f为index,ch为列表,会循环append上一层的输出通道
c1, c2 = ch[f], args[0]
if c2 != no: # if not output
#使用math.ceil将输出通道数 向上取整 变为8的倍数,如c2 * gw=60,会执行math.ceil(60/8)=8,然后8*8=64
c2 = make_divisible(c2 * gw, 8)
#将输入、输出、stride、padding组成新的args列表
args = [c1, c2, *args[1:]]
if m in [BottleneckCSP, C3, C3TR, C3Ghost]:
#对于可重复的模块,将重复次数插入到args列表中
args.insert(2, n) # number of repeats
n = 1
#如果待执行模块不是上述卷积模块,则定义相应的参数
elif m is nn.BatchNorm2d:
#args为BN的的输入通道数
args = [ch[f]]
elif m is Concat:
#对于Concat来说,f为待cat的两个层数的和
c2 = sum(ch[x] for x in f)
elif m is Detect:
#对于Detect层来说,args为[256, 512, 1024]
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
else:
c2 = ch[f]
#判断n是否大于1,大于时,重复多次该模块
m_ = nn.Sequential(*(m(*args) for _ in range(n))) if n > 1 else m(*args) # module
#t:该层的type名称,如果m中有‘__main__.’,则使用‘’替换掉
t = str(m)[8:-2].replace('__main__.', '') # module type
#每一层参数量计算
np = sum(x.numel() for x in m_.parameters()) # number params
#为每一层赋值indx,输出通道数,type,参数量
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
#将该层加入到lays中
layers.append(m_)
if i == 0:
ch = []
#将当前通道数添加到ch中
ch.append(c2)
return nn.Sequential(*layers), sorted(save)
3.Detect类
将输出来的x(bs,255,20,20)转换到x(bs,3,20,20,85)
如果是推理时,根据网格点,和锚框的wh信息来调整预测框。
根据**_make_grid**函数来,获得每个网格和原图尺寸大小的锚框,这个anchor_grid包含的是wh的信息。
class Detect(nn.Module):
stride = None # strides computed during build
onnx_dynamic = False # ONNX export parameter
export = False # export mode
def __init__(self, nc=80, anchors=(), ch=(), inplace=True): # detection layer
super().__init__()
self.nc = nc # number of classes
self.no = nc + 5 # number of outputs per anchor
self.nl = len(anchors) # number of detection layers
self.na = len(anchors[0]) // 2 # number of anchors
self.grid = [torch.zeros(1)] * self.nl # init grid
self.anchor_grid = [torch.zeros(1)] * self.nl # init anchor grid
self.register_buffer('anchors', torch.tensor(anchors).float().view(self.nl, -1, 2)) # shape(nl,na,2)
self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv
self.inplace = inplace # use in-place ops (e.g. slice assignment)
def forward(self, x):
z = [] # inference output
for i in range(self.nl):
x[i] = self.m[i](x[i]) # conv
bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
if not self.training: # inference
if self.onnx_dynamic or self.grid[i].shape[2:4] != x[i].shape[2:4]:
self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i)
y = x[i].sigmoid()
if self.inplace:
y[..., 0:2] = (y[..., 0:2] * 2 + self.grid[i]) * self.stride[i] # xy
y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
else: # for YOLOv5 on AWS Inferentia https://github.com/ultralytics/yolov5/pull/2953
xy, wh, conf = y.split((2, 2, self.nc + 1), 4) # y.tensor_split((2, 4, 5), 4) # torch 1.8.0
xy = (xy * 2 + self.grid[i]) * self.stride[i] # xy
wh = (wh * 2) ** 2 * self.anchor_grid[i] # wh
y = torch.cat((xy, wh, conf), 4)
z.append(y.view(bs, -1, self.no))
return x if self.training else (torch.cat(z, 1),) if self.export else (torch.cat(z, 1), x)
def _make_grid(self, nx=20, ny=20, i=0):
d = self.anchors[i].device
t = self.anchors[i].dtype
shape = 1, self.na, ny, nx, 2 # grid shape
y, x = torch.arange(ny, device=d, dtype=t), torch.arange(nx, device=d, dtype=t)
if check_version(torch.__version__, '1.10.0'): # torch>=1.10.0 meshgrid workaround for torch>=0.7 compatibility
yv, xv = torch.meshgrid(y, x, indexing='ij')
else:
yv, xv = torch.meshgrid(y, x)
grid = torch.stack((xv, yv), 2).expand(shape) - 0.5 # add grid offset, i.e. y = 2.0 * x - 0.5
anchor_grid = (self.anchors[i] * self.stride[i]).view((1, self.na, 1, 1, 2)).expand(shape)
return grid, anchor_grid
4.model类
通过送入一个图片进行前向传播来获取三个feature的尺寸比例,也就是8,16,32,在将配置文件中的锚框原图大小的wh,调整成对应feature尺度的wh。
3.train文件
待完善