(10×13),(16×30),(33×23),(30×61),(62×45),(59× 119), (116 × 90), (156 × 198),(373 × 326) ,顺序为w × h
Yolo的整个网络,吸取了Resnet、Densenet、FPN的精髓,可以说是融合了目标检测当前业界最 有效的全部技巧。
针对coco:80(类别)+ t x , t y , t w , t h , c o n f t_{x}, t_{y}, t_{w}, t_{h} ,conf tx,ty,tw,th,conf(每个框的x,y,w,h,conf) ,一共85,三个框 :85*3 = 255
使用交叉熵进行类别计算6.Ground Truth的计算
既然网络预测的是偏移值,那么在计算损失时,也是按照偏移值计算损失。现在我们有预测的值, 还需要真值Ground Truth的偏移值,用于计算损失的GT按照以下公式得到:
t x = G x − C x t y = G y − C y t w = log ( G w / P w ) t h = log ( G h / P h ) \begin{aligned} t x &=G x-C x \\ t y &=G y-C y \\ t w &=\log (G w / P w) \\ t h &=\log (G h / P h) \end{aligned} txtytwth=Gx−Cx=Gy−Cy=log(Gw/Pw)=log(Gh/Ph)
tw和th是物体所在边框的长宽和anchor box长宽之间的比率。不直接回归bounding box的长 宽,而是为避免训练带来不稳定的梯度,将尺度缩放到对数空间。如果直接预测相对形变tw 和 th,那么要求tw, th > 0 >0 >0 ,因为框的宽高不可能是负数,这样的话是在做一个有不等式条件约束的优 化问题,没法直接用SGD来做,所以先取一个对数变换,将其不等式约束去掉就可以了。
对于三个框,选取IOU值最大的那个框。
SPP
class SPP(nn.Module):
# Spatial Pyramid Pooling (SPP) layer https://arxiv.org/abs/1406.4729
def __init__(self, c1, c2, k=(5, 9, 13)):
super().__init__()
c_ = c1 // 2 # hidden channels
self.cv1 = Conv(c1, c_, 1, 1)
self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1)
self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])
def forward(self, x):
x = self.cv1(x)
with warnings.catch_warnings():
warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning
return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1))
BottleneckCSP
class BottleneckCSP(nn.Module):
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
super().__init__()
c_ = int(c2 * e) # hidden channels
self.cv1 = Conv(c1, c_, 1, 1)
self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False)
self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False)
self.cv4 = Conv(2 * c_, c2, 1, 1)
self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3)
self.act = nn.SiLU()
self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)))
Bottleneck
class Bottleneck(nn.Module):
# Standard bottleneck
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
super().__init__()
c_ = int(c2 * e) # hidden channels
self.cv1 = Conv(c1, c_, 1, 1)
self.cv2 = Conv(c_, c2, 3, 1, g=g)
self.add = shortcut and c1 == c2
def forward(self, x):
return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))