CoolFace
Modelpublic

InPeerReview/RemoteSensingChangeDetection-RSCD.HA2F

sourceHugging Faceupdated 10mo agoView on Hugging Face
2likes
decoder.py301 linesDownload Raw Back to model
1import torch2import torch.nn as nn3import torch.nn.functional as F4from einops import rearrange5from model.utils import weight_init6 7 8 9def drop_path(x, drop_prob: float = 0., training: bool = False):10    if drop_prob == 0. or not training:11        return x12    keep_prob = 1 - drop_prob13    shape = (x.shape[0],) + (1,) * (x.ndim - 1)14    random_tensor = keep_prob + torch.rand(shape, dtype=x.dtype, device=x.device)15    random_tensor.floor_()  # binarize16    output = x.div(keep_prob) * random_tensor17    return output18 19 20class DropPath(nn.Module):21    def __init__(self, drop_prob=None):22        super(DropPath, self).__init__()23        self.drop_prob = drop_prob24 25    def forward(self, x):26        return drop_path(x, self.drop_prob, self.training)27 28 29class Mlp(nn.Module):30    def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.):31        super().__init__()32        out_features = out_features or in_features33        hidden_features = hidden_features or in_features34        self.fc1 = nn.Linear(in_features, hidden_features)35        self.act = act_layer()36        self.fc2 = nn.Linear(hidden_features, out_features)37        self.drop = nn.Dropout(drop)38 39    def forward(self, x):40        x = self.fc1(x)41        x = self.act(x)42        x = self.drop(x)43        x = self.fc2(x)44        x = self.drop(x)45        return x46 47 48 49class CrossAttention(nn.Module):50    def __init__(self, dim1, dim2, num_heads=8, qkv_bias=False, attn_drop=0., proj_drop=0.):51        super().__init__()52        self.num_heads = num_heads53        head_dim = dim1 // num_heads54        self.scale = head_dim ** -0.555 56        self.q = nn.Linear(dim1, dim1, bias=qkv_bias)57        self.kv = nn.Linear(dim2, dim1 * 2, bias=qkv_bias)58 59        self.attn_drop = nn.Dropout(attn_drop)60        self.proj = nn.Linear(dim1, dim1)61        self.proj_drop = nn.Dropout(proj_drop)62 63    def forward(self, x, y):64        B1, N1, C1 = x.shape65        B2, N2, C2 = y.shape66 67        q = self.q(x).reshape(B1, N1, self.num_heads, C1 // self.num_heads).permute(0, 2, 1, 3)68        kv = self.kv(y).reshape(B2, N2, 2, self.num_heads, C1 // self.num_heads).permute(2, 0, 3, 1, 4)69 70        k, v = kv[0], kv[1]71 72        attn = (q @ k.transpose(-2, -1)) * self.scale73        attn = attn.softmax(dim=-1)74        attn = self.attn_drop(attn)75 76        x = (attn @ v).transpose(1, 2).reshape(B1, N1, C1)77 78        x = self.proj(x)79        x = self.proj_drop(x)80 81        return x82 83 84 85class Block(nn.Module):86    def __init__(self, dim1, dim2, num_heads, mlp_ratio=4., qkv_bias=False, drop=0., attn_drop=0.,87                 drop_path=0., act_layer=nn.GELU, norm_layer=nn.LayerNorm):88        super().__init__()89        self.norm1 = norm_layer(dim1)90        self.norm2 = norm_layer(dim2)91        self.attn = CrossAttention(dim1, dim2, num_heads=num_heads, qkv_bias=qkv_bias, attn_drop=attn_drop, proj_drop=drop)92        self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()93        self.norm3 = norm_layer(dim1)94        mlp_hidden_dim = int(dim1 * mlp_ratio)95        self.mlp = Mlp(in_features=dim1, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop)96 97    def forward(self, x, y):98        x = x + self.drop_path(self.attn(self.norm1(x), self.norm2(y)))99        x = x + self.drop_path(self.mlp(self.norm3(x)))100        return x101 102 103 104class ContentAwareAggregation(nn.Module):105    def __init__(self, low_dim, high_dim):106        super().__init__()107        self.project = nn.Sequential(108            nn.Conv2d(high_dim, low_dim, kernel_size=1),109            nn.BatchNorm2d(low_dim),110            nn.ReLU(inplace=True)111        )112 113        self.attn_gen = nn.Sequential(114            nn.Conv2d(low_dim, low_dim, kernel_size=3, padding=1, groups=low_dim),115            nn.BatchNorm2d(low_dim),116            nn.ReLU(inplace=True),117            nn.Conv2d(low_dim, low_dim, kernel_size=1),118            nn.Sigmoid()119        )120 121    def forward(self, low_feat, high_feat):122        high_feat = F.interpolate(high_feat, size=low_feat.shape[2:], mode='bilinear', align_corners=False)123        high_feat = self.project(high_feat)124        attn = self.attn_gen(low_feat + high_feat)125        out = attn * low_feat + high_feat126        return out127 128 129 130class FeatureInjector(nn.Module):131    def __init__(self, dim1=384, dim2=[64, 128, 256], num_heads=8, mlp_ratio=4., qkv_bias=False, drop=0., attn_drop=0.,132                    drop_path=0., act_layer=nn.ReLU, norm_layer=nn.LayerNorm):133        super().__init__()134 135        self.c2_c5 = Block(dim1, dim2[0], num_heads, mlp_ratio, qkv_bias, drop, attn_drop, drop_path, act_layer, norm_layer)136        self.c3_c5 = Block(dim1, dim2[1], num_heads, mlp_ratio, qkv_bias, drop, attn_drop, drop_path, act_layer, norm_layer)137        self.c4_c5 = Block(dim1, dim2[2], num_heads, mlp_ratio, qkv_bias, drop, attn_drop, drop_path, act_layer, norm_layer)138 139        self.fuse = nn.Conv2d(dim1*3, dim1, 1, bias=False)140        self.caa = ContentAwareAggregation(dim1, dim1)141 142        weight_init(self)143 144    def base_forward(self, c2, c3, c4, c5):145        H, W = c5.shape[2:]146 147        c2 = rearrange(c2, 'b c h w -> b (h w) c')148        c3 = rearrange(c3, 'b c h w -> b (h w) c')149        c4 = rearrange(c4, 'b c h w -> b (h w) c')150        c5 = rearrange(c5, 'b c h w -> b (h w) c')151 152        _c2 = self.c2_c5(c5, c2)153        _c2 = rearrange(_c2, 'b (h w) c -> b c h w', h=H, w=W)154 155        _c3 = self.c3_c5(c5, c3)156        _c3 = rearrange(_c3, 'b (h w) c -> b c h w', h=H, w=W)157 158        _c4 = self.c4_c5(c5, c4)159        _c4 = rearrange(_c4, 'b (h w) c -> b c h w', h=H, w=W)160 161        _c5 = self.fuse(torch.cat([_c2, _c3, _c4], dim=1))162 163        return _c5164 165    def forward(self, fx, fy):166        _c5x = self.base_forward(fx[0], fx[1], fx[2], fx[3])167        _c5y = self.base_forward(fy[0], fy[1], fy[2], fy[3])168 169 170        _c5x = self.caa(_c5x, _c5y)171        _c5y = self.caa(_c5y, _c5x)172 173        return _c5x, _c5y174 175 176class DualAttentionGate(nn.Module):177    def __init__(self, channels, ratio=8):178        super().__init__()179        self.channel_att = nn.Sequential(180            nn.AdaptiveAvgPool2d(1),  # [B,C,1,1]181            nn.Conv2d(channels, channels // ratio, 1, bias=False),  # [B,C/8,1,1]182            nn.ReLU(),183            nn.Conv2d(channels // ratio, channels, 1, bias=False),  # [B,C,1,1]184            nn.Sigmoid()185        )186 187        self.spatial_att = nn.Sequential(188            nn.Conv2d(2, 1, 7, padding=3, bias=False),  # 输入2通道(mean+std)189            nn.Sigmoid()  # 输出[B,1,H,W]190        )191 192    def forward(self, x):193 194        c_att = self.channel_att(x)195        mean = torch.mean(x, dim=1, keepdim=True)196        std = torch.std(x, dim=1, keepdim=True)197        s_att = self.spatial_att(torch.cat([mean, std], dim=1))198 199 200        return x * c_att * s_att201 202 203class SimplifiedFGFM(nn.Module):204    def __init__(self, in_channels, out_channels):205        super().__init__()206        self.down = nn.Conv2d(in_channels, out_channels, 1, bias=False)207        self.flow_make = nn.Conv2d(out_channels * 2, 4, 3, padding=1, bias=False)208        self.dual_att = DualAttentionGate(out_channels)209 210    def flow_warp(self, input, flow, size):211 212        out_h, out_w = size213        n, c, h, w = input.size()214 215        norm = torch.tensor([[[[out_w, out_h]]]]).type_as(input).to(input.device)216        grid = torch.meshgrid(217            torch.linspace(-1.0, 1.0, out_h),218            torch.linspace(-1.0, 1.0, out_w),219            indexing='ij'220        )221        grid = torch.stack((grid[1], grid[0]), 2).repeat(n, 1, 1, 1).type_as(input)222        grid = grid + flow.permute(0, 2, 3, 1) / norm223 224        return F.grid_sample(input, grid, align_corners=True)225 226    def forward(self, lowres_feature, highres_feature):227 228        l_feature = self.down(lowres_feature)229        l_feature_up = F.interpolate(l_feature, size=highres_feature.shape[2:], mode='bilinear', align_corners=True)230 231        flow = self.flow_make(torch.cat([l_feature_up, highres_feature], dim=1))232        flow_l, flow_h = flow[:, :2, :, :], flow[:, 2:, :, :]233 234        l_warp = self.flow_warp(l_feature, flow_l, highres_feature.shape[2:])235        h_warp = self.flow_warp(highres_feature, flow_h, highres_feature.shape[2:])236 237 238        fused = self.dual_att(l_warp + h_warp)239        return fused240 241 242 243class Decoder(nn.Module):244    def __init__(self, in_dim=[64, 128, 256, 384], decay=4, num_class=1):245        super().__init__()246        c2_channel, c3_channel, c4_channel, c5_channel = in_dim247 248        self.structure_enhance = FeatureInjector(dim1=c5_channel)249 250 251        self.fgfm_c4 = SimplifiedFGFM(in_channels=c5_channel, out_channels=c4_channel)252        self.fgfm_c3 = SimplifiedFGFM(in_channels=c4_channel, out_channels=c3_channel)253        self.fgfm_c2 = SimplifiedFGFM(in_channels=c3_channel, out_channels=c2_channel)254 255 256        self.classfier = nn.Sequential(257            nn.ConvTranspose2d(c2_channel, c2_channel, kernel_size=4, stride=2, padding=1),258            nn.Conv2d(c2_channel, num_class, 3, 1, padding=1, bias=False)259        )260 261 262        self.mlp = nn.ModuleList([263            nn.Sequential(264                nn.Conv2d(dim * 3, dim // decay, 1, bias=False),265                nn.BatchNorm2d(dim // decay),266                nn.ReLU(),267                nn.Conv2d(dim // decay, dim // decay, 3, 1, padding=1, bias=False),268                nn.ReLU(),269                nn.Conv2d(dim // decay, dim // decay, 3, 1, padding=1, bias=False),270                nn.ReLU(),271                nn.Conv2d(dim // decay, dim, 3, 1, padding=1, bias=False)272            ) for dim in in_dim273        ])274 275    def difference_modeling(self, x, y, block):276        f = torch.cat([x, y, torch.abs(x - y)], dim=1)277        return block(f)278 279    def forward(self, fx, fy):280        c2x, c3x, c4x = fx[:-1]281        c2y, c3y, c4y = fy[:-1]282 283 284        c5x, c5y = self.structure_enhance(fx, fy)285 286 287        c2 = self.difference_modeling(c2x, c2y, self.mlp[0])288        c3 = self.difference_modeling(c3x, c3y, self.mlp[1])289        c4 = self.difference_modeling(c4x, c4y, self.mlp[2])290        c5 = self.difference_modeling(c5x, c5y, self.mlp[3])291 292 293        c4f = self.fgfm_c4(c5, c4)294        c3f = self.fgfm_c3(c4f, c3)295        c2f = self.fgfm_c2(c3f, c2)296 297 298        pred = self.classfier(c2f)299        pred_mask = torch.sigmoid(pred)300 301        return pred_mask