Principle:Microsoft Onnxruntime Input Data Preparation
Metadata
| Field | Value |
|---|---|
| Principle Name | Input_Data_Preparation |
| Repository | Microsoft_Onnxruntime |
| Source Repository | https://github.com/microsoft/onnxruntime |
| Domain | ML_Inference, Model_Optimization |
| Last Updated | 2026-02-10 |
| Workflow | Python_Inference_Pipeline |
| Pair | 4 of 6 |
Overview
Construction of properly typed and shaped input tensors to match an ONNX model's input specification.
Description
ONNX Runtime requires input data as numpy arrays with specific dtypes and shapes matching the model's declared inputs. The input feed is a dictionary mapping input names (as strings) to numpy arrays. Correct dtype matching is critical -- the most common type is numpy.float32, corresponding to the ONNX type tensor(float).
This is an External Tool Doc for numpy, as the data preparation step relies entirely on numpy for array construction and type casting. The pattern is demonstrated at docs/python/examples/plot_load_and_predict.py:L52-53.
Key requirements for input data:
- Shape matching -- The numpy array dimensions must match the model's declared input shape, respecting any fixed dimensions.
- Type matching -- The numpy dtype must correspond to the ONNX tensor type (e.g.,
float32fortensor(float)). - Name matching -- The dictionary keys must match the input names returned by
session.get_inputs().
Theoretical Basis
The ONNX specification defines a type system for tensors that maps to numpy dtypes. The most common mappings are:
tensor(float)maps tonumpy.float32tensor(double)maps tonumpy.float64tensor(int64)maps tonumpy.int64tensor(int32)maps tonumpy.int32
The input feed dictionary structure allows models with multiple named inputs, where each input may have a different shape and type. This design supports complex models with heterogeneous input requirements.
Type mismatches between the numpy array dtype and the model's expected type will result in runtime errors. The .astype() method on numpy arrays is used to ensure correct type casting before inference.
Usage
Input data is constructed using numpy and organized into a dictionary keyed by input names:
import numpy
x = numpy.random.random((3, 4, 5)).astype(numpy.float32)
input_feed = {input_name: x}