SwinV2 AI Image Detector (ONNX)

This repository contains ONNX versions of the haywoodsloan/ai-image-detector-deploy model, which is based on the SwinV2 architecture and fine-tuned for classifying images as artificial (AI-generated) or real.

Model Details

  • Architecture: Swinv2ForImageClassification
  • Precision:
    • model.onnx: FP32 (Full Precision)
    • model_quantized.onnx: QUInt8 (Dynamic Quantization)
  • Input Size: 3x256x256
  • Num Labels: 2 (artificial, real)

Performance Metrics (CPU Inference)

Testing was performed on an Apple M-series CPU using onnxruntime.

Model Type File Size Latency (Batch 1) Speedup vs PyTorch Cosine Similarity
Original PyTorch 745 MB 234.0 ms 1.00x -
ONNX FP32 763 MB 151.0 ms 1.53x 1.000000
ONNX QUInt8 205 MB 183.8 ms 1.27x 0.999992

Note: The QUInt8 model provides a ~73% reduction in file size while maintaining extremely high accuracy.

How to use (Inference)

You can use these models with the onnxruntime library.

Installation

pip install onnxruntime numpy pillow transformers

Python Inference Code

import onnxruntime as ort
from transformers import AutoImageProcessor
from PIL import Image
import numpy as np
import torch

# 1. Load the model and processor
model_path = "model_quantized.onnx" # or "model.onnx"
processor = AutoImageProcessor.from_pretrained("haywoodsloan/ai-image-detector-dev-deploy")
session = ort.InferenceSession(model_path, providers=['CPUExecutionProvider'])

# 2. Prepare the image
image = Image.open("path_to_your_image.jpg").convert("RGB")
inputs = processor(images=image, return_tensors="np")

# 3. Run Inference
ort_inputs = {session.get_inputs()[0].name: inputs["pixel_values"]}
logits = session.run(None, ort_inputs)[0]

# 4. Process Outputs
predictions = np.argmax(logits, axis=-1)
labels = ["artificial", "real"]
print(f"Prediction: {labels[predictions[0]]}")

Conversion and Quantization

These models were converted using torch.onnx.export (opset 16). The quantized version was generated using dynamic QUInt8 quantization.

A specific fix was applied during quantization to strip intermediate shape annotations (value_info) that were causing conflicts in the SwinV2 graph during the onnxruntime shape inference pass.


license: mit

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Model tree for pranjal-pravesh/haywoodsloan-ai-image-detector

Unable to build the model tree, the base model loops to the model itself. Learn more.