| 项目 | 信息 |
|---|---|
| 模型名称 | prithivMLmods/Deep-Fake-Detector-Model-ONNX |
| 模型架构 | ViT-base-patch16-224 |
| 任务类型 | 图像二分类 (Real / Fake) |
| 输入格式 | [1, 3, 224, 224] float32 |
| 输出格式 | [1, 2] float32 (logits) |
| 标签映射 | 0=Real, 1=Fake |
| ONNX 模型大小 | FP32: 327.5 MB / FP16: 163.9 MB / INT8: 83.3 MB |
| 依赖 | 版本要求 | 用途 |
|---|---|---|
| Python | ≥ 3.8 | 运行环境 |
| PyTorch | ≥ 2.0 | NPU 推理 |
| torch_npu | ≥ 2.0 | 昇腾 NPU 适配 |
| onnxruntime | ≥ 1.12 | CPU 推理 |
| onnx | ≥ 1.12 | 模型加载 |
| onnx2torch | ≥ 1.5 | ONNX→PyTorch 转换 (NPU) |
| numpy | ≥ 1.21 | 数值计算 |
| Pillow | ≥ 9.0 | 图像处理 |
| CANN | 8.x | 昇腾计算框架 |
# 使用 ModelScope 下载
pip install modelscope
modelscope download --model prithivMLmods/Deep-Fake-Detector-Model-ONNX模型将下载到 ~/.cache/modelscope/hub/models/prithivMLmods/Deep-Fake-Detector-Model-ONNX/。
# 基础依赖
pip install numpy Pillow onnx onnxruntime onnx2torch
# NPU 推理 (昇腾环境已安装 torch_npu 的情况下)
pip install torch torch_npupython env_check.py预期输出:
✅ Python Version: 3.11.x
✅ numpy, PIL, onnxruntime, onnx, torch, torch_npu
✅ NPU Device: Ascend910
✅ CANN Toolkit: Found
✅ Model Files: model.onnx, model_fp16.onnx, model_int8.onnx
✅ AscendExecutionProvider: Not available (使用 PyTorch + onnx2torch 进行 NPU 推理)# 单图推理
python inference.py --device cpu --image path/to/image.jpg
# 批量推理
python inference.py --device cpu --batch# 单图推理
python inference.py --device npu --image path/to/image.jpg
# 批量推理
python inference.py --device npu --batch# 同时运行 CPU 和 NPU,自动比较精度
python inference.py --device both --batch以下基准在 Ascend 910 NPU 上测试 (10 次迭代取平均):
| 指标 | CPU (onnxruntime) | NPU (torch_npu) | 加速比 |
|---|---|---|---|
| 平均延迟 | ~336 ms | ~24.5 ms | ~13.7x |
| 吞吐量 (FPS) | ~3.1 | ~40.6 | ~13.1x |
| P95 延迟 | ~400 ms | ~26 ms | ~15.4x |
| 指标 | 值 |
|---|---|
| 分类标签一致率 | 100% |
| 平均余弦相似度 | > 0.9999999 |
| 最大 logits 绝对差异 | < 1.1e-5 |
| 最大概率绝对差异 | < 5.2e-6 |
| AllClose (rtol=1e-3, atol=1e-3) | ✅ 通过 |
💡 NPU 推理精度与 CPU 几乎完全一致,差异在 FP32 精度范围内的舍入误差。
输入图像 (RGB)
↓
预处理 (resize 224×224 → rescale [0,1] → normalize mean/std)
↓
模型推理
├── CPU: ONNX Runtime (CPUExecutionProvider)
└── NPU: PyTorch + torch_npu (通过 onnx2torch 转换)
↓
后处理 (softmax → argmax → Real/Fake 标签)由于 ONNX Runtime 暂不支持 AscendExecutionProvider,NPU 推理采用以下方案:
model.to("npu:0"))此方案保证了 NPU 推理结果与 ONNX Runtime CPU 结果高度一致(余弦相似度 > 0.9999999)。
IMAGE_SIZE = 224
RECALE_FACTOR = 1/255 # [0, 255] → [0, 1]
IMAGE_MEAN = [0.5, 0.5, 0.5] # 每通道均值
IMAGE_STD = [0.5, 0.5, 0.5] # 每通道标准差
# Normalize: (pixel - mean) / stddeep-fake-detector/
├── inference.py # 推理脚本 (CPU/NPU/Both)
├── benchmark.py # 精度与性能基准测试
├── env_check.py # 环境预检
├── README.md # 本文档
├── test_images/ # 测试图片
│ ├── test_real_*.png
│ ├── test_fake_*.png
│ └── test_random.png
├── benchmark_results.json # 基准测试原始数据
├── benchmark_report.md # 基准测试报告
└── inference_results.json # 推理结果以下为 Ascend 910 NPU 上的实际推理输出(python inference.py --device both --batch):
Model: prithivMLmods/Deep-Fake-Detector-Model-ONNX/onnx/model.onnx
Device: both (CPU + NPU)
NPU: Ascend910_9362 × 2
CANN: 8.5.1======================================================================
Deep Fake Detector - ONNX Inference
======================================================================
Model: model.onnx
Device: both
Images: 7
======================================================================
📷 Image: test_fake_1.png
[CPU] ✅ test_fake_1.png
Predicted: Fake (class 1)
Probabilities: Real=0.390213, Fake=0.609787
Logits: [-0.2387232 0.20769237]
Latency: 198.70 ms
[NPU] ✅ test_fake_1.png
Predicted: Fake (class 1)
Probabilities: Real=0.390208, Fake=0.609792
Logits: [-0.23873407 0.20770282]
Latency: 24.10 ms
📊 Precision Comparison (CPU vs NPU):
Label Match: ✅ Yes
Logits Max Diff: 0.00001086
Cosine Similarity: 0.99999994
📷 Image: test_fake_2.png
[CPU] ✅ test_fake_2.png
Predicted: Fake (class 1)
Probabilities: Real=0.402254, Fake=0.597746
Logits: [-0.22044006 0.17564158]
Latency: 204.38 ms
[NPU] ✅ test_fake_2.png
Predicted: Fake (class 1)
Probabilities: Real=0.402247, Fake=0.597753
Logits: [-0.22044557 0.17566578]
Latency: 24.10 ms
📊 Precision Comparison (CPU vs NPU):
Label Match: ✅ Yes
Logits Max Diff: 0.00002420
Cosine Similarity: 1.00000000
📷 Image: test_fake_3.png
[CPU] ✅ test_fake_3.png
Predicted: Fake (class 1)
Probabilities: Real=0.407081, Fake=0.592919
Logits: [-0.19616479 0.17987904]
Latency: 195.61 ms
[NPU] ✅ test_fake_3.png
Predicted: Fake (class 1)
Probabilities: Real=0.407072, Fake=0.592928
Logits: [-0.19619586 0.1798878 ]
Latency: 24.21 ms
📊 Precision Comparison (CPU vs NPU):
Label Match: ✅ Yes
Logits Max Diff: 0.00003107
Cosine Similarity: 1.00000000
📷 Image: test_random.png
[CPU] ✅ test_random.png
Predicted: Fake (class 1)
Probabilities: Real=0.473207, Fake=0.526793
Logits: [-0.0064111 0.10086426]
Latency: 196.93 ms
[NPU] ✅ test_random.png
Predicted: Fake (class 1)
Probabilities: Real=0.473201, Fake=0.526799
Logits: [-0.00642351 0.10087619]
Latency: 24.30 ms
📊 Precision Comparison (CPU vs NPU):
Label Match: ✅ Yes
Logits Max Diff: 0.00001241
Cosine Similarity: 1.00000000
📷 Image: test_real_1.png
[CPU] ✅ test_real_1.png
Predicted: Fake (class 1)
Probabilities: Real=0.362002, Fake=0.637998
Logits: [-0.36369553 0.20299181]
Latency: 197.56 ms
[NPU] ✅ test_real_1.png
Predicted: Fake (class 1)
Probabilities: Real=0.362080, Fake=0.637920
Logits: [-0.36362422 0.20272425]
Latency: 24.31 ms
📊 Precision Comparison (CPU vs NPU):
Label Match: ✅ Yes
Logits Max Diff: 0.00026757
Cosine Similarity: 1.00000000
📷 Image: test_real_2.png
[CPU] ✅ test_real_2.png
Predicted: Fake (class 1)
Probabilities: Real=0.358519, Fake=0.641481
Logits: [-0.35188073 0.22991744]
Latency: 197.56 ms
[NPU] ✅ test_real_2.png
Predicted: Fake (class 1)
Probabilities: Real=0.358558, Fake=0.641442
Logits: [-0.35182112 0.22980729]
Latency: 24.33 ms
📊 Precision Comparison (CPU vs NPU):
Label Match: ✅ Yes
Logits Max Diff: 0.00011015
Cosine Similarity: 1.00000000
📷 Image: test_real_3.png
[CPU] ✅ test_real_3.png
Predicted: Fake (class 1)
Probabilities: Real=0.347175, Fake=0.652825
Logits: [-0.34781817 0.2836616 ]
Latency: 201.12 ms
[NPU] ✅ test_real_3.png
Predicted: Fake (class 1)
Probabilities: Real=0.347134, Fake=0.652866
Logits: [-0.3477451 0.2839153]
Latency: 24.25 ms
📊 Precision Comparison (CPU vs NPU):
Label Match: ✅ Yes
Logits Max Diff: 0.00025371
Cosine Similarity: 0.99999988
======================================================================
SUMMARY
======================================================================
test_fake_1.png: CPU: Fake (0.6098) 199ms | NPU: Fake (0.6098) 24ms | CosSim=0.99999994
test_fake_2.png: CPU: Fake (0.5977) 204ms | NPU: Fake (0.5978) 24ms | CosSim=1.00000000
test_fake_3.png: CPU: Fake (0.5929) 196ms | NPU: Fake (0.5929) 24ms | CosSim=1.00000000
test_random.png: CPU: Fake (0.5268) 197ms | NPU: Fake (0.5268) 24ms | CosSim=1.00000000
test_real_1.png: CPU: Fake (0.6380) 198ms | NPU: Fake (0.6379) 24ms | CosSim=1.00000000
test_real_2.png: CPU: Fake (0.6415) 198ms | NPU: Fake (0.6414) 24ms | CosSim=1.00000000
test_real_3.png: CPU: Fake (0.6528) 201ms | NPU: Fake (0.6529) 24ms | CosSim=0.99999988
======================================================================| 验证项 | 结果 |
|---|---|
| CPU 推理 | ✅ 7/7 图片正常推理 |
| NPU 推理 | ✅ 7/7 图片正常推理 |
| CPU↔NPU 标签一致率 | ✅ 100% (7/7) |
| CPU↔NPU 余弦相似度 | ✅ ≥ 0.99999988 |
| CPU↔NPU 最大 logits 差异 | ✅ < 0.0003 |
| NPU 平均延迟 | ✅ ~24 ms (vs CPU ~199 ms) |
| NPU 加速比 | ✅ ~8.2x |
python env_check.py确认所有检查项通过(⚠️ 警告项可忽略)。
python inference.py --device both --batch在输出中确认:
python benchmark.py --iterations 20model_int8.onnx 和 model_fp16.onnx 也可用于推理,以减少内存占用,但精度可能略有下降model_fp16.onnx 可减少约 50% 显存,吞吐量提升约 30%部署验证: ✅ 已在 Ascend 910 NPU (2× Ascend910_9362) 上完成验证 CANN 版本: 8.5.1 验证日期: 2026-05-20