一、空間金字塔池化
SPP
?
# SPP結(jié)構(gòu),利用不同大小的池化核進行池化 5*5 9*9 13*13
# 先構(gòu)建kernel_size=5, stride=1, padding=2的最大池化層
# 再構(gòu)建kernel_size=9, stride=1, padding=4的最大池化層
# 再構(gòu)建kernel_size=13, stride=1, padding=6的最大池化層
# 池化后堆疊
#---------------------------------------------------#
class SpatialPyramidPooling(nn.Module):
def __init__(self, pool_sizes=[5, 9, 13]):
super(SpatialPyramidPooling, self).__init__()
self.maxpools = nn.ModuleList([nn.MaxPool2d(kernel_size=pool_size, stride=1, padding=pool_size//2) for pool_size in pool_sizes])
def forward(self, x):
features = [maxpool(x) for maxpool in self.maxpools[::-1]]
features = torch.cat(features + [x], dim=1) # x指的是未經(jīng)過最大池化的層
return features
SPPF
?
class SPPF(nn.Module):
# Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher
def __init__(self, c1, c2, k=5): # equivalent to SPP(k=(5, 9, 13))
super().__init__()
c_ = c1 // 2 # hidden channels
self.cv1 = Conv(c1, c_, 1, 1)
self.cv2 = Conv(c_ * 4, c2, 1, 1)
self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2)
def forward(self, x):
x = self.cv1(x)
with warnings.catch_warnings():
warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning
y1 = self.m(x)
y2 = self.m(y1)
return self.cv2(torch.cat([x, y1, y2, self.m(y2)], 1))
SPPCSPC
class SPPCSPC(nn.Module):
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5, k=(5, 9, 13)):
super(SPPCSPC, self).__init__()
c_ = int(2 * c2 * e) # hidden channels
self.cv1 = Conv(c1, c_, 1, 1)
self.cv2 = Conv(c1, c_, 1, 1)
self.cv3 = Conv(c_, c_, 3, 1)
self.cv4 = Conv(c_, c_, 1, 1)
self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])
self.cv5 = Conv(4 * c_, c_, 1, 1)
self.cv6 = Conv(c_, c_, 3, 1)
self.cv7 = Conv(2 * c_, c2, 1, 1)
def forward(self, x):
x1 = self.cv4(self.cv3(self.cv1(x)))
y1 = self.cv6(self.cv5(torch.cat([x1] + [m(x1) for m in self.m], 1)))
y2 = self.cv2(x)
return self.cv7(torch.cat((y1, y2), dim=1))
使用方式
第一步 各個代碼放入common.py中
?
第二步 找到y(tǒng)olo.py文件里的parse_model函數(shù),將類名加入進去
?
第三步 修改配置文件
?
在我自己的數(shù)據(jù)集上跑了一下,發(fā)現(xiàn)?SPPCSPC的效果是最好的~~~
二、上采樣方式
1. 最近鄰插值(Nearest neighbor interpolation)
YOLOV5中默認使用的是最近鄰插值‘nearest’
?
?2. 雙線性插值(Bi-Linear interpolation)
?若要改為雙線性插值只需在yaml文件中將nearest改為bilinear,然后在后面加上True即可
?文章來源:http://www.zghlxwxcb.cn/news/detail-524802.html
reference
空間金字塔池化改進 SPP / SPPF / ASPP / RFB / SPPCSPC_迪菲赫爾曼的博客-CSDN博客文章來源地址http://www.zghlxwxcb.cn/news/detail-524802.html
到了這里,關于Yolov5調(diào)整空間金字塔池化SPPCSPC/上采樣方式bilinear的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!