s
stella4528/CommunityForensics-DeepfakeDet-ViT-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

CommunityForensics-DeepfakeDet-ViT — 昇腾 NPU 适配

标签:#model-agent-tagged · #NPU · #Ascend · #Ascend-910 · #image-classification · #deepfake · #deepfake-detection

本模型卡片已标注 #NPU、#Ascend、#Ascend-910 与 #model-agent-tagged 标签,符合昇腾模型适配比赛提交要求。

模型简介

buildborderless/CommunityForensics-DeepfakeDet-ViT 是 Deepfake(AI 生成图像)检测模型:ViT-Small 视觉 Transformer,在 270 万样本 / 4803 个生成器上训练(论文 "Community Forensics: Using Thousands of Generators to Train Fake Image Detectors",CVPR 2025)。输入图像经 sigmoid 单类输出 fake 概率 (fake_prob > 0.5 判定为 AI 生成),用于社区开源深伪检测框架 OpenSight。

  • 架构:ViTForImageClassification(hidden 384 / intermediate 1536 / 12 层 / 6 头 / patch 16 / 输入 384×384×3),classifier = Linear(384 → 1),logits → sigmoid
  • 全量 21,811,969 参数,fp32,纯 PyTorch 算子(Linear/LayerNorm/GELU/softmax 等)

适配说明

  1. 官方 transformers 格式:model.safetensors(87,270,764 B)+ config.json + preprocessor_config.json,ViTForImageClassification.from_pretrained 直接加载
  2. 预处理契约(preprocessor_config.json):resize 最短边 440 → center crop 384 → CLIP 归一化(mean [0.48145466,0.4578275,0.40821073] / std [0.26862954,0.26130258,0.27577711])
  3. transformers 版本兼容:官方要求 transformers>=5.4.0(旧版 ViTImageProcessor 不支持 shortest_edge size 键,调用会 ValueError);本脚本内置手动预处理 fallback (PIL resize/crop + torch 归一化,与契约数值一致),transformers 4.57.x 亦可完整运行
  4. NPU 推理:torch.npu.is_available() 检测 + torch.npu.set_device,模型与输入 tensor 均 to("npu:0");全部算子为纯 PyTorch,在昇腾 NPU 上原生执行,无 CUDA 内核、 无 CPU fallback(图像解码/缩放属输入前处理,在 CPU 完成,非模型算子)
  5. 输出:logits -> sigmoid -> fake_prob,verdict = fake if fake_prob>0.5 else real

环境依赖

组件版本(实测)
Python3.11(aarch64)
torch2.9.0+cpu
torch_npu2.9.0.post1(CANN 25.5.5)
transformers4.57.6(官方要求 >=5.4.0,本脚本含手动预处理 fallback)
pillow12.2.0
numpy1.26.4

推理步骤

  1. 克隆本仓库并安装依赖:
git clone https://gitcode.com/stella4528/CommunityForensics-DeepfakeDet-ViT-npu
cd CommunityForensics-DeepfakeDet-ViT-npu
pip install -r requirements.txt
  1. 默认用例(内置合成示例图,自动选择 NPU;权重缺失时自动下载):
python3 inference.py
  1. 指定设备 / 自定义图像:
python3 inference.py --device npu
python3 inference.py --device cpu --image my_image.jpg
python3 inference.py --device npu --image deepfake_sample.png
  1. 预期输出:pixel_values (1,3,384,384) -> logits (1,1) -> fake_prob, 打印 verdict: fake/real 与 NPU_INFERENCE_PASS=True,结果落盘到 results/。

验证结果

实测结果见下方;适配脚本与输出截图见 assets/。

  • 设备:Ascend 910(npu:0,Ascend910_9362)
  • CPU 前向:PASS(666.00 ms,fake_prob=0.7624,verdict=fake,finite_all=True)
  • NPU 前向:PASS(200.65 ms,fake_prob=0.7598,verdict=fake,finite_all=True, 与 CPU 结果一致(差 <0.003),输入 384×384 合成示例图)
  • 输出:单类 sigmoid fake 概率 + fake/real 判定

文件说明

文件说明
inference.py昇腾 NPU 推理脚本(ViT 分类前向 + 手动预处理 fallback)
weights/model.safetensors原始权重(87,270,764 B,fp32)
weights/config.json模型配置(ViT-Small 384/16,num_labels=1)
weights/preprocessor_config.json预处理契约(440 resize / 384 crop / CLIP normalize)
requirements.txt环境依赖清单

参考

  • 原始模型:https://huggingface.co/buildborderless/CommunityForensics-DeepfakeDet-ViT
  • 论文:https://huggingface.co/papers/2411.04125(CVPR 2025)
  • 许可协议:MIT