一、空间金字塔池化

SPP

# SPP结构,利用不同大小的池化核进行池化 5*5 9*9 13*13# 先构建kernel_size=5, stride=1, padding=2的最大池化层# 再构建kernel_size=9, stride=1, padding=4的最大池化层# 再构建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指的是未经过最大池化的层 return features

SPPF

class SPPF(nn.Module):# Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocherdef __init__(self, c1, c2, k=5):# equivalent to SPP(k=(5, 9, 13))super().__init__()c_ = c1 // 2# hidden channelsself.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() warningy1 = 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/CrossStagePartialNetworksdef __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 channelsself.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中

第二步 找到yolo.py文件里的parse_model函数,将类名加入进去

第三步 修改配置文件

在我自己的数据集上跑了一下,发现SPPCSPC的效果是最好的~~~

二、上采样方式

1. 最近邻插值(Nearest neighbor interpolation)

YOLOV5中默认使用的是最近邻插值‘nearest’

2. 双线性插值(Bi-Linear interpolation)

若要改为双线性插值只需在yaml文件中将nearest改为bilinear,然后在后面加上True即可

reference

空间金字塔池化改进 SPP / SPPF / ASPP / RFB / SPPCSPC_迪菲赫尔曼的博客-CSDN博客