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

TFF 时序特征融合模块

跨时间门控机制:双时相特征先相减得到粗变化表征,再分别与两时刻特征拼接,经深度可分离卷积、1×1 卷积 + Sigmoid 打分后加权融合,增强感兴趣变化、抑制非感兴趣变化

时间序列遥感变化检测TFF时序特征融合ICME2023

结构图

TFF 时序特征融合模块结构图
TFF 时序特征融合模块结构图

跨时间门控机制:双时相特征先相减得到粗变化表征,再分别与两时刻特征拼接,经深度可分离卷积、1×1 卷积 + Sigmoid 打分后加权融合,增强感兴趣变化、抑制非感兴趣变化

论文信息

  • 论文:STNet: Spatial and Temporal feature fusion network for change detection in remote sensing images
  • 出处:ICME 2023

核心痛点

遥感变化检测常被当普通二分类,仅靠特征拼接或特征相减得到变化表征;可双时相影像受天气、季节、光照等干扰,产生大量"非感兴趣变化"(如车辆、设备),简单拼接相减对干扰一视同仁,抓不住真正的变化。

模块亮点

  • 跨时间门控机制:双时相特征先相减得到粗变化表征,再分别与两时刻特征拼接,经深度可分离卷积、1×1 卷积 + Sigmoid 打分后加权融合,增强感兴趣变化、抑制非感兴趣变化
  • 消融涨点显著:WHU 数据集上,基线 F1 为 79.65%,只加 TFF 就提升到 84.20%(+4.55);配合 SFF 的 STNet 达到 87.46% F1,LEVIR-CD、CLCD 上同样 SOTA
  • 轻量不贵:用深度可分离卷积替代普通卷积,大幅降低参数量与计算量;STNet 仅 14.6M 参数、9.61G FLOPs,计算代价在对比方法中最低

适用场景

领域为遥感影像变化检测;原文在 WHU、LEVIR-CD、CLCD 验证了双时相二元变化检测;可迁移到"双时相 / 双路特征对比融合"任务,如医学影像前后片对比、视频变化检测、孪生匹配网络的特征融合。

插入位置建议

  • 替换 IFNet、DTCDSCN、SNUNet 等孪生变化检测网络中的特征拼接或相减操作
  • 放在双流骨干每个残差块输出后逐尺度融合双时相特征,原文就是在 ResNet-18 每个残差块后接 TFF
  • 用在任意两路特征需要"先对比再融合"的场所,如孪生跟踪网络模板与搜索特征的融合

适用任务

  • 遥感影像变化检测

完整代码

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


"""
在本文中提出了一种基于空间和时间特征融合的网络STNet。具体而言,我们设计了一个时间特征融合(TFF)模块,使用跨时间门控机制结合双时态特征来强调感兴趣的变化;
部署了一个空间特征融合模块(SFF),使用跨尺度注意机制捕获细粒度信息,以恢复变化表示的空间细节。
"""

def dsconv_3x3(in_channel, out_channel):
    return nn.Sequential(
        nn.Conv2d(in_channel, in_channel, kernel_size=3, stride=1, padding=1, groups=in_channel),
        nn.Conv2d(in_channel, out_channel, kernel_size=1, stride=1, padding=0, groups=1),
        nn.BatchNorm2d(out_channel),
        nn.ReLU(inplace=True)
    )

def conv_1x1(in_channel, out_channel):
    return nn.Sequential(
        nn.Conv2d(in_channel, out_channel, kernel_size=1, stride=1, padding=0, bias=False),
        nn.BatchNorm2d(out_channel),
        nn.ReLU(inplace=True)
    )


class TFF(nn.Module):
    def __init__(self, in_channel, out_channel):
        super(TFF, self).__init__()
        self.catconvA = dsconv_3x3(in_channel * 2, in_channel)
        self.catconvB = dsconv_3x3(in_channel * 2, in_channel)
        self.catconv = dsconv_3x3(in_channel * 2, out_channel)
        self.convA = nn.Conv2d(in_channel, 1, 1)
        self.convB = nn.Conv2d(in_channel, 1, 1)
        self.sigmoid = nn.Sigmoid()

    def forward(self, xA, xB):
        x_diff = xA - xB

        x_diffA = self.catconvA(torch.cat([x_diff, xA], dim=1))
        x_diffB = self.catconvB(torch.cat([x_diff, xB], dim=1))

        A_weight = self.sigmoid(self.convA(x_diffA))
        B_weight = self.sigmoid(self.convB(x_diffB))

        xA = A_weight * xA
        xB = B_weight * xB

        x = self.catconv(torch.cat([xA, xB], dim=1))

        return x


if __name__ == '__main__':
    in_channel = 3
    out_channel = 3
    block = TFF(in_channel, out_channel)

    # Create dummy inputs
    xA = torch.rand(1, in_channel, 32, 32)
    xB = torch.rand(1, in_channel, 32, 32)

    # Forward pass
    output = block(xA, xB)
    print(f"Input A size: {xA.size()}")
    print(f"Input B size: {xB.size()}")
    print(f"Output size: {output.size()}")


class SelfAttentionBlock(nn.Module):
    """
    query_feats: (B, C, h, w)
    key_feats: (B, C, h, w)
    value_feats: (B, C, h, w)

    output: (B, C, h, w)
    """

    def __init__(self, key_in_channels, query_in_channels, transform_channels, out_channels,
                 key_query_num_convs, value_out_num_convs):
        super(SelfAttentionBlock, self).__init__()
        self.key_project = self.buildproject(
            in_channels=key_in_channels,
            out_channels=transform_channels,
            num_convs=key_query_num_convs,
        )
        self.query_project = self.buildproject(
            in_channels=query_in_channels,
            out_channels=transform_channels,
            num_convs=key_query_num_convs
        )
        self.value_project = self.buildproject(
            in_channels=key_in_channels,
            out_channels=transform_channels,
            num_convs=value_out_num_convs
        )
        self.out_project = self.buildproject(
            in_channels=transform_channels,
            out_channels=out_channels,
            num_convs=value_out_num_convs
        )
        self.transform_channels = transform_channels

    def forward(self, query_feats, key_feats, value_feats):
        batch_size = query_feats.size(0)

        query = self.query_project(query_feats)
        query = query.reshape(*query.shape[:2], -1)
        query = query.permute(0, 2, 1).contiguous()  # (B, h*w, C)

        key = self.key_project(key_feats)
        key = key.reshape(*key.shape[:2], -1)  # (B, C, h*w)

        value = self.value_project(value_feats)
        value = value.reshape(*value.shape[:2], -1)
        value = value.permute(0, 2, 1).contiguous()  # (B, h*w, C)

        sim_map = torch.matmul(query, key)

        sim_map = (self.transform_channels ** -0.5) * sim_map
        sim_map = F.softmax(sim_map, dim=-1)  # (B, h*w, K)

        context = torch.matmul(sim_map, value)  # (B, h*w, C)
        context = context.permute(0, 2, 1).contiguous()
        context = context.reshape(batch_size, -1, *query_feats.shape[2:])  # (B, C, h, w)

        context = self.out_project(context)  # (B, C, h, w)
        return context

    def buildproject(self, in_channels, out_channels, num_convs):
        convs = nn.Sequential(
            nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=False),
            nn.BatchNorm2d(out_channels),
            nn.ReLU(inplace=True)
        )
        for _ in range(num_convs - 1):
            convs.append(
                nn.Sequential(
                    nn.Conv2d(out_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=False),
                    nn.BatchNorm2d(out_channels),
                    nn.ReLU(inplace=True)
                )
            )
        if len(convs) > 1:
            return nn.Sequential(*convs)
        return convs[0]

def conv_3x3(in_channel, out_channel):
    return nn.Sequential(
        nn.Conv2d(in_channel, out_channel, kernel_size=3, stride=1, padding=1, bias=False),
        nn.BatchNorm2d(out_channel),
        nn.ReLU(inplace=True)
    )



class SFF(nn.Module):
    def __init__(self, in_channel):
        super(SFF, self).__init__()
        self.conv_small = conv_1x1(in_channel, in_channel)
        self.conv_big = conv_1x1(in_channel, in_channel)
        self.catconv = conv_3x3(in_channel *2, in_channel)
        self.attention = SelfAttentionBlock(
            key_in_channels=in_channel,
            query_in_channels = in_channel,
            transform_channels = in_channel // 2,
            out_channels = in_channel,
            key_query_num_convs=2,
            value_out_num_convs=1
        )

    def forward(self, x_small, x_big):
        img_size =x_big.size(2), x_big.size(3)
        x_small = F.interpolate(x_small, img_size, mode="bilinear", align_corners=False)
        x = self.conv_small(x_small) + self.conv_big(x_big)
        new_x = self.attention(x, x, x_big)

        out = self.catconv(torch.cat([new_x, x_big], dim=1))
        return out


if __name__ == '__main__':
    in_channel = 3
    block = SFF(in_channel)

    # Create dummy inputs
    x_small = torch.rand(1, in_channel, 32, 32)
    x_big = torch.rand(1, in_channel, 32, 32)

    # Forward pass
    output = block(x_small, x_big)
    print(f"Input small size: {x_small.size()}")
    print(f"Input big size: {x_big.size()}")
    print(f"Output size: {output.size()}")

环境与依赖

Python 3.9+

继续浏览

下一篇THA 交谈头注意力跨头双向通信:在 softmax 前对注意力 logits、softmax 后对注意力权重各插一个可学习线性投影 Pl、Pw,让信息跨头流动,每个头的注意力由所有 query 和 key 共同决定;

相关资源