模型名称: buildborderless/CommunityForensics-DeepfakeDet-ViT
模型链接: HuggingFace
AtomGit 镜像: AtomGit
模型描述: Vision Transformer (ViT-Small) 深度伪造检测模型,基于 Community Forensics 论文在 270 万张图像(4800+ 生成器)上训练的 forensic 分类器。
模型架构: ViT-Small (Vision Transformer)
参数规模: 35,986,562 (~36M)
输入规格:
输出规格:
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | >= 2.1.0 | PyTorch 框架 |
| torch_npu | >= 2.1.0 | 昇腾 NPU 后端 |
| transformers | >= 4.50.0 | HuggingFace Transformers |
| pillow | >= 9.0 | 图像处理 |
| numpy | >= 1.24 | 数值计算 |
| 昇腾驱动 | CANN 8.0+ | 推荐 CANN 8.5.1 |
安装命令:
pip install torch_npu transformers pillow numpy# 检查 NPU 设备
npu-smi info
# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"方式一: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.jsonpytorch_model.bin (或 model.safetensors)preprocessor_config.json# 单张图像推理
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| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model-path | str | 必填 | 模型 checkpoint 路径(含 config.json + 权重文件) |
| --image-path | str | None | 单张图像路径 |
| --image-dir | str | None | 批量图像目录 |
| --batch-size | int | 4 | 批处理大小 |
| --device | str | npu:0 | NPU 设备 |
| --threshold | float | 0.5 | FAKE 分类阈值 |
======================================================================
[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)
======================================================================[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运行命令:
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
======================================================================运行命令:
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运行命令:
# 下载 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| 算子 | 用途 | NPU 状态 |
|---|---|---|
| Conv2d | Patch Embedding (16×16) | ✅ 通过 |
| LayerNorm | Post-Attention + Post-MLP | ✅ 通过 |
| GELU | MLP 激活函数 | ✅ 通过 |
| MultiheadAttention | Self-Attention (12 heads) | ✅ 通过 |
| Linear (QKV) | Q/K/V 投影 | ✅ 通过 |
| Linear (Head) | 分类头 | ✅ 通过 |
| Softmax | Attention 权重 | ✅ 通过 |
| torch.softmax | 分类输出概率 | ✅ 通过 |
| ViTImageProcessor | 图像预处理 | ✅ 通过 |
| 项目 | 值 |
|---|---|
| NPU 型号 | Ascend910_9362 |
| NPU 数量 | 2 |
| CANN 版本 | 8.5.1 |
| torch_npu | available |
| PyTorch | 2.9.0+cpu |
测试数据: 基于论文 Community Forensics 数据集
评测指标(论文报告):
| 指标 | 结果 | 说明 |
|---|---|---|
| Accuracy | 97.2% | 整体准确率 |
| F1 Score | 0.968 | 二分类 F1 |
| AUC-ROC | 0.992 | ROC 曲线下面积 |
| FP Rate | 2.1% | 误报率 |
评测命令:
# 使用完整评测数据集
python inference.py --model-path ./model_files --image-dir ./eval_dataset/ --batch-size 8


ai.gitcode.com/hf_mirrors)@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},
}