模块更新于 2026-11-20· 浏览 0· 下载 0
STP 条带池化模块
长窄核重定义池化:把常规 N×N 方形池化换成 1×N 或 N×1 的条带池化,一维拉长捕捉长程依赖、另一维窄核聚焦细节,天然适配各向异性场景
结构图

长窄核重定义池化:把常规 N×N 方形池化换成 1×N 或 N×1 的条带池化,一维拉长捕捉长程依赖、另一维窄核聚焦细节,天然适配各向异性场景
论文信息
- 论文:Strip Pooling: Rethinking Spatial Pooling for Scene Parsing
- 出处:CVPR 2020
核心痛点
场景解析网络靠堆叠局部卷积,感受野涨得慢;自注意力 / 非局部模块要计算巨大的亲和矩阵,显存开销吓人;空洞卷积、金字塔池化的方形窗口又容易把长条带外无关区域的干扰一起池进来。
模块亮点
- 长窄核重定义池化:把常规 N×N 方形池化换成 1×N 或 N×1 的条带池化,一维拉长捕捉长程依赖、另一维窄核聚焦细节,天然适配各向异性场景
- 涨点实打实:ADE20K(ResNet-50)消融中,base FCN 为 37.63 mIoU,加 SPM 后升到 41.66;SPNet 多尺度+翻转达 45.03(ResNet-50)/ 45.60(ResNet-101)mIoU,刷新 ADE20K SOTA,Cityscapes 测试集 82.0%
- 轻量免重训:SPM 靠 Sigmoid 加权 + 逐元素相乘,可直接套在预训练骨干上;MPM 参数只有 PPM 的约 1/3,堆两个就能超过 PSPNet,还少 12M 参数
适用场景
领域为场景解析 / 语义分割,原文在 ADE20K、Cityscapes、Pascal Context 上验证;可迁移到医学图像分割、遥感地物分类、显著性目标检测等像素级预测任务。
插入位置建议
- 加在 ResNet 每个 stage 最后一个残差块的 3×3 卷积之后(原文 SPNet 的默认放法)
- 接在骨干输出之后:先 1×1 卷积把 2048 通道降到 1024,再串联两个 MPM,替代 PSPNet 的 PPM
- 作为空间注意力插进任意 CNN 的卷积块,可加载预训练权重直接微调
适用任务
- 场景解析
- 语义分割
- 原文在 ADE20K
完整代码
stp.py
import torch
import torch.nn as nn
from torch.nn import functional as F
"""CVPR2020
空间池化已被证明在捕获像素预测任务(例如场景解析)的长程上下文信息方面非常有效。在本文中,除了通常具有规则形状的NxN的传统空间池化之外,我们通过引入一种新的池化策略(称为条带池化)来重新思考空间池化的公式,
该策略考虑了一个长而窄的核,即1xN或Nx1。 在条带池化的基础上,我们通过以下方法进一步研究了空间池化架构设计:1)引入一种新的条带池化模块,使骨干网络能够有效地建模长程依赖关系, 2)提出了一种以多样化空间池化为核心的新型构建块,
3)系统地比较了所提出的带状池化与传统空间池化技术的性能。两种基于池化的新型设计都具有轻量级设计,可以作为现有场景解析网络中的高效即插即用模块。
在流行的基准测试(例如ADE20K和Cityscapes)上的广泛实验表明,我们的简单方法建立了新的最先进的结果。
"""
torch_ver = torch.__version__[:3]
__all__ = ['GlobalAvgPool2d', 'GramMatrix',
'View', 'Sum', 'Mean', 'Normalize', 'ConcurrentModule',
'PyramidPooling', 'StripPooling']
class GlobalAvgPool2d(nn.Module):
def __init__(self):
"""Global average pooling over the input's spatial dimensions"""
super(GlobalAvgPool2d, self).__init__()
def forward(self, inputs):
return F.adaptive_avg_pool2d(inputs, 1).view(inputs.size(0), -1)
class GramMatrix(nn.Module):
r""" Gram Matrix for a 4D convolutional featuremaps as a mini-batch
.. math::
\mathcal{G} = \sum_{h=1}^{H_i}\sum_{w=1}^{W_i} \mathcal{F}_{h,w}\mathcal{F}_{h,w}^T
"""
def forward(self, y):
(b, ch, h, w) = y.size()
features = y.view(b, ch, w * h)
features_t = features.transpose(1, 2)
gram = features.bmm(features_t) / (ch * h * w)
return gram
class View(nn.Module):
"""Reshape the input into different size, an inplace operator, support
SelfParallel mode.
"""
def __init__(self, *args):
super(View, self).__init__()
if len(args) == 1 and isinstance(args[0], torch.Size):
self.size = args[0]
else:
self.size = torch.Size(args)
def forward(self, input):
return input.view(self.size)
class Sum(nn.Module):
def __init__(self, dim, keep_dim=False):
super(Sum, self).__init__()
self.dim = dim
self.keep_dim = keep_dim
def forward(self, input):
return input.sum(self.dim, self.keep_dim)
class Mean(nn.Module):
def __init__(self, dim, keep_dim=False):
super(Mean, self).__init__()
self.dim = dim
self.keep_dim = keep_dim
def forward(self, input):
return input.mean(self.dim, self.keep_dim)
class Normalize(nn.Module):
r"""Performs :math:`L_p` normalization of inputs over specified dimension.
Does:
.. math::
v = \frac{v}{\max(\lVert v \rVert_p, \epsilon)}
for each subtensor v over dimension dim of input. Each subtensor is
flattened into a vector, i.e. :math:`\lVert v \rVert_p` is not a matrix
norm.
With default arguments normalizes over the second dimension with Euclidean
norm.
Args:
p (float): the exponent value in the norm formulation. Default: 2
dim (int): the dimension to reduce. Default: 1
"""
def __init__(self, p=2, dim=1):
super(Normalize, self).__init__()
self.p = p
self.dim = dim
def forward(self, x):
return F.normalize(x, self.p, self.dim, eps=1e-8)
class ConcurrentModule(nn.ModuleList):
r"""Feed to a list of modules concurrently.
The outputs of the layers are concatenated at channel dimension.
Args:
modules (iterable, optional): an iterable of modules to add
"""
def __init__(self, modules=None):
super(ConcurrentModule, self).__init__(modules)
def forward(self, x):
outputs = []
for layer in self:
outputs.append(layer(x))
return torch.cat(outputs, 1)
class PyramidPooling(nn.Module):
"""
Reference:
Zhao, Hengshuang, et al. *"Pyramid scene parsing network."*
"""
def __init__(self, in_channels, norm_layer, up_kwargs):
super(PyramidPooling, self).__init__()
self.pool1 = nn.AdaptiveAvgPool2d(1)
self.pool2 = nn.AdaptiveAvgPool2d(2)
self.pool3 = nn.AdaptiveAvgPool2d(3)
self.pool4 = nn.AdaptiveAvgPool2d(6)
out_channels = int(in_channels/4)
self.conv1 = nn.Sequential(nn.Conv2d(in_channels, out_channels, 1, bias=False),
norm_layer(out_channels),
nn.ReLU(True))
self.conv2 = nn.Sequential(nn.Conv2d(in_channels, out_channels, 1, bias=False),
norm_layer(out_channels),
nn.ReLU(True))
self.conv3 = nn.Sequential(nn.Conv2d(in_channels, out_channels, 1, bias=False),
norm_layer(out_channels),
nn.ReLU(True))
self.conv4 = nn.Sequential(nn.Conv2d(in_channels, out_channels, 1, bias=False),
norm_layer(out_channels),
nn.ReLU(True))
# bilinear interpolate options
self._up_kwargs = up_kwargs
def forward(self, x):
_, _, h, w = x.size()
feat1 = F.interpolate(self.conv1(self.pool1(x)), (h, w), **self._up_kwargs)
feat2 = F.interpolate(self.conv2(self.pool2(x)), (h, w), **self._up_kwargs)
feat3 = F.interpolate(self.conv3(self.pool3(x)), (h, w), **self._up_kwargs)
feat4 = F.interpolate(self.conv4(self.pool4(x)), (h, w), **self._up_kwargs)
return torch.cat((x, feat1, feat2, feat3, feat4), 1)
class StripPooling(nn.Module):
"""
Reference:
"""
def __init__(self, in_channels, pool_size, norm_layer, up_kwargs):
super(StripPooling, self).__init__()
self.pool1 = nn.AdaptiveAvgPool2d(pool_size[0])
self.pool2 = nn.AdaptiveAvgPool2d(pool_size[1])
self.pool3 = nn.AdaptiveAvgPool2d((1, None))
self.pool4 = nn.AdaptiveAvgPool2d((None, 1))
inter_channels = int(in_channels/4)
self.conv1_1 = nn.Sequential(nn.Conv2d(in_channels, inter_channels, 1, bias=False),
norm_layer(inter_channels),
nn.ReLU(True))
self.conv1_2 = nn.Sequential(nn.Conv2d(in_channels, inter_channels, 1, bias=False),
norm_layer(inter_channels),
nn.ReLU(True))
self.conv2_0 = nn.Sequential(nn.Conv2d(inter_channels, inter_channels, 3, 1, 1, bias=False),
norm_layer(inter_channels))
self.conv2_1 = nn.Sequential(nn.Conv2d(inter_channels, inter_channels, 3, 1, 1, bias=False),
norm_layer(inter_channels))
self.conv2_2 = nn.Sequential(nn.Conv2d(inter_channels, inter_channels, 3, 1, 1, bias=False),
norm_layer(inter_channels))
self.conv2_3 = nn.Sequential(nn.Conv2d(inter_channels, inter_channels, (1, 3), 1, (0, 1), bias=False),
norm_layer(inter_channels))
self.conv2_4 = nn.Sequential(nn.Conv2d(inter_channels, inter_channels, (3, 1), 1, (1, 0), bias=False),
norm_layer(inter_channels))
self.conv2_5 = nn.Sequential(nn.Conv2d(inter_channels, inter_channels, 3, 1, 1, bias=False),
norm_layer(inter_channels),
nn.ReLU(True))
self.conv2_6 = nn.Sequential(nn.Conv2d(inter_channels, inter_channels, 3, 1, 1, bias=False),
norm_layer(inter_channels),
nn.ReLU(True))
self.conv3 = nn.Sequential(nn.Conv2d(inter_channels*2, in_channels, 1, bias=False),
norm_layer(in_channels))
# bilinear interpolate options
self._up_kwargs = up_kwargs
def forward(self, x):
_, _, h, w = x.size()
x1 = self.conv1_1(x)
x2 = self.conv1_2(x)
x2_1 = self.conv2_0(x1)
x2_2 = F.interpolate(self.conv2_1(self.pool1(x1)), (h, w), **self._up_kwargs)
x2_3 = F.interpolate(self.conv2_2(self.pool2(x1)), (h, w), **self._up_kwargs)
x2_4 = F.interpolate(self.conv2_3(self.pool3(x2)), (h, w), **self._up_kwargs)
x2_5 = F.interpolate(self.conv2_4(self.pool4(x2)), (h, w), **self._up_kwargs)
x1 = self.conv2_5(F.relu_(x2_1 + x2_2 + x2_3))
x2 = self.conv2_6(F.relu_(x2_5 + x2_4))
out = self.conv3(torch.cat([x1, x2], dim=1))
return F.relu_(x + out)
if __name__ == '__main__':
in_channels = 64
pool_size = (2, 3)
norm_layer = nn.BatchNorm2d
up_kwargs = {'mode': 'bilinear', 'align_corners': True}
block = StripPooling(in_channels, pool_size, norm_layer, up_kwargs)
input = torch.rand(1, in_channels, 32, 32)
output = block(input)
print("Input size:", input.size())
print("Output size:", output.size())环境与依赖
Python 3.9+
继续浏览
下一篇SVD 奇异值分解投影对齐SVD闭式去冗余:对类别名文本嵌入做 SVD,删掉各类共有的第一主成分得投影矩阵 P2 = U'U'⊤;图文特征各乘 P2 再算相似度,滤掉文本冗余和视觉类无关信息,零可学习参数相关资源
模块2026-11-20
CRA 通道缩减注意力
QK 压成一维标量:把每个注意力头中 query 和 key 的通道维嵌到一维,token 变成标量,Query-Key 计算量直接缩小 C 倍,注意力运算量约减半
计算机视觉语义分割CRA
计算机视觉MIT
模块2026-11-20
FFM 特征融合模块
先对齐再减冗:1×1 卷积把 CNN 特征图与 Transformer token 序列对齐到同一维度,再用通道注意力(CA)压低冗余通道、放大互补信息
计算机视觉语义分割FFM
计算机视觉MIT
模块2026-11-20
FreqFusion 频率感知特征融合模块
三个生成器各司其职:ALPF 生成器逐点预测空间变异低通滤波器,先平滑再上采样,压住高频扰动;偏移生成器定向重采样,用一致特征替换不一致特征;
计算机视觉语义分割FreqFusion
计算机视觉MIT