SMOGY-Ai-images-detector 是一个基于 Swin Transformer 架构的 AI 生成图像检测模型。该模型用于区分 AI 生成图像(artificial)与真实图像(human),经过 50,000+ 张图像的微调训练。
本仓库提供该模型的昇腾 NPU 适配版本,支持在华为昇腾 Ascend910 NPU 上进行推理。
图像分类(Image Classification) — 二分类:artificial(AI 生成) / human(真实)
{0: "artificial", 1: "human"}该模型的原始 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.jpgpython3 compare_cpu_npu.py使用测试图片 test_image.jpg(猫的图片)进行推理:
设备: CPU
预测类别: human (id: 1)
类别概率:
artificial: 0.012342
human: 0.987658
Logits: [[-2.6471102 1.7352068]]
推理耗时: 687.96 ms设备: Ascend910 NPU
预测类别: human (id: 1)
类别概率:
artificial: 0.012788
human: 0.987212
Logits: [[-2.6281729 1.7182388]]
推理耗时: 268.59 ms使用相同的测试图片,分别在 CPU 和 NPU 上运行推理,比较输出 logits 和概率的差异。具体指标包括:
| 指标 | 值 |
|---|---|
| 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.96 | 1.00x |
| NPU (Ascend910) | 268.59 | 2.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
CC-BY-NC-4.0