社区取证深度伪造检测器 (Community Forensics, arXiv:2411.04125) 的 ViT-Small 分类器, 完成
昇腾 NPU (Ascend910) 端到端适配。输入任意图片, 输出 fake / real 类别与概率
(单 logit → sigmoid = P(fake), 阈值 0.5)。
/opt/atomgit/npu_adapt/buildborderless/CommunityForensics-DeepfakeDet-ViT| 项目 | 内容 |
|---|---|
| 任务 | 图像分类 (image-classification) — AI 生成图像取证 |
| 架构 | ViTForImageClassification (ViT-Small, patch16, 384×384) |
| 输入 | 任意尺寸图片, Resize+CenterCrop 至 384×384, CLIP 归一化 |
| 输出 | 单 logit → sigmoid = P(fake); ≥0.5 判定 fake, 否则 real |
| 权重 | model.safetensors (fp32, 约 87 MB) |
| 训练 | 270 万张图片 / 4800+ 生成器, 双分类 (真实 vs AI 生成) |
| License | MIT |
torch 2.9.0+cpu + torch_npu 2.9.0.post1transformers 4.57.6 / timm 1.0.28 / Pillow / fastapi / uvicornnpu:0, 全域无 CUDA 调用 (已移除所有 cuda 相关语句)# 1) 创建 venv 软链 (复用已装好 torch_npu 的基线环境)
ln -s /opt/atomgit/npu_adapt/lerobot/smolvla_base/.venv .venv
# 2) 单图推理 (CLI)
./.venv/bin/python inference.py --img assets/sample_real.jpg
# 3) FastAPI 服务 (根路径上传表单, POST /predict 返回 JSON, /health 健康检查)
./.venv/bin/python inference.py --server
# 然后: curl -F "file=@assets/sample_real.jpg" http://127.0.0.1:8000/predict权重缓存固定 ./model_weights/buildborderless/CommunityForensics-DeepfakeDet-ViT/,
优先本地缓存, 缺失时自动从镜像下载 (ai.gitcode.com/hf_mirrors → hf-mirror.com 回退)。
$ ./.venv/bin/python inference.py --img assets/sample_real.jpg
[权重] 命中本地缓存: .../model.safetensors (87270764 B)
[模型] 结构构建完成, 耗时 0.2s, num_labels=1, intermediate=1536, heads=6
============================================================
模型 : buildborderless/CommunityForensics-DeepfakeDet-ViT
设备 : npu:0
推理耗时 : 0.179s
判定 : real (fake_prob=2.4e-05)
============================================================
{
"model": "buildborderless/CommunityForensics-DeepfakeDet-ViT",
"pipeline_tag": "image-classification",
"device": "npu:0",
"label": "real",
"confidence": 0.999976,
"fake_prob": 2.4e-05,
"real_prob": 0.999976,
"logit": -10.620016,
"inference_time_s": 0.179
}{
"model": "buildborderless/CommunityForensics-DeepfakeDet-ViT",
"pipeline_tag": "image-classification",
"device": "npu:0",
"label": "real",
"confidence": 0.999976,
"fake_prob": 2.4e-05,
"real_prob": 0.999976,
"logit": -10.620016,
"inference_time_s": 0.18,
"filename": "sample_real.jpg"
}


原始日志见 assets/_logs/ (npu_device_call.log, model_result.json 等)。
| 端点 | 方法 | 说明 |
|---|---|---|
/ | GET | HTML 上传表单 |
/predict | POST | multipart 上传图片, 返回推理 JSON (含 label/fake_prob/real_prob) |
/health | GET | 健康检查 {"status":"ok","device":"npu:0"} |
import torch; import torch_npu; torch.npu.set_device(0); 设备字符串统一 npu:0。cuda 相关语句, 无 torch.cuda.* / .to("cuda")。AutoModelForImageClassification.from_pretrained(本地权重目录) + AutoImageProcessor.from_pretrained(本地权重目录), 不触网加载。./model_weights/<MODEL_ID>/ 优先本地, 缺失才下载; 镜像限流自动退避。num_labels: config 用 num_classes=1 表达单输出 (logit→sigmoid=P(fake)), transformers 不自动映射到 num_labels (默认 2), 需显式传入 num_labels=1;intermediate_size: config 为 3072, 权重实测为 1536 (ViT-Small, mlp_ratio 4), 从 model.safetensors 探测;num_attention_heads: config 为 12, 权重基座 timm vit_small_patch16_384 为 6, 显式传入 6。@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},
}