Skip to content

Mobilenet does not accept batched inputs #680

@Rohanjames1997

Description

@Rohanjames1997

Bug Report

Which model does this pertain to?

Mobilenet

Describe the bug

The documentation of mobilenet says that the expected input shape is (N x 3 x H x W), where N is the batch size, and H and W are expected to be at least 224.

But when downloading the mobilenetv2-7.onnx model and running a simple inference session on it, it fails when running on any batch size > 1

Reproduction instructions

System Information

OS Platform and Distribution: Ubuntu 22.04.5 LTS
ONNX version: 1.17.0
Backend/Runtime version: onnxruntime 1.21.0

Provide a code snippet to reproduce your errors.

import onnxruntime
import requests
import numpy as np

MODEL_PATH = 'mobilenet.onnx'
model_url = "https://github.com/onnx/models/raw/refs/heads/main/validated/vision/classification/mobilenet/model/mobilenetv2-7.onnx"

# Download the model
response = requests.get(model_url)
with open(MODEL_PATH, 'wb') as f:
    f.write(response.content)

# Load model
sess = onnxruntime.InferenceSession(MODEL_PATH)
input_name = sess.get_inputs()[0].name
input_shape = sess.get_inputs()[0].shape
input_type = sess.get_inputs()[0].type

print("Input shape:", input_shape)
print("Input type:", input_type)

# Create a batched input (batch size = 4)
batched_input = np.random.rand(4, 3, 224, 224).astype(np.float32)

# Try inference - should raise an error
try:
    output = sess.run(None, {input_name: batched_input})
except Exception as e:
    print("\n❌ Inference failed with batched input:")
    print(e)

Result

❌ Inference failed with batched input:
[ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Got invalid dimensions for input: data for the following indices
 index: 0 Got: 4 Expected: 1
 Please fix either the inputs/outputs or the model.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions