File size: 14,655 Bytes
7529108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
"""
HAT: Hybrid Attention Transformer for Image Super-Resolution (Chen et al., CVPR 2023)
Adapted for 4-band Multispectral Satellite Imagery [B04 (Red), B03 (Green), B02 (Blue), B08 (NIR)].

Key Architectural Innovations for Satellite SRM:
1. Spatial Self-Attention (W-MSA / SW-MSA): Resolves complex geometric boundaries, roads, building outlines.
2. Channel Attention Block (CAB): Interleaved spectral cross-band modulation. Preserves spectral indices (NDVI, NDWI).
3. Overlapping Cross-Attention Block (OCAB): Eliminates window boundary artifacts without blocking seams.
4. Residual Hybrid Attention Group (RHAG): Deep residual feature hierarchy.
5. Bicubic Skip + Dual Prediction Head: 4-band 2.5 m reflectances + per-pixel aleatoric uncertainty (Laplace NLL).
"""

from __future__ import annotations

import math
import torch
import torch.nn as nn
import torch.nn.functional as F
def up_bicubic(x: torch.Tensor, s: int = 4) -> torch.Tensor:
    return F.interpolate(x, scale_factor=s, mode="bicubic", align_corners=False)

def window_partition(x: torch.Tensor, window_size: int) -> torch.Tensor:
    """x: (B, H, W, C) -> (num_windows*B, window_size, window_size, C)"""
    B, H, W, C = x.shape
    x = x.view(B, H // window_size, window_size, W // window_size, window_size, C)
    windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, C)
    return windows


def window_reverse(windows: torch.Tensor, window_size: int, H: int, W: int) -> torch.Tensor:
    """windows: (num_windows*B, window_size, window_size, C) -> (B, H, W, C)"""
    B = int(windows.shape[0] / (H * W / window_size / window_size))
    x = windows.view(B, H // window_size, W // window_size, window_size, window_size, -1)
    x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1)
    return x


class ChannelAttention(nn.Module):
    """Squeeze-and-Excitation Spectral Channel Attention."""
    def __init__(self, dim: int, reduction: int = 4):
        super().__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.conv_du = nn.Sequential(
            nn.Conv2d(dim, dim // reduction, 1, padding=0, bias=True),
            nn.ReLU(inplace=True),
            nn.Conv2d(dim // reduction, dim, 1, padding=0, bias=True),
            nn.Sigmoid()
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        # x: (B, C, H, W)
        w = self.conv_du(self.avg_pool(x))
        return x * w


class ChannelAttentionBlock(nn.Module):
    """Channel Attention Block (CAB) to capture inter-band spectral correlation."""
    def __init__(self, dim: int, reduction: int = 4):
        super().__init__()
        self.body = nn.Sequential(
            nn.Conv2d(dim, dim, 3, 1, 1),
            nn.GELU(),
            nn.Conv2d(dim, dim, 3, 1, 1),
            ChannelAttention(dim, reduction=reduction)
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return x + self.body(x)


class Mlp(nn.Module):
    def __init__(self, in_features: int, hidden_features: int | None = None,
                 out_features: int | None = None, drop: float = 0.0):
        super().__init__()
        out_features = out_features or in_features
        hidden_features = hidden_features or in_features
        self.fc1 = nn.Linear(in_features, hidden_features)
        self.act = nn.GELU()
        self.fc2 = nn.Linear(hidden_features, out_features)
        self.drop = nn.Dropout(drop)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.drop(self.fc2(self.drop(self.act(self.fc1(x)))))


class WindowAttention(nn.Module):
    def __init__(self, dim: int, window_size: tuple[int, int], num_heads: int,
                  qkv_bias: bool = True, qk_scale: float | None = None,
                  attn_drop: float = 0.0, proj_drop: float = 0.0):
        super().__init__()
        self.dim = dim
        self.window_size = window_size
        self.num_heads = num_heads
        head_dim = dim // num_heads
        self.scale = qk_scale or head_dim**-0.5

        # Relative position bias table
        self.relative_position_bias_table = nn.Parameter(
            torch.zeros((2 * window_size[0] - 1) * (2 * window_size[1] - 1), num_heads)
        )
        nn.init.trunc_normal_(self.relative_position_bias_table, std=0.02)

        coords_h = torch.arange(self.window_size[0])
        coords_w = torch.arange(self.window_size[1])
        coords = torch.stack(torch.meshgrid([coords_h, coords_w], indexing="ij"))
        coords_flatten = torch.flatten(coords, 1)
        relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :]
        relative_coords = relative_coords.permute(1, 2, 0).contiguous()
        relative_coords[:, :, 0] += self.window_size[0] - 1
        relative_coords[:, :, 1] += self.window_size[1] - 1
        relative_coords[:, :, 0] *= 2 * self.window_size[1] - 1
        relative_position_index = relative_coords.sum(-1)
        self.register_buffer("relative_position_index", relative_position_index)

        self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
        self.attn_drop = nn.Dropout(attn_drop)
        self.proj = nn.Linear(dim, dim)
        self.proj_drop = nn.Dropout(proj_drop)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        B_, N, C = x.shape
        qkv = self.qkv(x).reshape(B_, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
        q, k, v = qkv[0], qkv[1], qkv[2]

        q = q * self.scale
        attn = (q @ k.transpose(-2, -1))

        relative_position_bias = self.relative_position_bias_table[self.relative_position_index.view(-1)].view(
            self.window_size[0] * self.window_size[1], self.window_size[0] * self.window_size[1], -1
        )
        relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous()
        attn = attn + relative_position_bias.unsqueeze(0)

        attn = F.softmax(attn, dim=-1)
        attn = self.attn_drop(attn)
        x = (attn @ v).transpose(1, 2).reshape(B_, N, C)
        return self.proj_drop(self.proj(x))


class HybridAttentionBlock(nn.Module):
    """
    Hybrid Attention Block (HAB):
    Fuses spatial Window Multi-head Self-Attention (W-MSA) with Channel Attention Block (CAB).
    """
    def __init__(self, dim: int, num_heads: int, window_size: int = 8, shift_size: int = 0,
                 mlp_ratio: float = 2.0, drop: float = 0.0, attn_drop: float = 0.0):
        super().__init__()
        self.dim = dim
        self.num_heads = num_heads
        self.window_size = window_size
        self.shift_size = shift_size

        self.norm1 = nn.LayerNorm(dim)
        self.attn = WindowAttention(
            dim, window_size=(window_size, window_size), num_heads=num_heads,
            attn_drop=attn_drop, proj_drop=drop
        )

        # Spectral Channel Attention branch
        self.cab = ChannelAttentionBlock(dim, reduction=4)

        self.norm2 = nn.LayerNorm(dim)
        self.mlp = Mlp(in_features=dim, hidden_features=int(dim * mlp_ratio), drop=drop)

    def forward(self, x: torch.Tensor, H: int, W: int) -> torch.Tensor:
        B, L, C = x.shape
        shortcut = x

        # 1. Spatial Window Self-Attention
        x_norm = self.norm1(x).view(B, H, W, C)
        pad_r = (self.window_size - W % self.window_size) % self.window_size
        pad_b = (self.window_size - H % self.window_size) % self.window_size
        x_pad = F.pad(x_norm, (0, 0, 0, pad_r, 0, pad_b))
        _, Hp, Wp, _ = x_pad.shape

        if self.shift_size > 0:
            shifted_x = torch.roll(x_pad, shifts=(-self.shift_size, -self.shift_size), dims=(1, 2))
        else:
            shifted_x = x_pad

        x_windows = window_partition(shifted_x, self.window_size)
        x_windows = x_windows.view(-1, self.window_size * self.window_size, C)
        attn_windows = self.attn(x_windows)
        attn_windows = attn_windows.view(-1, self.window_size, self.window_size, C)
        shifted_x = window_reverse(attn_windows, self.window_size, Hp, Wp)

        if self.shift_size > 0:
            attn_x = torch.roll(shifted_x, shifts=(self.shift_size, self.shift_size), dims=(1, 2))
        else:
            attn_x = shifted_x

        if pad_r > 0 or pad_b > 0:
            attn_x = attn_x[:, :H, :W, :].contiguous()

        attn_out = attn_x.view(B, H * W, C)

        # 2. Channel Attention Branch (operates in B, C, H, W)
        feat_2d = x.transpose(1, 2).view(B, C, H, W)
        cab_out = self.cab(feat_2d).flatten(2).transpose(1, 2)

        # Merge spatial and channel representations
        x = shortcut + attn_out + cab_out

        # 3. Feedforward MLP
        x = x + self.mlp(self.norm2(x))
        return x


class OverlappingCrossAttentionBlock(nn.Module):
    """
    Overlapping Cross-Attention Block (OCAB):
    Enables cross-window interaction across window boundaries to prevent grid artifacts.
    """
    def __init__(self, dim: int, num_heads: int, window_size: int = 8, mlp_ratio: float = 2.0, drop: float = 0.0):
        super().__init__()
        self.dim = dim
        self.num_heads = num_heads
        self.window_size = window_size
        self.norm1 = nn.LayerNorm(dim)
        self.attn = WindowAttention(dim, window_size=(window_size, window_size), num_heads=num_heads, proj_drop=drop)
        self.norm2 = nn.LayerNorm(dim)
        self.mlp = Mlp(in_features=dim, hidden_features=int(dim * mlp_ratio), drop=drop)
        self.conv = nn.Conv2d(dim, dim, 3, 1, 1)

    def forward(self, x: torch.Tensor, H: int, W: int) -> torch.Tensor:
        B, L, C = x.shape
        shortcut = x

        # Window attention with 3x3 depthwise-style boundary blend
        x_norm = self.norm1(x).view(B, H, W, C)
        pad_r = (self.window_size - W % self.window_size) % self.window_size
        pad_b = (self.window_size - H % self.window_size) % self.window_size
        x_pad = F.pad(x_norm, (0, 0, 0, pad_r, 0, pad_b))
        _, Hp, Wp, _ = x_pad.shape

        x_windows = window_partition(x_pad, self.window_size).view(-1, self.window_size * self.window_size, C)
        attn_windows = self.attn(x_windows).view(-1, self.window_size, self.window_size, C)
        attn_rev = window_reverse(attn_windows, self.window_size, Hp, Wp)

        if pad_r > 0 or pad_b > 0:
            attn_rev = attn_rev[:, :H, :W, :].contiguous()

        # Cross-window 3x3 convolution
        feat_2d = attn_rev.permute(0, 3, 1, 2).contiguous()
        conv_out = self.conv(feat_2d).flatten(2).transpose(1, 2)

        x = shortcut + conv_out
        x = x + self.mlp(self.norm2(x))
        return x


class ResidualHybridAttentionGroup(nn.Module):
    """
    Residual Hybrid Attention Group (RHAG):
    Stack of HybridAttentionBlocks (HAB) + OverlappingCrossAttentionBlock (OCAB) + residual conv.
    """
    def __init__(self, dim: int, depth: int, num_heads: int, window_size: int = 8,
                 mlp_ratio: float = 2.0, drop: float = 0.0):
        super().__init__()
        self.blocks = nn.ModuleList([
            HybridAttentionBlock(
                dim=dim, num_heads=num_heads, window_size=window_size,
                shift_size=0 if (i % 2 == 0) else window_size // 2,
                mlp_ratio=mlp_ratio, drop=drop
            )
            for i in range(depth)
        ])
        self.ocab = OverlappingCrossAttentionBlock(
            dim=dim, num_heads=num_heads, window_size=window_size,
            mlp_ratio=mlp_ratio, drop=drop
        )
        self.conv = nn.Conv2d(dim, dim, 3, 1, 1)

    def forward(self, x: torch.Tensor, H: int, W: int) -> torch.Tensor:
        shortcut = x
        for blk in self.blocks:
            x = blk(x, H, W)
        x = self.ocab(x, H, W)
        B, L, C = x.shape
        x_2d = x.transpose(1, 2).view(B, C, H, W)
        x_conv = self.conv(x_2d).flatten(2).transpose(1, 2)
        return shortcut + x_conv


class UpsamplePixelShuffle(nn.Module):
    """4x sub-pixel convolution upsampler."""
    def __init__(self, in_channels: int, out_channels: int, scale: int = 4):
        super().__init__()
        self.conv = nn.Conv2d(in_channels, out_channels * (scale**2), 3, padding=1)
        self.pixel_shuffle = nn.PixelShuffle(scale)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.pixel_shuffle(self.conv(x))


class HAT(nn.Module):
    """
    Hybrid Attention Transformer (HAT) for 4-Band Multispectral Super-Resolution Mapping.
    Outputs:
    - sr: (B, 4, 4*H, 4*W) 2.5 m Super-Resolved reflectances
    - log_scale: (B, 1, 4*H, 4*W) Aleatoric uncertainty log-sigma
    """
    def __init__(self, in_chans: int = 4, out_chans: int = 4, embed_dim: int = 96,
                 depths: tuple[int, ...] = (4, 4, 4, 4), num_heads: tuple[int, ...] = (6, 6, 6, 6),
                 window_size: int = 8, mlp_ratio: float = 2.0, scale: int = 4, drop_rate: float = 0.0):
        super().__init__()
        self.in_chans = in_chans
        self.out_chans = out_chans
        self.scale = scale
        self.window_size = window_size

        # Shallow feature extraction
        self.conv_first = nn.Conv2d(in_chans, embed_dim, 3, 1, 1)

        # Deep feature extraction: stack of RHAG groups
        self.groups = nn.ModuleList([
            ResidualHybridAttentionGroup(
                dim=embed_dim, depth=depths[i], num_heads=num_heads[i],
                window_size=window_size, mlp_ratio=mlp_ratio, drop=drop_rate
            )
            for i in range(len(depths))
        ])
        self.norm = nn.LayerNorm(embed_dim)
        self.conv_after_body = nn.Conv2d(embed_dim, embed_dim, 3, 1, 1)

        # High-resolution sub-pixel reconstruction
        self.upsample = UpsamplePixelShuffle(embed_dim, embed_dim, scale=scale)
        self.conv_last = nn.Conv2d(embed_dim, out_chans, 3, 1, 1)

        # Spatial aleatoric uncertainty head
        self.unc_head = nn.Sequential(
            nn.Conv2d(embed_dim, embed_dim // 2, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(embed_dim // 2, 1, 3, padding=1)
        )

    def forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
        H, W = x.shape[2], x.shape[3]
        bicubic_base = up_bicubic(x, self.scale)

        feat0 = self.conv_first(x)
        feat = feat0.flatten(2).transpose(1, 2)
        for group in self.groups:
            feat = group(feat, H, W)
        feat = self.norm(feat)
        feat = feat.transpose(1, 2).view(feat0.shape)
        feat = feat0 + self.conv_after_body(feat)

        hr_feat = self.upsample(feat)
        residual = self.conv_last(hr_feat)
        sr = bicubic_base + residual
        log_scale = self.unc_head(hr_feat).clamp(-9.0, 2.0)
        return sr, log_scale