模块更新于 2026-11-20· 浏览 3· 下载 0
CoordGate 坐标门控模块
乘法门加坐标编码:特征图先过标准卷积,再与"坐标图经逐像素 MLP 编码"得到的门控图逐元素相乘,按空间位置选择性放大或衰减各通道滤波器,效果上相当于局部连接网络
结构图

乘法门加坐标编码:特征图先过标准卷积,再与"坐标图经逐像素 MLP 编码"得到的门控图逐元素相乘,按空间位置选择性放大或衰减各通道滤波器,效果上相当于局部连接网络
论文信息
- 论文:CoordGate: Efficiently Computing Spatially-Varying Convolutions in Convolutional Neural Networks
- 出处:arXiv 2024
核心痛点
光学系统的点扩散函数(PSF)会给图像施加静态但空间上变化的卷积,去模糊等反卷积任务恰恰要求 CNN 学会这种卷积。但 CNN 的权重共享天生缺乏空间感知,约 500×500 的输入就要堆大量 3×3 卷积,U-Net 被迫又深又宽;CoordConv 把坐标混入数据通路,作用不一致;LCN 则参数爆炸、难训练、推理慢。
模块亮点
- 乘法门加坐标编码:特征图先过标准卷积,再与"坐标图经逐像素 MLP 编码"得到的门控图逐元素相乘,按空间位置选择性放大或衰减各通道滤波器,效果上相当于局部连接网络
- 推理开销极小:门控只依赖坐标、与输入数据无关,推理时相比标准卷积只多一次逐元素乘法
- 浅网络打赢深网络:显微镜图像去模糊实验中,加 CoordGate 的最浅模型 CG U-Net(3) 反超最深的 U-Net(6),参数量还少 60 倍;也优于 CoordConv-UNet 和 MultiWienerNet
适用场景
领域为计算成像 / 空间变化的卷积与反卷积;原文在 2 万张活细胞显微镜图像上验证了去除模拟真实镜头的静态 PSF 模糊;也可迁移到显微成像、天文图像去模糊等需要 CNN 编码空间位置信息的任务。
插入位置建议
- 加在 U-Net 每个下采样或上采样节点处,原文的 CG U-Net 系列就是这样插的
- 放在任意卷积块之后:卷积输出乘上坐标门控图,做残差学习时再补一个 1×1 卷积恢复通道数
- 替换 CoordConv 的坐标拼接方案,让网络显式拿到位置信息而不污染数据通路
适用任务
- 计算成像
- 空间变化的卷积与反卷积
完整代码
coordgate.py
import torch
import torch.nn as nn
"""CoordGate:高效计算卷积神经网络中的空间变化卷积
由于点扩散函数 (PSF),光学成像系统的分辨率固有限制,该函数对图像施加静态但空间变化的卷积。这种退化可以通过卷积神经网络(CNN)来解决,特别是通过去模糊技术。
然而,目前的解决方案在有效计算空间变化卷积方面面临一定的局限性。在本文中,我们提出了CoordGate,这是一种新颖的轻量级模块,它使用乘法门和坐标编码网络来高效计算CNN中的空间变化卷积。
CoordGate允许根据滤波器的空间位置对滤波器进行选择性放大或衰减,有效地充当本地连接的神经网络。
CoordGate模块是一个用于深度学习的PyTorch模块,设计用于在卷积神经网络中加入位置信息或映射信息,从而增强模型处理图像的能力。
这个模块允许通过不同的编码方式引入额外的空间信息,可以是位置编码、一个预定义的映射、或者通过双线性插值计算得到的映射。
这样的设计使得CoordGate模块非常适合处理需要考虑空间关系的任务,比如图像分类、分割或者其他视觉任务。
主要特点和应用:
灵活性:通过支持不同类型的编码,CoordGate模块可以适应各种任务和数据特性,提供了一种灵活的方式来融合空间信息。
增强的空间感知能力:将空间位置或自定义映射信息整合到卷积网络中,有助于提高模型对图像空间结构的理解,尤其是对于需要精确空间定位或分析的任务。
自适应特征调整:对于'map'和'bilinear'编码,CoordGate能够根据输入图像的特定特征动态调整权重,从而更好地处理图像中的变化和复杂性。
"""
class CoordGate(nn.Module):
def __init__(self, enc_channels, out_channels, size: list = [256, 256], enctype='pos', **kwargs):
super(CoordGate, self).__init__()
'''
type can be:'pos' - position encoding
'regularised'
'''
self.enctype = enctype
self.enc_channels = enc_channels
if enctype == 'pos':
encoding_layers = kwargs['encoding_layers']
x_coord, y_coord = torch.linspace(-1, 1, int(size[0])), torch.linspace(-1, 1, int(size[1]))
self.register_buffer('pos', torch.stack(torch.meshgrid((x_coord, y_coord), indexing='ij'), dim=-1).view(-1,
2)) # .to(device)
self.encoder = nn.Sequential()
for i in range(encoding_layers):
if i == 0:
self.encoder.add_module('linear' + str(i), nn.Linear(2, enc_channels))
else:
self.encoder.add_module('linear' + str(i), nn.Linear(enc_channels, enc_channels))
elif (enctype == 'map') or (enctype == 'bilinear'):
initialiser = kwargs['initialiser']
if 'downsample' in kwargs.keys():
self.sample = kwargs['downsample']
else:
self.sample = [1, 1]
self.map = nn.Parameter(initialiser)
self.conv = nn.Conv2d(enc_channels, out_channels, 1, padding='same')
self.relu = nn.ReLU()
def forward(self, x):
'''
x is (bs,nc,nx,ny)
'''
if self.enctype == 'pos':
gate = self.encoder(self.pos).view(1, x.shape[2], x.shape[3], x.shape[1]).permute(0, 3, 1, 2)
gate = torch.nn.functional.relu(gate) # ?
x = self.conv(x * gate)
return x
elif self.enctype == 'map':
map = self.relu(self.map).repeat_interleave(self.sample[0], dim=2).repeat_interleave(self.sample[1], dim=3)
x = self.conv(x * map)
return x
elif self.enctype == 'bilinear':
# if self.enc_channels == 9:
map = create_bilinear_coeff_map_cart_3x3(self.map[:, 0:1], self.map[:, 1:2])
# else:
# map = create_bilinear_coeff_map_cart_5x5(angles,distances)
map = self.relu(map).repeat_interleave(self.sample[0], dim=2).repeat_interleave(self.sample[1], dim=3)
x = self.conv(x * map)
return x
def create_bilinear_coeff_map_cart_3x3(x_disp, y_disp):
shape = x_disp.shape
x_disp = x_disp.reshape(-1)
y_disp = y_disp.reshape(-1)
# Determine the quadrant based on the signs of the displacements
primary_indices = torch.zeros_like(x_disp, dtype=torch.long)
primary_indices[(x_disp >= 0) & (y_disp >= 0)] = 0 # Quadrant 1
primary_indices[(x_disp < 0) & (y_disp >= 0)] = 2 # Quadrant 2
primary_indices[(x_disp < 0) & (y_disp < 0)] = 4 # Quadrant 3
primary_indices[(x_disp >= 0) & (y_disp < 0)] = 6 # Quadrant 4
# Define the number of directions
num_directions = 8
# Compute the indices for the primary and secondary directions
secondary_indices = ((primary_indices + 1) % num_directions).long()
tertiary_indices = (primary_indices - 1).long()
tertiary_indices[tertiary_indices < 0] = num_directions - 1
x_disp = x_disp.abs()
y_disp = y_disp.abs()
coeffs = torch.zeros((x_disp.size(0), num_directions + 1), device=x_disp.device)
batch_indices = torch.arange(x_disp.size(0), device=x_disp.device)
coeffs[batch_indices, primary_indices] = (x_disp * y_disp)
coeffs[batch_indices, secondary_indices] = x_disp * (1 - y_disp)
coeffs[batch_indices, tertiary_indices] = (1 - x_disp) * y_disp
coeffs[batch_indices, -1] = (1 - x_disp) * (1 - y_disp)
swappers = (primary_indices == 0) | (primary_indices == 4)
coeffs[batch_indices[swappers], secondary_indices[swappers]] = (1 - x_disp[swappers]) * y_disp[swappers]
coeffs[batch_indices[swappers], tertiary_indices[swappers]] = x_disp[swappers] * (1 - y_disp[swappers])
coeffs = coeffs.view(shape[0], shape[2], shape[3], num_directions + 1).permute(0, 3, 1, 2)
reorderer = [0, 1, 2, 7, 8, 3, 6, 5, 4]
return coeffs[:, reorderer, :, :]
if __name__ == '__main__':
# 创建 CoordGate 模块的实例
enc_channels = 32
out_channels = 32
size = [256, 256]
enctype = 'pos'
encoding_layers = 2
initialiser = torch.rand((out_channels, 2))
kwargs = {'encoding_layers': encoding_layers, 'initialiser': initialiser}
block = CoordGate(enc_channels, out_channels, size, enctype, **kwargs)
# 生成随机输入数据
input_size = (1, enc_channels, size[0], size[1])
input_data = torch.rand(input_size)
# 对输入数据进行前向传播
output = block(input_data)
# 打印输入和输出数据的形状
print("Input size:", input_data.size())
print("Output size:", output.size())
环境与依赖
Python 3.9+
继续浏览
下一篇CorNet 标签相关性网络残差式相关性增强:接在预测层之后,先用 sigmoid 把 logits 转成标签概率,经瓶颈层(维度 R≪V)双向映射学习标签共现模式,再残差加回原始 logits,躲开 V×V 巨型矩阵相关资源
模块2026-11-20
EVSS 高效视觉状态空间模块
先变形、再单向扫描:每个 EVS 块只做一次单向 S6 选择性扫描,扫描前先翻转或转置特征、奇偶交替,近零代价从不同方向捕获非局部信息,每 4 个模块自动还原空间结构
计算机视觉图像复原EVSS
计算机视觉MIT
模块2026-11-20
CPIA-SA 跨范式交互与对齐自注意力
双注意力各管一摊:输入按通道对半分流,一半走 SPC-SA 在通道维做转置注意力,用 EPGO 动态生成 K 值做 Top-K 稀疏;另一半走 SPR-SA 用卷积近似空间自注意力,专注雨纹与纹理恢复
计算机视觉图像复原CPIASA
计算机视觉MIT
模块2026-11-20
DEConv 细节增强卷积
五路卷积并联:vanilla 卷积提强度信息,中心差分 CDC、角度差分 ADC、水平差分 HDC、垂直差分 VDC 提梯度信息,把 Sobel 这类传统局部描述子先验显式编码进卷积层
计算机视觉图像去雾DEConv
计算机视觉MIT