Add ONNX Runtime usage example
Browse files- examples/ort_usage_example.py +124 -0
examples/ort_usage_example.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
granite-docling ONNX Usage Example with ONNX Runtime
|
| 4 |
+
Demonstrates how to use the converted granite-docling model for document processing
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import onnxruntime as ort
|
| 8 |
+
import numpy as np
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import json
|
| 11 |
+
|
| 12 |
+
def load_granite_docling_onnx(model_path: str):
|
| 13 |
+
"""Load granite-docling ONNX model"""
|
| 14 |
+
print(f"Loading granite-docling ONNX model from: {model_path}")
|
| 15 |
+
|
| 16 |
+
session = ort.InferenceSession(model_path)
|
| 17 |
+
|
| 18 |
+
# Print model information
|
| 19 |
+
print("Model Information:")
|
| 20 |
+
print(f" Inputs:")
|
| 21 |
+
for inp in session.get_inputs():
|
| 22 |
+
print(f" {inp.name}: {inp.shape} ({inp.type})")
|
| 23 |
+
|
| 24 |
+
print(f" Outputs:")
|
| 25 |
+
for out in session.get_outputs():
|
| 26 |
+
print(f" {out.name}: {out.shape} ({out.type})")
|
| 27 |
+
|
| 28 |
+
return session
|
| 29 |
+
|
| 30 |
+
def preprocess_document_image(image_path: str) -> np.ndarray:
|
| 31 |
+
"""Preprocess document image for granite-docling inference"""
|
| 32 |
+
|
| 33 |
+
# Load and resize image to 512x512 (SigLIP2 requirement)
|
| 34 |
+
image = Image.open(image_path).convert('RGB')
|
| 35 |
+
image = image.resize((512, 512))
|
| 36 |
+
|
| 37 |
+
# Convert to numpy array and normalize
|
| 38 |
+
pixel_values = np.array(image).astype(np.float32) / 255.0
|
| 39 |
+
|
| 40 |
+
# Normalize using SigLIP2 parameters (from preprocessor_config.json)
|
| 41 |
+
mean = np.array([0.485, 0.456, 0.406])
|
| 42 |
+
std = np.array([0.229, 0.224, 0.225])
|
| 43 |
+
|
| 44 |
+
pixel_values = (pixel_values - mean) / std
|
| 45 |
+
|
| 46 |
+
# Reshape to [batch_size, channels, height, width]
|
| 47 |
+
pixel_values = pixel_values.transpose(2, 0, 1) # HWC -> CHW
|
| 48 |
+
pixel_values = pixel_values[np.newaxis, :] # Add batch dimension
|
| 49 |
+
|
| 50 |
+
return pixel_values
|
| 51 |
+
|
| 52 |
+
def create_text_inputs(prompt: str = "Convert this document to DocTags:") -> tuple:
|
| 53 |
+
"""Create text inputs for granite-docling"""
|
| 54 |
+
|
| 55 |
+
# Simple tokenization (in practice, use proper tokenizer)
|
| 56 |
+
# This is a simplified example - use actual granite-docling tokenizer
|
| 57 |
+
tokens = [1] + [i for i in range(2, len(prompt.split()) + 2)] + [2] # Simple token mapping
|
| 58 |
+
|
| 59 |
+
input_ids = np.array([tokens], dtype=np.int64)
|
| 60 |
+
attention_mask = np.ones((1, len(tokens)), dtype=np.int64)
|
| 61 |
+
|
| 62 |
+
return input_ids, attention_mask
|
| 63 |
+
|
| 64 |
+
def run_granite_docling_inference(session, image_path: str):
|
| 65 |
+
"""Run complete granite-docling inference"""
|
| 66 |
+
|
| 67 |
+
print(f"Processing document: {image_path}")
|
| 68 |
+
|
| 69 |
+
# Prepare inputs
|
| 70 |
+
pixel_values = preprocess_document_image(image_path)
|
| 71 |
+
input_ids, attention_mask = create_text_inputs()
|
| 72 |
+
|
| 73 |
+
print(f"Input shapes:")
|
| 74 |
+
print(f" pixel_values: {pixel_values.shape}")
|
| 75 |
+
print(f" input_ids: {input_ids.shape}")
|
| 76 |
+
print(f" attention_mask: {attention_mask.shape}")
|
| 77 |
+
|
| 78 |
+
# Run inference
|
| 79 |
+
outputs = session.run(None, {
|
| 80 |
+
'pixel_values': pixel_values,
|
| 81 |
+
'input_ids': input_ids,
|
| 82 |
+
'attention_mask': attention_mask
|
| 83 |
+
})
|
| 84 |
+
|
| 85 |
+
logits = outputs[0]
|
| 86 |
+
print(f"Output logits shape: {logits.shape}")
|
| 87 |
+
|
| 88 |
+
# Decode logits to tokens (simplified)
|
| 89 |
+
predicted_tokens = np.argmax(logits, axis=-1)
|
| 90 |
+
print(f"Predicted tokens shape: {predicted_tokens.shape}")
|
| 91 |
+
|
| 92 |
+
# In practice, decode tokens to DocTags markup using proper tokenizer
|
| 93 |
+
print("✅ Inference completed successfully")
|
| 94 |
+
|
| 95 |
+
return predicted_tokens
|
| 96 |
+
|
| 97 |
+
def main():
|
| 98 |
+
"""Main example usage"""
|
| 99 |
+
|
| 100 |
+
model_path = "model.onnx" # Path to downloaded ONNX model
|
| 101 |
+
|
| 102 |
+
try:
|
| 103 |
+
# Load model
|
| 104 |
+
session = load_granite_docling_onnx(model_path)
|
| 105 |
+
|
| 106 |
+
# Run inference on example document
|
| 107 |
+
# (Replace with actual document image path)
|
| 108 |
+
image_path = "example_document.png"
|
| 109 |
+
|
| 110 |
+
if os.path.exists(image_path):
|
| 111 |
+
result = run_granite_docling_inference(session, image_path)
|
| 112 |
+
print("✅ granite-docling ONNX inference successful!")
|
| 113 |
+
else:
|
| 114 |
+
print("⚠️ No example document provided")
|
| 115 |
+
print(" Create a test document image to run inference")
|
| 116 |
+
|
| 117 |
+
except Exception as e:
|
| 118 |
+
print(f"❌ Example failed: {e}")
|
| 119 |
+
import traceback
|
| 120 |
+
traceback.print_exc()
|
| 121 |
+
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
import os
|
| 124 |
+
main()
|