学术
模块更新于 2026-11-20· 浏览 7· 下载 2

OCGA 最优选择图注意力模块

KNN 稀疏建图:每个像素当作图节点,按欧氏距离只挑最相似的 top-k 个像素连边,开销大幅降低;先用带步长卷积降采样、聚合后反卷积还原尺寸,省算力

计算机视觉遥感图像道路提取OCGA图注意力TGRS2025

结构图

OCGA 最优选择图注意力模块结构图
OCGA 最优选择图注意力模块结构图

KNN 稀疏建图:每个像素当作图节点,按欧氏距离只挑最相似的 top-k 个像素连边,开销大幅降低;先用带步长卷积降采样、聚合后反卷积还原尺寸,省算力

论文信息

  • 论文:Multiscale Adaptive Decoder and Diversity Selection Network for Road Extraction in Remote Sensing Image
  • 出处:IEEE TGRS 2025

核心痛点

遥感影像中道路是稀疏线性目标,像素占比小、常被遮挡、颜色与背景高度相似;传统 U 型网络的跳跃连接只是原样传递特征,细小道路漏检、背景误检成路,FP 和 FN 都压不下去。

模块亮点

  • KNN 稀疏建图:每个像素当作图节点,按欧氏距离只挑最相似的 top-k 个像素连边,开销大幅降低;先用带步长卷积降采样、聚合后反卷积还原尺寸,省算力
  • 专治道路稀疏:图注意力自适应学习边权、聚合特征相似的邻居节点,让稀疏道路像素互相增强、聚焦单类道路
  • 整体模型涨点:MADSNet 在 GoogleEarth、Massachusetts、CHN6-CUG 三个数据集上全面领先,GoogleEarth 上 IoU 88.51%、F1 93.87%,CHN6-CUG 上 IoU 比 U-Net 高 13.03%、比 SGCN 高 2.94%

适用场景

领域为遥感图像道路提取;可迁移到血管、裂缝、岸线等稀疏线性目标分割,以及各类 U 型分割网络。

插入位置建议

  • 放在编码器各层输出之后、经跳跃连接送入解码器之前,原文 MADSNet 就是把 OCGA 放在跳跃连接里
  • 替换 U-Net、LinkNet、D-LinkNet 等网络 skip connection 里的简单拼接或逐元素相加
  • 接在全局注意力 / Transformer 解码块之前:OCGA 管局部图感知,窗口注意力管长程依赖,原文 OCGA 与 RIA 就是这么搭配的

适用任务

  • 遥感图像道路提取

完整代码

ocga.py
import torch
import torch.nn as nn
import torch.nn.functional as F
from functools import partial

nonlinearity = partial(F.relu, inplace=True)


def build_upsample_layer(cfg):
    if cfg['type'] == 'deconv':
        in_channels = cfg['in_channels']
        out_channels = cfg['out_channels']
        kernel_size = cfg['kernel_size']
        stride = cfg['stride']
        padding = (kernel_size - 1) // 2

        return nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride=stride, padding=padding)


def adj_index(h, k, node_num):
    dist = torch.cdist(h, h, p=2)
    each_adj_index = torch.topk(dist, k, dim=2).indices
    adj = torch.zeros(
        h.size(0), node_num, node_num,
        dtype=torch.int, device=h.device, requires_grad=False
    ).scatter_(dim=2, index=each_adj_index, value=1)
    return adj


class GraphAttentionLayer(nn.Module):

    def __init__(self, in_features, out_features, alpha=0.2, concat=True):
        super(GraphAttentionLayer, self).__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.alpha = alpha
        self.concat = concat

        # 权重矩阵W和注意力机制参数a
        self.W = nn.Parameter(torch.empty(size=(in_features, out_features)))
        self.a = nn.Parameter(torch.empty(size=(2 * out_features, 1)))

        self.activation = nn.ELU()
        self.leakyrelu = nn.LeakyReLU(self.alpha)
        self._init_weights()

    # 帮助模型稳定训练
    def _init_weights(self):
        nn.init.xavier_uniform_(self.W.data, gain=1.414)
        nn.init.xavier_uniform_(self.a.data, gain=1.414)

    def forward(self, h, adj):
        Wh = torch.matmul(h, self.W)  # 计算h和W的矩阵乘积
        e = self._prepare_attentional_mechanism_input(Wh)
        # 设置未连接节点分数设置非常低的-9e15,以便在接下来的softmax中趋于0
        attention = torch.where(adj > 0, e, -9e15 * torch.ones_like(e))
        attention = F.softmax(attention, dim=2)
        h_prime = torch.matmul(attention, Wh)

        if self.concat:
            return self.activation(h_prime)
        else:
            return h_prime

    def _prepare_attentional_mechanism_input(self, Wh):
        # 计算注意力参数a对应的两部分与Wh的矩阵乘积
        Wh1 = torch.matmul(Wh, self.a[:self.out_features, :])
        Wh2 = torch.matmul(Wh, self.a[self.out_features:, :])
        e = Wh1 + Wh2.transpose(1, 2)  # 将Wh1和Wh2转置后相加,形成注意力的未标准化分数
        return self.leakyrelu(e)

    def __repr__(self):
        return self.__class__.__name__ + ' (' + str(self.in_features) + ' -> ' + str(self.out_features) + ')'


class OCGA(nn.Module):

    def __init__(self, in_feature, out_feature, top_k=11, token=3, alpha=0.2, num_heads=1):
        super(OCGA, self).__init__()
        self.top_k = top_k
        hidden_feature = in_feature
        self.conv = nn.Sequential(
            nn.Conv2d(in_feature, hidden_feature, token, stride=token),
            nn.BatchNorm2d(hidden_feature),
            nn.ReLU(inplace=True)
        )
        self.attentions = [
            GraphAttentionLayer(
                hidden_feature, hidden_feature, alpha=alpha, concat=True
            ) for _ in range(num_heads)
        ]

        for i, attention in enumerate(self.attentions):
            self.add_module('attention_{}'.format(i), attention)

        # 定义一个输出的注意力层
        self.out_att = GraphAttentionLayer(
            hidden_feature * num_heads, out_feature, alpha=alpha, concat=False)

        self.deconv = build_upsample_layer(
            cfg=dict(type='deconv',
                     in_channels=out_feature, out_channels=out_feature,
                     kernel_size=token, stride=token)
        )
        self.activation = nn.ELU()
        self._init_weights()

    def _init_weights(self):
        for m in [self.deconv]:
            nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
            nn.init.constant_(m.bias, 0)

    def forward(self, x):
        h = self.conv(x)
        batch_size, in_feature, column, row = h.shape
        # 计算节点数量
        node_num = column * row
        h = h.view(
            batch_size, in_feature, node_num).permute(0, 2, 1)
        adj = adj_index(h, self.top_k, node_num)

        # 如有多头的设置,需要拼接
        h = torch.cat([att(h, adj) for att in self.attentions], dim=2)
        h = self.activation(self.out_att(h, adj))

        h = h.view(batch_size, column, row, -1).permute(0, 3, 1, 2)
        h = F.interpolate(
            self.deconv(h), x.shape[-2:], mode='bilinear', align_corners=True)
        return F.relu(h + x)

if __name__ == '__main__':
    batch_size = 2
    in_channels = 8
    out_channels = 8
    H, W = 32, 32
    token = 3
    top_k = 8            # 一定要 < node_num
    num_heads = 2

    block = OCGA(
        in_feature=in_channels,
        out_feature=out_channels,
        top_k=top_k,
        token=token,
        num_heads=num_heads
    ).to('cuda')

    x = torch.randn(batch_size, in_channels, H, W).to('cuda')

    y = block(x)


    print('Input shape :', x.shape)
    print('Output shape:', y.shape)

环境与依赖

Python 3.9+

继续浏览

下一篇OFU 球面偏移场上采样模块球面几何引导采样:构造 (sin u, cos u, sin v, cos v, cos u·cos v) 五维球面几何图并与特征图拼接,仅用两个卷积层预测偏移场 (Δu, Δv),

相关资源