模型名称: 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 一致性验证和性能测试。
| 项目 | 值 |
|---|---|
| NPU | Ascend910_9362 (Ascend910B), device_count=2, visible npu:0 (phy 6) |
| CANN | 8.5.1 |
| torch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1+gitee7ba04 |
| transformers | 5.9.0 |
| Python | 3.11 |
| HF_ENDPOINT | https://hf-mirror.com |
| NPU 显存 | 61.27 GiB total, 60.69 GiB free (benchmark 时) |
| OS | Ubuntu 22.04.5 LTS |
torch.npu.is_available()=True, npu-smi OK, npu:0 可用。
pip install torch torch-npu transformers Pillow numpy
# transformers >=5.4.0 必需,否则 preprocessor 会因 shortest_edge 报错或
pip install -r requirements.txt默认命令(需 NPU 环境):
python inference.py
python inference.py --mode validate # CPU-NPU 一致性
python inference.py --mode benchmark # 性能推理自动选择 npu:0(若可用),加载 ViTImageProcessor + ViTForImageClassification 到 npu:0,固定合成输入,多图批量验证。
执行 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: PASSreal 判定,logits 负值,fake_prob < 0.27,符合单样本合成输入预期。执行 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)执行 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三张证据图由 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/dtypeassets/model_result.png - 默认 inference 输出与 PASS

shortest_edge 触发 ValueError(有意防止静默错尺度)。Hardware: NPU, NPU, Ascend, Ascend910, ViT, deepfake-detection, image-classification