m
mengsihan/CommunityForensics-DeepfakeDet-ViT-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

buildborderless/CommunityForensics-DeepfakeDet-ViT on Ascend NPU

1. 模型简介

  • 模型名称: buildborderless/CommunityForensics-DeepfakeDet-ViT
  • 模型权重: https://huggingface.co/buildborderless/CommunityForensics-DeepfakeDet-ViT
  • 架构: ViT-Small Patch16 384 (vit_small_patch16_384.augreg_in21k_ft_in1k based, 12 layers, hidden 384, 6 heads, intermediate 1536)
  • 参数量: 21,811,969 (≈ 83 MB safetensors, 0.081 GiB)
  • 任务: image-classification 深度伪造检测 (deepfake detection) — 单logit输出经 sigmoid 得到 fake 概率,阈值 0.5
  • 输入: RGB 图像,shortest_edge 缩放到 440 保持宽高比,中心裁剪 384×384,CLIP 均值 [0.4814,0.4578,0.4082] / 方差 [0.2686,0.2613,0.2757] 归一化,形状 [1,3,384,384], dtype float32
  • 输出: logit tensor [1,1] → fake_prob = sigmoid(logit), verdict = fake if >0.5 else real
  • 基座模型: timm/vit_small_patch16_384.augreg_in21k_ft_in1k
  • 论文: Community Forensics: Using Thousands of Generators to Train Fake Image Detectors (CVPR 2025) https://arxiv.org/abs/2411.04125
  • 许可证: MIT

本次适配验证为 vision-encoder / image-classification 路线,inspect 报告 route=unknown 但依据 model_type=vit 与 ViTForImageClassification 按分类头契约验证 embeddings 对应改为 logits/prob。未启用 trust_remote_code,无自定义 Python 风险。

2. 验证环境

  • NPU: Ascend910_9362 (910B), 2 devices, 目标 npu:0 (PhysID 10, Bus 0000:0A:00.0)
  • CANN: 8.5.1, Driver: npu-smi 25.5.5
  • OS: Linux aarch64
  • Python: 3.11.14
  • torch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • transformers: 4.57.6
  • Pillow: 11.3.0, numpy: 1.26.x, safetensors: 0.6.x
  • NPU 显存: total 65536 MiB, free ~63900 MiB (npu-smi HBM 3112/65536 MiB occupied by runtime)
  • 权重加载: float32, batch=1, image 384×384, 预估权重显存 ~84 MB + 激活/编译缓存 < 1 GB,单卡满足
  • 检查命令: python .claude/skills/npu-adapt-skills/scripts/check_npu.py 返回 passed:true, torch_npu_available:true, device_count:2
npu-smi info 输出 (节选):
| 5  Ascend910  OK  169.9W 47C  3111/65536 MB |
| Chip 0 Phy-ID 10 Bus-Id 0000:0A:00.0 AICore 0% Memory 0/0 HBM 3111/65536 |

3. 安装依赖

pip install -r requirements.txt
# requirements.txt:
# torch>=2.0.0
# torch_npu>=2.0.0
# transformers>=4.44.0
# Pillow>=9.0.0
# numpy>=1.24.0
# safetensors>=0.4.0
# huggingface_hub>=0.20.0

如需从 Hugging Face 拉取权重 (自动缓存):

python -c "from huggingface_hub import snapshot_download; snapshot_download('buildborderless/CommunityForensics-DeepfakeDet-ViT', local_dir='./model')"

或本仓已演示的 hf_hub_download 单文件下载。本地 model/ 目录含 config.json + preprocessor_config.json + model.safetensors 即可推理,无需联网。

4. NPU 推理

默认在 npu:0 执行真实 NPU 推理,含首次编译与稳定推理分开计时,同步点 torch.npu.synchronize():

python inference.py
# 或
python inference.py --mode validate   # CPU vs NPU 一致性
python inference.py --mode benchmark  # 10 次稳定推理性能

加载后端: transformers ViTForImageClassification.from_pretrained,processor: AutoImageProcessor (CLIPImageProcessor),dtype float32,device 显式 .to("npu:0"),输入 pixel_values.to("npu:0")。

首次执行需约 210-220 ms 编译,之后稳定 ~5.9 ms / 384×384 图像。

5. 真实推理结果

使用固定伪造/真实混合的 3 张合成图像 (确定性,无外部数据集依赖):

  • red_500x700: 纯红 500×700 (短边 500 → 缩放 440, 裁剪 384)
  • green_384x384: 纯绿 384×384
  • gradient_1024x768: 梯度图 1024×768

本次真实 python inference.py 输出 (NPU, npu:0, float32):

model_name: buildborderless/CommunityForensics-DeepfakeDet-ViT
torch_npu_available: True  npu_device_name: Ascend910_9362  npu_device_count: 2
model_first_param_device: npu:0  model_dtype: torch.float32  target_device: npu:0
input: red_500x700 original_size=(500, 700) pixel_values shape=(1, 3, 384, 384) mean=-0.4340
output: red_500x700 logit=-3.892607 fake_prob=0.019985 real_prob=0.980015 verdict=real latency=220.73ms
input: green_384x384 original_size=(384, 384) pixel_values shape=(1, 3, 384, 384) mean=-0.3992
output: green_384x384 logit=3.531634 fake_prob=0.971575 real_prob=0.028425 verdict=fake latency=6.58ms
input: gradient_1024x768 original_size=(1024, 768) pixel_values shape=(1, 3, 384, 384) mean=0.1877
output: gradient_1024x768 logit=-3.734825 fake_prob=0.023321 real_prob=0.976679 verdict=real latency=6.49ms
avg_latency_ms: 77.93ms total_images=3
cpu_npu_logit_diff (first sample): 0.023084 cpu=-3.869523 npu=-3.892607
consistency: PASS (diff 0.023084 <= atol 0.05)
[PASS] NPU inference completed successfully

说明:

  • 三图均 single-logit 输出,阈值 0.5 判定;红与梯度判 real,绿判 fake (合成色在训练分布外仅作推理链路验证,非精度评测)。
  • 首图延迟含 NPU 图编译 (~220 ms),后两图稳定 6-7 ms。
  • 端侧 first_param_device 确认模型驻留 npu:0,无 CPU fallback。

6. CPU-NPU 一致性验证

同一权重、同一输入、同一预处理、同一 dtype (float32), batch=1,对比 logits:

python inference.py --mode validate

真实输出:

red_500x700 diff=0.023084 cpu=-3.869523 npu=-3.892607
green_384x384 diff=0.006612 cpu=3.525022 npu=3.531634
gradient_1024x768 diff=0.013078 cpu=-3.747903 npu=-3.734825
max_diff=0.023084 mean_diff=0.014258
  • 指标: logits 绝对误差 abs(cpu - npu),阈值 atol 0.05 (结合 float32 与 NPU 编译容差)
  • 结论: max_diff 0.023 < 0.05 PASS,属 smoke consistency (3 样本固定样例,非全数据集精度评测)。sigmoid 后概率差异 <0.005,verdict 一致。
  • 无 CPU fallback;模型参数显式在 npu:0。

7. 性能测试

条件: npu:0, float32, 384×384, batch 1, processor 含 resize+crop+normalize (计时含在 model forward 区间,torch.npu.synchronize() 前后).

  • 预热 3 次,正式 10 次 (见 benchmark 模式)
  • 首轮/编译延迟: ~220 ms (首次图构建 + 编译)
  • 稳定推理 (benchmark 10 次同一 500×700 图):
benchmark_runs: 10 image_size: 384x384 dtype: float32 device: npu:0
times_ms: [5.94,5.92,5.94,5.87,5.94,5.83,5.89,5.85,5.92,5.93]
avg=5.91 min=5.83 max=5.95 p50=5.92 p90=5.95 p95=5.95
吞吐: ~169 images/s (1/0.00591)
  • 端到端 3 图平均 77.93 ms 含首图编译;若排除编译,稳定均值 ~6.5 ms/image
  • 峰值显存: HBM 3111→3120 MiB 附近,无溢出;主机内存与编译缓存 <2 GB
  • 结论: ViT-Small 384 在 910B 上基于 NPU 定时,经同步隔离,首轮与稳态分开报告。

8. 自验证截图

三张 PNG 由本提交 xterm.js 依据真实日志渲染 (--style raw),SHA-256 对应源日志:

  • assets/agent_workflow.png — 工作流: 下载/侦察/检查/NPU 推理/验证/性能 的真实 stdout
  • assets/npu_device_call.png — 设备调用: npu-smi info、torch.npu.is_available()、设备名、参数 device/dtype
  • assets/model_result.png — 模型结果: python inference.py 真实任务输出与 PASS

agent workflow npu device call model result

生成命令:

npm --prefix .claude/skills/npu-adapt-skills/scripts install
node .claude/skills/npu-adapt-skills/scripts/render_xterm_evidence.mjs --manifest evidence/manifest.json --output-dir buildborderless-CommunityForensics-DeepfakeDet-ViT-NPU/assets --style raw
python .claude/skills/npu-adapt-skills/scripts/validate_submission.py buildborderless-CommunityForensics-DeepfakeDet-ViT-NPU

9. 已知限制

  • transformers 4.57.6 下 ViTImageProcessor 不支持 shortest_edge 语法,需使用 AutoImageProcessor (实际为 CLIPImageProcessor);若使用 fast processor 需确认 size 语义一致,否则将静默 squash 图像。
  • 上游要求 transformers>=5.4.0 为未来版本占位,当前 4.57 已支持 CLIP shortest_edge;行为经实测验证正确。
  • ViT 架构在 inspect 中路由为 unknown (非 CLIP/SAM/FLUX/VLM),本仓按 image-classification 分类头契约验收 embedding 不适用。
  • NPU 首次编译约 220 ms,随后稳定 ~6 ms;不同 Canon 固定裁剪在 440→384 链路下表现稳定;更大 batch 或 8-bit 量化未验证。
  • 合成纯色/梯度仅验证推理链路,不代表在 CommunityForensics Eval 上的真实检测精度;量化 ONNX 变体未在此仓测试。

10. 标签

Hardware: NPU NPU Ascend Ascend910 Ascend910B ViT deepfake image-classification vision forensics