Yolov5是目标检测领域中一种高效的神经网络结构,是Yolov系列的最新版本。本文将以Yolov5网络结构为中心,从多个方面对其进行详细阐述。
一、骨干网络
骨干网络是指网络的主干部分,用于提取图像的特征表示。Yolov5的骨干网络采用CSPNet(Cross Stage Partial Network)架构,相较于传统的ResNet等网络,CSPNet可以显著减小网络的参数量和运算量。该网络结构在既保证检测精度的情况下,显著提高了训练和推理的效率。
import torch.nn as nn
class CSPDarknet(nn.Module):
def __init__(self, layers):
super(CSPDarknet, self).__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(32)
self.relu = nn.LeakyReLU(0.1, inplace=True)
self.layer1 = self.make_layers(32, layers[0])
self.layer2 = self.make_layers(64, layers[1], stride=2)
self.layer3 = self.make_layers(128, layers[2], stride=2)
self.layer4 = self.make_layers(256, layers[3], stride=2)
self.layer5 = self.make_layers(512, layers[4], stride=2)
self.layer6 = self.make_layers(1024, layers[5], stride=2)
self._initialize_weights()
def make_layers(self, in_channels, num_blocks, stride=1):
layers = []
layers.append(('res0', ResBlock(in_channels, in_channels * 2, shortcut=False)))
for i in range(num_blocks):
layers.append(('residual_%d' % i, ResBlock(in_channels * 2, in_channels, stride)))
return nn.Sequential(OrderedDict(layers))
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x1 = self.layer1(x)
x2 = self.layer2(x1)
x3 = self.layer3(x2)
x4 = self.layer4(x3)
x5 = self.layer5(x4)
x6 = self.layer6(x5)
return x4, x5, x6
二、特征金字塔
目标检测任务中,不同大小不同层次的目标需要被检测到,并且需要提取多尺度的特征。Yolov5使用FPN(Feature Pyramid Network)特征金字塔结构,通过特征上采样和特征拼接的方式实现多层次、多尺度特征的融合。它可以同时处理不同尺度的目标,提高模型的检测效果。
class YOLOv5(nn.Module):
def __init__(self, cfg, ch=3):
super(YOLOv5, self).__init__()
self.ch = ch
self.model, self.save = parse_model(cfg)
self.nc = int(self.model[-1]['filters'])
self.nl = len(self.model)
self.stem = Focus(ch, 80, 3)
self.m = nn.Sequential(*self.model[1:])
self.init_weights()
def forward(self, x):
x = self.stem(x)
yolo_out, _, _ = [], [], []
for i in range(self.nl):
x = self.m[i](x)
if i in [2, 4, 6]:
yolo_out.append(x)
elif i == 8:
x = self.m[i](x, yolo_out[-1])
yolo_out.append(x)
return yolo_out
三、激活函数
激活函数在神经网络中扮演着至关重要的角色,Yolov5使用的激活函数是Mish。Mish激活函数在保持与ReLU相同的计算速度的同时,提高了模型的精度。
class Mish(nn.Module):
def __init__(self):
super(Mish, self).__init__()
def forward(self, x):
return x * torch.tanh(F.softplus(x))
class MishModule(nn.Module):
def __init__(self, parent):
super(MishModule, self).__init__()
self.model = parent.model
for i, m in enumerate(self.model.children()):
self.model[i] = Mish() if type(m) == nn.ReLU else m
def forward(self, x):
return self.model(x)
四、预测头
Yolov5的预测头由三个卷积层构成,用于对特征图进行输出通道的降维,并且进行边界框和目标类别的预测。预测头可以预测多种不同尺度下的目标,实现多尺度目标检测。
class Conv(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size=1, stride=1):
super().__init__()
self.conv = nn.Conv2d(in_channel, out_channel, kernel_size, stride, kernel_size // 2, bias=False)
self.bn = nn.BatchNorm2d(out_channel)
self.act = nn.LeakyReLU(0.1, inplace=True)
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = self.act(x)
return x
class PredictionLayer(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.pred = nn.Sequential(
Conv(in_channels, in_channels * 2),
Conv(in_channels * 2, in_channels),
nn.Conv2d(in_channels, out_channels, kernel_size=1)
)
def forward(self, x):
x = self.pred(x)
return x
五、总结
Yolov5是目标检测领域中一种高效的神经网络结构,采用了CSPNet骨干网络和FPN特征金字塔结构。同时,使用Mish激活函数和预测头实现多尺度目标检测。该网络结构在保证检测精度的同时,大大提高了训练和推理的效率,主要应用于实时目标检测和视频分析等领域。