#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)num_labels=1 → fake_prob = sigmoid(logit))model.safetensors 84M)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_9362 ×2, CANN 8.5.1, cann-8.5.1)HF_ENDPOINT=https://hf-mirror.com (环境变量已配置,无需手动设置代理)pip install -r requirements.txt默认在 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说明:
npu:0。torch.npu.synchronize();首轮含图编译,稳定阶段预热 3 次、正式测试 10 次。shortest_edge=440 + center-crop 逻辑,避免非正方形图像被拉伸。(1, 3, 384, 384) float32, device npu:0, 合成图 seed=42 (mean=0.1812 std=0.8808)(1, 1) logit, device npu:00.590272 → fake_prob=0.643428 → verdict: fake (阈值 0.5)0.595608 → fake_prob=0.644651 (同权重、同输入、同预处理)Ascend910_9362, torch_npu 2.9.0.post1, 参数 device npu:0完整输出见 assets/model_result.png(python inference.py 真实终端日志)。
相同权重 (ac6ee457), 相同合成输入 (seed=42), 相同预处理、dtype float32、eval 模式:
(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 允许放宽后通过。详见 scripts/compare_outputs.py 报告。
| 指标 | 值 |
|---|---|
| 首轮(compile+infer) | 219.5 ms |
| avg / min / max | 5.88 / 5.82 / 5.94 ms |
| p50 / p90 / p95 | 5.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 deviceassets/model_result.png — 默认 python inference.py 的真实完整输出(含 input/output 摘要、同步耗时、fake_prob、verdict、性能与状态)npu:0 推理验证,非训练适配;fit() 仅在官方训练中使用。OwensLab/CommunityForensics-Eval (206GB)。onnx/model_int8.onnx 等) 未在此仓直接验证,采用 PyTorch FP32 直推路径为基准。transformers>=5.4.0 以正确支持 shortest_edge 预处理;更低版本会崩溃而非静默拉伸。