Skip to content

select_model_inputs_outputs changes an output's shape metadata #857

Description

@jdaciuk

select_model_inputs_outputs seems to change the shape metadata of the output selected. The output itself is still OK.

For example

from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType
from skl2onnx.helpers.onnx_helper import select_model_inputs_outputs
from sklearn.ensemble import RandomForestClassifier
import numpy as np
import onnxruntime


# Get simple ONNX model
feature_names = "abcd"
X = np.random.normal(size=(500, len(feature_names)))
y = np.random.randint(0, 2, 500)
model = RandomForestClassifier(n_estimators=10)
model.fit(X, y)
# The output names of onnx_model will be ['output_label', 'output_probability']
onnx_model = convert_sklearn(
    model,
    initial_types=[("input", FloatTensorType((None, len(feature_names))))],
)

# print session output shape before selecting outputs
with open("model.onnx", "wb") as f:
    f.write(onnx_model.SerializeToString())
session = onnxruntime.InferenceSession("model.onnx", None)
print("Output 0 shape before using select_model_inputs_outputs")
print(session.get_outputs()[0].shape)

# print session output shape after selecting outputs
onnx_model = select_model_inputs_outputs(onnx_model, outputs=["output_label"])
with open("model.onnx", "wb") as f:
    f.write(onnx_model.SerializeToString())
session = onnxruntime.InferenceSession("model.onnx", None)
print("\nOutput 0 shape after using select_model_inputs_outputs")
print(session.get_outputs()[0].shape)

Prints

Output 0 shape before using select_model_inputs_outputs
[None]

Output 0 shape after using select_model_inputs_outputs
[]

It seems like any original output shape, not just [None], will go to [] after selecting it as an output.

Is this indeed just the output metadata that's being affected? Is there any workaround for now that would let me correct the shape after selecting model outputs?

The larger context is that I'm trying to serve my model with Triton, which complains that my model's output shape is [] or empty.

I'm using skl2onnx==1.11.1

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions