Download code/section43_v3.py from qinxunhui/deepellipse-circleid: direct link, hf CLI and curl.
- Browser
- Download file 10 kB
-
https://huggingface.co/qinxunhui/deepellipse-circleid/resolve/main/code/section43_v3.py
- Command line
-
hf download hf://qinxunhui/deepellipse-circleid/code/section43_v3.py
-
curl -L -o section43_v3.py https://huggingface.co/qinxunhui/deepellipse-circleid/resolve/main/code/section43_v3.py
10 kB
| """ | |
| §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 | |
| 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" | |
| 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 | |