e
gcw_GSiqzzLf/SMOGY-Ai-images-detector-npu
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

SMOGY-Ai-images-detector-NPU

模型介绍

SMOGY-Ai-images-detector 是一个基于 Swin Transformer 架构的 AI 生成图像检测模型。该模型用于区分 AI 生成图像(artificial)与真实图像(human),经过 50,000+ 张图像的微调训练。

本仓库提供该模型的昇腾 NPU 适配版本,支持在华为昇腾 Ascend910 NPU 上进行推理。

原始模型地址

  • ModelScope: https://www.modelscope.cn/models/onnx-community/SMOGY-Ai-images-detector-ONNX
  • HuggingFace: https://huggingface.co/Smogy/SMOGY-Ai-images-detector

任务类型

图像分类(Image Classification) — 二分类:artificial(AI 生成) / human(真实)

模型框架

  • 架构: Swin Transformer (SwinForImageClassification)
  • 参数: ~60M
  • 输入尺寸: 224×224 像素,3 通道 RGB
  • 输出: 2 个类别的 logits

输入格式

  • 图像文件(.jpg, .png, .jpeg),推荐 224×224
  • 预处理:ImageNet 标准化(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])

输出格式

  • logits: 长度 2 的浮点数向量
  • probabilities: softmax 归一化后的概率
  • 类别: {0: "artificial", 1: "human"}

依赖环境

  • Python 3.10+
  • PyTorch 2.0+
  • torch_npu
  • transformers
  • Pillow
  • NumPy

NPU 适配说明

该模型的原始 ONNX 格式为量化版本(UINT8/BNB4),无法直接在昇腾 NPU 上通过 ONNX Runtime 运行。本仓库使用原始 HuggingFace PyTorch 模型,在昇腾 NPU 上通过 torch_npu 进行推理。CPU 和 NPU 推理结果精度对比显示误差小于 1%。

环境准备

# 安装依赖
pip install torch torchvision transformers pillow numpy

# 验证 NPU 可用
python3 -c "import torch; print(torch.npu.is_available())"

推理命令

# CPU 推理
python3 inference.py --device cpu --image test_image.jpg

# NPU 推理
python3 inference.py --device npu --image test_image.jpg

精度对比

python3 compare_cpu_npu.py

推理结果

使用测试图片 test_image.jpg(猫的图片)进行推理:

CPU 推理结果

设备: CPU
预测类别: human (id: 1)
类别概率:
  artificial: 0.012342
  human: 0.987658
Logits: [[-2.6471102  1.7352068]]
推理耗时: 687.96 ms

NPU 推理结果

设备: Ascend910 NPU
预测类别: human (id: 1)
类别概率:
  artificial: 0.012788
  human: 0.987212
Logits: [[-2.6281729  1.7182388]]
推理耗时: 268.59 ms

CPU/NPU 精度测试方法

使用相同的测试图片,分别在 CPU 和 NPU 上运行推理,比较输出 logits 和概率的差异。具体指标包括:

  • Logits 平均绝对误差(MAE)
  • Logits 最大绝对误差(MaxAE)
  • Logits 平均相对误差(%)
  • 概率平均绝对误差
  • 分类一致率
  • 余弦距离

CPU/NPU 精度测试结果

指标值
CPU 预测human
NPU 预测human
分类一致率100%
Logits 平均绝对误差 (MAE)0.017953
Logits 最大绝对误差 (MaxAE)0.018937
Logits 平均相对误差0.85%
概率平均绝对误差0.000445
概率余弦距离0.00000007
CPU 推理耗时687.96 ms
NPU 推理耗时268.59 ms
NPU 加速比2.56x

结论:NPU 与 CPU 推理结果误差 < 1%,精度验证通过。

部署和推理方法

核心代码

import torch
from PIL import Image
from transformers import AutoModelForImageClassification, AutoImageProcessor

# 加载模型和处理器
model_path = "Smogy/SMOGY-Ai-images-detector"
model = AutoModelForImageClassification.from_pretrained(model_path)
processor = AutoImageProcessor.from_pretrained(model_path)

# 切换到 NPU
device = torch.device("npu:0")
model = model.to(device)
model.eval()

# 加载并预处理图片
image = Image.open("test_image.jpg").convert("RGB")
inputs = processor(images=image, return_tensors="pt")
inputs = {k: v.to(device) for k, v in inputs.items()}

# 推理
with torch.no_grad():
    outputs = model(**inputs)

logits = outputs.logits
probabilities = torch.nn.functional.softmax(logits, dim=-1)
predicted_class = logits.argmax(-1).item()
print(f"预测: {model.config.id2label[predicted_class]}")

性能测试结果

平台推理耗时 (ms)加速比
CPU (Intel Xeon)687.961.00x
NPU (Ascend910)268.592.56x

文件说明

  • inference.py — 推理脚本(支持 CPU/NPU)
  • compare_cpu_npu.py — CPU vs NPU 精度对比脚本
  • requirements.txt — Python 依赖
  • test_image.jpg — 测试图片
  • README.md — 本说明文档

推理成功证据

本仓库提供完整的推理脚本,支持 CPU 和 NPU 双平台推理:

# NPU 推理
python3 inference.py --device npu

# CPU 推理
python3 inference.py --device cpu

推理完成后会输出推理结果和耗时,表明模型在 NPU 上推理成功。

模型标签

#+NPU #+CV #+图像分类 #+昇腾 #+AI检测 #+SwinTransformer

License

CC-BY-NC-4.0