When using ONNX Runtime for inference, the main goal is to get fast and accurate predictions from your model. The key metrics to check are inference latency (how fast the model predicts) and prediction accuracy (how correct the predictions are). Latency matters because ONNX Runtime is often used to speed up models in real-time apps. Accuracy matters because a faster model is useless if it gives wrong answers.
ONNX Runtime inference in PyTorch - Model Metrics & Evaluation
Suppose your ONNX model predicts if an email is spam or not. After running inference on 100 emails, you get:
| Predicted Spam | Predicted Not Spam |
|----------------|--------------------|
| True Positives (TP) = 40 | False Negatives (FN) = 10 |
| False Positives (FP) = 5 | True Negatives (TN) = 45 |
This matrix helps calculate precision, recall, and accuracy to check if ONNX Runtime inference keeps prediction quality.
Imagine your ONNX model detects fraud in transactions. If you want to avoid missing fraud cases, focus on high recall. But if you want to avoid false alarms, focus on high precision. ONNX Runtime helps by making inference fast, so you can try different thresholds quickly to find the best balance.
For example, increasing recall might catch more fraud but also flag more good transactions wrongly. ONNX Runtime lets you test these tradeoffs efficiently.
Good: Low latency (e.g., under 10 milliseconds per prediction), accuracy close to the original model (e.g., 95%+), and balanced precision and recall depending on use case.
Bad: High latency (e.g., over 100 milliseconds), accuracy drop (e.g., below 80%), or very low precision or recall indicating poor prediction quality after conversion to ONNX.
- Accuracy drop: Sometimes converting to ONNX changes model behavior, causing lower accuracy.
- Ignoring latency: Focusing only on accuracy and ignoring inference speed misses the main benefit of ONNX Runtime.
- Data leakage: Testing on data used in training inflates accuracy falsely.
- Overfitting signs: Very high accuracy on test but poor real-world results means model or data issues, not ONNX Runtime.
Your ONNX Runtime inference model has 98% accuracy but only 12% recall on fraud detection. Is it good for production? Why or why not?
Answer: No, it is not good. Even though accuracy is high, the very low recall means the model misses most fraud cases. For fraud detection, recall is critical because missing fraud is costly. You should improve recall before using this model in production.
Practice
Solution
Step 1: Understand ONNX Runtime's role
ONNX Runtime is designed to run machine learning models efficiently across various hardware.Step 2: Compare options
Training or converting to TensorFlow is not the main purpose; visualization is unrelated.Final Answer:
To run the model faster and on different devices -> Option CQuick Check:
ONNX Runtime = fast, cross-device inference [OK]
- Confusing inference with training
- Thinking ONNX Runtime converts models to other frameworks
- Assuming it is for visualization
Solution
Step 1: Recall ONNX Runtime loading syntax
ONNX Runtime uses InferenceSession to load ONNX models.Step 2: Check each option
session = onnxruntime.InferenceSession('model.onnx') uses correct syntax. session = onnx.load('model.onnx') is for ONNX library, not runtime. session = torch.load('model.onnx') is PyTorch loading, not ONNX. session = onnxruntime.load_model('model.onnx') is invalid method.Final Answer:
session = onnxruntime.InferenceSession('model.onnx') -> Option DQuick Check:
InferenceSession loads ONNX models [OK]
- Using torch.load for ONNX files
- Confusing onnx.load with onnxruntime
- Trying non-existent load_model method
print(output)?
import onnxruntime
import numpy as np
session = onnxruntime.InferenceSession('model.onnx')
input_name = session.get_inputs()[0].name
input_data = np.array([[1.0, 2.0, 3.0]], dtype=np.float32)
output = session.run(None, {input_name: input_data})
print(output)Solution
Step 1: Understand session.run output
session.run returns a list of numpy arrays with model outputs.Step 2: Analyze code behavior
Input is correctly prepared as numpy array; no error occurs; output is a list with prediction arrays.Final Answer:
A list containing the model's prediction array -> Option AQuick Check:
session.run returns list of outputs [OK]
- Expecting a single numpy array instead of list
- Thinking input must be a PyTorch tensor
- Assuming session.run returns None
TypeError: expected np.ndarray (got list). What is the likely cause?Solution
Step 1: Interpret the error message
The error says input expected is numpy array but got list.Step 2: Identify cause
Passing input as Python list instead of np.ndarray causes this error.Final Answer:
Input data was passed as a Python list instead of a numpy array -> Option BQuick Check:
Input must be numpy array, not list [OK]
- Passing Python lists directly as input
- Assuming model file corruption causes this error
- Ignoring session creation errors
Solution
Step 1: Check input shape requirements
Model expects (batch_size, 3, 224, 224) so batch size 5 means shape (5,3,224,224).Step 2: Validate each option's input shape and type
import numpy as np batch_input = np.random.rand(5, 3, 224, 224).astype(np.float32) output = session.run(None, {input_name: batch_input}) uses correct shape and numpy float32 array. batch_input = torch.rand(5, 3, 224, 224) output = session.run(None, {input_name: batch_input}) uses torch tensor (not accepted). batch_input = np.random.rand(3, 224, 224).astype(np.float32) output = session.run(None, {input_name: batch_input}) misses batch dimension. batch_input = np.random.rand(5, 224, 224, 3).astype(np.float32) output = session.run(None, {input_name: batch_input}) swaps channel and width/height.Final Answer:
import numpy as np batch_input = np.random.rand(5, 3, 224, 224).astype(np.float32) output = session.run(None, {input_name: batch_input}) -> Option AQuick Check:
Correct shape and numpy array input [OK]
- Using torch tensors instead of numpy arrays
- Incorrect input shape order
- Missing batch dimension
Ready to prove it?
Pass the test and this pattern moves unknown
