本文档记录 google/efficientnet-b0 EfficientNet-B0 图像分类模型在昇腾 NPU(Ascend 910B3)上的迁移适配、精度评测与性能验证结果。
EfficientNet 是 Google 提出的通过 NAS(神经架构搜索)优化的 CNN 系列,B0 是最轻量基线版本(5.3M 参数)。通过复合缩放策略(同时调整深度/宽度/分辨率)实现极佳的精度-效率权衡。在 ImageNet-1k 上训练,支持 1000 类分类。以 timm 格式发布,采用 timm.create_model() 加载。
相关获取地址:
| 组件 | 版本 |
|---|---|
torch | 2.8.0 |
torch_npu | 2.8.0.post4 |
timm | 1.0.27 |
CANN | 8.5.1 |
8 × Ascend 910B3timm.create_model() + safetensors(timm 格式)conda create -n google--efficientnet-b0 python=3.11 -y
conda activate google--efficientnet-b0
pip install torch==2.8.0 torch_npu==2.8.0.post4 \
-i https://pypi.tuna.tsinghua.edu.cn/simple
pip install timm safetensors pillow numpy \
-i https://pypi.tuna.tsinghua.edu.cn/simplepython inference.py --model_path ./google--efficientnet-b0 --image photo.jpg --device npufrom inference import TimmClassifier
clf = TimmClassifier(model_path="./google--efficientnet-b0", device="npu")
probs = clf.predict(["photo.jpg"])
# probs.shape → (1, 1000)python inference.py --model_path ./google--efficientnet-b0 --device npu预期输出:Top-5 分类索引及置信度,无运行时错误(使用随机合成图像作为输入)。
测试条件:6 张合成 224×224 图像,batch_size=8,NPU 预热 1 轮。
| 指标 | 数值 |
|---|---|
| NPU 吞吐量 | 115.2 img/s |
| CPU/NPU 加速比 | 49.8 × |
EfficientNet-B0 仅 5.3M 参数,NPU 加速比高达 49.8×,得益于 CNN 卷积在 NPU 上的原生优化。适合实时图像分类场景。
分别在 CPU 和 NPU 上对 6 张合成图像推理(timm.create_model 创建的相同模型),从以下维度评估:
(1 - 余弦相似度) × 100%(要求 < 1.0%)| 指标 | 数值 | 说明 |
|---|---|---|
| 平均余弦相似度 | 0.999997 | 接近完美 |
| MAE | 0.000002 | 逐元素误差极小 |
| 精度误差率 | 0.0003% | 远低于 1% 要求 |
| Top-1 准确率 | 100.0% | 6/6 完全一致 |
| 图像 | CPU Top-1 | NPU Top-1 | 匹配 | 余弦 |
|---|---|---|---|---|
| 0 | 984 | 984 | ✅ | 0.999997 |
| 1 | 984 | 984 | ✅ | 0.999998 |
| 2 | 984 | 984 | ✅ | 0.999996 |
| 3 | 984 | 984 | ✅ | 0.999997 |
| 4 | 984 | 984 | ✅ | 0.999997 |
| 5 | 984 | 984 | ✅ | 0.999998 |
结论:精度误差率 0.0003%,远低于 1% 阈值。NPU 与 CPU 输出在分类概率和 Top-1 标签上完全一致,评测通过。
import timm, json
from safetensors.torch import load_file
with open("config.json") as f: cfg = json.load(f)
model = timm.create_model(cfg["architecture"], pretrained=False, num_classes=1000)
model.load_state_dict(load_file("model.safetensors"), strict=False)
model.to("npu:0").eval()import timm, torch, torch_npu
from safetensors.torch import load_file
from PIL import Image
model = timm.create_model('efficientnet_b0', pretrained=False, num_classes=1000)
model.load_state_dict(load_file('model.safetensors'), strict=False)
model.to('npu:0').eval()
data_cfg = timm.data.resolve_data_config(model=model)
transform = timm.data.create_transform(**data_cfg, is_training=False)
img = transform(Image.open('photo.jpg').convert('RGB')).unsqueeze(0)
with torch.no_grad():
probs = torch.softmax(model(img.to('npu:0')), dim=-1)timm.create_model() 加载,非 HuggingFace AutoModel