Views
No views yet
Swinv2ForImageClassificationmodel.onnx: FP32 (Full Precision)model_quantized.onnx: QUInt8 (Dynamic Quantization)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 |
onnxruntime library.pip install onnxruntime numpy pillow transformers1import onnxruntime as ort
2from transformers import AutoImageProcessor
3from PIL import Image
4import numpy as np
5import torch
6
7# 1. Load the model and processor
8model_path = "model_quantized.onnx" # or "model.onnx"
9processor = AutoImageProcessor.from_pretrained("haywoodsloan/ai-image-detector-dev-deploy")
10session = ort.InferenceSession(model_path, providers=['CPUExecutionProvider'])
11
12# 2. Prepare the image
13image = Image.open("path_to_your_image.jpg").convert("RGB")
14inputs = processor(images=image, return_tensors="np")
15
16# 3. Run Inference
17ort_inputs = {session.get_inputs()[0].name: inputs["pixel_values"]}
18logits = session.run(None, ort_inputs)[0]
19
20# 4. Process Outputs
21predictions = np.argmax(logits, axis=-1)
22labels = ["artificial", "real"]
23print(f"Prediction: {labels[predictions[0]]}")torch.onnx.export (opset 16). The quantized version was generated using dynamic QUInt8 quantization.value_info) that were causing conflicts in the SwinV2 graph during the onnxruntime shape inference pass.