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 
 
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
"""
论文 §4.2 段级频谱特征 — PyTorch 可训练封装。

FFT(扩窗分窗 + 整圆)在 elipse_feature / DataLoader 中 CPU 预计算;
本模块实现:全局谱池化 → 门控融合 → BN+LN+Conv1d → 谱轴 MHA → f̄_freq。

默认:N_θ=360, M=10, s_l=36, L_win=72, w=5(与论文 §4.2 / §7.1.3 一致)。
"""

from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Union

import numpy as np
import torch
import torch.nn as nn

from elipse_feature import (
    DEFAULT_N_SEGMENTS,
    DEFAULT_N_THETA,
    FeaFields,
    FeaTensor,
    S_RES_CHANNELS,
    SegmentGeometry,
    build_segment_geometry,
    compute_fft_amplitude_segmented,
    compute_spectral_fft_features,
    extract_ellipse_features,
    preprocess_rgb_to_skeleton,
    responsibility_indices,
    unpack_fea_fields,
    window_indices,
)

PathLike = Union[str, Path]
CHANNEL_LABELS = [r"$\Delta r$", r"$\Delta\theta$", r"$\kappa$", r"$\tau$"]
compute_fft_amplitude_batch = compute_fft_amplitude_segmented


@dataclass
class Section42Config:
    n_theta: int = DEFAULT_N_THETA
    n_segments: int = DEFAULT_N_SEGMENTS
    global_pool_win: int = 5
    c1: int = 32
    d_freq: int = 64
    conv_kernel: int = 3
    num_heads: int = 4


# ---------------------------------------------------------------------------
# §4.2.1 全局谱频轴池化
# ---------------------------------------------------------------------------


class GlobalSpectrumPool(nn.Module):
    """A_global (N_θ/2×4) → 沿频轴 AvgPool1d(w, stride=w) → (s_l×4)。"""

    def __init__(self, kernel: int = 5):
        super().__init__()
        self.pool = nn.AvgPool1d(kernel_size=kernel, stride=kernel)

    def forward(self, amp_global: torch.Tensor) -> torch.Tensor:
        """
        amp_global: (B, N_θ/2, 4) 或 (N_θ/2, 4)
        返回 amp_global_bar: (B, s_l, 4)
        """
        if amp_global.dim() == 2:
            amp_global = amp_global.unsqueeze(0)
        x = amp_global.transpose(1, 2).contiguous()
        x = self.pool(x)
        return x.transpose(1, 2).contiguous()


# ---------------------------------------------------------------------------
# §4.2.3 全局—局部频率门控融合
# ---------------------------------------------------------------------------


class FrequencyGateFusion(nn.Module):
    """α⊙A_loc + (1-α)⊙Ā_global,α=σ(W[loc;glob]+b)。"""

    def __init__(self, c_in: int = S_RES_CHANNELS):
        super().__init__()
        self.proj = nn.Linear(2 * c_in, c_in)

    def forward(
        self,
        amp_loc: torch.Tensor,
        amp_glob_bar: torch.Tensor,
    ) -> torch.Tensor:
        """
        amp_loc: (B, M, s_l, 4)
        amp_glob_bar: (B, s_l, 4)
        返回 amp_fuse: (B, M, s_l, 4)
        """
        g = amp_glob_bar.unsqueeze(1).expand_as(amp_loc)
        alpha = torch.sigmoid(self.proj(torch.cat([amp_loc, g], dim=-1)))
        return alpha * amp_loc + (1.0 - alpha) * g


# ---------------------------------------------------------------------------
# §4.2.4 融合谱域编码
# ---------------------------------------------------------------------------


class SpectralConvNet(nn.Module):
    """
    BN → LN → Conv1d(4→C1) → Conv1d(C1→d_freq),频率轴长度保持 s_l。
    输入 (B, 4, s_l),输出 (B, d_freq, s_l)。
    """

    def __init__(
        self,
        s_l: int,
        c_in: int = S_RES_CHANNELS,
        c1: int = 32,
        d_freq: int = 64,
        kernel: int = 3,
    ):
        super().__init__()
        pad = kernel // 2
        self.s_l = s_l
        self.bn = nn.BatchNorm1d(c_in)
        self.ln = nn.LayerNorm([c_in, s_l])
        self.conv1 = nn.Conv1d(c_in, c1, kernel_size=kernel, padding=pad)
        self.conv2 = nn.Conv1d(c1, d_freq, kernel_size=kernel, padding=pad)
        self.act = nn.GELU()

    def forward(self, amp: torch.Tensor) -> torch.Tensor:
        x = self.bn(amp)
        x = self.ln(x)
        x = self.act(self.conv1(x))
        x = self.conv2(x)
        return x


class SpectralAxisAttention(nn.Module):
    """f' = LayerNorm(f + MHA(f)),沿谱轴(长度 s_l)。"""

    def __init__(self, d_freq: int = 64, num_heads: int = 4):
        super().__init__()
        self.mha = nn.MultiheadAttention(
            d_freq,
            num_heads,
            batch_first=True,
        )
        self.ln = nn.LayerNorm(d_freq)

    def forward(self, f: torch.Tensor) -> torch.Tensor:
        """f: (N, d_freq, s_l) → f': 同形状。"""
        x = f.transpose(1, 2)
        attn, _ = self.mha(x, x, x, need_weights=False)
        return self.ln(x + attn).transpose(1, 2)


class Section42Encoder(nn.Module):
    """§4.2 完整编码器。"""

    def __init__(self, config: Optional[Section42Config] = None):
        super().__init__()
        self.config = config or Section42Config()
        self.geom = build_segment_geometry(
            self.config.n_theta,
            self.config.n_segments,
        )
        self.global_pool = GlobalSpectrumPool(self.config.global_pool_win)
        self.gate_fusion = FrequencyGateFusion()
        self.spectral = SpectralConvNet(
            s_l=self.geom.s_l,
            c1=self.config.c1,
            d_freq=self.config.d_freq,
            kernel=self.config.conv_kernel,
        )
        self.spectral_attn = SpectralAxisAttention(
            self.config.d_freq,
            self.config.num_heads,
        )

    def _normalize_amp_shapes(
        self,
        amp_segment: torch.Tensor,
        amp_global: torch.Tensor,
        device: torch.device,
        dtype: torch.dtype,
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        """统一为 (B, M, s_l, 4) 与 (B, N_θ/2, 4)。"""
        amp_seg = amp_segment.to(device=device, dtype=dtype)
        amp_glo = amp_global.to(device=device, dtype=dtype)

        if amp_seg.dim() == 3:
            amp_seg = amp_seg.unsqueeze(0)
        if amp_seg.shape[-1] == S_RES_CHANNELS and amp_seg.shape[-2] != self.geom.s_l:
            amp_seg = amp_seg.permute(0, 1, 3, 2)

        if amp_glo.dim() == 2:
            amp_glo = amp_glo.unsqueeze(0)
        if amp_glo.shape[-1] != S_RES_CHANNELS and amp_glo.shape[-2] == S_RES_CHANNELS:
            amp_glo = amp_glo.transpose(-1, -2)

        return amp_seg, amp_glo

    def _fuse_from_numpy_fft(
        self,
        s_res: torch.Tensor,
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        """无预计算幅值时,CPU 侧算 amp_segment / amp_global。"""
        b = s_res.shape[0]
        seg_list: List[torch.Tensor] = []
        glo_list: List[torch.Tensor] = []
        for i in range(b):
            sp = compute_spectral_fft_features(
                s_res[i].detach().cpu().numpy(),
                self.geom.n_segments,
            )
            seg_list.append(torch.from_numpy(sp.amp_segment))
            glo_list.append(torch.from_numpy(sp.amp_global))
        device, dtype = s_res.device, s_res.dtype
        return (
            torch.stack(seg_list, dim=0).to(device=device, dtype=dtype),
            torch.stack(glo_list, dim=0).to(device=device, dtype=dtype),
        )

    def forward(
        self,
        s_res: torch.Tensor,
        *,
        amp_segment: Optional[torch.Tensor] = None,
        amp_global: Optional[torch.Tensor] = None,
        return_amp: bool = True,
    ) -> Dict[str, torch.Tensor]:
        """
        s_res: (N_theta, 4) 或 (B, N_theta, 4)
        amp_segment: 可选 (B, M, s_l, 4) 或 (M, s_l, 4)
        amp_global:  可选 (B, N_θ/2, 4) 或 (N_θ/2, 4)

        返回:
          f_freq: (B, M, d_freq, s_l) — §4.2.4 经 MHA 后的 f'_freq
          f_bar:  (B, M, d_freq) — §4.2.5 谱轴均值,供 §4.3 Query
          amp_fuse: (B, M, s_l, 4) 可选
        """
        batched = s_res.dim() == 3
        if not batched:
            s_res = s_res.unsqueeze(0)

        b, n, c = s_res.shape
        assert n == self.geom.n_theta and c == S_RES_CHANNELS
        m_count = self.geom.n_segments
        device = s_res.device
        dtype = s_res.dtype

        if amp_segment is not None and amp_global is not None:
            amp_seg, amp_glo = self._normalize_amp_shapes(
                amp_segment, amp_global, device, dtype
            )
        else:
            amp_seg, amp_glo = self._fuse_from_numpy_fft(s_res)

        amp_glob_bar = self.global_pool(amp_glo)
        amp_fuse = self.gate_fusion(amp_seg, amp_glob_bar)

        amp_conv = amp_fuse.permute(0, 1, 3, 2).reshape(
            b * m_count, S_RES_CHANNELS, self.geom.s_l
        )
        f_flat = self.spectral(amp_conv)
        f_flat = self.spectral_attn(f_flat)
        f_freq = f_flat.reshape(b, m_count, self.config.d_freq, self.geom.s_l)
        f_bar = f_freq.mean(dim=-1)

        out: Dict[str, torch.Tensor] = {
            "f_freq": f_freq,
            "f_bar": f_bar,
            "amp_fuse": amp_fuse,
            "amp_global_bar": amp_glob_bar,
        }
        if return_amp:
            out["amp"] = amp_seg
            out["amp_global"] = amp_glo
        if not batched:
            out = {k: v.squeeze(0) for k, v in out.items()}
        return out

    def forward_from_fea(
        self,
        fea_tensor: FeaTensor,
        *,
        return_amp: bool = True,
    ) -> Dict[str, torch.Tensor]:
        """
        使用 DataLoader 输出的 fea_tensor 运行 §4.2。

        输入:amp_segment (M,s_l,4)、amp_global (N_θ/2,4);s_res 仅作形状校验/回退。
        """
        fields = (
            fea_tensor.fields
            if isinstance(fea_tensor, FeaTensor)
            else unpack_fea_fields(fea_tensor)
        )
        device = next(self.parameters()).device
        s_res = fields.s_res.float().to(device)
        amp_segment = fields.amp_segment.float().to(device)
        amp_global = fields.amp_global.float().to(device)
        return self.forward(
            s_res,
            amp_segment=amp_segment,
            amp_global=amp_global,
            return_amp=return_amp,
        )

    def trainable_parameters(self) -> List[torch.nn.Parameter]:
        return [p for p in self.parameters() if p.requires_grad]

    def forward_from_image(
        self,
        rgb: PathLike,
        *,
        img_size: int = 100,
    ) -> Tuple[dict, object, Dict[str, torch.Tensor]]:
        prep = preprocess_rgb_to_skeleton(rgb, size=img_size)
        bundle = extract_ellipse_features(
            prep["skeleton"],
            n_theta=self.config.n_theta,
            half_norm=img_size / 2.0,
        )
        s_res = torch.from_numpy(bundle.sequence.s_res).float()
        amp_pre = amp_glo = None
        if bundle.spectral is not None:
            amp_pre = torch.from_numpy(bundle.spectral.amp_segment).float()
            amp_glo = torch.from_numpy(bundle.spectral.amp_global).float()
        return prep, bundle, self.forward(
            s_res,
            amp_segment=amp_pre,
            amp_global=amp_glo,
        )

    def save_checkpoint(self, path: Union[str, Path], extra: Optional[dict] = None) -> None:
        torch.save(
            {
                "config": self.config,
                "state_dict": self.state_dict(),
                "extra": extra or {},
            },
            path,
        )

    @classmethod
    def load_checkpoint(
        cls,
        path: Union[str, Path],
        map_location: Optional[str] = None,
        strict: bool = True,
    ) -> "Section42Encoder":
        payload = torch.load(path, map_location=map_location, weights_only=False)
        cfg = payload.get("config") or Section42Config()
        if isinstance(cfg, dict):
            cfg = Section42Config(**cfg)
        model = cls(cfg)
        model.load_state_dict(payload["state_dict"], strict=strict)
        return model


def extract_section42(
    s_res: np.ndarray,
    config: Optional[Section42Config] = None,
    encoder: Optional[Section42Encoder] = None,
    device: Optional[str] = None,
    amp_segment: Optional[np.ndarray] = None,
    amp_global: Optional[np.ndarray] = None,
) -> Dict[str, np.ndarray]:
    enc = encoder or Section42Encoder(config)
    if device:
        enc = enc.to(device)
    enc.eval()
    with torch.no_grad():
        t = torch.from_numpy(s_res).float()
        amp_s = torch.from_numpy(amp_segment).float() if amp_segment is not None else None
        amp_g = torch.from_numpy(amp_global).float() if amp_global is not None else None
        if device:
            t = t.to(device)
            if amp_s is not None:
                amp_s = amp_s.to(device)
            if amp_g is not None:
                amp_g = amp_g.to(device)
        out = enc(
            t,
            amp_segment=amp_s,
            amp_global=amp_g,
            return_amp=True,
        )
    return {k: v.detach().cpu().numpy() for k, v in out.items()}


def demo(image_path: str = "data/images/20938.png") -> Dict[str, np.ndarray]:
    enc = Section42Encoder()
    enc.eval()
    with torch.no_grad():
        _, bundle, out_t = enc.forward_from_image(image_path)
    out = {k: v.detach().cpu().numpy() for k, v in out_t.items()}
    g = enc.geom
    print(f"§4.2 demo: {image_path}")
    print(f"  M={g.n_segments}, s_l={g.s_l}, L_win={g.l_win}, d_freq={enc.config.d_freq}")
    print(f"  amp_fuse          {out['amp_fuse'].shape}")
    print(f"  amp_global_bar    {out['amp_global_bar'].shape}")
    print(f"  f_freq            {out['f_freq'].shape}")
    print(f"  f_bar             {out['f_bar'].shape}")
    return out


def demo_from_dataloader(
    sample_ratio: float = 0.001,
    batch_size: int = 2,
    config: Optional[Section42Config] = None,
    device: str = "cpu",
) -> Dict[str, torch.Tensor]:
    """从 exp6_data_loader 取 fea_tensor,调试 §4.2(amp_segment + amp_global)。"""
    from exp6_data_loader import create_dataloaders

    cfg = config or Section42Config()
    enc = Section42Encoder(cfg).to(device)
    enc.eval()
    g = enc.geom

    train_loader, _, _, _ = create_dataloaders(
        batch_size=batch_size,
        num_workers=0,
        sample_ratio=sample_ratio,
        verbose=False,
    )

    batch = next(iter(train_loader))
    input_all, labels, pen_ids, writers, image_ids = batch
    fea_tensor, ske_tensor, img_tensor = input_all

    print("[section42] dataloader batch")
    print(f"  fea_tensor.data: {fea_tensor.data.shape}")
    print(f"  img_tensor: {tuple(img_tensor.shape)}  label: {labels.tolist()}")
    print(f"  M={g.n_segments}, s_l={g.s_l}, w={cfg.global_pool_win}, d_freq={cfg.d_freq}")

    fea_one = FeaTensor(data=fea_tensor.data[0])
    fields = fea_one.fields
    print(f"  s_res:       {tuple(fields.s_res.shape)}")
    print(f"  amp_segment: {tuple(fields.amp_segment.shape)}")
    print(f"  amp_global:  {tuple(fields.amp_global.shape)}")

    with torch.no_grad():
        out_one = enc.forward_from_fea(fea_one)

    n_trainable = sum(p.numel() for p in enc.trainable_parameters())
    print("[section42] forward_from_fea (single)")
    print(f"  writer: {writers[0]}  image_id: {image_ids[0]}")
    print(f"  trainable params: {n_trainable:,}")
    print(f"  amp_global_bar: {tuple(out_one['amp_global_bar'].shape)}")
    print(f"  amp_fuse:       {tuple(out_one['amp_fuse'].shape)}")
    print(f"  f_freq:         {tuple(out_one['f_freq'].shape)}")
    print(f"  f_bar:          {tuple(out_one['f_bar'].shape)}")

    print("[section42] batch forward")
    fields_b = fea_tensor.fields
    s_b = fields_b.s_res.float().to(device)
    amp_s = fields_b.amp_segment.float().to(device)
    amp_g = fields_b.amp_global.float().to(device)
    with torch.no_grad():
        out_b = enc.forward(s_b, amp_segment=amp_s, amp_global=amp_g)
    print(f"  batch s_res:       {tuple(s_b.shape)}")
    print(f"  batch amp_segment: {tuple(amp_s.shape)}")
    print(f"  batch amp_global:  {tuple(amp_g.shape)}")
    print(f"  batch f_freq:      {tuple(out_b['f_freq'].shape)}")
    print(f"  batch f_bar:       {tuple(out_b['f_bar'].shape)}")

    return out_one


def main() -> None:
    import argparse

    p = argparse.ArgumentParser(description="§4.2 segment spectral encoder")
    p.add_argument("image", nargs="?", default="data/images/20938.png")
    p.add_argument("--save", type=str, default=None)
    p.add_argument("--device", default="cpu")
    p.add_argument(
        "--from-dataloader",
        action="store_true",
        help="使用 exp6_data_loader 输出的 fea_tensor 调试 §4.2",
    )
    p.add_argument("--sample-ratio", type=float, default=0.001)
    p.add_argument("--batch-size", type=int, default=2)
    args = p.parse_args()

    if args.from_dataloader:
        demo_from_dataloader(
            sample_ratio=args.sample_ratio,
            batch_size=args.batch_size,
            device=args.device,
        )
        return

    out = demo(args.image)
    if args.save:
        np.savez(args.save, **out)
        print("saved:", args.save)


if __name__ == "__main__":
    demo_from_dataloader()