#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ DeepEllipse:§4.1 + §4.2 + §4.3 端到端书写者识别模型。 - 输入:exp6_data_loader 的 (fea_tensor, img_tensor, pen_ids, labels) - 骨干:Section41Encoder → Section42Encoder → Section43Encoder - §4.1.3 默认 ConvNeXt(冻结),权重按平台自动选择(Windows: model/…,Linux: checkpoints/model_convnext/…) - 头:Linear(f_ellipse → num_classes),默认 CrossEntropyLoss 训练 用法: python deep_elipse_model.py --mode train --debug python deep_elipse_model.py --mode val --checkpoint checkpoints/deep_ellipse/best.pth python deep_elipse_model.py --mode test --checkpoint checkpoints/deep_ellipse/best.pth """ from __future__ import annotations import argparse import os import time from dataclasses import asdict, dataclass, field from pathlib import Path from typing import Dict, List, Optional, Tuple, Union import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.data import DataLoader from tqdm import tqdm from elipse_feature import DEFAULT_IMG_SIZE, FeaTensor from exp6_data_loader import Config as DataConfig, create_dataloaders from section41 import Section41Config, Section41Encoder from section42 import Section42Config, Section42Encoder from section43 import ( Section43Config, Section43Encoder, forward_pipeline_batch_from_dataloader, ) from section43_v2 import Section43ConfigV2 from section43_v3 import Section43ConfigV3, build_section43_encoder from section43_v3_1 import Section43ConfigV3_1, Section43EncoderV3_1 from section42_cwt import ( CWTPhiEncoder, Section42CWTConfig, forward_pipeline_batch_from_dataloader_cwt, ) from resnet_point_feature import default_resnet_checkpoint from convnext_point_feature import default_convnext_checkpoint PathLike = Union[str, Path] # --------------------------------------------------------------------------- # 配置 # --------------------------------------------------------------------------- @dataclass class DeepEllipseConfig: """端到端 DeepEllipse 超参。""" # §4.x 子模块(None → 各 Section 默认) cfg41: Optional[Section41Config] = None cfg42: Optional[Section42Config] = None cfg43: Optional[Section43Config] = None # §4.3 方案:v1 / v2 / v3 / v3.1 四选一 use_scheme_v2: bool = False use_scheme_v3: bool = False use_scheme_v3_1: bool = False seq_backend: Optional[str] = None # Table7: transformer|conformer|zipformer|dolphin|synsig v2_pre_smooth: bool = True v2_pool_mode: str = "sp" # max | mean | sp v3_pool_mode: str = "avg" # avg | max v3_1_stage_pool: str = "dbsa" # global | seg_sigmoid | dbsa | cwt_angular | cwt_dual cwt_scale_affinity: bool = False # E2:各 stage 独立 scale 子集 MLP cwt_scale_index_mode: str = "uniform" # uniform | physics(H3 E2-phys) angular_norm: str = "sum_relu" # softmax | sum_relu(默认 sum_relu,E1 消融用 softmax) e3_cross_query: str = "cwt_cnn" # phi | cwt_cnn(仅 cwt_dual 路1) e3_cross_query_path2: str = "maxpool" # maxpool | orig_cnn(仅 cwt_dual 路2) e3_cwt_branch: bool = True # False → E3a 仅路1(512) # E2b(cwt_angular 扩展) e2_cwt_branch: bool = False cwt_phi_valid_emb: int = 0 e2_cross_path2: str = "orig_cnn" # maxpool | orig_cnn gcc_enabled: bool = False gcc_gate_init: float = -2.0 gcc_d_stage_emb: int = 16 gcc_dw_kernels_bu: tuple = (3, 5, 7) gcc_dw_kernels_td: tuple = (7, 5, 3) phi_tx_layers: int = 0 phi_tx_heads: int = 4 phi_tx_ffn: int = 256 phi_tx_dropout: float = 0.1 num_classes: int = 45 num_pens: int = 16 half_norm: float = DEFAULT_IMG_SIZE / 2.0 dropout: float = 0.1 deep_backend: str = "convnext" resnet_checkpoint: str = field(default_factory=default_resnet_checkpoint) convnext_checkpoint: str = field(default_factory=default_convnext_checkpoint) freeze41_deep: bool = True # 训练 batch_size: int = 16 num_epochs: int = 30 lr: float = 1e-4 weight_decay: float = 2e-4 grad_clip: float = 1.0 num_workers: int = 0 pin_memory: bool = True checkpoint_dir: str = "checkpoints/deep_ellipse" log_every: int = 20 sample_ratio: float = 1.0 min_samples: int = 0 device: str = field( default_factory=lambda: "cuda" if torch.cuda.is_available() else "cpu" ) def build_section_configs(self) -> Tuple[Section41Config, Section42Config, Section43Config]: cfg41 = self.cfg41 or Section41Config( deep_backend=self.deep_backend, resnet_checkpoint=self.resnet_checkpoint, convnext_checkpoint=self.convnext_checkpoint, ) cfg42 = self.cfg42 or Section42Config() scheme_flags = (self.use_scheme_v2, self.use_scheme_v3, self.use_scheme_v3_1) if sum(int(x) for x in scheme_flags) > 1: raise ValueError("use_scheme_v2 / v3 / v3_1 至多一个为 True") if self.use_scheme_v3_1: base = vars(self.cfg43) if self.cfg43 is not None else {} cfg43 = Section43ConfigV3_1( **{ k: v for k, v in base.items() if k in Section43ConfigV3_1.__dataclass_fields__ }, pool_mode=self.v3_pool_mode, # type: ignore[arg-type] stage_pool_mode=self.v3_1_stage_pool, cwt_scale_affinity=self.cwt_scale_affinity, cwt_scale_index_mode=self.cwt_scale_index_mode, angular_norm=self.angular_norm, # type: ignore[arg-type] e3_cross_query=self.e3_cross_query, e3_cross_query_path2=self.e3_cross_query_path2, e3_cwt_branch=self.e3_cwt_branch, e2_cwt_branch=self.e2_cwt_branch, cwt_phi_valid_emb=self.cwt_phi_valid_emb, e2_cross_path2=self.e2_cross_path2, gcc_enabled=self.gcc_enabled, gcc_gate_init=self.gcc_gate_init, gcc_d_stage_emb=self.gcc_d_stage_emb, gcc_dw_kernels_bu=self.gcc_dw_kernels_bu, gcc_dw_kernels_td=self.gcc_dw_kernels_td, phi_tx_layers=self.phi_tx_layers, phi_tx_heads=self.phi_tx_heads, phi_tx_ffn=self.phi_tx_ffn, phi_tx_dropout=self.phi_tx_dropout, ) elif self.use_scheme_v3 and self.seq_backend: from section43_v3_seq import Section43ConfigV3Seq out_dim = 256 if self.seq_backend in ("synsig", "dolphin") else 768 cfg43 = Section43ConfigV3Seq( pool_mode=self.v3_pool_mode, seq_backend=self.seq_backend, # type: ignore[arg-type] backend_out_dim=out_dim, ) elif self.use_scheme_v3: base = vars(self.cfg43) if self.cfg43 is not None else {} cfg43 = Section43ConfigV3( **{ k: v for k, v in base.items() if k in Section43ConfigV3.__dataclass_fields__ }, pool_mode=self.v3_pool_mode, # type: ignore[arg-type] ) elif self.use_scheme_v2: base = vars(self.cfg43) if self.cfg43 is not None else {} cfg43 = Section43ConfigV2( **{ k: v for k, v in base.items() if k in Section43ConfigV2.__dataclass_fields__ }, use_pre_smooth=self.v2_pre_smooth, pool_mode=self.v2_pool_mode, # type: ignore[arg-type] ) else: cfg43 = self.cfg43 or Section43Config() return cfg41, cfg42, cfg43 # --------------------------------------------------------------------------- # 模型 # --------------------------------------------------------------------------- class DeepEllipseModel(nn.Module): """ DataLoader → §4.1 → §4.2 → §4.3 → 分类 logits。 forward 返回 logits;`return_details=True` 时附带各段中间量。 """ def __init__(self, config: DeepEllipseConfig): super().__init__() self.config = config cfg41, cfg42, cfg43 = config.build_section_configs() self.enc41 = Section41Encoder(cfg41) self.enc42 = Section42Encoder(cfg42) if config.use_scheme_v3_1: # 直接实例化,避免旧版 section43_v3.build_section43_encoder 无 use_v3_1 参数 self.enc43 = Section43EncoderV3_1(cfg43) # type: ignore[arg-type] elif config.use_scheme_v3 and config.seq_backend: from section43_v3_seq import Section43EncoderV3Seq self.enc43 = Section43EncoderV3Seq(cfg43) # type: ignore[arg-type] else: self.enc43 = build_section43_encoder( cfg43, use_v2=config.use_scheme_v2, use_v3=config.use_scheme_v3, ) self.cwt_encoder: Optional[CWTPhiEncoder] = None if config.use_scheme_v3_1 and config.v3_1_stage_pool in ("cwt_angular", "cwt_dual"): cwt_cfg = Section42CWTConfig( d_phi=cfg43.d_phi, # type: ignore[union-attr] n_scales=cfg43.cwt_n_scales, # type: ignore[union-attr] wavelet=cfg43.cwt_wavelet, # type: ignore[union-attr] scale_affinity=cfg43.cwt_scale_affinity, # type: ignore[union-attr] scale_index_mode=cfg43.cwt_scale_index_mode, # type: ignore[union-attr] ) self.cwt_encoder = CWTPhiEncoder(cwt_cfg) if config.freeze41_deep: for p in self.enc41.deep_branch.parameters(): p.requires_grad = False dropout_rate = 0.2 if config.use_scheme_v3_1 else config.dropout if config.use_scheme_v2 or config.use_scheme_v3 or config.use_scheme_v3_1: d_out = cfg43.d_ellipse else: d_out = cfg43.d_h self.pen_embed = nn.Embedding(config.num_pens, cfg43.d_pen) self.dropout = nn.Dropout(dropout_rate) self.classifier = nn.Linear(d_out, config.num_classes) self._init_classifier() def _init_classifier(self) -> None: nn.init.xavier_uniform_(self.classifier.weight) nn.init.zeros_(self.classifier.bias) @property def half_norm(self) -> float: return self.config.half_norm def pen_embedding(self, pen_ids: Optional[torch.Tensor]) -> Optional[torch.Tensor]: if pen_ids is None: return None idx = pen_ids.long().clamp(0, self.config.num_pens - 1) return self.pen_embed(idx) @property def d_ellipse(self) -> int: if ( self.config.use_scheme_v2 or self.config.use_scheme_v3 or self.config.use_scheme_v3_1 ): return self.enc43.config.d_ellipse return self.enc43.config.d_h @property def d_geo(self) -> int: return self.enc41.config.d_geo @property def d_pen(self) -> int: return self.enc43.config.d_pen def encode_branch( self, fea_tensor: FeaTensor, img_tensor: torch.Tensor, pen_ids: Optional[torch.Tensor] = None, *, training: bool = False, cwt_scalo: Optional[torch.Tensor] = None, ) -> Tuple[Dict[str, torch.Tensor], Dict[str, torch.Tensor], Dict[str, torch.Tensor]]: """§4.1–§4.3 特征(无分类头)。 CWT 模式:§4.1/§4.2 与 DBSA 相同;仅在 enc43 前注入 phi_cwt; enc43.pyramid + stage_proj 与旧 checkpoint 键名一致,可 partial 加载微调。 """ e_pen = self.pen_embedding(pen_ids) if self.cwt_encoder is not None: return forward_pipeline_batch_from_dataloader_cwt( fea_tensor, img_tensor, self.enc41, self.enc42, self.enc43, half_norm=self.half_norm, e_pen=e_pen, training=training, cwt_phi_encoder=self.cwt_encoder, cwt_scalo_batch=cwt_scalo, ) return forward_pipeline_batch_from_dataloader( fea_tensor, img_tensor, self.enc41, self.enc42, self.enc43, half_norm=self.half_norm, e_pen=e_pen, training=training, ) def encode_for_fusion( self, fea_tensor: FeaTensor, img_tensor: torch.Tensor, pen_ids: Optional[torch.Tensor] = None, *, training: bool = False, cwt_scalo: Optional[torch.Tensor] = None, ) -> Dict[str, torch.Tensor]: """ 供 §5.1 融合:f_ellipse (§4.3 d_h)、g_vec (§4.1)、E_pen(共享 pen_embed)。 """ o41, o42, o43 = self.encode_branch( fea_tensor, img_tensor, pen_ids, training=training, cwt_scalo=cwt_scalo ) e_pen = self.pen_embedding(pen_ids) return { "f_ellipse": o43["f_ellipse"], "g_vec": o41["g_vec"], "e_pen": e_pen, "o41": o41, "o42": o42, "o43": o43, } def forward( self, fea_tensor: FeaTensor, img_tensor: torch.Tensor, pen_ids: Optional[torch.Tensor] = None, *, training: bool = False, return_details: bool = False, cwt_scalo: Optional[torch.Tensor] = None, ) -> Union[torch.Tensor, Tuple[torch.Tensor, Dict[str, torch.Tensor]]]: o41, o42, o43 = self.encode_branch( fea_tensor, img_tensor, pen_ids, training=training, cwt_scalo=cwt_scalo, ) feat = self.dropout(o43["f_ellipse"]) logits = self.classifier(feat) if not return_details: return logits details = { "o41": o41, "o42": o42, "o43": o43, "f_ellipse": o43["f_ellipse"], } return logits, details def trainable_parameter_groups(self) -> List[Dict]: """AdamW 参数组:§4.1/42/43 + 头。""" return [{"params": [p for p in self.parameters() if p.requires_grad]}] def count_parameters(self) -> Tuple[int, int]: total = sum(p.numel() for p in self.parameters()) trainable = sum(p.numel() for p in self.parameters() if p.requires_grad) return total, trainable def save_checkpoint( self, path: PathLike, *, epoch: int, optimizer: torch.optim.Optimizer, val_acc: float, label_classes: List[str], extra: Optional[dict] = None, ) -> None: path = Path(path) path.parent.mkdir(parents=True, exist_ok=True) torch.save( { "epoch": epoch, "val_acc": val_acc, "model_state_dict": self.state_dict(), "optimizer_state_dict": optimizer.state_dict(), "label_encoder_classes": label_classes, "config": self.config, "extra": extra or {}, }, path, ) @classmethod def load_from_checkpoint( cls, path: PathLike, *, num_classes: Optional[int] = None, map_location: Optional[str] = None, strict: bool = True, ) -> Tuple["DeepEllipseModel", dict]: payload = torch.load(path, map_location=map_location, weights_only=False) cfg = payload.get("config") if cfg is None: cfg = DeepEllipseConfig(num_classes=num_classes or 45) elif isinstance(cfg, dict): cfg = DeepEllipseConfig(**cfg) elif num_classes is not None: cfg.num_classes = num_classes if num_classes is not None: cfg.num_classes = num_classes model = cls(cfg) model.load_state_dict(payload["model_state_dict"], strict=strict) return model, payload # --------------------------------------------------------------------------- # 损失 / 指标 # --------------------------------------------------------------------------- def classification_loss( logits: torch.Tensor, labels: torch.Tensor, criterion: Optional[nn.Module] = None, ) -> torch.Tensor: """默认交叉熵;labels 中 -1 为无效标签(跳过)。""" crit = criterion or nn.CrossEntropyLoss() valid = labels >= 0 if not valid.any(): return logits.sum() * 0.0 return crit(logits[valid], labels[valid]) @torch.no_grad() def batch_accuracy(logits: torch.Tensor, labels: torch.Tensor) -> Tuple[int, int]: valid = labels >= 0 if not valid.any(): return 0, 0 pred = logits[valid].argmax(dim=-1) correct = (pred == labels[valid]).sum().item() return correct, valid.sum().item() # --------------------------------------------------------------------------- # 训练 / 验证 / 测试 # --------------------------------------------------------------------------- def train_one_epoch( model: DeepEllipseModel, loader: DataLoader, criterion: nn.Module, optimizer: torch.optim.Optimizer, device: torch.device, *, max_batches: Optional[int] = None, grad_clip: float = 1.0, ) -> Tuple[float, float]: model.train() total_loss = 0.0 correct = 0 total = 0 n_batches = 0 pbar = tqdm(enumerate(loader), total=len(loader), desc="train", leave=False) for batch_idx, batch in pbar: if max_batches is not None and batch_idx >= max_batches: break input_all, labels, pen_ids, _writers, _image_ids = batch fea_tensor, _ske, img_tensor, cwt_scalo = input_all labels = labels.to(device) pen_ids = pen_ids.to(device) img_tensor = img_tensor.to(device) if cwt_scalo is not None: cwt_scalo = cwt_scalo.to(device) optimizer.zero_grad(set_to_none=True) logits = model( fea_tensor, img_tensor, pen_ids, training=True, cwt_scalo=cwt_scalo ) loss = classification_loss(logits, labels, criterion) loss.backward() if grad_clip > 0: nn.utils.clip_grad_norm_(model.parameters(), grad_clip) optimizer.step() bs_correct, bs_total = batch_accuracy(logits.detach(), labels) correct += bs_correct total += bs_total total_loss += loss.item() n_batches += 1 pbar.set_postfix(loss=f"{loss.item():.4f}", acc=f"{bs_correct/max(bs_total,1):.3f}") avg_loss = total_loss / max(n_batches, 1) acc = correct / max(total, 1) return avg_loss, acc @torch.no_grad() def evaluate( model: DeepEllipseModel, loader: DataLoader, criterion: nn.Module, device: torch.device, *, max_batches: Optional[int] = None, ) -> Tuple[float, float]: model.eval() total_loss = 0.0 correct = 0 total = 0 n_batches = 0 total = len(loader) if max_batches is None else min(len(loader), max_batches) for batch_idx, batch in enumerate(tqdm(loader, total=total, desc="eval", leave=False)): if max_batches is not None and batch_idx >= max_batches: break input_all, labels, pen_ids, _writers, _image_ids = batch fea_tensor, _ske, img_tensor, cwt_scalo = input_all labels = labels.to(device) pen_ids = pen_ids.to(device) img_tensor = img_tensor.to(device) if cwt_scalo is not None: cwt_scalo = cwt_scalo.to(device) logits = model( fea_tensor, img_tensor, pen_ids, training=False, cwt_scalo=cwt_scalo ) loss = classification_loss(logits, labels, criterion) bs_correct, bs_total = batch_accuracy(logits, labels) correct += bs_correct total += bs_total total_loss += loss.item() n_batches += 1 avg_loss = total_loss / max(n_batches, 1) acc = correct / max(total, 1) return avg_loss, acc @torch.no_grad() def infer_batch( model: DeepEllipseModel, loader: DataLoader, device: torch.device, *, max_batches: Optional[int] = None, ) -> List[dict]: """推理:返回每条样本的 pred / prob / writer / image_id。""" model.eval() results: List[dict] = [] for batch_idx, batch in enumerate(loader): if max_batches is not None and batch_idx >= max_batches: break input_all, labels, pen_ids, writers, image_ids = batch fea_tensor, _ske, img_tensor, cwt_scalo = input_all pen_ids = pen_ids.to(device) img_tensor = img_tensor.to(device) if cwt_scalo is not None: cwt_scalo = cwt_scalo.to(device) logits = model( fea_tensor, img_tensor, pen_ids, training=False, cwt_scalo=cwt_scalo ) probs = F.softmax(logits, dim=-1) preds = probs.argmax(dim=-1) for i in range(logits.size(0)): results.append( { "image_id": image_ids[i], "writer_id": writers[i], "label": int(labels[i].item()), "pred": int(preds[i].item()), "confidence": float(probs[i, preds[i]].item()), } ) return results # --------------------------------------------------------------------------- # 主流程 # --------------------------------------------------------------------------- def build_model_and_loaders( cfg: DeepEllipseConfig, *, debug: bool = False, ) -> Tuple[DeepEllipseModel, DataLoader, DataLoader, Optional[DataLoader], object]: sample_ratio = 0.02 if debug else cfg.sample_ratio min_samples = DataConfig.DEBUG_MIN_SAMPLES if debug else cfg.min_samples batch_size = 4 if debug else cfg.batch_size num_workers = 0 if debug else cfg.num_workers train_loader, val_loader, test_loader, label_encoder = create_dataloaders( batch_size=batch_size, num_workers=num_workers, pin_memory=cfg.pin_memory and torch.cuda.is_available(), sample_ratio=sample_ratio, min_samples=min_samples, use_precomputed=DataConfig.USE_PRECOMPUTED_FEA, verbose=not debug, ) num_classes = len(label_encoder.classes_) cfg.num_classes = num_classes model = DeepEllipseModel(cfg) if debug: # 验证集 CSV 未采样时 batch 极多;调试阶段复用训练子集 val_loader = train_loader return model, train_loader, val_loader, test_loader, label_encoder def run_train(cfg: DeepEllipseConfig, *, debug: bool = False, resume: Optional[str] = None) -> None: device = torch.device(cfg.device) if debug: cfg.num_epochs = min(cfg.num_epochs, 2) model, train_loader, val_loader, _test_loader, label_encoder = build_model_and_loaders( cfg, debug=debug ) model.to(device) total, trainable = model.count_parameters() print(f"DeepEllipse 参数量: total={total:,} trainable={trainable:,}") criterion = nn.CrossEntropyLoss() optimizer = torch.optim.AdamW( model.trainable_parameter_groups()[0]["params"], lr=cfg.lr, weight_decay=cfg.weight_decay, ) start_epoch = 0 best_acc = 0.0 if resume: _m, payload = DeepEllipseModel.load_from_checkpoint(resume, map_location=cfg.device) model.load_state_dict(payload["model_state_dict"], strict=True) if "optimizer_state_dict" in payload: optimizer.load_state_dict(payload["optimizer_state_dict"]) start_epoch = int(payload.get("epoch", 0)) + 1 best_acc = float(payload.get("val_acc", 0.0)) print(f"续训: epoch={start_epoch}, best_val_acc={best_acc:.4f}") ckpt_dir = Path(cfg.checkpoint_dir) ckpt_dir.mkdir(parents=True, exist_ok=True) classes = label_encoder.classes_.tolist() max_batches = 15 if debug else None for epoch in range(start_epoch, cfg.num_epochs): t0 = time.time() train_loss, train_acc = train_one_epoch( model, train_loader, criterion, optimizer, device, max_batches=max_batches, grad_clip=cfg.grad_clip, ) val_loss, val_acc = evaluate( model, val_loader, criterion, device, max_batches=max_batches, ) elapsed = time.time() - t0 print( f"Epoch {epoch + 1}/{cfg.num_epochs} " f"train loss={train_loss:.4f} acc={train_acc:.4f} " f"val loss={val_loss:.4f} acc={val_acc:.4f} " f"({elapsed:.1f}s)" ) last_path = ckpt_dir / "last.pth" model.save_checkpoint( last_path, epoch=epoch, optimizer=optimizer, val_acc=val_acc, label_classes=classes, ) if val_acc >= best_acc: best_acc = val_acc best_path = ckpt_dir / "best.pth" model.save_checkpoint( best_path, epoch=epoch, optimizer=optimizer, val_acc=val_acc, label_classes=classes, ) print(f" -> best saved: {best_path} (acc={best_acc:.4f})") print(f"训练结束, best val acc = {best_acc:.4f}") def run_eval( cfg: DeepEllipseConfig, checkpoint: str, split: str = "val", *, debug: bool = False, ) -> None: device = torch.device(cfg.device) _model, train_loader, val_loader, test_loader, label_encoder = build_model_and_loaders( cfg, debug=debug ) cfg.num_classes = len(label_encoder.classes_) model, _payload = DeepEllipseModel.load_from_checkpoint( checkpoint, num_classes=cfg.num_classes, map_location=cfg.device, ) model.to(device) if split == "train": loader = train_loader elif split == "test" and test_loader is not None: loader = test_loader else: loader = val_loader criterion = nn.CrossEntropyLoss() max_batches = 10 if debug else None loss, acc = evaluate(model, loader, criterion, device, max_batches=max_batches) print(f"[{split}] loss={loss:.4f} acc={acc:.4f} checkpoint={checkpoint}") def run_infer_test( cfg: DeepEllipseConfig, checkpoint: Optional[str] = None, *, debug: bool = False, ) -> None: """快速推理 smoke test(可用随机初始化权重)。""" device = torch.device(cfg.device) model, train_loader, val_loader, _test_loader, label_encoder = build_model_and_loaders( cfg, debug=debug ) model.to(device) if checkpoint and Path(checkpoint).is_file(): state = torch.load(checkpoint, map_location=cfg.device, weights_only=False) model.load_state_dict(state["model_state_dict"], strict=True) print(f"loaded: {checkpoint}") else: print("infer smoke: 随机初始化权重") loader = val_loader max_batches = 5 if debug else 20 results = infer_batch(model, loader, device, max_batches=max_batches) correct = sum(1 for r in results if r["label"] >= 0 and r["pred"] == r["label"]) labeled = sum(1 for r in results if r["label"] >= 0) print(f"infer samples={len(results)} acc={correct/max(labeled,1):.4f}") # 打印前 5 条 for r in results[:5]: wname = label_encoder.classes_[r["pred"]] if r["pred"] < len(label_encoder.classes_) else "?" print( f" id={r['image_id']} writer={r['writer_id']} " f"label={r['label']} pred={r['pred']}({wname}) conf={r['confidence']:.3f}" ) def parse_args() -> argparse.Namespace: p = argparse.ArgumentParser(description="DeepEllipse §4.1–4.3 训练与推理") p.add_argument( "--mode", choices=("train", "val", "test", "infer"), default="train", help="train=训练; val/test=评估; infer=推理 smoke", ) p.add_argument("--checkpoint", type=str, default=None) p.add_argument("--resume", type=str, default=None) p.add_argument("--debug", action="store_true", help="小样本/少 epoch 快速验证") p.add_argument("--device", type=str, default=None) p.add_argument("--epochs", type=int, default=None) p.add_argument("--batch-size", type=int, default=None) p.add_argument("--lr", type=float, default=None) p.add_argument( "--deep", choices=("patch", "resnet", "convnext"), default="convnext", help=f"§4.1.3 深度分支(默认 convnext + {default_convnext_checkpoint()})", ) p.add_argument( "--resnet-ckpt", type=str, default=None, help=f"ResNet 权重路径(默认 {default_resnet_checkpoint()})", ) p.add_argument( "--convnext-ckpt", type=str, default=None, help=f"ConvNeXt 权重路径(默认 {default_convnext_checkpoint()})", ) p.add_argument("--sample-ratio", type=float, default=None) p.add_argument( "--scheme-v2", action="store_true", help="§4.3 v2(可配合 --pool-mode / --no-pre-smooth 做消融)", ) p.add_argument( "--no-pre-smooth", action="store_true", help="v2:关闭角向预平滑(默认开启)", ) p.add_argument( "--pool-mode", choices=("max", "mean", "sp"), default="sp", help="v2 序列池化:max/mean→128维,sp→512维", ) p.add_argument( "--scheme-v3", action="store_true", help="§4.3 v3:ConvNeXt-Tiny 1D 序列金字塔(默认 f_ellipse=768)", ) p.add_argument( "--scheme-v3-1", action="store_true", help="§4.3 v3.1:SGPSR 频谱引导金字塔 stage 路由(f_ellipse=128)", ) p.add_argument( "--v3-pool", choices=("avg", "max"), default="avg", help="v3 / v3.1 全局池化方式", ) p.add_argument( "--v3-1-stage-pool", choices=("global", "seg_sigmoid", "dbsa", "cwt_angular", "cwt_dual"), default="dbsa", help="v3.1:dbsa=DBSA-v2;cwt_angular=CWT-GAPA;cwt_dual=E3 双域 1024", ) p.add_argument( "--angular-norm", choices=("softmax", "sum_relu"), default="sum_relu", help="CWT 角向池化归一化:sum_relu(默认)/ softmax(E1 消融)", ) p.add_argument( "--e3-cross-query", choices=("phi", "cwt_cnn"), default="cwt_cnn", help="cwt_dual 路1:phi(φ→H_orig) 或 cwt_cnn(H_cwt→H_orig)", ) p.add_argument( "--e3-cross-query-path2", choices=("maxpool", "orig_cnn"), default="maxpool", help="cwt_dual 路2:maxpool 或 orig_cnn(H_orig→H_cwt)", ) p.add_argument( "--e3-no-cwt-branch", action="store_true", help="cwt_dual 消融 E3a:关闭路2 CWT MaxPool,仅 GAPA 512", ) p.add_argument( "--cwt-scale-affinity", action="store_true", help="CWT-GAPA:各 stage 独立 scale 子集编码(E2/E2b)", ) p.add_argument( "--cwt-scale-index-mode", choices=("uniform", "physics"), default="uniform", help="E2 尺度分组:uniform | physics", ) p.add_argument( "--e2-cwt-branch", action="store_true", help="E2b:cwt_angular 启用路2 CWT 域 orig→cwt,输出 1024", ) p.add_argument( "--e2-valid-phi", action="store_true", help="E2b:φ MLP 输入拼接 valid embedding(8 维)", ) p.add_argument( "--e2-phi-tx-layers", type=int, default=0, help="E2b:每 stage φ 角向 Transformer 层数(0=关闭,推荐 2)", ) p.add_argument( "--e2-cross-path2", choices=("maxpool", "orig_cnn"), default="orig_cnn", help="E2b 路2:maxpool 或 orig_cnn(H_orig→H_cwt)", ) p.add_argument( "--e2b", action="store_true", help="E2b 快捷:scaleAff + validPhi + phiTx2L + sum_relu + dual orig_cnn", ) return p.parse_args() def main() -> None: args = parse_args() cfg = DeepEllipseConfig(deep_backend=args.deep) if args.resnet_ckpt: cfg.resnet_checkpoint = args.resnet_ckpt if args.convnext_ckpt: cfg.convnext_checkpoint = args.convnext_ckpt if args.device: cfg.device = args.device if args.epochs is not None: cfg.num_epochs = args.epochs if args.batch_size is not None: cfg.batch_size = args.batch_size if args.lr is not None: cfg.lr = args.lr if args.sample_ratio is not None: cfg.sample_ratio = args.sample_ratio scheme_count = sum(int(x) for x in (args.scheme_v2, args.scheme_v3, args.scheme_v3_1)) if scheme_count > 1: raise ValueError("--scheme-v2 / --scheme-v3 / --scheme-v3-1 至多选一个") if args.scheme_v3_1: cfg.use_scheme_v3_1 = True cfg.v3_pool_mode = args.v3_pool cfg.v3_1_stage_pool = args.v3_1_stage_pool cfg.cwt_scale_affinity = args.cwt_scale_affinity cfg.cwt_scale_index_mode = args.cwt_scale_index_mode cfg.angular_norm = args.angular_norm cfg.e3_cross_query = args.e3_cross_query cfg.e3_cross_query_path2 = args.e3_cross_query_path2 cfg.e3_cwt_branch = not args.e3_no_cwt_branch if getattr(args, "e2b", False): cfg.v3_1_stage_pool = "cwt_angular" cfg.cwt_scale_affinity = True cfg.angular_norm = "sum_relu" cfg.e2_cwt_branch = True cfg.cwt_phi_valid_emb = 8 cfg.phi_tx_layers = 2 cfg.e2_cross_path2 = "orig_cnn" else: cfg.e2_cwt_branch = getattr(args, "e2_cwt_branch", False) cfg.cwt_phi_valid_emb = 8 if getattr(args, "e2_valid_phi", False) else 0 cfg.phi_tx_layers = getattr(args, "e2_phi_tx_layers", 0) cfg.e2_cross_path2 = getattr(args, "e2_cross_path2", "orig_cnn") if args.v3_1_stage_pool == "cwt_dual" and cfg.angular_norm == "softmax": cfg.angular_norm = "sum_relu" if cfg.e2_cwt_branch and cfg.angular_norm == "softmax": cfg.angular_norm = "sum_relu" cfg.dropout = 0.2 _, _, cfg43 = cfg.build_section_configs() print( f"§4.3 v3.1: {cfg43.scheme_label()}, " f"f_ellipse dim = {cfg43.d_ellipse}, dropout = {cfg.dropout}" ) elif args.scheme_v3: cfg.use_scheme_v3 = True cfg.v3_pool_mode = args.v3_pool _, _, cfg43 = cfg.build_section_configs() print(f"§4.3 v3: {cfg43.scheme_label()}, f_ellipse dim = {cfg43.d_ellipse}") elif args.scheme_v2: cfg.use_scheme_v2 = True cfg.v2_pre_smooth = not args.no_pre_smooth cfg.v2_pool_mode = args.pool_mode _, _, cfg43 = cfg.build_section_configs() print(f"§4.3 v2: {cfg43.scheme_label()}, f_ellipse dim = {cfg43.d_ellipse}") if args.debug: cfg.num_epochs = 2 cfg.batch_size = 4 cfg.num_workers = 0 cfg.sample_ratio = 0.01 cfg.min_samples = 8 print("debug mode: sample_ratio=0.01, batch=4, epochs=2") if args.mode == "train": run_train(cfg, debug=args.debug, resume=args.resume) elif args.mode in ("val", "test"): ckpt = args.checkpoint or str(Path(cfg.checkpoint_dir) / "best.pth") if not Path(ckpt).is_file(): raise FileNotFoundError(f"checkpoint not found: {ckpt}") run_eval(cfg, ckpt, split=args.mode, debug=args.debug) elif args.mode == "infer": run_infer_test(cfg, checkpoint=args.checkpoint, debug=args.debug) else: raise ValueError(args.mode) if __name__ == "__main__": main()