fix(actpred): ONNX opset 升级 + NumPy 兼容 + 断点续训 + MobileNetV2 离线弹性

- export_onnx: opset 17→18,dynamic_axes 改 dynamic_shapes 消除弃用警告
- predict: action_stats 改为 torch tensor,消除 NumPy 2.0 __array_wrap__ 警告
- train/pretrain: 每 epoch 保存 last.pt,新增 --resume 断点续训练
- model: MobileNetEncoder 下载失败时回退到已缓存权重
- _set.sh: 安装时预缓存 MobileNetV2 权重,失败给出明确指引
- test: 新增 TestResumeCheckpoint(2 用例),总计 58 测试全绿
This commit is contained in:
qing committed 2026-08-10 04:07:37 +00:00
1 parent 9fc3542771
commit d17677bade
9 files changed
+183 -18

No files matched your search

+24 -7
View File
@@ -36,13 +36,30 @@ class MobileNetEncoder(nn.Module):
except Exception as e:
if not pretrained:
raise RuntimeError("加载 MobileNetV2 架构失败") from e
raise RuntimeError(
"MobileNetV2 预训练权重下载失败,可能是网络受限。"
"请在联网环境中预下载权重"
"(运行 torchvision.models.mobilenet_v2(weights='IMAGENET1K_V1')),"
"并将 ~/.cache/torch/hub/checkpoints/ 下的权重文件"
"拷贝到目标机器同路径。"
) from e
# 主源下载失败,尝试加载已缓存的权重文件
try:
import os
hub_dir = torch.hub.get_dir()
cache_dir = os.path.join(hub_dir, "checkpoints")
weight_file = getattr(weights, "file_name",
"mobilenet_v2-b0353104.pth")
cached_path = os.path.join(cache_dir, weight_file)
if os.path.exists(cached_path):
print(f"INFO: 使用缓存权重 {cached_path}")
backbone = mobilenet_v2(weights=None)
backbone.load_state_dict(torch.load(
cached_path, map_location="cpu",
weights_only=True))
else:
raise FileNotFoundError(cached_path)
except Exception:
raise RuntimeError(
"MobileNetV2 预训练权重下载失败,可能是网络受限。"
"请在联网环境中预下载权重"
"(运行 torchvision.models.mobilenet_v2(weights='IMAGENET1K_V1')),"
"并将 ~/.cache/torch/hub/checkpoints/ 下的权重文件"
"拷贝到目标机器同路径。"
) from e
self.features = backbone.features
self.pool = nn.AdaptiveAvgPool2d(1)
self.proj = nn.Linear(1280, feature_dim)