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

FSSA 频域稀疏自注意力

到频域里做注意力:对特征图做傅里叶变换,高频信息在频域被显著突出,再在频域做自注意力,直接建模不同频率成分的关联,最后逆变换回空间域,输入输出都是普通特征图

计算机视觉遥感图像超分辨率FSSA频域学习TGRS2025

结构图

FSSA 频域稀疏自注意力结构图
FSSA 频域稀疏自注意力结构图

到频域里做注意力:对特征图做傅里叶变换,高频信息在频域被显著突出,再在频域做自注意力,直接建模不同频率成分的关联,最后逆变换回空间域,输入输出都是普通特征图

论文信息

  • 论文:Mitigating Texture Bias: A Remote Sensing Super-Resolution Method Focusing on High-Frequency Texture Reconstruction
  • 出处:IEEE TGRS 2025

核心痛点

遥感超分是病态问题,退化会丢失大量高频细节;现有模型提取频域特征、建模频率关联的能力有限,Transformer 全像素算注意力开销巨大、窗口化后感受野受限,已有频域方法又忽略频率间关系,可能放大有害频率。

模块亮点

  • 到频域里做注意力:对特征图做傅里叶变换,高频信息在频域被显著突出,再在频域做自注意力,直接建模不同频率成分的关联,最后逆变换回空间域,输入输出都是普通特征图
  • 稀疏省算力又抗噪:用角度局部敏感哈希(LSH)分桶、只在桶内算注意力,多轮哈希降误分率,相邻桶交互防止特征被隔开,频域稀疏度约 93%,压计算量、抑噪声
  • 整体模型涨点明确:FSSA 是 FAT 的核心组件,FAT 在 RSSR25、AID、DOTA 上平均 PSNR、SSIM 最高且 LPIPS 最低;RSSR25 上 FAT† 比 TTST 高 0.032 dB,×2 尺度比 SwinFIR 高 0.054 dB

适用场景

领域为遥感图像超分辨率;原文在 RSSR25 及 AID、DOTA v1.0 上做了多尺度验证;也适合自然图像超分、去噪、去模糊等任务。

插入位置建议

  • 与 Swin Transformer 块中的 W-MSA 并联,原文 MAT 层即 W-MSA(LN(X)) + FSSA(X) + X
  • 串在 Transformer 块末端,原文每个 SRTB 结尾都接一个 FSSA
  • 放在任意骨干的全局特征提取处,模块内部自动完成 FFT 与逆变换

适用任务

  • 遥感图像超分辨率

完整代码

fssa.py
import torch
import torch.nn as nn
import torch.nn.functional as F



def batched_index_select(values, indices):
    last_dim = values.shape[-1]
    return values.gather(1, indices[:, :, None].expand(-1, -1, last_dim))


def default_conv(in_channels, out_channels, kernel_size, stride=1, bias=True):
    return nn.Conv2d(
        in_channels, out_channels, kernel_size,
        padding=(kernel_size // 2), stride=stride, bias=bias)


class BasicBlock(nn.Sequential):
    def __init__(
            self, conv, in_channels, out_channels, kernel_size, stride=1, bias=True,
            bn=False, act=nn.PReLU()):

        m = [conv(in_channels, out_channels, kernel_size, bias=bias)]
        if bn:
            m.append(nn.BatchNorm2d(out_channels))
        if act is not None:
            m.append(act)

        super(BasicBlock, self).__init__(*m)


class SparseAttention(nn.Module):
    def __init__(self, n_hashes=4, channels=256, k_size=3, reduction=8, chunk_size=120, conv=default_conv, res_scale=1):
        super(SparseAttention, self).__init__()
        self.chunk_size = chunk_size
        self.n_hashes = n_hashes
        self.reduction = reduction
        self.res_scale = res_scale
        self.conv_match = BasicBlock(conv, channels, channels // reduction, k_size, bn=False, act=None)
        self.conv_assembly = BasicBlock(conv, channels, channels, 1, bn=False, act=None)

    def LSH(self, hash_buckets, x):
        # x: [N,H*W,C]
        N = x.shape[0]
        device = x.device

        # generate random rotation matrix
        rotations_shape = (1, x.shape[-1], self.n_hashes, hash_buckets // 2)  # [1,C,n_hashes,hash_buckets//2]
        random_rotations = torch.randn(rotations_shape, dtype=x.dtype, device=device).expand(N, -1, -1,
                                                                                             -1)  # [N, C, n_hashes, hash_buckets//2]

        # locality sensitive hashing
        rotated_vecs = torch.einsum('btf,bfhi->bhti', x, random_rotations)  # [N, n_hashes, H*W, hash_buckets//2]
        rotated_vecs = torch.cat([rotated_vecs, -rotated_vecs], dim=-1)  # [N, n_hashes, H*W, hash_buckets]

        # get hash codes
        hash_codes = torch.argmax(rotated_vecs, dim=-1)  # [N,n_hashes,H*W]

        # add offsets to avoid hash codes overlapping between hash rounds
        offsets = torch.arange(self.n_hashes, device=device)
        offsets = torch.reshape(offsets * hash_buckets, (1, -1, 1))
        hash_codes = torch.reshape(hash_codes + offsets, (N, -1,))  # [N,n_hashes*H*W]

        return hash_codes

    def add_adjacent_buckets(self, x):
        x_extra_back = torch.cat([x[:, :, -1:, ...], x[:, :, :-1, ...]], dim=2)
        x_extra_forward = torch.cat([x[:, :, 1:, ...], x[:, :, :1, ...]], dim=2)
        return torch.cat([x, x_extra_back, x_extra_forward], dim=3)

    def forward(self, input):

        N, _, H, W = input.shape
        x_embed = self.conv_match(input).view(N, -1, H * W).contiguous().permute(0, 2, 1)
        y_embed = self.conv_assembly(input).view(N, -1, H * W).contiguous().permute(0, 2, 1)
        L, C = x_embed.shape[-2:]

        # number of hash buckets/hash bits
        hash_buckets = min(L // self.chunk_size + (L // self.chunk_size) % 2, 64)

        # get assigned hash codes/bucket number
        hash_codes = self.LSH(hash_buckets, x_embed)  # [N,n_hashes*H*W]
        hash_codes = hash_codes.detach()

        # group elements with same hash code by sorting
        _, indices = hash_codes.sort(dim=-1)  # [N,n_hashes*H*W]
        _, undo_sort = indices.sort(dim=-1)  # undo_sort to recover original order
        mod_indices = (indices % L)  # now range from (0->H*W)
        x_embed_sorted = batched_index_select(x_embed, mod_indices)  # [N,n_hashes*H*W,C]
        y_embed_sorted = batched_index_select(y_embed, mod_indices)  # [N,n_hashes*H*W,C]

        # pad the embedding if it cannot be divided by chunk_size
        padding = self.chunk_size - L % self.chunk_size if L % self.chunk_size != 0 else 0
        x_att_buckets = torch.reshape(x_embed_sorted, (N, self.n_hashes, -1, C))  # [N, n_hashes, H*W,C]
        y_att_buckets = torch.reshape(y_embed_sorted, (N, self.n_hashes, -1, C * self.reduction))
        if padding:
            pad_x = x_att_buckets[:, :, -padding:, :].clone()
            pad_y = y_att_buckets[:, :, -padding:, :].clone()
            x_att_buckets = torch.cat([x_att_buckets, pad_x], dim=2)
            y_att_buckets = torch.cat([y_att_buckets, pad_y], dim=2)

        x_att_buckets = torch.reshape(x_att_buckets, (
        N, self.n_hashes, -1, self.chunk_size, C))  # [N, n_hashes, num_chunks, chunk_size, C]
        y_att_buckets = torch.reshape(y_att_buckets, (N, self.n_hashes, -1, self.chunk_size, C * self.reduction))

        x_match = F.normalize(x_att_buckets, p=2, dim=-1, eps=5e-5)

        # allow attend to adjacent buckets
        x_match = self.add_adjacent_buckets(x_match)
        y_att_buckets = self.add_adjacent_buckets(y_att_buckets)

        # unormalized attention score
        raw_score = torch.einsum('bhkie,bhkje->bhkij', x_att_buckets,
                                 x_match)  # [N, n_hashes, num_chunks, chunk_size, chunk_size*3]

        # softmax
        bucket_score = torch.logsumexp(raw_score, dim=-1, keepdim=True)
        score = torch.exp(raw_score - bucket_score)  # (after softmax)
        bucket_score = torch.reshape(bucket_score, [N, self.n_hashes, -1])

        # attention
        ret = torch.einsum('bukij,bukje->bukie', score, y_att_buckets)  # [N, n_hashes, num_chunks, chunk_size, C]
        ret = torch.reshape(ret, (N, self.n_hashes, -1, C * self.reduction))

        # if padded, then remove extra elements
        if padding:
            ret = ret[:, :, :-padding, :].clone()
            bucket_score = bucket_score[:, :, :-padding].clone()

        # recover the original order
        ret = torch.reshape(ret, (N, -1, C * self.reduction))  # [N, n_hashes*H*W,C]
        bucket_score = torch.reshape(bucket_score, (N, -1,))  # [N,n_hashes*H*W]
        ret = batched_index_select(ret, undo_sort)  # [N, n_hashes*H*W,C]
        bucket_score = bucket_score.gather(1, undo_sort)  # [N,n_hashes*H*W]

        # weighted sum multi-round attention
        ret = torch.reshape(ret, (N, self.n_hashes, L, C * self.reduction))  # [N, n_hashes*H*W,C]
        bucket_score = torch.reshape(bucket_score, (N, self.n_hashes, L, 1))
        probs = nn.functional.softmax(bucket_score, dim=1)
        ret = torch.sum(ret * probs, dim=1)

        ret = ret.permute(0, 2, 1).view(N, -1, H, W).contiguous() * self.res_scale + input
        return ret

class FourierSparseSelfAttention(nn.Module):
    def __init__(self,n_hashes=4, embed_dim=128, fft_norm='ortho'):
        # bn_layer not used
        super(FourierSparseSelfAttention, self).__init__()
        self.conv_layer = torch.nn.Conv2d(embed_dim * 2, embed_dim * 2, 1, 1, 0)
        self.relu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
        self.sparse_attention = SparseAttention( n_hashes=n_hashes, channels=embed_dim* 2, k_size=3, reduction=4, chunk_size=144, conv=default_conv, res_scale=1)

        self.fft_norm = fft_norm

    def forward(self, x):
        batch = x.shape[0]
        fft_dim = (-2, -1)
        ffted = torch.fft.rfftn(x, dim=fft_dim, norm=self.fft_norm)
        ffted = torch.stack((ffted.real, ffted.imag), dim=-1)
        ffted = ffted.permute(0, 1, 4, 2, 3).contiguous()
        ffted = ffted.view((batch, -1,) + ffted.size()[3:])

        ffted = self.sparse_attention(ffted)

        ffted = ffted.view((batch, -1, 2,) + ffted.size()[2:]).permute(0, 1, 3, 4,
                                                                       2).contiguous()
        ffted = torch.complex(ffted[..., 0], ffted[..., 1])

        ifft_shape_slice = x.shape[-2:]
        output = torch.fft.irfftn(ffted, s=ifft_shape_slice, dim=fft_dim, norm=self.fft_norm)

        return output




if __name__ == '__main__':
    dim = 128
    block = FourierSparseSelfAttention(n_hashes=2, embed_dim=dim).to('cuda')
    input = torch.ones([1, dim, 120, 120]).to('cuda')
    output = block(input)
    print(input.size())
    print(output.size())

环境与依赖

Python 3.9+

继续浏览

下一篇GBC 门控瓶颈卷积低秩瓶颈省参数:BottConv 用逐点卷积把特征投影进低秩子空间,深度卷积在子空间内做空间提取后再投回,复杂度按低秩比例近似下降;替换原始卷积后参数量与计算负载大幅降低、性能影响极小

相关资源