Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Microsoft Onnxruntime Input Data Preparation

From Leeroopedia


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., float32 for tensor(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 to numpy.float32
  • tensor(double) maps to numpy.float64
  • tensor(int64) maps to numpy.int64
  • tensor(int32) maps to numpy.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}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment