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
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默认命令(使用真实权重在 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 的处理器。
以下为本次在 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 覆盖):
所有样例端到端 verdict 一致,说明 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执行 python inference.py --mode benchmark --device npu:0(固定 384×384 输入,3 次预热,10 次正式,torch.npu.synchronize 计时):
本次实测(npu:0, batch=1, float32):
注意首帧因 NPU 编译开销显著高于稳定态,报告中区分首帧与稳定态。
三张证据图均由 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 float32assets/model_result.png:python inference.py 默认输出,含真实 logit/prob/verdict 与 PASSsize dict must contain height and width,已通过手册实现规避。Hardware: NPU, NPU, Ascend, Ascend910, ViT, deepfake-detection, image-classification, CommunityForensics