yolov5s.yaml 文件
以yolov5s.yaml 为例
其中有设置好的anchor
每一行代表在某一层的anchor. 一行的三组数字分别代表三个anchor的宽和高 基准
且小anchor 是在大特征图上,大anchor是在小特征图上。以输入图片大小640*640为例:
最终提取三个特征图大小分别为 80 X 80 , 40 X 40, 20X20
那么 [10, 13, 16,30, 33,23] 将应用在80 X 80 的特征图上
[30,61, 62,45, 59,119] 应用在 40 X40 的特征图上
[116,90, 156,198, 373,326] 在 20 X 20 的特征图上
Detect 函数
定义在yolo.Detect里面
class Detect(nn.Module):
stride = None # strides computed during build
onnx_dynamic = False # ONNX export parameter
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