f
gcw_pCMO8DfP/CommunityForensics-DeepfakeDet-ViT
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

CommunityForensics-DeepfakeDet-ViT · 昇腾 Ascend NPU 适配版

社区取证深度伪造检测器 (Community Forensics, arXiv:2411.04125) 的 ViT-Small 分类器, 完成 昇腾 NPU (Ascend910) 端到端适配。输入任意图片, 输出 fake / real 类别与概率 (单 logit → sigmoid = P(fake), 阈值 0.5)。

  • 原始模型: https://huggingface.co/buildborderless/CommunityForensics-DeepfakeDet-ViT
  • 适配工程: /opt/atomgit/npu_adapt/buildborderless/CommunityForensics-DeepfakeDet-ViT
  • 本仓库: https://ai.gitcode.com/gcw_pCMO8DfP/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 生成)
LicenseMIT

环境

  • 昇腾 Ascend910×2, CANN 8.5.1, torch 2.9.0+cpu + torch_npu 2.9.0.post1
  • transformers 4.57.6 / timm 1.0.28 / Pillow / fastapi / uvicorn
  • 设备统一 npu: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 回退)。

CLI 推理示例

$ ./.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
}

FastAPI 返回示例 (POST /predict)

{
  "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"
}

截图 (昇腾 NPU 推理验证)

部署与任务工作流

agent_workflow

NPU 设备调用 (CLI 单图推理)

npu_device_call

FastAPI 推理结果

model_result

原始日志见 assets/_logs/ (npu_device_call.log, model_result.json 等)。

推理服务 API

端点方法说明
/GETHTML 上传表单
/predictPOSTmultipart 上传图片, 返回推理 JSON (含 label/fake_prob/real_prob)
/healthGET健康检查 {"status":"ok","device":"npu:0"}

适配说明 (NPU 关键点)

  1. 初始化顺序: import torch; import torch_npu; torch.npu.set_device(0); 设备字符串统一 npu:0。
  2. CUDA 清理: 源码与推理脚本中已删除全部 cuda 相关语句, 无 torch.cuda.* / .to("cuda")。
  3. 离线加载: AutoModelForImageClassification.from_pretrained(本地权重目录) + AutoImageProcessor.from_pretrained(本地权重目录), 不触网加载。
  4. 权重缓存: ./model_weights/<MODEL_ID>/ 优先本地, 缺失才下载; 镜像限流自动退避。
  5. config 修正 (重要): 仓库 config.json 与权重不完全一致, 需按权重实测构建:
    • 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},
}