File size: 11,575 Bytes
519ab98 | 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 | """
§4.2 扩展:CWT 角向谱先验 φ(t)。
从 S_res 或预计算 scalogram 构造逐角位 Query 特征,供 §4.3 CWT-GAPA 使用。
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple, TYPE_CHECKING
import numpy as np
import torch
import torch.nn as nn
from elipse_feature import (
CWT_N_SCALES_DEFAULT,
CWT_WAVELET_DEFAULT,
S_RES_CHANNELS,
compute_cwt_scalogram,
default_cwt_scale_indices,
default_cwt_scales,
)
from section41 import (
Section41Encoder,
)
from section42 import Section42Encoder
from section43 import (
build_enc43_valid_mask_kwargs,
forward_pipeline_batch_to_upstream,
)
from section43_v3 import ConvNeXt1DPyramid
if TYPE_CHECKING:
from section43 import Section43Encoder
@dataclass
class Section42CWTConfig:
d_phi: int = 64
n_scales: int = CWT_N_SCALES_DEFAULT
wavelet: str = CWT_WAVELET_DEFAULT
mlp_hidden: int = 128
dropout: float = 0.1
scale_affinity: bool = False
scale_index_mode: str = "uniform" # uniform | physics
n_stages: int = 4
def _numpy_cwt_batch(s_res: torch.Tensor, cfg: Section42CWTConfig) -> torch.Tensor:
"""s_res (B, N, 4) → cwt_scalo (B, 4, K, N) on CPU numpy。"""
arr = s_res.detach().cpu().numpy()
scales = default_cwt_scales(cfg.n_scales)
batch = []
for i in range(arr.shape[0]):
batch.append(compute_cwt_scalogram(arr[i], scales=scales, wavelet=cfg.wavelet))
out = np.stack(batch, axis=0)
return torch.from_numpy(out)
class CWTPhiEncoder(nn.Module):
"""
scalogram |W(a,b)| → φ(t)。
统一 MLP(scale_affinity=False):四通道×K 尺度展平 → φ(t) ∈ R^{d_phi}。
scale_affinity=True 时由 §4.3 CWTAngularStagePool 按 stage 子集编码。
"""
def __init__(self, cfg: Optional[Section42CWTConfig] = None):
super().__init__()
self.config = cfg or Section42CWTConfig()
c = self.config
in_dim = S_RES_CHANNELS * c.n_scales
self.mlp = nn.Sequential(
nn.Linear(in_dim, c.mlp_hidden),
nn.GELU(),
nn.Dropout(c.dropout),
nn.Linear(c.mlp_hidden, c.d_phi),
)
@property
def raw_dim(self) -> int:
return S_RES_CHANNELS * self.config.n_scales
def encode_scalogram(self, cwt_scalo: torch.Tensor) -> torch.Tensor:
"""
cwt_scalo: (B, 4, K, N_theta)
→ phi_cwt: (B, N_theta, d_phi)
"""
b, _, k, n = cwt_scalo.shape
flat = cwt_scalo.permute(0, 3, 1, 2).reshape(b * n, -1)
phi = self.mlp(flat).reshape(b, n, -1)
return phi
def forward(
self,
s_res: torch.Tensor,
*,
cwt_scalo: Optional[torch.Tensor] = None,
return_scalogram: bool = False,
) -> Dict[str, torch.Tensor]:
"""
s_res: (B, N_theta, 4) 或 (N_theta, 4)
cwt_scalo: 可选预计算 (B, 4, K, N_theta)
"""
batched = s_res.dim() == 3
if not batched:
s_res = s_res.unsqueeze(0)
if cwt_scalo is not None and cwt_scalo.dim() == 3:
cwt_scalo = cwt_scalo.unsqueeze(0)
if cwt_scalo is None:
cwt_scalo = _numpy_cwt_batch(s_res, self.config).to(
device=s_res.device, dtype=s_res.dtype
)
else:
cwt_scalo = cwt_scalo.to(device=s_res.device, dtype=s_res.dtype)
out: Dict[str, torch.Tensor] = {"cwt_scalo": cwt_scalo}
if not self.config.scale_affinity:
out["phi_cwt"] = self.encode_scalogram(cwt_scalo)
if not batched:
squeezed: Dict[str, torch.Tensor] = {}
for k, v in out.items():
squeezed[k] = v.squeeze(0)
out = squeezed
return out
class CWTStagePhiEncoder(nn.Module):
"""scale_affinity=True:每 stage 独立 MLP,输入为该 stage 尺度子集。"""
def __init__(self, cfg: Section42CWTConfig, scale_indices: List[List[int]]):
super().__init__()
self.scale_indices = scale_indices
self.mlps = nn.ModuleList()
for idx in scale_indices:
in_dim = S_RES_CHANNELS * len(idx)
self.mlps.append(
nn.Sequential(
nn.Linear(in_dim, cfg.mlp_hidden),
nn.GELU(),
nn.Dropout(cfg.dropout),
nn.Linear(cfg.mlp_hidden, cfg.d_phi),
)
)
def forward(self, cwt_scalo: torch.Tensor) -> List[torch.Tensor]:
"""
cwt_scalo: (B, 4, K, N)
→ list of phi_s_full (B, N, d_phi),长度 = n_stages
"""
b, _, _, n = cwt_scalo.shape
outputs: List[torch.Tensor] = []
for mlp, idx in zip(self.mlps, self.scale_indices):
sub = cwt_scalo[:, :, idx, :]
flat = sub.permute(0, 3, 1, 2).reshape(b * n, -1)
phi = mlp(flat).reshape(b, n, -1)
outputs.append(phi)
return outputs
class CWTStagePhiEncoderValid(nn.Module):
"""
E2b:scale 子集展平 + valid embedding → MLP → φ_raw。
valid_ang: (B, N) 或 None(默认全 1)。
"""
def __init__(
self,
cfg: Section42CWTConfig,
scale_indices: List[List[int]],
valid_emb_dim: int = 8,
):
super().__init__()
self.valid_emb_dim = valid_emb_dim
self.scale_indices = scale_indices
self.valid_emb = nn.Embedding(2, valid_emb_dim)
self.mlps = nn.ModuleList()
for idx in scale_indices:
in_dim = S_RES_CHANNELS * len(idx) + valid_emb_dim
self.mlps.append(
nn.Sequential(
nn.Linear(in_dim, cfg.mlp_hidden),
nn.GELU(),
nn.Dropout(cfg.dropout),
nn.Linear(cfg.mlp_hidden, cfg.d_phi),
)
)
def forward(
self,
cwt_scalo: torch.Tensor,
valid_ang: Optional[torch.Tensor] = None,
) -> List[torch.Tensor]:
b, _, _, n = cwt_scalo.shape
if valid_ang is None:
valid_ang = torch.ones(
b, n, device=cwt_scalo.device, dtype=cwt_scalo.dtype
)
elif valid_ang.dim() == 1:
valid_ang = valid_ang.unsqueeze(0).expand(b, -1)
valid_idx = valid_ang.long().clamp(0, 1)
valid_emb = self.valid_emb(valid_idx)
outputs: List[torch.Tensor] = []
for mlp, idx in zip(self.mlps, self.scale_indices):
sub = cwt_scalo[:, :, idx, :]
flat = sub.permute(0, 3, 1, 2).reshape(b * n, -1)
vemb = valid_emb.reshape(b * n, self.valid_emb_dim)
x = torch.cat([flat, vemb], dim=-1)
phi = mlp(x).reshape(b, n, -1)
outputs.append(phi)
return outputs
def build_scale_indices(
cfg: Section42CWTConfig,
*,
n_theta: int = 360,
) -> List[List[int]]:
if cfg.scale_index_mode == "physics":
try:
from elipse_feature import physics_cwt_scale_indices
except ImportError as exc:
raise ImportError(
"cwt scale_index_mode=physics 需要新版 elipse_feature.py "
"(含 physics_cwt_scale_indices);请同步 elipse_feature.py "
"或改用 scale_index_mode=uniform(E2b 默认)"
) from exc
return physics_cwt_scale_indices(
cfg.n_scales,
cfg.n_stages,
n_theta=n_theta,
wavelet=cfg.wavelet,
)
return default_cwt_scale_indices(cfg.n_scales, cfg.n_stages)
def flatten_scalogram(cwt_scalo: torch.Tensor) -> torch.Tensor:
"""
cwt_scalo (B, 4, K, N_theta) → (B, 4*K, N_theta) 供 1D CNN 金字塔。
"""
b, c, k, n = cwt_scalo.shape
return cwt_scalo.permute(0, 3, 1, 2).reshape(b, n, c * k).transpose(1, 2)
class CWTPyramid(nn.Module):
"""
Scalogram 1D CNN 金字塔,结构与 ConvNeXt1DPyramid 同构(360→180→90→45→22)。
"""
def __init__(
self,
in_channels: int,
dims: Tuple[int, ...] = (96, 192, 384, 768),
depths: Tuple[int, ...] = (3, 3, 9, 3),
*,
stem_kernel: int = 4,
stem_stride: int = 2,
dw_kernel: int = 7,
mlp_ratio: int = 4,
):
super().__init__()
self.pyramid = ConvNeXt1DPyramid(
in_channels=in_channels,
dims=dims,
depths=depths,
stem_kernel=stem_kernel,
stem_stride=stem_stride,
dw_kernel=dw_kernel,
mlp_ratio=mlp_ratio,
)
def forward(self, cwt_scalo: torch.Tensor) -> List[torch.Tensor]:
"""
cwt_scalo: (B, 4, K, N)
→ list of H_cwt^s (B, C_s, L_s),长度 = n_stages
"""
x = flatten_scalogram(cwt_scalo)
_, pyramid_feats = self.pyramid(x)
return pyramid_feats
def cwt_pyramid_in_channels(n_scales: int, n_ch: int = S_RES_CHANNELS) -> int:
return n_ch * n_scales
class CWTStageProjector(nn.Module):
"""各 CWT pyramid stage 投影到 d_h。"""
def __init__(self, stage_dims: Tuple[int, ...], d_h: int):
super().__init__()
self.projs = nn.ModuleList([nn.Linear(c, d_h) for c in stage_dims])
def forward(self, stage_idx: int, x: torch.Tensor) -> torch.Tensor:
"""x: (B, C_s, L_s) → (B, L_s, d_h)"""
return self.projs[stage_idx](x.transpose(1, 2))
def enrich_o42_with_cwt(
o42: Dict[str, torch.Tensor],
s_res_b: torch.Tensor,
cwt_phi_encoder: nn.Module,
cwt_scalo_batch: Optional[torch.Tensor],
device: torch.device,
) -> Dict[str, torch.Tensor]:
"""将 CWT 角向特征注入 §4.2 输出,供 §4.3 CWT-GAPA 使用。"""
cwt_out = cwt_phi_encoder(
s_res_b,
cwt_scalo=cwt_scalo_batch.to(device) if cwt_scalo_batch is not None else None,
)
if "phi_cwt" in cwt_out:
o42["phi_cwt"] = cwt_out["phi_cwt"]
o42["cwt_scalo"] = cwt_out["cwt_scalo"]
return o42
def forward_pipeline_batch_from_dataloader_cwt(
fea_tensor,
img_tensor: torch.Tensor,
enc41: Section41Encoder,
enc42: Section42Encoder,
enc43: "Section43Encoder",
*,
half_norm: float,
e_pen: Optional[torch.Tensor] = None,
training: bool = False,
cwt_phi_encoder: nn.Module,
cwt_scalo_batch: Optional[torch.Tensor] = None,
) -> Tuple[Dict[str, torch.Tensor], Dict[str, torch.Tensor], Dict[str, torch.Tensor]]:
"""
CWT-GAPA:复用 section43 原始 §4.1/§4.2 流水线,仅在 enc43 前注入 CWT。
§4.3 ConvNeXt1D 金字塔与 stage_proj 与 DBSA/global 完全一致,便于从旧 checkpoint 微调。
"""
device = next(enc43.parameters()).device
o41, o42 = forward_pipeline_batch_to_upstream(
fea_tensor, img_tensor, enc41, enc42, half_norm=half_norm
)
s_res_b = fea_tensor.fields.s_res.float().to(device)
o42 = enrich_o42_with_cwt(o42, s_res_b, cwt_phi_encoder, cwt_scalo_batch, device)
enc43_kwargs = build_enc43_valid_mask_kwargs(enc43, fea_tensor, device)
o43 = enc43.forward_from_upstream(
o41, o42, e_pen, training=training, return_details=not training, **enc43_kwargs
)
return o41, o42, o43
if __name__ == "__main__":
enc = CWTPhiEncoder()
s_res = torch.randn(2, 360, 4)
out = enc(s_res, return_scalogram=True)
print("phi_cwt", tuple(out["phi_cwt"].shape))
print("cwt_scalo", tuple(out["cwt_scalo"].shape))
|