liuhongwei-2026/CommunityForensics-DeepfakeDet-ViT
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

CommunityForensics-DeepfakeDet-ViT — 昇腾 NPU 部署

基于 Vision Transformer (ViT-Small) 的 AI 生成图像检测模型,适配华为 Ascend 910B/910C NPU。

原模型: buildborderless/CommunityForensics-DeepfakeDet-ViT

论文: Community Forensics: Using Thousands of Generators to Train Fake Image Detectors


模型简介

属性说明
任务AI 生成图像检测(深度伪造检测)
架构ViT-Small (vit_small_patch16_384)
输入尺寸384 × 384 RGB
输出二分类概率(Sigmoid),≥0.5 为 AI 生成
精度准确率 97.2%,AUC-ROC 0.992
训练数据15+ 生成器、4600+ 模型、270 万张图像
许可证MIT

环境准备

1. 昇腾 NPU 驱动与 CANN

确保已安装昇腾 NPU 驱动和 CANN 软件包,版本建议 CANN 7.0.RC1 及以上。

# 验证 NPU 可用
npu-smi info

2. Python 环境

# 创建虚拟环境(推荐 Python 3.9+)
python3 -m venv venv
source venv/bin/activate

# 安装依赖
pip install -r requirements.txt

torch_npu 需根据 CANN 版本安装,参考 Ascend PyTorch:

pip install torch-npu==2.1.0.post8

3. 验证 NPU 可用

import torch
import torch_npu  # noqa: F401
print(torch.npu.is_available())  # 应输出 True
print(torch.npu.device_count())  # NPU 卡数量

快速推理

单张图像推理

python inference.py --image_path /path/to/test_image.jpg

默认从 HuggingFace 镜像 ai.gitcode.com/hf_mirrors/buildborderless/CommunityForensics-DeepfakeDet-ViT 自动下载模型。

指定模型来源

# 从 HuggingFace Hub
python inference.py --image_path test.jpg --model_path buildborderless/CommunityForensics-DeepfakeDet-ViT

# 从本地目录
python inference.py --image_path test.jpg --model_path ./model_weights

# 强制 CPU 推理
python inference.py --image_path test.jpg --cpu

输出示例

[INFO] 推理设备: npu:0
[INFO] 加载模型: ai.gitcode.com/hf_mirrors/buildborderless/CommunityForensics-DeepfakeDet-ViT
[INFO] 模型加载完成,参数量: 22,078,465
[INFO] 输入图像: test.jpg  (1024×1024)
============================================================
  结果: AI-Generated (Deepfake)
  伪造概率 (AI-Generated): 98.73%
  置信度: 98.73%
  推理耗时: 45.23 ms
============================================================

项目结构

CommunityForensics-DeepfakeDet-ViT/
├── inference.py           # 推理脚本(Ascend NPU 适配)
├── README.md              # 本部署说明文档
├── requirements.txt       # 环境依赖清单
└── assets/                # 截图素材
    ├── agent_workflow.png # 部署/推理流程截图
    ├── npu_device_call.png# NPU 设备调用截图
    └── model_result.png   # 推理结果截图

推理流程

输入图像 ──→ 预处理 (Resize 384 + CenterCrop + Normalize) ──→ ViT 编码器 ──→ 分类头 ──→ Sigmoid ──→ 概率
                                                                  ↑
                                                           Ascend NPU (torch_npu)

预处理细节

  • Resize: 384px(保持宽高比)
  • CenterCrop: 384 × 384
  • Normalize: mean = [0.48145466, 0.4578275, 0.40821073], std = [0.26862954, 0.26130258, 0.27577711]

在 AtomGit 上部署

本仓库已集成至 AtomGit 模型仓,支持一键拉取:

git clone https://atomgit.com/liuhongwei-2026/CommunityForensics-DeepfakeDet-ViT.git
cd CommunityForensics-DeepfakeDet-ViT
pip install -r requirements.txt
python inference.py --image_path example.jpg

引用

@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},
}

许可证

MIT License