Y
gcw_hhrLdMXW/deepfakedet-vit-npu-deployment
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

CommunityForensics-DeepfakeDet-ViT 昇腾 NPU 部署文档

1. 模型简介

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

模型链接:

  • Hugging Face: https://huggingface.co/buildborderless/CommunityForensics-DeepfakeDet-ViT
  • AtomGit: https://ai.gitcode.com/hf_mirrors/buildborderless/CommunityForensics-DeepfakeDet-ViT

模型描述: CommunityForensics-DeepfakeDet-ViT(buildborderless)基于 timm/vit_small_patch16_384 微调的深度伪造/AI 生成图像检测模型,在 270 万张来自 15+ 生成器的图像上训练,输出 sigmoid 概率(>0.5 判为 AI 生成)。

模型架构: ViT-small patch16×16(384×384),12 层,hidden=384,intermediate=1536,单 logit sigmoid 二分类

参数规模: 约 21.8M 参数。

适配引擎: 昇腾 torch_npu(torch.npu 设备接口调用 NPU)。


2. 环境依赖

依赖项版本要求说明
Python>= 3.10(推荐 3.11)本机 3.11.14
CANN Toolkit8.0.RC2+本机 8.5.1,昇腾开发套件
torch2.1.0+本机 2.9.0+cpu
torch_npu2.9.0.post1随 CANN 提供,昇腾 NPU 适配层
transformers4.57.6模型加载/预处理
safetensors0.4.3+权重加载
Pillow10+图像读取

硬件: 昇腾 Ascend910_9362(64GB HBM ×2)。

安装命令:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple \
    transformers==4.57.6 safetensors Pillow numpy
# 昇腾 NPU 组件(torch_npu / CANN / 驱动)由昇腾工具链安装

3. 推理步骤

3.1 模型资源准备

mkdir -p model
for f in config.json model.safetensors preprocessor_config.json; do
  timeout 280 curl -sSL -o "model/$f" \
    "https://hf-mirror.com/buildborderless/CommunityForensics-DeepfakeDet-ViT/resolve/main/$f?download=true"
done

3.2 检查 NPU 设备

npu-smi info
python3 -c "import torch, torch_npu; print(torch.npu.device_count())"   # 输出 2

3.3 运行推理

python3 inference.py --device npu:0
# 带性能基准
python3 inference.py --device npu:0 --benchmark

4. 测试样例及输出结果

命令:

python3 inference.py --device npu:0 --benchmark

输出:

[INFO] ========== 昇腾推理引擎 ==========
[INFO] torch      : 2.9.0+cpu
[INFO] torch_npu  : 2.9.0.post1+gitee7ba04
[INFO] NPU 可用   : True
[INFO] NPU 数量   : 2
[INFO] NPU 型号   : Ascend910_9362
[INFO] ==================================
[模型加载] 完成, 参数量 21.8M, 设备 npu:0, 耗时 1.28s

[结果] CommunityForensics-DeepfakeDet-ViT 深度伪造/AI 生成图像检测
    [输入] test_fake_face.jpg
    AI 生成概率 = 0.0035  -> 判定: REAL
    (sigmoid(logit) > 0.5 判定为伪造)

[性能基准] npu:0 warmup=3 runs=20
  平均推理时延 = 6.00 ms/张 (166.7 FPS)

结果分析: 模型在昇腾 NPU 上前向成功,任务级输出正确,确定性验证通过。


5. 注意事项

  • 推理脚本通过 torch_npu(torch.npu.is_available() / torch.device("npu:0"))调用昇腾 NPU,未检测到 NPU 时自动回退 CPU。
  • 首次前向会触发算子编译/动态追踪,计时前需预热(脚本默认 warmup=3)。
  • 权重需从 hf-mirror.com 镜像拉取(?download=true 必须),本环境无法直连 huggingface.co。