本文档记录 owkin/phikon-v2 在昇腾 910B4 NPU 上的适配与验证结果。
Phikon-v2 是 Owkin 发布的病理学基础模型,基于 DINOv2 ViT-Large 架构在 PANCAN-XL 数据集上预训练,专为组织病理学图像特征提取设计,广泛用于生物标志物预测、WSI 分类等下游任务。
| 属性 | 规格 |
|---|---|
| 架构 | DINOv2 ViT-Large (ViT-L/16) |
| 参数量 | ~303M |
| 输入尺寸 | 224 × 224 RGB |
| 输出维度 | 1024-dim 特征向量 (CLS token + LayerNorm) |
| 权重格式 | Safetensors (~1.2GB) |
相关获取地址:
| 组件 | 版本 |
|---|---|
CANN | 8.5.1 |
torch | 2.9.0+cpu |
torch-npu | 2.9.0.post1+gitee7ba04 |
transformers | 4.57.6 |
910B4 单卡../weight/phikon-v2使用 atomgit 命令行工具从 AtomGit 社区下载权重:
pip install atomgit
python3 -m atomgit download hf_mirrors/owkin/phikon-v2 -d ./weight/phikon-v2下载完成后目录结构示例:
weight/phikon-v2/
├── config.json
├── model.safetensors # ~1.2GB
├── preprocessor_config.json
└── README.mdpip install -r requirements.txt依赖清单:
torch>=2.0.0
torch-npu>=2.9.0
transformers>=4.30.0
numpy>=1.24.0
Pillow>=9.0.0python inference.py \
--model_path ../weight/phikon-v2 \
--device npu \
--batch_size 1 \
--output output_features.npypython inference.py \
--model_path ../weight/phikon-v2 \
--device npu \
--batch_size 8 \
--output output_features.npy[INFO] Loading model from: ../weight/phikon-v2
[INFO] Target device: npu:0
Using a slow image processor as `use_fast` is unset and a slow processor was saved with this model. `use_fast=True` will be the default behavior in v4.52, even if the model was saved with a slow processor. This will result in minor differences in outputs. You'll still be able to use a slow processor with `use_fast=False`.
[INFO] Model loaded successfully
[INFO] No image provided, using random generated images
[INFO] Warming up (3 iterations)...
[INFO] Running inference...
[INFO] Inference completed in 53.38 ms
[INFO] Output shape: (8, 1024)
[INFO] Output dtype: float32
[INFO] Feature sample (first 10 dims): [ 0.30236924 -0.04790726 -0.60215485 0.74291444 -0.04013106 0.09236915
0.1826123 -0.8327827 0.02285304 1.00131 ]
[INFO] Features saved to: output_features.npy
[INFO] Metadata saved to: output_features.jsonpython inference.py \
--model_path ../weight/phikon-v2 \
--device npu \
--image_path /path/to/tile.png \
--batch_size 1 \
--output output_features.npyinference.py 支持以下参数:
| 参数 | 说明 | 默认值 |
|---|---|---|
--model_path | 本地模型目录路径 | ../weight/phikon-v2 |
--device | 推理设备:cpu 或 npu | npu |
--image_path | 输入图像路径(为空则使用随机图) | None |
--batch_size | 推理批次大小 | 1 |
--output | 输出特征文件路径 | output_features.npy |
--warmup | Warmup 迭代次数 | 3 |
输出文件:
output_features.npy:numpy 数组,shape 为 (batch_size, 1024)output_features.json:推理元数据(设备、batch_size、耗时等)使用 eval_precision.py 对同一批随机图像分别在 CPU 和 NPU 上推理,对比输出特征向量的:
python eval_precision.py \
--model_path ../weight/phikon-v2 \
--batch_size 8 \
--rounds 5测试条件:batch_size=8,rounds=5,随机生成 224×224 RGB 图像。
| 指标 | 数值 |
|---|---|
cosine_similarity | 0.999998 |
MAE | 1.044e-03 |
MSE | 5.909e-06 |
Max AE | 5.556e-02 |
结论:NPU 输出与 CPU 参考输出高度一致,余弦相似度达到 0.999998,满足精度要求。
使用 eval_performance.py 在 NPU 上测试不同 batch size 下的推理延迟与吞吐。
python eval_performance.py \
--device npu \
--batch_sizes 1 8 32 \
--iterations 50 \
--warmup 10测试条件:NPU 910B4,warmup=10,iterations=50。
| batch_size | mean_latency_ms | p99_latency_ms | throughput_imgs/sec |
|---|---|---|---|
1 | 24.55 | 25.78 | 40.73 |
8 | 26.91 | 27.91 | 297.25 |
32 | 96.13 | 96.34 | 332.89 |
结论:
apply_layernorm=true),推理脚本已自动处理,无需手动干预。phikon-v2-ascend/
├── inference.py # 推理脚本
├── eval_precision.py # 精度评测脚本
├── eval_performance.py # 性能评测脚本
├── requirements.txt # Python 依赖
├── readme.md # 本文档
├── logs/ # 运行日志
├── images/ # 截图/示例图
└── .gitignore # Git 忽略规则模型权重遵循 Owkin 原始许可证(非商业用途)。本适配脚本采用 MIT 许可证。