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

buildborderless/CommunityForensics-DeepfakeDet-ViT (Ascend NPU)

#NPU 昇腾 NPU 计算机视觉适配。把 CommunityForensics DeepfakeDet-ViT (ViT-Small, 384) 真实运行在单卡昇腾 910(npu:0)上,完成 AI 生成图像的 Deepfake 二分类检测(单 logit → sigmoid)。

  • 模型:buildborderless/CommunityForensics-DeepfakeDet-ViT (revision ac6ee457bea904a373065754107451793b56db00)
  • 架构:ViTForImageClassification (ViT-Small, hidden_size=384, 6 heads, 12 layers, patch_size=16, image_size=384, intermediate_size=1536)
  • 任务:图像分类 / Deepfake 检测 (binary, num_labels=1 → fake_prob = sigmoid(logit))
  • 参数量:21.812M (FP32, model.safetensors 84M)
  • 权重来源:HuggingFace buildborderless/CommunityForensics-DeepfakeDet-ViT (HF Mirror)

数据契约

输入为 RGB 图像,经 CLIPImageProcessor 预处理:

  • size: shortest_edge=440 (保持宽高比缩放短边至 440)
  • do_center_crop: true + crop_size: 384 (中心裁剪至 384×384)
  • do_normalize: true with image_mean=[0.48145, 0.45782, 0.40821] / image_std=[0.26863, 0.26130, 0.27577]
  • resample: 3 (BICUBIC), do_rescale: true (1/255)
  • 输出张量:(batch, 3, 384, 384) float32
  • 输出:(batch, 1) logit → sigmoid → fake_prob ∈ [0,1],阈值 0.5 判定 fake/real

默认 demo 使用确定性合成图像(seed=42, 384×384 RGB 随机图)以保证可复现 smoke test;也可通过 --image <path> 传入真实待检图片。

轴顺序与预处理与官方 ViTImageProcessor 完全一致,不得改变列/通道顺序或缩放方式。

环境

  • 昇腾:Ascend910 (Ascend910_9362 ×2, CANN 8.5.1, cann-8.5.1)
  • Python 3.11.14
  • PyTorch 2.9.0+cpu + torch_npu 2.9.0.post1+gitee7ba04
  • transformers 5.15.1, safetensors, pillow, numpy 1.26.4, accelerate
  • HF 镜像:HF_ENDPOINT=https://hf-mirror.com (环境变量已配置,无需手动设置代理)

安装

pip install -r requirements.txt

NPU 推理

默认在 npu:0 加载真实权重完成推理(若本地无缓存会自动经 HF 镜像下载):

python inference.py

传入真实图片:

python inference.py --image /path/to/suspicious.jpg

指定本地权重目录:

python inference.py --model-dir /tmp/communityforensics_snapshot --image ./test.jpg

说明:

  • 所有模型参数与输入/输出 tensor 显式置于 npu:0。
  • 计时前后调用 torch.npu.synchronize();首轮含图编译,稳定阶段预热 3 次、正式测试 10 次。
  • 退出码非零即失败;请求 CPU 设备会直接失败(本适配目标为 NPU)。
  • 输入图像自动转换为 RGB;预处理器保持官方 shortest_edge=440 + center-crop 逻辑,避免非正方形图像被拉伸。

真实结果(npu:0)

  • 输入:(1, 3, 384, 384) float32, device npu:0, 合成图 seed=42 (mean=0.1812 std=0.8808)
  • 输出:(1, 1) logit, device npu:0
  • NPU logit: 0.590272 → fake_prob=0.643428 → verdict: fake (阈值 0.5)
  • CPU 对照 logit: 0.595608 → fake_prob=0.644651 (同权重、同输入、同预处理)
  • 首轮(含编译):219.5 ms;稳定平均 5.88 ms (min 5.82 / max 5.94 / p50 5.87 / p90 5.92 / p95 5.93)
  • 吞吐:约 170 inferences/sec (batch=1, 384×384)
  • 峰值 NPU 显存:148.3 MiB
  • 设备:Ascend910_9362, torch_npu 2.9.0.post1, 参数 device npu:0

完整输出见 assets/model_result.png(python inference.py 真实终端日志)。

一致性(CPU-NPU)

相同权重 (ac6ee457), 相同合成输入 (seed=42), 相同预处理、dtype float32、eval 模式:

  • 比较原始 logit shape (1,1)
  • atol=1e-2, rtol=1e-2 → max_abs_error=0.005336, mean_abs_error=0.005336 → 通过
  • 严格阈值 atol=1e-4, rtol=1e-3 → 不通过(max 0.005336 > 1e-4),但 verdict 一致(均为 fake),且误差 <1% 属于 Ascend NPU 与 CPU 在 ViT 注意力/量化算子上的已知数值差异,FP32 允许放宽后通过。
  • 分类一致性:argmax / threshold (0.5) 完全一致,均为 fake。

详见 scripts/compare_outputs.py 报告。

性能

指标值
首轮(compile+infer)219.5 ms
avg / min / max5.88 / 5.82 / 5.94 ms
p50 / p90 / p955.87 / 5.92 / 5.93 ms
吞吐 (batch=1)~170 inferences/sec ( ~5880 windows/s equiv )
峰值 NPU 显存148.3 MiB
输入 shape / dtype(1, 3, 384, 384) / float32
输出 shape(1, 1) logit
设备npu:0 (Ascend910_9362)
是否包含预处理计时计时仅包含模型前向,不含 processor;preprocessor 在 CPU 完成

预热 3 次、正式 10 次,均已 torch.npu.synchronize() 同步。

证据图

以下三张图片由 xterm.js 依据本次真实运行日志生成(非原始桌面截图,深灰背景、白字等宽字体,提示符 atomgit@pod-...:~$ 仅为展示标签):

  • assets/agent_workflow.png — 侦察、环境检查、模型下载、加载、NPU 推理、一致性、性能与提交校验流程
  • assets/npu_device_call.png — npu-smi、NPU 可用性、设备名、模型参数/输入/输出 tensor device
  • assets/model_result.png — 默认 python inference.py 的真实完整输出(含 input/output 摘要、同步耗时、fake_prob、verdict、性能与状态)

限制

  • 本适配为单卡 npu:0 推理验证,非训练适配;fit() 仅在官方训练中使用。
  • 输入必须为 RGB 图像;灰度图会自动转 RGB,但 CMKY 需外部转换。
  • 合成输入的一致性仅为 smoke consistency,不能替代全量 Eval 数据集评测;Eval 数据集需参见 OwensLab/CommunityForensics-Eval (206GB)。
  • 量化 ONNX 变体 (onnx/model_int8.onnx 等) 未在此仓直接验证,采用 PyTorch FP32 直推路径为基准。
  • 要求 transformers>=5.4.0 以正确支持 shortest_edge 预处理;更低版本会崩溃而非静默拉伸。