RuntimeError
onnxruntime.capi.onnxruntime_pybind11_state.RuntimeError
Stack trace
onnxruntime.capi.onnxruntime_pybind11_state.RuntimeError: [ONNXRuntimeError] : 1 : FAIL : Load model failed:Input shape mismatch: model expects [1,3,224,224] but got [1,3,256,256]
Why it happens
ONNX models require input tensors to have exact shapes as defined in the model's input signature. If the input tensor shape differs, ONNX Runtime cannot bind the input properly, causing a RuntimeError. This often happens when preprocessing steps produce images or tensors with unexpected dimensions.
Detection
Validate input tensor shapes against the model's input signature before inference. Log input shapes and compare them to the expected shapes to catch mismatches early.
Causes & fixes
Input tensor shape does not match the ONNX model's expected input shape.
Resize or reshape your input data to exactly match the model's input dimensions before passing it to the ONNX Runtime session.
Preprocessing pipeline outputs images or tensors with incorrect dimensions (e.g., wrong resize or crop).
Adjust your preprocessing code to resize and normalize inputs to the exact shape the model expects, such as using cv2.resize or PIL resize with correct parameters.
Model was exported with a fixed input shape but inference code uses dynamic or different shapes.
Either export the model with dynamic axes to support variable input sizes or ensure inference inputs strictly follow the fixed shape used during export.
Code: broken vs fixed
import onnxruntime
import numpy as np
session = onnxruntime.InferenceSession('model.onnx')
input_name = session.get_inputs()[0].name
input_data = np.random.randn(1, 3, 256, 256).astype(np.float32)
# This line triggers the shape mismatch error
outputs = session.run(None, {input_name: input_data}) import os
import onnxruntime
import numpy as np
# No hardcoded keys needed for ONNX Runtime
session = onnxruntime.InferenceSession('model.onnx')
input_name = session.get_inputs()[0].name
# Reshape input to expected shape (1, 3, 224, 224)
input_data = np.random.randn(1, 3, 224, 224).astype(np.float32)
outputs = session.run(None, {input_name: input_data})
print('Inference succeeded with correct input shape') Workaround
Catch the RuntimeError exception, log the input tensor shape, and attempt to resize or reshape the input dynamically before retrying inference.
Prevention
Always verify and enforce input tensor shapes match the ONNX model's input signature by integrating shape validation and preprocessing checks in your inference pipeline.