File size: 10,000 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 | """
§4.3 v3:角向一维序列 ConvNeXt-Tiny 式特征金字塔。
将 §4.1 输出的 s_seq (B, N_θ, d_seq) 视为圆周 1D 序列,经 stem + 多阶段
Depthwise-Conv + LayerNorm + MLP 块(结构对齐 ConvNeXt-Tiny),逐级下采样后
全局池化得到 f_ellipse,供 DeepEllipse 分类头(CE)使用。
与 section43 (v1)、section43_v2 并存,供消融对比。
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import Dict, List, Literal, Optional, Tuple
import torch
import torch.nn as nn
import torch.nn.functional as F
from section43 import Section43Config, Section43Encoder
PoolModeV3 = Literal["avg", "max"]
def _circular_pad1d(x: torch.Tensor, pad: int) -> torch.Tensor:
if pad <= 0:
return x
return torch.cat([x[..., -pad:], x, x[..., :pad]], dim=-1)
class ConvNeXt1DBlock(nn.Module):
"""ConvNeXt 块的一维版本:圆周 DW-Conv7 + LN + 1×1 MLP(channels_last)。"""
def __init__(
self,
dim: int,
*,
kernel_size: int = 7,
mlp_ratio: int = 4,
layer_scale_init: float = 1e-6,
):
super().__init__()
self.pad = kernel_size // 2
self.dwconv = nn.Conv1d(dim, dim, kernel_size=kernel_size, groups=dim, bias=True)
self.norm = nn.LayerNorm(dim)
hidden = dim * mlp_ratio
self.pwconv1 = nn.Linear(dim, hidden)
self.act = nn.GELU()
self.pwconv2 = nn.Linear(hidden, dim)
self.gamma = nn.Parameter(layer_scale_init * torch.ones(dim))
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""x: (B, C, L)"""
residual = x
x = _circular_pad1d(x, self.pad)
x = self.dwconv(x)
x = x.transpose(1, 2)
x = self.norm(x)
x = self.pwconv2(self.act(self.pwconv1(x)))
x = self.gamma * x
x = x.transpose(1, 2)
return residual + x
class ConvNeXt1DDownsample(nn.Module):
"""Stage 间下采样:LN + Conv1d stride=2(长度约减半)。"""
def __init__(self, in_dim: int, out_dim: int):
super().__init__()
self.norm = nn.LayerNorm(in_dim)
self.conv = nn.Conv1d(in_dim, out_dim, kernel_size=2, stride=2)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.norm(x.transpose(1, 2)).transpose(1, 2)
return self.conv(x)
class ConvNeXt1DPyramid(nn.Module):
"""
ConvNeXt-Tiny 风格 1D 金字塔。
默认 4 个 stage,通道 [96,192,384,768],每 stage depth [3,3,9,3]。
"""
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__()
if len(dims) != len(depths):
raise ValueError("dims 与 depths 长度须一致")
self.dims = dims
self.stem_conv = nn.Conv1d(
in_channels,
dims[0],
kernel_size=stem_kernel,
stride=stem_stride,
padding=stem_kernel // 2,
)
self.stem_norm = nn.LayerNorm(dims[0])
self.stages = nn.ModuleList()
self.downsamples = nn.ModuleList()
for i in range(len(depths)):
blocks = nn.Sequential(
*[
ConvNeXt1DBlock(
dims[i],
kernel_size=dw_kernel,
mlp_ratio=mlp_ratio,
)
for _ in range(depths[i])
]
)
self.stages.append(blocks)
if i < len(depths) - 1:
self.downsamples.append(ConvNeXt1DDownsample(dims[i], dims[i + 1]))
self.out_dim = dims[-1]
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, List[torch.Tensor]]:
"""
x: (B, C_in, L)
返回 (f_map, pyramid_maps),f_map 为最后一级特征图。
"""
x = self.stem_conv(x)
x = self.stem_norm(x.transpose(1, 2)).transpose(1, 2)
pyramid: List[torch.Tensor] = []
for i, stage in enumerate(self.stages):
x = stage(x)
pyramid.append(x)
if i < len(self.downsamples):
x = self.downsamples[i](x)
return x, pyramid
@dataclass
class Section43ConfigV3(Section43Config):
"""§4.3 v3:ConvNeXt-Tiny 1D 金字塔。"""
pyramid_dims: Tuple[int, ...] = (96, 192, 384, 768)
pyramid_depths: Tuple[int, ...] = (3, 3, 9, 3)
stem_kernel: int = 4
stem_stride: int = 2
dw_kernel: int = 7
mlp_ratio: int = 4
pool_mode: PoolModeV3 = "avg"
@property
def d_ellipse(self) -> int:
return self.pyramid_dims[-1]
def scheme_label(self) -> str:
return f"v3(ConvNeXt1D-{self.pool_mode})"
def pool_sequence_v3(
x: torch.Tensor,
mode: PoolModeV3,
valid_mask: Optional[torch.Tensor] = None,
) -> torch.Tensor:
"""x: (B, C, L) → (B, C)"""
if mode == "max":
return x.max(dim=-1).values
if valid_mask is not None:
w = valid_mask.to(x.device).unsqueeze(1)
denom = w.sum(dim=-1).clamp(min=1.0)
return (x * w).sum(dim=-1) / denom
return x.mean(dim=-1)
class Section43EncoderV3(nn.Module):
"""§4.3 v3:s_seq → ConvNeXt1D 金字塔 → 全局池化 → f_ellipse。"""
def __init__(self, config: Optional[Section43ConfigV3] = None):
super().__init__()
self.config = config or Section43ConfigV3()
cfg = self.config
self.pyramid = ConvNeXt1DPyramid(
in_channels=cfg.d_seq,
dims=cfg.pyramid_dims,
depths=cfg.pyramid_depths,
stem_kernel=cfg.stem_kernel,
stem_stride=cfg.stem_stride,
dw_kernel=cfg.dw_kernel,
mlp_ratio=cfg.mlp_ratio,
)
def forward(
self,
s_seq: torch.Tensor,
f_freq: Optional[torch.Tensor] = None,
g_vec: Optional[torch.Tensor] = None,
e_pen: Optional[torch.Tensor] = None,
*,
valid_mask: Optional[torch.Tensor] = None,
training: bool = False,
return_details: bool = True,
) -> Dict[str, torch.Tensor]:
del f_freq, g_vec, e_pen, training
cfg = self.config
batched = s_seq.dim() == 3
if not batched:
s_seq = s_seq.unsqueeze(0)
if valid_mask is not None and valid_mask.dim() == 1:
valid_mask = valid_mask.unsqueeze(0)
x = s_seq.transpose(1, 2)
f_map, pyramid_feats = self.pyramid(x)
f_ellipse = pool_sequence_v3(f_map, cfg.pool_mode, valid_mask)
out: Dict[str, torch.Tensor] = {
"f_ellipse": f_ellipse,
"H_ell": f_map.transpose(1, 2),
"s_smooth": s_seq,
}
if return_details:
out["pyramid_feats"] = pyramid_feats
out["f_map"] = f_map
if not batched:
squeezed = {}
for k, v in out.items():
if k == "pyramid_feats":
squeezed[k] = [t.squeeze(0) for t in v]
elif isinstance(v, torch.Tensor):
squeezed[k] = v.squeeze(0)
else:
squeezed[k] = v
out = squeezed
return out
def forward_from_upstream(
self,
o41: Dict[str, torch.Tensor],
o42: Dict[str, torch.Tensor],
e_pen: Optional[torch.Tensor] = None,
*,
valid_mask: Optional[torch.Tensor] = None,
training: bool = False,
return_details: bool = True,
) -> Dict[str, torch.Tensor]:
return self.forward(
o41["s_seq"],
o42.get("f_freq"),
o41.get("g_vec"),
e_pen,
valid_mask=valid_mask,
training=training,
return_details=return_details,
)
def build_section43_encoder(
config: Optional[Section43Config] = None,
*,
use_v2: bool = False,
use_v3: bool = False,
use_v3_1: bool = False,
) -> nn.Module:
"""工厂:v1 / v2 / v3 / v3.1 四选一。"""
from section43_v2 import Section43ConfigV2, Section43EncoderV2
flags = sum(int(x) for x in (use_v2, use_v3, use_v3_1))
if flags > 1:
raise ValueError("use_v2 / use_v3 / use_v3_1 至多一个为 True")
if use_v3_1:
from section43_v3_1 import Section43ConfigV3_1, Section43EncoderV3_1
if config is None:
return Section43EncoderV3_1()
if isinstance(config, Section43ConfigV3_1):
return Section43EncoderV3_1(config)
return Section43EncoderV3_1(
Section43ConfigV3_1(
**{
k: v
for k, v in vars(config).items()
if k in Section43ConfigV3_1.__dataclass_fields__
}
)
)
if use_v3:
if config is None:
return Section43EncoderV3()
if isinstance(config, Section43ConfigV3):
return Section43EncoderV3(config)
return Section43EncoderV3(
Section43ConfigV3(
**{k: v for k, v in vars(config).items() if k in Section43ConfigV3.__dataclass_fields__}
)
)
if use_v2:
if config is None:
return Section43EncoderV2()
if isinstance(config, Section43ConfigV2):
return Section43EncoderV2(config)
return Section43EncoderV2(
Section43ConfigV2(
**{k: v for k, v in vars(config).items() if k in Section43ConfigV2.__dataclass_fields__}
)
)
if config is None:
return Section43Encoder()
return Section43Encoder(config)
def v3_needs_valid_mask(enc43: nn.Module) -> bool:
"""v3 金字塔长度与角锚不对齐,暂不使用 valid_mask。"""
return False
|