wrhhh/buildborderless-CommunityForensics-DeepfakeDet-ViT-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

buildborderless/CommunityForensics-DeepfakeDet-ViT on Ascend NPU

1. 模型简介

CommunityForensics-DeepfakeDet-ViT 是基于 ViT-Small 的深度伪造图像检测模型,训练于 2.7M 样本、4,803 个生成器,发表于 CVPR 2025《Community Forensics: Using Thousands of Generators to Train Fake Image Detectors》。架构为 hidden_size=384, 6 attention heads, 12 layers, patch_size=16, intermediate_size=1536, 输入 384×384,输出单 logit 经 sigmoid 得到 fake 概率(>0.5 判 fake)。权重格式 safetensors (83.23 MB, 21.81M 参数),预处理为 shortest_edge 440 + center_crop 384 + CLIP 归一化。HuggingFace 地址:https://huggingface.co/buildborderless/CommunityForensics-DeepfakeDet-ViT

2. 验证环境

  • NPU: Ascend910_9362 (2 devices, tested on npu:0)
  • npu-smi: 25.5.5, Health OK, Power ~176W, Temp 43C, HBM 3109/65536 MB (free 62366/62740 MB)
  • torch: 2.9.0+cpu, torch_npu: 2.9.0.post1+gitee7ba04, CANN 8.5.1
  • transformers: >=4.57.0 (manual preprocessing compatible with 5.4 shortest_edge logic)
  • OS: openEuler aarch64, Python 3.11
  • Model dir: /opt/atomgit/.cache/huggingface/hub/models--buildborderless--CommunityForensics-DeepfakeDet-ViT/snapshots/ac6ee457bea904a373065754107451793b56db00

3. 安装依赖

pip install -r requirements.txt
# requirements.txt:
# torch==2.9.0
# torch-npu==2.9.0.post1
# transformers>=4.57.0
# huggingface_hub
# safetensors
# Pillow
# numpy

4. NPU 推理

默认命令(使用真实权重在 npu:0 上推理):

python inference.py
# 或显式指定
python inference.py --device npu:0
# CPU 对比验证
python inference.py --mode validate
# 性能测试
python inference.py --mode benchmark --device npu:0

推理脚本会自动通过 snapshot_download 或本地缓存定位模型权重,无需手动下载;输入为合成随机图像或 --image 指定的真实图像,预处理为手动实现 shortest_edge 440 → center_crop 384 → CLIP 归一化,不依赖 transformers 5.4 的处理器。

5. 真实推理结果

以下为本次在 npu:0 上执行 python inference.py 的真实输出(固定种子 800x600 随机图像):

[INFO] MODEL_ID=buildborderless/CommunityForensics-DeepfakeDet-ViT
[INFO] MODEL_DIR=.../ac6ee457bea904a373065754107451793b56db00 exists=True
[INFO] config architect=['ViTForImageClassification'] model_type=vit hidden_size=384 heads=6 layers=12 num_labels=1 image_size=384
[INFO] device=npu:0 npu_available=True device_count=2
[INFO] device_name=Ascend910_9362 HBM free 62366 MB total 62740 MB
[LOAD] model param device: npu:0 dtype: torch.float32 num_params: 21.81M
[INPUT] synthetic random image 800x600 seed0 (fallback, size (800, 600))
[PRE] original (800, 600) -> resized (586, 439) -> tensor torch.Size([1, 3, 384, 384])
[INFER] logits tensor([[1.2189]], device='npu:0') shape torch.Size([1, 1]) logit 1.218852 -> prob 0.771861 verdict fake elapsed 121.87ms
[OUTPUT] logit=1.218852 fake_prob=0.771861 real_prob=0.228139 verdict=fake threshold=0.5
[OUTPUT] device=npu:0 dtype=float32 elapsed=121.87ms
[RESULT] PASS NPU inference succeeded on npu:0 device_name=Ascend910_9362
[CHECK] cpu vs npu prob diff 1.800954e-03 verdict match True
[CHECK] consistency PASS

其他固定样例(--mode validate 覆盖):

  • random_800x600_seed0: cpu_prob 0.773662 / npu_prob 0.771861 diff 1.80e-03 verdict fake/fake
  • random_720x1280_seed42: cpu_logit 0.042432 / npu_logit 0.025390 prob 0.510606/0.506347 diff 4.25e-03 fake/fake(极限宽高比测试最敏感)
  • gray_384x384: cpu_prob 0.003221 / npu_prob 0.003262 diff 4.13e-05 real/real
  • random_500x500_seed123: cpu_prob 0.874570 / npu_prob 0.873565 diff 1.00e-03 fake/fake

所有样例端到端 verdict 一致,说明 NPU 推理语义正确。

6. CPU-NPU 一致性验证

执行 python inference.py --mode validate 对比 4 张固定图像:

| 图像 | CPU logit / prob | NPU logit / prob | |Δlogit| | |Δprob| | verdict一致 | |---|---|---|---|---| | random_800x600_seed0 | 1.229108 / 0.773662 | 1.218852 / 0.771861 | 1.02e-02 | 1.80e-03 | ✅ fake/fake | | random_720x1280_seed42 | 0.042432 / 0.510606 | 0.025390 / 0.506347 | 1.70e-02 | 4.25e-03 | ✅ fake/fake | | gray_384x384 | -5.734862 / 0.003221 | -5.722054 / 0.003262 | 1.28e-02 | 4.13e-05 | ✅ real/real | | random_500x500_seed123 | 1.941986 / 0.874570 | 1.932856 / 0.873565 | 9.13e-03 | 1.00e-03 | ✅ fake/fake | | 汇总 | | | max 1.70e-02 mean 1.23e-02 | max 4.25e-03 mean 1.77e-03 | 4/4 |

判定阈值:prob 差 <0.01 且 logit 差 <0.05 且 verdict 一致 → PASS。本次 4/4 通过,余量充足。NPU 与 CPU 均为 float32,误差来自 Ascend 硬件浮点实现差异,属正常范围。

验证命令:

python inference.py --mode validate
# 输出 [COMPARE] PASS CPU-NPU consistency 4/4 within tolerance

7. 性能测试

执行 python inference.py --mode benchmark --device npu:0(固定 384×384 输入,3 次预热,10 次正式,torch.npu.synchronize 计时):

本次实测(npu:0, batch=1, float32):

  • 单次推理(首帧含编译):121.87 ms(inference.py 默认首帧)
  • 稳定态 avg 6.61 ms / min 6.58 ms / max 6.68 ms / p50 6.61 ms / p90 6.64 ms / p95 6.66 ms(validate 中 benchmark)
  • 独立 benchmark 运行 avg 6.15 ms / min 6.09 ms / max 6.20 ms / p50 6.14 ms / p90 6.19 ms / p95 6.20 ms throughput 162.62 imgs/s
  • 典型稳定范围:6.1–6.7 ms,吞吐 ~151–162 imgs/s
  • HBM 占用实测 114 MB(npu-smi Process memory)+ 权重 83 MB,总峰值 < 300 MB,远低于 65536 MB 上限
  • CPU 对比:同图像 CPU 耗时 ~653 ms,NPU 加速比 ~100×

注意首帧因 NPU 编译开销显著高于稳定态,报告中区分首帧与稳定态。

8. 自验证截图

三张证据图均由 scripts/render_xterm_evidence.mjs 从本次真实日志渲染(raw 模式):

  • assets/agent_workflow.png:完整工作流(环境检查→获取模型→分析模型→NPU适配→NPU验证含 validate 与 benchmark)
  • assets/npu_device_call.png:npu-smi info、torch.npu.is_available()==True、device_name Ascend910_9362、模型参数 device npu:0 dtype float32
  • assets/model_result.png:python inference.py 默认输出,含真实 logit/prob/verdict 与 PASS

9. 已知限制

  • 必须使用手动预处理(shortest_edge 440 + center_crop 384 + CLIP 归一化),直接使用 ViTImageProcessor 在 transformers 4.x 会因不支持 shortest_edge 而报错;transformers 5.4+ 可直接用处理器但仍建议手动以保证跨版本一致。
  • transformers 5.4 以下若强制使用处理器会得到错误提示 size dict must contain height and width,已通过手册实现规避。
  • 首帧编译延迟 ~120ms 属于 NPU 正常编译开销,后续稳定 6ms。
  • CPU-NPU prob 差异可达 0.004 左右(logit 0.017),但 verdict 完全一致;不建议将阈值卡在 0.5±0.005 的极边界场景直接以 prob 数值做严格回归,仅以 verdict 为准。
  • ONNX 变体(onnx/)未在此仓库验证,仅 PyTorch safetensors 路径。
  • FP32 精度;未测试 FP16/BF16 量化。

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, ViT, deepfake-detection, image-classification, CommunityForensics