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

buildborderless/CommunityForensics-DeepfakeDet-ViT on Ascend NPU

1. 模型简介

模型名称: buildborderless/CommunityForensics-DeepfakeDet-ViT
模型类型: Vision Transformer (ViT-Small) - ViTForImageClassification
任务: image-classification / Deepfake Detection - 检测 AI 生成图像 (fake vs real)
论文: Community Forensics: Using Thousands of Generators to Train Fake Image Detectors (CVPR 2025) - University of Michigan
架构: ViT-Small patch16_384 (hidden_size=384, 12 layers, 6 heads, patch 16, intermediate 1536, image_size 384, num_labels=1 sigmoid)
Base: timm/vit_small_patch16_384.augreg_in21k_ft_in1k, 训练数据 2.7M / 4803 generators
权重: model.safetensors 87 MB, FP32, transformers >=5.4.0
输入: RGB PIL Image, 短边 resize 440 (保持比例) + center crop 384 + CLIP normalize (mean [0.4814,0.4578,0.4082] std [0.2686,0.2613,0.2758]) -> pixel_values [1,3,384,384]
输出: 单 logit [1,1] -> sigmoid -> fake_prob, threshold 0.5 (fake >0.5 else real)
来源: https://huggingface.co/buildborderless/CommunityForensics-DeepfakeDet-ViT, commit ac6ee457bea904a373065754107451793b56db00

本仓在单卡 Ascend910B (npu:0) 上完成真实推理、CPU-NPU 一致性验证和性能测试。

2. 验证环境

项目值
NPUAscend910_9362 (Ascend910B), device_count=2, visible npu:0 (phy 6)
CANN8.5.1
torch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04
transformers5.9.0
Python3.11
HF_ENDPOINThttps://hf-mirror.com
NPU 显存61.27 GiB total, 60.69 GiB free (benchmark 时)
OSUbuntu 22.04.5 LTS

torch.npu.is_available()=True, npu-smi OK, npu:0 可用。

3. 安装依赖

pip install torch torch-npu transformers Pillow numpy
# transformers >=5.4.0 必需,否则 preprocessor 会因 shortest_edge 报错

或

pip install -r requirements.txt

4. NPU 推理

默认命令(需 NPU 环境):

python inference.py
python inference.py --mode validate   # CPU-NPU 一致性
python inference.py --mode benchmark  # 性能

推理自动选择 npu:0(若可用),加载 ViTImageProcessor + ViTForImageClassification 到 npu:0,固定合成输入,多图批量验证。

5. 真实推理结果

执行 python inference.py (npu:0, float32, 4 张合成图):

Model: buildborderless/CommunityForensics-DeepfakeDet-ViT
Task: image-classification (deepfake detection)
Architecture: ViTForImageClassification (vit_small_patch16_384, hidden=384, layers=12, heads=6)
Device: npu:0
Device name: Ascend910_9362
Dtype: float32 (weights float32)
Model loaded in 23.397s
Processor: do_resize=True, shortest_edge=440, do_center_crop=True, crop_size=SizeDict(height=384, width=384), do_normalize=True
Input contract: RGB PIL Image -> processor (shortest_edge 440, center_crop 384) -> pixel_values [1,3,384,384] float32
Output contract: logits [1,1] -> sigmoid -> fake_prob, verdict threshold 0.5

=== NPU Inference Results ===
[synthetic_red_500x400] input=(500, 400) -> pixel_values [1, 3, 384, 384] -> logits [1, 1] logit=-3.8926 fake_prob=0.0200 real_prob=0.9800 verdict=real latency=221.50ms
[synthetic_blue_384x384] input=(384, 384) -> pixel_values [1, 3, 384, 384] -> logits [1, 1] logit=-1.0280 fake_prob=0.2635 real_prob=0.7365 verdict=real latency=6.76ms
[synthetic_gray_640x480] input=(640, 480) -> pixel_values [1, 3, 384, 384] -> logits [1, 1] logit=-5.7221 fake_prob=0.0033 real_prob=0.9967 verdict=real latency=6.72ms
[synthetic_gradient] input=(384, 384) -> pixel_values [1, 3, 384, 384] -> logits [1, 1] logit=-4.2534 fake_prob=0.0140 real_prob=0.9860 verdict=real latency=6.55ms

=== Summary ===
Device used: npu:0
All 4 images processed successfully
Status: PASS
  • 首帧 221.50ms 含 NPU 编译,后续稳定 ~6.5ms。
  • 四张图均为 real 判定,logits 负值,fake_prob < 0.27,符合单样本合成输入预期。
  • 输入输出契约验证通过:pixel_values 固定 384x384,logits [1,1]。

6. CPU-NPU 一致性验证

执行 python inference.py --mode validate:

=== CPU-NPU Consistency Validation ===
[synthetic_red_500x400] CPU logit=-3.869525 prob=0.020442 | NPU logit=-3.892607 prob=0.019985 | diff_logit=0.023082 diff_prob=0.000457
[synthetic_blue_384x384] CPU logit=-1.029379 prob=0.263204 | NPU logit=-1.027992 prob=0.263474 | diff_logit=0.001387 diff_prob=0.000269

Max prob diff: 0.000457
Consistency: PASS (CPU-NPU diff < 0.01)
  • 两图 NPU vs CPU prob 差异 0.000457 / 0.000269,logit 差异 0.023 / 0.001,远小于阈值 0.01 (prob) / 0.05 (logit)。
  • 验证为单样本一致性 (smoke consistency),非完整数据集评测。浮点误差来自 NPU 算子实现,属正常范围。

7. 性能测试

执行 python inference.py --mode benchmark (warmup 3, 10 iter, batch=1, 384x384, float32, torch.npu.synchronize 计时):

Benchmark 10 iterations (batch=1, 384x384, dtype=float32)...
 iter 1: 6.88ms
 iter 2: 6.28ms
 iter 3: 6.65ms
 iter 4: 6.23ms
 iter 5: 6.65ms
 iter 6: 6.25ms
 iter 7: 6.68ms
 iter 8: 6.26ms
 iter 9: 6.67ms
 iter 10: 6.28ms

--- Results ---
Batch size: 1, Image: 384x384, dtype: float32, device: npu:0
Avg latency: 6.48 ms
Min: 6.23 ms, Max: 6.88 ms
p50: 6.47 ms, p90: 6.70 ms, p95: 6.79 ms
Throughput: 154.24 images/s
NPU memory: free=60.69 GiB, total=61.27 GiB, used_by_process~0.58 GiB
Weight size: ~87 MB (safetensors)
Benchmark: PASS
  • 稳定推理 ~6.48ms/张,吞吐 154 images/s,满足实时检测。
  • 首帧编译不计入 benchmark,计时区间已同步。

8. 自验证截图

三张证据图由 scripts/render_xterm_evidence.mjs --style raw 从本次真实日志渲染:

  • assets/agent_workflow.png - 下载、侦察、NPU 推理、验证、性能全流程
  • assets/npu_device_call.png - npu-smi、torch.npu.is_available、device name、param device/dtype
  • assets/model_result.png - 默认 inference 输出与 PASS

agent workflow npu device call model result

9. 已知限制

  • 当前为合成输入 smoke 验证,未跑 CommunityForensics Eval (206GB) 全量评测;强度、泛化指标需在目标数据上补充。
  • 预处理要求 transformers >=5.4.0,旧版本会因 shortest_edge 触发 ValueError(有意防止静默错尺度)。
  • 量化 ONNX 变体 (int8/uint8/q4) 未在本仓 NPU 验证,如需 CPU 量化部署需另行对比 FP32 与量化差异。
  • 输入需保持 RGB,未做多 batch / 多分辨率压力测试,仅验证 batch=1 固定 384。
  • NPU 编译首帧 ~220ms,实际服务需预热。

10. 标签

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