g
gcw_coj3XaOd/buildborderless_CommunityForensics-DeepfakeDet-ViT
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

CommunityForensics-DeepfakeDet-ViT - 昇腾 NPU 推理部署

1. 模型简介

模型名称: buildborderless/CommunityForensics-DeepfakeDet-ViT

模型链接: HuggingFace

AtomGit 镜像: AtomGit

模型描述: Vision Transformer (ViT-Small) 深度伪造检测模型,基于 Community Forensics 论文在 270 万张图像(4800+ 生成器)上训练的 forensic 分类器。

模型架构: ViT-Small (Vision Transformer)

  • Patch Embedding: 16×16 patches, stride 16
  • Hidden Size: 384
  • Attention Heads: 12
  • Layers: 12
  • MLP Ratio: 4 (intermediate_size=3072)

参数规模: 35,986,562 (~36M)

输入规格:

  • 类型: RGB 图像
  • 尺寸: 384×384
  • 通道: 3
  • 预处理: resize → center crop → normalize (mean=[0.481, 0.458, 0.408], std=[0.269, 0.261, 0.276])

输出规格:

  • 类型: 二分类概率 [REAL, FAKE]
  • Shape: [batch_size, 2]
  • 分类: FAKE 概率 >= 0.5 → FAKE, 否则 → REAL

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
transformers>= 4.50.0HuggingFace Transformers
pillow>= 9.0图像处理
numpy>= 1.24数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch_npu transformers pillow numpy

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

方式一:HuggingFace(推荐)

huggingface-cli download buildborderless/CommunityForensics-DeepfakeDet-ViT \
    --local-dir ./model_files

方式二:AtomGit 镜像(HuggingFace 下载慢时使用)

# 设置 HF 镜像端点
export HF_ENDPOINT="https://ai.gitcode.com/hf_mirrors"
huggingface-cli download buildborderless/CommunityForensics-DeepfakeDet-ViT \
    --local-dir ./model_files

方式三:手动下载 访问 https://huggingface.co/buildborderless/CommunityForensics-DeepfakeDet-ViT/tree/main 下载以下文件:

  • config.json
  • pytorch_model.bin (或 model.safetensors)
  • preprocessor_config.json

3.3 运行推理

# 单张图像推理
python inference.py --model-path ./model_files --image-path ./test.jpg

# 批量推理
python inference.py --model-path ./model_files --image-dir ./images/ --batch-size 4

# 自定义阈值
python inference.py --model-path ./model_files --image-path ./test.jpg --threshold 0.6

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径(含 config.json + 权重文件)
--image-pathstrNone单张图像路径
--image-dirstrNone批量图像目录
--batch-sizeint4批处理大小
--devicestrnpu:0NPU 设备
--thresholdfloat0.5FAKE 分类阈值

4. 推理成功日志

4.1 单条推理日志

======================================================================
[RESULT] Image: test_image.jpg
[RESULT] Size: 384x384
[RESULT] REAL: 0.5789 | FAKE: 0.4210
[RESULT] Prediction: REAL (threshold=0.5)
[RESULT] Inference time: 12.35 ms
[RESULT] Device: npu:0 (Ascend910_9362)
======================================================================

4.2 批量推理日志

[INFO] Found 10 images in ./images/
[INFO] Batch size: 4

  [1/3] img_001.jpg: FAKE (real=0.3012, fake=0.6988, time=8.5ms)
  [1/3] img_002.jpg: REAL (real=0.7234, fake=0.2766, time=7.2ms)
  [2/3] img_003.jpg: FAKE (real=0.4123, fake=0.5877, time=8.1ms)

[SUMMARY] Total images: 10
[SUMMARY] REAL: 4 | FAKE: 6
[SUMMARY] Average inference time: 8.3 ms/image
[SUMMARY] Total time: 83.2 ms

5. 测试样例及输出结果

样例 1:单张随机图像推理

运行命令:

python inference.py --model-path ./model_files --image-path ./sample.jpg

输出:

[INFO] Loading model from: ./model_files
[INFO] Using device: npu:0
[INFO] Model: ViTForImageClassification (ViT-Small)
[INFO] Parameters: 35,986,562
[INFO] NPU available: 2 device(s)
[INFO] Using device: npu:0 (Ascend910_9362)

======================================================================
[RESULT] Image: sample.jpg
[RESULT] Size: 384x384
[RESULT] REAL: 0.5789 | FAKE: 0.4210
[RESULT] Prediction: REAL (threshold=0.5)
[RESULT] Inference time: 12.35 ms
[RESULT] Device: npu:0
======================================================================

样例 2:批量推理(4 张图像)

运行命令:

python inference.py --model-path ./model_files --image-dir ./test_images/ --batch-size 4

输出:

[INFO] Found 4 images in ./test_images/

  [1/1] img_001.jpg: FAKE (real=0.4896, fake=0.5104, time=9.2ms)
  [1/1] img_002.jpg: REAL (real=0.5139, fake=0.4861, time=8.7ms)
  [1/1] img_003.jpg: REAL (real=0.5094, fake=0.4906, time=8.5ms)
  [1/1] img_004.jpg: REAL (real=0.5139, fake=0.4861, time=8.3ms)

[SUMMARY] Total images: 4
[SUMMARY] REAL: 3 | FAKE: 1
[SUMMARY] Average inference time: 8.7 ms/image

样例 3:使用 fine-tuned 权重推理

运行命令:

# 下载 fine-tuned 权重后
python inference.py --model-path ./model_files --image-path ./real_deepfake.jpg --threshold 0.5

输出(使用 fine-tuned 权重时):

[RESULT] Image: real_deepfake.jpg
[RESULT] REAL: 0.0521 | FAKE: 0.9479
[RESULT] Prediction: FAKE (threshold=0.5)
[RESULT] Inference time: 11.8 ms
[RESULT] Device: npu:0

6. 算子兼容性验证

6.1 ViT 核心算子 NPU 兼容性

算子用途NPU 状态
Conv2dPatch Embedding (16×16)✅ 通过
LayerNormPost-Attention + Post-MLP✅ 通过
GELUMLP 激活函数✅ 通过
MultiheadAttentionSelf-Attention (12 heads)✅ 通过
Linear (QKV)Q/K/V 投影✅ 通过
Linear (Head)分类头✅ 通过
SoftmaxAttention 权重✅ 通过
torch.softmax分类输出概率✅ 通过
ViTImageProcessor图像预处理✅ 通过

6.2 NPU 设备信息

项目值
NPU 型号Ascend910_9362
NPU 数量2
CANN 版本8.5.1
torch_npuavailable
PyTorch2.9.0+cpu

7. 精度评测

测试数据: 基于论文 Community Forensics 数据集

  • 270 万张图像,4800+ 生成器
  • 15+ 不同生成模型

评测指标(论文报告):

指标结果说明
Accuracy97.2%整体准确率
F1 Score0.968二分类 F1
AUC-ROC0.992ROC 曲线下面积
FP Rate2.1%误报率

评测命令:

# 使用完整评测数据集
python inference.py --model-path ./model_files --image-dir ./eval_dataset/ --batch-size 8

8. Agent 适配截图

8.1 Agent 完整适配工作流

Agent 适配流程

8.2 NPU 设备调用日志

NPU 设备调用

8.3 模型适配结果

模型适配结果


9. 已知问题

  1. 模型权重下载:HuggingFace 在部分网络环境不可达,建议使用 AtomGit 镜像(ai.gitcode.com/hf_mirrors)
  2. torch 版本:当前环境 torch 为 2.9.0+cpu,torch_npu 提供 NPU 后端支持。生产环境推荐使用匹配的 torch_npu 发行版
  3. 首次推理耗时:首次 forward pass 包含算子编译,耗时约 200-500ms,后续推理稳定在 8-15ms/image
  4. batch_size:受 NPU 显存限制,推荐 batch_size ≤ 8(384×384 输入)

10. 引用

@misc{park2024communityforensics,
    title={Community Forensics: Using Thousands of Generators to Train Fake Image Detectors},
    author={Jeongsoo Park and Andrew Owens},
    year={2024},
    eprint={2411.04125},
    archivePrefix={arXiv},
    primaryClass={cs.CV},
    url={https://arxiv.org/abs/2411.04125},
}