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

buildborderless/CommunityForensics-DeepfakeDet-ViT · 昇腾 NPU 适配

模型简介

buildborderless/CommunityForensics-DeepfakeDet-ViT 是一个基于 ViT 的 DeepFake 检测模型 (ViTForImageClassification,二分类 real/fake)。输入图像 384×384,使用 CLIP 归一化 (mean=[0.48145466,0.4578275,0.40821073],std=[0.26862954,0.26130258,0.27577711])并做 center crop 384。

模型权重分类头 shape 为 [2, 384](2 类),但 config.json 未声明 num_labels (仅有 num_classes:1)。本项目在内存中将 config.num_labels 强制覆盖为 2 后再加载权重, 以匹配分类头维度。

类别映射:{0: "real", 1: "fake"}(0=真实,1=伪造)。

环境依赖

  • Python 3.11,PyTorch 2.9.0+cpu,torch_npu 2.9.0.post1,transformers 4.57.x
  • 昇腾 Ascend910(NPU 设备 npu:0 / npu:1)
  • 依赖见 requirements.txt

推理入口(inference.py)

适配脚本仅使用 NPU 设备(npu:N),全代码域无任何 CUDA 调用;NPU 不可用时直接报错退出, 不做 CPU/CUDA 回退。权重首次运行从 GitCode HF 镜像自动下载并缓存到 model_weights/buildborderless/CommunityForensics-DeepfakeDet-ViT/,命中缓存时打印 [cache-hit]。

方式一:命令行单图推理

python inference.py --img test_input.png --device npu:0 [--output_json result.json]

stdout 输出 JSON,含 model、requested_device、actual_device、npu_name、exit_status、 inference_ms 以及 label / real_prob / fake_prob / logits。

方式二:FastAPI 服务

python inference.py --serve --port 8004
# 另一终端:
curl -s -F "file=@test_input.png" http://127.0.0.1:8004/predict

POST /predict 上传图片文件,返回与 CLI 相同的推理 JSON;GET / 返回服务与设备元信息。

验证结果

  • CLI:python inference.py --img test_input.png --device npu:0 在 npu:0(Ascend910_9362)上 exit_status:0。
  • FastAPI:端口 8004 启动后 POST /predict 返回推理 JSON。
  • 证据截图见 assets/。

权重来源

  • 模型主页:HuggingFace buildborderless/CommunityForensics-DeepfakeDet-ViT
  • 镜像下载:https://ai.gitcode.com/hf_mirrors/buildborderless/CommunityForensics-DeepfakeDet-ViT/resolve/main/<file>
  • 必需文件:config.json、preprocessor_config.json、model.safetensors