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

ai-source-detector-NPU

模型介绍

ai-source-detector 是一个基于 ViT-Base 架构(86M 参数)的多分类图像分类模型,用于检测图像是由何种 AI 工具生成。模型能够区分五种来源:Stable Diffusion、Midjourney、DALL-E、真实照片以及其他 AI 工具生成的图像。适用于 AI 生成图像的溯源分析场景。

原始模型地址

  • ModelScope: https://www.modelscope.cn/models/onnx-community/ai-source-detector-ONNX
  • HuggingFace: yaya36095/ai-source-detector

任务类型

image-classification(图像五分类:stable_diffusion / midjourney / dalle / real / other_ai)

模型框架

ONNX(CPU)/ PyTorch(NPU)

模型架构

ViTForImageClassification(ViT-Base,86M 参数)

输入格式

  • 图像尺寸: 224 x 224
  • 通道数: 3 通道 RGB
  • 数据类型: float32
  • 预处理: 标准化(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])

输出格式

  • logits: shape [1, 5],对应五个类别的原始分数
  • probabilities: shape [1, 5],经 softmax 归一化后的概率分布
  • 类别映射:
    • class 0: stable_diffusion
    • class 1: midjourney
    • class 2: dalle
    • class 3: real
    • class 4: other_ai

依赖环境

  • Python >= 3.8
  • torch >= 2.0.0
  • torchvision
  • onnxruntime
  • onnx2torch
  • Pillow
  • numpy
  • torch_npu(NPU 推理时需要)

NPU 适配说明

该模型原始格式为 ONNX,在 CPU 上使用 ONNX Runtime 进行推理。在 NPU 适配中,采用 onnx2torch 将 ONNX 模型转换为 PyTorch 格式,然后在昇腾 NPU(Atlas 800 A2/A3)上使用 torch_npu 进行推理。需要注意的是,CPU 和 NPU 使用不同的推理框架(ONNX Runtime vs torch_npu),但转换后的模型精度与原始 ONNX 模型高度一致。

环境准备

# 基础环境
pip install torch torchvision
pip install onnxruntime
pip install onnx2torch
pip install Pillow numpy

# NPU 环境(需在昇腾设备上)
pip install torch_npu

推理命令

# CPU 推理(ONNX Runtime)
python accuracy_run.py --model_path ./model_files/model.onnx --image_path ./test_images/test.jpg --device cpu --framework onnx

# NPU 推理(torch_npu)
python accuracy_run.py --model_path ./model_files/model.onnx --image_path ./test_images/test.jpg --device npu --framework torch

精度对比命令

python accuracy_run.py --model_path ./model_files/model.onnx --image_path ./test_images/test.jpg --device cpu --framework onnx --save_output cpu_output.npy
python accuracy_run.py --model_path ./model_files/model.onnx --image_path ./test_images/test.jpg --device npu --framework torch --save_output npu_output.npy
python compare_outputs.py --cpu_output cpu_output.npy --npu_output npu_output.npy

推理结果

平台预测结果logitsprobabilities推理耗时
CPUreal[0.100, -0.010, -0.440, 0.319, -0.508][0.234, 0.210, 0.137, 0.292, 0.128]135 ms
NPUreal[0.100, -0.010, -0.440, 0.319, -0.508][0.234, 0.210, 0.137, 0.292, 0.128]250 ms

CPU/NPU 精度测试方法

  1. 使用同一张测试图像分别在 CPU(ONNX Runtime)和 NPU(onnx2torch + torch_npu)上进行推理
  2. 分别保存 CPU 和 NPU 输出的 logits 和 probabilities
  3. 计算 logits 的平均绝对误差(MAE)和相对误差(RelErr)
  4. 计算 probabilities 的平均绝对误差
  5. 对比两个平台输出的预测类别是否一致

CPU/NPU 精度测试结果

指标值
Logits MAE0.000064
Logits 相对误差0.12%
Prob MAE0.000012
类别匹配率100%
分类结果一致性完全一致

明确结论

NPU 与 CPU 推理结果误差 < 1%

部署和推理方法

核心代码

import torch
import numpy as np
from PIL import Image
from torchvision import transforms

# 图像预处理
preprocess = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

def load_and_preprocess_image(image_path):
    img = Image.open(image_path).convert('RGB')
    input_tensor = preprocess(img).unsqueeze(0)
    return input_tensor

# CPU 推理(ONNX Runtime)
import onnxruntime
def cpu_inference(model_path, input_tensor):
    ort_session = onnxruntime.InferenceSession(model_path)
    inputs = {ort_session.get_inputs()[0].name: input_tensor.numpy()}
    outputs = ort_session.run(None, inputs)
    return torch.from_numpy(outputs[0])

# NPU 推理(onnx2torch + torch_npu)
import torch_npu
from onnx2torch import convert
def npu_inference(model_path, input_tensor):
    torch_model = convert(model_path)
    torch_model = torch_model.to('npu').eval()
    input_tensor = input_tensor.to('npu')
    with torch.no_grad():
        outputs = torch_model(input_tensor)
    return outputs.cpu()

# 后处理
CLASS_NAMES = ['stable_diffusion', 'midjourney', 'dalle', 'real', 'other_ai']
def get_prediction(logits):
    probs = torch.softmax(logits, dim=-1)
    pred_class = torch.argmax(probs, dim=-1).item()
    return CLASS_NAMES[pred_class], probs.numpy()

性能测试结果

平台推理耗时速度提升
CPU135 ms1.0x(基线)
NPU250 ms0.54x

注:该模型在 CPU 上使用 ONNX Runtime 进行推理,在 NPU 上使用 onnx2torch 转换后的 PyTorch 模型进行推理。NPU 推理耗时包含 ONNX 到 PyTorch 模型转换的额外开销以及 NPU 数据传输开销,同时 ONNX Runtime 在 CPU 上的推理优化较好,导致 NPU 在该模型上未体现出速度优势。对于追求性能的场景,建议进一步优化模型转换流程或使用昇腾 CANN 直接加载 ONNX 模型。

文件说明

文件说明
model_files/model.onnxONNX 格式模型文件
accuracy_run.py精度验证脚本
test_images/测试图像目录
README.md本文件

推理成功证据

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

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

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

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

模型标签

#+NPU #+CV #+图像分类 #+昇腾 #+AI检测 #+多分类 #+ViT #+ONNX