File size: 28,256 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 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 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Inference-time model assembly helpers.
Extracted verbatim from the training-side scripts (``train_test_deepelipse.py``
and ``train_ay_bce.py``) so that the released inference path does not depend on
any training entry point. Nothing in this file trains: these are the
checkpoint-architecture resolution, checkpoint loading and frozen-backbone
feature-extraction utilities that ``reproduce_best_ay.py`` needs in order to
recompute ``Y`` and the style-affinity adjacency ``A`` from released weights.
"""
from __future__ import annotations
import argparse
from pathlib import Path
import os
from collections import OrderedDict, defaultdict
from typing import List, Optional, Tuple
import torch
import torch.nn as nn
from tqdm import tqdm
from ay_bce_head import PairwiseBCEHead
from convnext_point_feature import default_convnext_checkpoint
from deep_elipse_model import DeepEllipseConfig, DeepEllipseModel
from resnet_point_feature import default_resnet_checkpoint
class TrainConfig:
"""Inference-time stand-in for the training-side config.
The extracted helpers read exactly one attribute off the original training
config object -- the target device -- so a same-named placeholder keeps the
copied function bodies identical to their originals.
"""
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def unpack_dataloader_batch(input_all):
"""exp6_data_loader: (fea_tensor, ske_tensor, img_tensor[, cwt_scalo])。"""
fea_tensor, ske_tensor, img_tensor = input_all[:3]
cwt_scalo = input_all[3] if len(input_all) > 3 else None
return fea_tensor, ske_tensor, img_tensor, cwt_scalo
def normalize_state_dict_keys(state_dict: dict) -> OrderedDict:
"""去掉 DataParallel 的 module. 前缀。"""
out = OrderedDict()
for k, v in state_dict.items():
nk = k[7:] if k.startswith("module.") else k
out[nk] = v
return out
def detect_enc43_backbone(state_dict: dict) -> str:
keys = list(state_dict.keys())
if any("enc43.pyramid." in k for k in keys):
return "convnext1d"
if any("enc43.ms_stem." in k for k in keys):
return "multiscale_stem"
return "unknown"
def peek_reload_checkpoint(pretrained_path: str):
raw = torch.load(pretrained_path, map_location="cpu", weights_only=False)
if isinstance(raw, dict) and "model_state_dict" in raw:
sd = normalize_state_dict_keys(raw["model_state_dict"])
meta_cfg = raw.get("config", {}) or {}
elif isinstance(raw, dict):
sd = normalize_state_dict_keys(raw)
meta_cfg = {}
else:
sd = normalize_state_dict_keys(raw)
meta_cfg = {}
return sd, meta_cfg, detect_enc43_backbone(sd)
def detect_architecture_from_state_dict(state_dict: dict) -> dict:
"""从权重键名推断训练时的 §4.3 / CWT 架构。"""
keys = list(state_dict.keys())
arch = {
"use_scheme_v3_1": False,
"use_scheme_v3": False,
"v3_1_stage_pool": "dbsa",
"cwt_scale_affinity": False,
"cwt_scale_index_mode": "uniform",
"angular_norm": "sum_relu",
"e3_cross_query": "cwt_cnn",
"e3_cross_query_path2": "maxpool",
"e3_cwt_branch": True,
"e2_cwt_branch": False,
"cwt_phi_valid_emb": 0,
"e2_cross_path2": "orig_cnn",
"phi_tx_layers": 0,
"gcc_enabled": False,
}
if any("enc43.segment_pool.gcc." in k for k in keys):
arch["gcc_enabled"] = True
if any("enc43.pyramid." in k for k in keys):
arch["use_scheme_v3_1"] = True
elif any("enc43.ms_stem." in k for k in keys):
return arch
if any("enc43.segment_pool.cwt_pyramid." in k for k in keys):
arch["v3_1_stage_pool"] = "cwt_dual"
elif any(k.startswith("cwt_encoder.") for k in keys):
arch["v3_1_stage_pool"] = "cwt_angular"
elif any("enc43.segment_pool.pools." in k for k in keys):
arch["v3_1_stage_pool"] = "cwt_angular"
elif any("enc43.segment_pool.fine_pool." in k for k in keys):
arch["v3_1_stage_pool"] = "dbsa"
elif any("enc43.segment_pool.gates." in k for k in keys):
arch["v3_1_stage_pool"] = "seg_sigmoid"
elif arch["use_scheme_v3_1"] and not any("enc43.segment_pool." in k for k in keys):
arch["v3_1_stage_pool"] = "global"
if any("enc43.segment_pool.stage_phi_enc." in k for k in keys):
arch["cwt_scale_affinity"] = True
if any("enc43.segment_pool.stage_phi_enc.valid_emb." in k for k in keys):
arch["cwt_phi_valid_emb"] = 8
if any("enc43.segment_pool.phi_transformers." in k for k in keys):
arch["phi_tx_layers"] = 2
if arch["v3_1_stage_pool"] == "cwt_angular" and any(
"enc43.segment_pool.cwt_pyramid." in k for k in keys
):
arch["e2_cwt_branch"] = True
if any("enc43.segment_pool.path2_pools." in k for k in keys):
arch["e2_cross_path2"] = "orig_cnn"
if arch["v3_1_stage_pool"] == "cwt_dual":
arch["e3_cwt_branch"] = any(
"enc43.segment_pool.cwt_pyramid." in k for k in keys
)
if any("enc43.segment_pool.path2_pools." in k for k in keys):
arch["e3_cross_query_path2"] = "orig_cnn"
q_w_key = "enc43.segment_pool.pools.0.q_proj.weight"
if q_w_key in state_dict:
in_dim = state_dict[q_w_key].shape[1]
arch["e3_cross_query"] = "phi" if in_dim == 64 else "cwt_cnn"
return arch
def _merge_checkpoint_arch(meta_cfg: dict, state_dict: dict) -> dict:
inferred = detect_architecture_from_state_dict(state_dict)
merged = dict(inferred)
if meta_cfg.get("use_scheme_v3_1"):
merged["use_scheme_v3_1"] = True
if meta_cfg.get("use_scheme_v3"):
merged["use_scheme_v3"] = True
if meta_cfg.get("v3_1_stage_pool"):
merged["v3_1_stage_pool"] = meta_cfg["v3_1_stage_pool"]
if meta_cfg.get("cwt_scale_affinity"):
merged["cwt_scale_affinity"] = bool(meta_cfg["cwt_scale_affinity"])
if meta_cfg.get("cwt_scale_index_mode"):
merged["cwt_scale_index_mode"] = meta_cfg["cwt_scale_index_mode"]
if meta_cfg.get("angular_norm"):
merged["angular_norm"] = meta_cfg["angular_norm"]
if meta_cfg.get("e3_cross_query"):
merged["e3_cross_query"] = meta_cfg["e3_cross_query"]
if meta_cfg.get("e3_cross_query_path2"):
merged["e3_cross_query_path2"] = meta_cfg["e3_cross_query_path2"]
if "e3_cwt_branch" in meta_cfg:
merged["e3_cwt_branch"] = bool(meta_cfg["e3_cwt_branch"])
if "e2_cwt_branch" in meta_cfg:
merged["e2_cwt_branch"] = bool(meta_cfg["e2_cwt_branch"])
if meta_cfg.get("cwt_phi_valid_emb"):
merged["cwt_phi_valid_emb"] = int(meta_cfg["cwt_phi_valid_emb"])
if meta_cfg.get("e2_cross_path2"):
merged["e2_cross_path2"] = meta_cfg["e2_cross_path2"]
if meta_cfg.get("phi_tx_layers") is not None:
merged["phi_tx_layers"] = int(meta_cfg["phi_tx_layers"])
if meta_cfg.get("gcc_enabled") is not None:
merged["gcc_enabled"] = bool(meta_cfg["gcc_enabled"])
return merged
def align_args_with_checkpoint(args, parser, ckpt_path: str, *, purpose: str) -> None:
"""在 build_elipse_model 之前,按 checkpoint 对齐 scheme / stage_pool。"""
if not ckpt_path or not os.path.isfile(ckpt_path):
return
sd, ckpt_cfg, ckpt_backbone = peek_reload_checkpoint(ckpt_path)
arch = _merge_checkpoint_arch(ckpt_cfg, sd)
user_scheme = args.scheme_v2 or args.scheme_v3 or args.scheme_v3_1
if purpose in ("test", "resume"):
if arch["use_scheme_v3_1"]:
args.scheme_v3_1 = True
args.scheme_v2 = False
args.scheme_v3 = False
elif arch["use_scheme_v3"]:
args.scheme_v3 = True
args.scheme_v2 = False
args.scheme_v3_1 = False
args.v3_1_stage_pool = arch["v3_1_stage_pool"]
args.cwt_scale_affinity = arch["cwt_scale_affinity"]
args.gcc_enabled = arch.get("gcc_enabled", False)
if hasattr(args, "cwt_scale_index_mode"):
args.cwt_scale_index_mode = arch.get("cwt_scale_index_mode", "uniform")
if hasattr(args, "angular_norm"):
args.angular_norm = arch.get("angular_norm", "sum_relu")
if hasattr(args, "e3_cross_query"):
args.e3_cross_query = arch.get("e3_cross_query", "cwt_cnn")
if hasattr(args, "e3_cross_query_path2"):
args.e3_cross_query_path2 = arch.get("e3_cross_query_path2", "maxpool")
if hasattr(args, "e3_no_cwt_branch"):
args.e3_no_cwt_branch = not arch.get("e3_cwt_branch", True)
if hasattr(args, "e2_cwt_branch"):
args.e2_cwt_branch = arch.get("e2_cwt_branch", False)
if hasattr(args, "e2_valid_phi"):
args.e2_valid_phi = arch.get("cwt_phi_valid_emb", 0) > 0
if hasattr(args, "e2_phi_tx_layers"):
args.e2_phi_tx_layers = arch.get("phi_tx_layers", 0)
if hasattr(args, "e2_cross_path2"):
args.e2_cross_path2 = arch.get("e2_cross_path2", "orig_cnn")
print(
f"📦 checkpoint 对齐架构 ({purpose}): "
f"scheme_v3_1={args.scheme_v3_1}, pool={args.v3_1_stage_pool}, "
f"cwt_scale_affinity={args.cwt_scale_affinity}, "
f"cwt_scale_index_mode={arch.get('cwt_scale_index_mode', 'uniform')}, "
f"angular_norm={arch.get('angular_norm', 'sum_relu')}, "
f"e3_cross_query={arch.get('e3_cross_query', 'cwt_cnn')}, "
f"e3_cross_query_path2={arch.get('e3_cross_query_path2', 'maxpool')}, "
f"e3_cwt_branch={arch.get('e3_cwt_branch', True)}, "
f"e2_cwt_branch={arch.get('e2_cwt_branch', False)}, "
f"cwt_phi_valid_emb={arch.get('cwt_phi_valid_emb', 0)}, "
f"phi_tx_layers={arch.get('phi_tx_layers', 0)}"
)
return
if ckpt_backbone == "convnext1d" and not user_scheme:
print("⚠️ checkpoint 含 enc43.pyramid → 自动启用 --scheme-v3-1")
args.scheme_v3_1 = True
if ckpt_backbone == "convnext1d" and not args.scheme_v3_1 and not args.scheme_v3:
parser.error("checkpoint 为 ConvNeXt1D,须加 --scheme-v3-1 或 --scheme-v3")
if ckpt_backbone == "multiscale_stem" and (args.scheme_v3_1 or args.scheme_v3):
parser.error("checkpoint 为 v1 multiscale_stem,不应加 --scheme-v3-1")
def load_partial_weights(model, pretrained_path, verbose_ignore: bool = False):
"""
从预训练模型加载权重,自动跳过形状不匹配的层。
"""
print(f"load_partial_weights: {pretrained_path}")
raw = torch.load(pretrained_path, map_location=TrainConfig.DEVICE, weights_only=False)
if isinstance(raw, dict) and "model_state_dict" in raw:
pretrained_dict = normalize_state_dict_keys(raw["model_state_dict"])
elif isinstance(raw, dict):
pretrained_dict = normalize_state_dict_keys(raw)
else:
pretrained_dict = normalize_state_dict_keys(raw)
model_dict = model.state_dict()
ckpt_backbone = detect_enc43_backbone(pretrained_dict)
model_backbone = detect_enc43_backbone(model_dict)
print(f"checkpoint §4.3 骨干: {ckpt_backbone} | 当前模型: {model_backbone}")
filtered_dict = OrderedDict()
matched_count = 0
skipped_count = 0
ignored_count = 0
ignored_groups: defaultdict = defaultdict(int)
print("--- 开始权重过滤与加载 ---")
for k, v in pretrained_dict.items():
if k in model_dict:
if v.shape == model_dict[k].shape:
filtered_dict[k] = v
matched_count += 1
else:
skipped_count += 1
print(f"[SKIP] 形状不匹配: {k}")
print(f" 旧权重形状: {v.shape}")
print(f" 新模型形状: {model_dict[k].shape}")
if k == "enc41.proj_deep.weight":
print(" (切换 deep_backend 时 proj_deep 维变化属预期)")
else:
ignored_count += 1
prefix = ".".join(k.split(".")[:3])
ignored_groups[prefix] += 1
if verbose_ignore:
print(f"[IGNORE] 新模型中不存在该层: {k}")
model_dict.update(filtered_dict)
model.load_state_dict(model_dict, strict=False)
def _count_loaded(tag: str) -> int:
return sum(1 for k in filtered_dict if tag in k)
pyramid_ckpt = sum(1 for k in pretrained_dict if "enc43.pyramid." in k)
pyramid_loaded = _count_loaded("enc43.pyramid.")
stage_proj_ckpt = sum(1 for k in pretrained_dict if "enc43.stage_proj." in k)
stage_proj_loaded = _count_loaded("enc43.stage_proj.")
cwt_ckpt = sum(1 for k in pretrained_dict if k.startswith("cwt_encoder."))
cwt_loaded = _count_loaded("cwt_encoder.")
print("-----------------------------")
print(f"成功加载层数: {matched_count}")
print(f"跳过层数 (形状不符): {skipped_count}")
print(f"忽略层数 (新模型无对应键): {ignored_count}")
if ignored_groups and not verbose_ignore:
top = sorted(ignored_groups.items(), key=lambda x: -x[1])[:8]
print("忽略键前缀 Top:", ", ".join(f"{p}({n})" for p, n in top))
if pyramid_ckpt:
print(f"enc43.pyramid: {pyramid_loaded}/{pyramid_ckpt} 层已加载")
if stage_proj_ckpt:
print(f"enc43.stage_proj: {stage_proj_loaded}/{stage_proj_ckpt} 层已加载")
if cwt_ckpt or cwt_loaded:
print(f"cwt_encoder: {cwt_loaded}/{cwt_ckpt} 层已加载(CWT 头通常重新初始化)")
print("权重加载完成。")
if ckpt_backbone == "convnext1d" and model_backbone != "convnext1d":
raise ValueError(
"checkpoint 含 enc43.pyramid,但当前模型不是 v3/v3.1 ConvNeXt1D。\n"
"请在训练命令中加 --scheme-v3-1(CWT 微调示例:\n"
" --scheme-v3-1 --v3-1-stage-pool cwt_angular --reload_model <ckpt>)"
)
if pyramid_ckpt > 0 and pyramid_loaded == 0:
raise ValueError(
"enc43.pyramid 权重一层都未加载。请确认已加 --scheme-v3-1 且 deep_elipse_model.py / "
"section43_v3_1.py 已同步到服务器。"
)
return model
def build_elipse_model(
num_classes: int,
use_single_model_weiht: bool = False,
use_scheme_v2: bool = False,
use_scheme_v3: bool = False,
use_scheme_v3_1: bool = False,
v2_pre_smooth: bool = True,
v2_pool_mode: str = "sp",
v3_pool_mode: str = "avg",
v3_1_stage_pool: str = "global",
cwt_scale_affinity: bool = False,
cwt_scale_index_mode: str = "uniform",
angular_norm: str = "sum_relu",
e3_cross_query: str = "cwt_cnn",
e3_cross_query_path2: str = "maxpool",
e3_cwt_branch: bool = True,
e2_cwt_branch: bool = False,
cwt_phi_valid_emb: int = 0,
e2_cross_path2: str = "orig_cnn",
phi_tx_layers: int = 0,
phi_tx_heads: int = 4,
phi_tx_ffn: int = 256,
gcc_enabled: bool = False,
deep_backend: str = "convnext",
resnet_checkpoint: Optional[str] = None,
convnext_checkpoint: Optional[str] = None,
seq_backend: Optional[str] = None,
):
"""构建 DeepEllipseModel(函数名保留以兼容原脚本调用)。"""
cfg = DeepEllipseConfig(
num_classes=num_classes,
deep_backend=deep_backend,
resnet_checkpoint=resnet_checkpoint or default_resnet_checkpoint(),
convnext_checkpoint=convnext_checkpoint or default_convnext_checkpoint(),
use_scheme_v2=use_scheme_v2,
use_scheme_v3=use_scheme_v3,
use_scheme_v3_1=use_scheme_v3_1,
seq_backend=seq_backend,
v2_pre_smooth=v2_pre_smooth,
v2_pool_mode=v2_pool_mode,
v3_pool_mode=v3_pool_mode,
v3_1_stage_pool=v3_1_stage_pool,
cwt_scale_affinity=cwt_scale_affinity,
cwt_scale_index_mode=cwt_scale_index_mode,
angular_norm=angular_norm,
e3_cross_query=e3_cross_query,
e3_cross_query_path2=e3_cross_query_path2,
e3_cwt_branch=e3_cwt_branch,
e2_cwt_branch=e2_cwt_branch,
cwt_phi_valid_emb=cwt_phi_valid_emb,
e2_cross_path2=e2_cross_path2,
phi_tx_layers=phi_tx_layers,
phi_tx_heads=phi_tx_heads,
phi_tx_ffn=phi_tx_ffn,
gcc_enabled=gcc_enabled,
)
model = DeepEllipseModel(cfg)
if use_single_model_weiht:
checkpoint_path = "./checkpoints/deepelipse0526/best_model_epoch0_best.pth"
if os.path.isfile(checkpoint_path):
print(f"load deep ellipse: {checkpoint_path}")
checkpoint = torch.load(checkpoint_path, map_location=TrainConfig.DEVICE, weights_only=False)
model.load_state_dict(checkpoint["model_state_dict"], strict=False)
print("DeepEllipseModel 创建成功")
if use_scheme_v3_1:
scheme = model.enc43.config.scheme_label()
elif use_scheme_v3:
scheme = model.enc43.config.scheme_label()
elif use_scheme_v2:
scheme = model.enc43.config.scheme_label()
else:
scheme = "v1(MaxPool)"
print(f" §4.3 方案: {scheme}")
print(f" f_ellipse dim: {model.d_ellipse}")
print(f" §4.1.3 deep_backend: {model.enc41.deep_branch.backend}")
if model.enc41.deep_branch.backend == "convnext":
ckpt = getattr(model.enc41.config, "convnext_checkpoint", None)
print(f" §4.1.3 convnext_ckpt: {ckpt}")
elif model.enc41.deep_branch.backend == "resnet":
ckpt = getattr(model.enc41.config, "resnet_checkpoint", None)
print(f" §4.1.3 resnet_ckpt: {ckpt}")
print(f" E_pen dim (d_pen): {model.d_pen}")
total_params = sum(p.numel() for p in model.parameters())
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f" 总参数量:{total_params:,}")
print(f" 可训练参数:{trainable_params:,} ({trainable_params / total_params * 100:.2f}%)")
return model
def load_checkpoint(filepath: str, model, optimizer=None, scheduler=None, device=None):
"""加载检查点;模型权重走 partial 匹配,避免 strict 因架构不一致报错。"""
if device is None:
device = TrainConfig.DEVICE
print(f"📦 加载检查点: {filepath}")
checkpoint = torch.load(filepath, map_location=device, weights_only=False)
load_partial_weights(model, filepath)
if optimizer and "optimizer_state_dict" in checkpoint:
try:
optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
except Exception as exc:
print(f"⚠️ 优化器状态未加载: {exc}")
if scheduler and checkpoint.get("scheduler_state_dict"):
try:
scheduler.load_state_dict(checkpoint["scheduler_state_dict"])
except Exception as exc:
print(f"⚠️ scheduler 状态未加载: {exc}")
print("✅ 检查点加载完成")
return {
"start_epoch": checkpoint.get("epoch", 0) + 1,
"best_val_acc": checkpoint.get("val_acc", 0.0),
"label_encoder_classes": checkpoint.get("label_encoder_classes"),
}
def unpack_batch(input_all):
return unpack_dataloader_batch(input_all)
def freeze_backbone(model: nn.Module) -> None:
model.eval()
for p in model.parameters():
p.requires_grad = False
def unfreeze_backbone(model: nn.Module, *, unfreeze_deep: bool = True) -> int:
"""Enable grads on backbone. Returns #trainable params."""
model.train()
n = 0
for name, p in model.named_parameters():
if (not unfreeze_deep) and (
"enc41.deep_branch" in name or name.startswith("enc41.deep_branch")
):
p.requires_grad = False
continue
p.requires_grad = True
n += p.numel()
return n
def extract_f_ellipse(
model,
fea,
img,
pen_ids,
cwt_scalo,
device,
*,
train_mode: bool = False,
):
if model is None:
# head-only mode: fea already carries precomputed f_ellipse
f = fea.to(device)
return f if train_mode else f.detach()
if hasattr(fea, "to"):
fea = fea.to(device)
img = img.to(device)
pen_ids = pen_ids.to(device)
if cwt_scalo is not None:
cwt_scalo = cwt_scalo.to(device)
_, details = model(
fea,
img,
pen_ids,
training=train_mode,
return_details=True,
cwt_scalo=cwt_scalo,
)
f = details["f_ellipse"]
return f if train_mode else f.detach()
def extract_logits(
model,
fea,
img,
pen_ids,
cwt_scalo,
device,
*,
train_mode: bool = False,
):
"""CE-head writer logits from a DeepEllipse forward (same as training export)."""
if hasattr(fea, "to"):
fea = fea.to(device)
img = img.to(device)
pen_ids = pen_ids.to(device)
if cwt_scalo is not None:
cwt_scalo = cwt_scalo.to(device)
logits = model(
fea,
img,
pen_ids,
training=train_mode,
return_details=False,
cwt_scalo=cwt_scalo,
)
return logits if train_mode else logits.detach()
def collect_test_y(
backbone,
loader,
device,
) -> Tuple[List[str], torch.Tensor]:
"""Run the frozen CE head over the test loader → (image_ids, Y[N,C])."""
backbone.eval()
ids: List[str] = []
ys: List[torch.Tensor] = []
for input_all, labels, pen_ids, writer_ids, image_ids in tqdm(
loader, desc="Extract Y (CE head)"
):
fea, ske, img, cwt = unpack_batch(input_all)
logits = extract_logits(
backbone, fea, img, pen_ids, cwt, device, train_mode=False
)
ids.extend([str(x) for x in image_ids])
ys.append(logits.cpu())
return ids, torch.cat(ys, dim=0)
def collect_test_h_and_y(
backbone,
head: PairwiseBCEHead,
loader,
device,
) -> Tuple[List[str], torch.Tensor, torch.Tensor]:
"""One backbone pass: affinity embedding h and CE logits Y."""
head.eval()
backbone.eval()
ids: List[str] = []
hs: List[torch.Tensor] = []
ys: List[torch.Tensor] = []
for input_all, labels, pen_ids, writer_ids, image_ids in tqdm(
loader, desc="Extract h+Y (test)"
):
fea, ske, img, cwt = unpack_batch(input_all)
if hasattr(fea, "to"):
fea = fea.to(device)
img = img.to(device)
pen_ids = pen_ids.to(device)
if cwt is not None:
cwt = cwt.to(device)
logits, details = backbone(
fea, img, pen_ids, training=False, return_details=True, cwt_scalo=cwt
)
f = details["f_ellipse"].detach()
h = head(f)
ids.extend([str(x) for x in image_ids])
hs.append(h.cpu())
ys.append(logits.detach().cpu())
return ids, torch.cat(hs, dim=0), torch.cat(ys, dim=0)
def collect_test_h(
backbone,
head: PairwiseBCEHead,
loader,
device,
) -> Tuple[List[str], torch.Tensor]:
head.eval()
if backbone is not None:
backbone.eval()
ids: List[str] = []
hs: List[torch.Tensor] = []
for input_all, labels, pen_ids, writer_ids, image_ids in tqdm(
loader, desc="Extract h (test)"
):
fea, ske, img, cwt = unpack_batch(input_all)
f = extract_f_ellipse(
backbone, fea, img, pen_ids, cwt, device, train_mode=False
)
h = head(f)
ids.extend([str(x) for x in image_ids])
hs.append(h.cpu())
return ids, torch.cat(hs, dim=0)
def _checkpoint_has_paper679_phi(ckpt_path) -> bool:
"""True if the backbone checkpoint carries the recovered standalone phi weights."""
try:
raw = torch.load(str(ckpt_path), map_location="cpu", weights_only=False)
except Exception:
return False
state = raw.get("model_state_dict", raw) if isinstance(raw, dict) else raw
return any(
k.endswith("segment_pool.phi_transformers.0.norm.weight") for k in state
)
def build_backbone(
args, num_classes: int, device, *, freeze: bool = True
) -> DeepEllipseModel:
ns = argparse.Namespace(
scheme_v2=False,
scheme_v3=False,
scheme_v3_1=True,
v3_1_stage_pool="cwt_angular",
cwt_scale_affinity=True,
cwt_scale_index_mode="uniform",
angular_norm="sum_relu",
e3_cross_query="cwt_cnn",
e3_cross_query_path2="maxpool",
e3_no_cwt_branch=False,
e2_cwt_branch=True,
e2_valid_phi=True,
e2_phi_tx_layers=2,
e2_cross_path2="orig_cnn",
gcc_enabled=False,
e2b=True,
)
parser = argparse.ArgumentParser()
align_args_with_checkpoint(ns, parser, args.backbone_ckpt, purpose="test")
# Paper-679 path: keep GCC module in the codebase/ckpt, but run the
# standalone phi_transformers branch with recovered weights so A/Y from
# the model match Private 0.67963 (do not load segment_pool.gcc.*).
paper_679 = bool(getattr(args, "paper_679", False))
phi_sidecar = getattr(args, "phi_sidecar", None)
if paper_679:
ns.gcc_enabled = False
if getattr(ns, "e2_phi_tx_layers", 0) <= 0:
ns.e2_phi_tx_layers = 2
print("📄 paper-679: gcc_enabled=False (standalone phi_transformers)")
model = build_elipse_model(
num_classes,
use_scheme_v3_1=True,
v3_1_stage_pool=ns.v3_1_stage_pool,
cwt_scale_affinity=ns.cwt_scale_affinity,
cwt_scale_index_mode=getattr(ns, "cwt_scale_index_mode", "uniform"),
angular_norm=getattr(ns, "angular_norm", "sum_relu"),
e3_cross_query=getattr(ns, "e3_cross_query", "cwt_cnn"),
e3_cross_query_path2=getattr(ns, "e3_cross_query_path2", "maxpool"),
e3_cwt_branch=not getattr(ns, "e3_no_cwt_branch", False),
e2_cwt_branch=getattr(ns, "e2_cwt_branch", True),
cwt_phi_valid_emb=8 if getattr(ns, "e2_valid_phi", True) else 0,
e2_cross_path2=getattr(ns, "e2_cross_path2", "orig_cnn"),
phi_tx_layers=getattr(ns, "e2_phi_tx_layers", 2),
gcc_enabled=getattr(ns, "gcc_enabled", False),
deep_backend=args.deep,
resnet_checkpoint=args.resnet_ckpt,
convnext_checkpoint=args.convnext_ckpt,
)
model.to(device)
load_checkpoint(args.backbone_ckpt, model, device=device)
if paper_679 or phi_sidecar:
pool = model.enc43.segment_pool
if getattr(pool, "phi_transformers", None) is None:
raise RuntimeError("paper-679 model has no phi_transformers (gcc still on?)")
# In-ckpt weights are the primary source: the released backbone carries the
# recovered standalone phi under enc43.segment_pool.phi_transformers.*, and
# load_partial_weights above already matched them.
in_ckpt = _checkpoint_has_paper679_phi(args.backbone_ckpt)
side = phi_sidecar
if side is None:
cand = [
Path("paper679_phi_transformers.pt"),
Path(args.backbone_ckpt).resolve().parent / "paper679_phi_transformers.pt",
]
side = next((c for c in cand if c.is_file()), None)
if in_ckpt:
print("📄 paper-679 phi loaded from backbone checkpoint "
"(enc43.segment_pool.phi_transformers.*)")
if side is not None and Path(side).is_file():
blob = torch.load(str(side), map_location="cpu", weights_only=False)
phi_sd = blob.get("phi_transformers", blob)
pool.phi_transformers.load_state_dict(phi_sd, strict=True)
print(f"📄 paper-679 phi sidecar override: {side} ({len(phi_sd)} tensors)")
elif not in_ckpt:
raise FileNotFoundError(
"paper-679 needs recovered phi weights: neither "
"enc43.segment_pool.phi_transformers.* in the checkpoint nor a "
"paper679_phi_transformers.pt sidecar was found"
)
if freeze:
freeze_backbone(model)
else:
n = unfreeze_backbone(
model, unfreeze_deep=bool(getattr(args, "unfreeze_deep", True))
)
print(f"Backbone unfrozen: {n:,} trainable params (unfreeze_deep={args.unfreeze_deep})")
return model
|