赞
踩
转载自:SPP和SPPF(in YOLOv5) - 知乎 (zhihu.com)
SPP是空间金字塔池化,作用是一个实现一个自适应尺寸的输出。(传统的池化层如最大池化、平均池化的输出大小是和输入大小挂钩的,但是我们最后做全连接层实现分类的时候需要指定全连接的输入,所以我们需要一种方法让神经网络在某层得到一个固定维度的输出,而且这种方法最好不是resize(resize会失真),由此SPP应运而生,其最早是何凯明提出,应用于RCNN模型)当今的SPP在faster-rcnn上已经发展为今天的Multi-Scale-ROI-Align,而在Yolo上发展为SPPF。
- 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))
SPP的代码简化为:
- class SPP(nn.Module):
- def __init__(self):
- super().__init__()
- self.maxpool1 = nn.MaxPool2d(kernel_size = 5, stride = 1, padding=2)
- self.maxpool2 = nn.MaxPool2d(9, 1, padding=4)
- self.maxpool3 = nn.MaxPool2d(13, 1, padding=6)
-
- def forward(self, x):
- o1 = self.maxpool1(x)
- o2 = self.maxpool2(x)
- o3 = self.maxpool3(x)
- return torch.cat([x, o1, o2, o3], dim=1)
- 假设输入为[B,C,H,W]
- 得到输出形状为[B,C*4,H,W] # x, o1, o2, o3的形状都是[B,C,H,W],在dim=1上cat一下,就是通道堆叠了。
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))

SPPF的代码简化为:
- class SPPF(nn.Module):
- def __init__(self):
- super().__init__()
- self.maxpool = nn.MaxPool2d(5, 1, padding=2)
-
- def forward(self, x):
- o1 = self.maxpool(x)
- o2 = self.maxpool(o1)
- o3 = self.maxpool(o2)
- return torch.cat([x, o1, o2, o3], dim=1)
- SPPF的输出和SPP形状是一样的
从形状上来说,SPP和SPPF的目的是相同的,只是在结构上略有差异,从SPP改进为SPPF后,模型的计算量变小了很多,模型速度提升:
- import time
- import torch
- import torch.nn as nn
-
-
- class SPP(nn.Module):
- def __init__(self):
- super().__init__()
- self.maxpool1 = nn.MaxPool2d(5, 1, padding=2)
- self.maxpool2 = nn.MaxPool2d(9, 1, padding=4)
- self.maxpool3 = nn.MaxPool2d(13, 1, padding=6)
-
- def forward(self, x):
- o1 = self.maxpool1(x)
- o2 = self.maxpool2(x)
- o3 = self.maxpool3(x)
- return torch.cat([x, o1, o2, o3], dim=1)
-
-
- class SPPF(nn.Module):
- def __init__(self):
- super().__init__()
- self.maxpool = nn.MaxPool2d(5, 1, padding=2)
-
- def forward(self, x):
- o1 = self.maxpool(x)
- o2 = self.maxpool(o1)
- o3 = self.maxpool(o2)
- return torch.cat([x, o1, o2, o3], dim=1)
-
-
- def main():
- input_tensor = torch.rand(8, 32, 16, 16)
- spp = SPP()
- sppf = SPPF()
- output1 = spp(input_tensor)
- output2 = sppf(input_tensor)
-
- print(torch.equal(output1, output2))
-
- t_start = time.time()
- for _ in range(100):
- spp(input_tensor)
- print(f"spp time: {time.time() - t_start}")
-
- t_start = time.time()
- for _ in range(100):
- sppf(input_tensor)
- print(f"sppf time: {time.time() - t_start}")
-
-
- if __name__ == '__main__':
- main()

结果为:
True
spp time: 0.5373051166534424
sppf time: 0.20780706405639648
SPP和SPPF的图结构
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。