question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create onnx model with reshape node

See original GitHub issue

Hi All,

Currently, I create a simple ONNX model with reshape node with below code:

shape = [2,4]
shape2 = [2,4]

input_data = np.random.randn(*shape).astype(np.int64)

original_shape = [2, 3, 4]
input_data2 = np.random.random_sample(original_shape).astype(np.int64)

const_node = onnx.helper.make_node('Constant', inputs=[], outputs=['const_shape'],
                               value=onnx.helper.make_tensor(
                                   name='const_tensor',
                                   data_type=onnx.TensorProto.INT64,
                                   dims=input_data.shape,
                                   vals=input_data.flatten()))

node1 = onnx.helper.make_node('Identity', inputs=['add1'], outputs=['identity1'], name='identity_node1')
node2 = onnx.helper.make_node('Abs', inputs=['identity1'], outputs=['add2'], name='abs_node2')
node3 = onnx.helper.make_node('Identity', inputs=['add2'], outputs=['identity2'], name='identity_node2')
node4 = onnx.helper.make_node('Abs', inputs=['identity2'], outputs=['add3'], name='abs_node3')
node5 = onnx.helper.make_node('Identity', inputs=['add3'], outputs=['identity3'], name='identity_node3')
node6 = onnx.helper.make_node('Abs', inputs=['identity3'], outputs=['add4'], name='abs_node4')
node7 = onnx.helper.make_node('Identity', inputs=['add4'], outputs=['identity4'], name='identity_node4')
node8 = onnx.helper.make_node('Abs', inputs=['identity4'], outputs=['add5'], name='abs_node5')
node9 = onnx.helper.make_node('Add', inputs=['add5', 'identity3'], outputs=['add6'], name='add_node1')
node10 = onnx.helper.make_node('Identity', inputs=['add6'], outputs=['identity5'], name='identity_node5')
node11 = onnx.helper.make_node('Abs', inputs=['identity5'], outputs=['add7'], name='abs_node6')
node12 = onnx.helper.make_node('Reshape', inputs=['add7', 'const_shape'], outputs=['reshape'])

graph = onnx.helper.make_graph([const_node, node1, node2, node3, node4, node5, node6, node7, node8, node9, node10, node11, node12], 'test_graph',
                    [onnx.helper.make_tensor_value_info('add1', onnx.TensorProto.FLOAT, shape)],
                    [onnx.helper.make_tensor_value_info('reshape', onnx.TensorProto.FLOAT, shape2)])

model = onnx.helper.make_model(graph, producer_name='ngraph ONNX Importer')

onnx.save(model, 'model.onnx')

model_path = 'model.onnx'
session = onnxruntime.InferenceSession(model_path, None)
input_name1 = session.get_inputs()[0].name

a = np.random.rand(*shape).astype("float32")

print(a)
raw_result = session.run(None, {input_name1: a})

output_data = np.array(raw_result[0])
print(output_data)

the error log as below:

    sess = C.InferenceSession(session_options, self._model_path, True, self._read_config_from_model)
onnxruntime.capi.onnxruntime_pybind11_state.Fail: [ONNXRuntimeError] : 1 : FAIL : Load model from model.onnx failed:Node () Op (Reshape) [ShapeInferenceError] Invalid position of 0

I have refer the sample code https://www.programcreek.com/python/example/122583/onnx.helper.make_node to create onnx model with reshape node, but I still get error.

So could anyone show me the way to create onnx model with reshape node?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
hoaquocphancommented, Apr 19, 2021

Hi @jcwchen

Thank you for your support. I can create the simple model with reshape node and it can run successfully. I also have 2 questions need support from you:

1/ Please see my below snippet code

original_shape = [2, 3, 4]
shape_of_shape=[3,]
new_shape=[1, 1, 24]
node = onnx.helper.make_node('Reshape', inputs=['data', 'shape'], outputs=['reshaped'])
graph = onnx.helper.make_graph([node], 'test_graph',
                    [onnx.helper.make_tensor_value_info('data', onnx.TensorProto.FLOAT, original_shape), 
                    onnx.helper.make_tensor_value_info('shape', onnx.TensorProto.INT64, shape_of_shape)],
                    [onnx.helper.make_tensor_value_info('reshaped', onnx.TensorProto.FLOAT, new_shape)])
model = onnx.helper.make_model(graph, producer_name='ngraph ONNX Importer')
onnx.save(model, 'model.onnx')
model_path = 'model.onnx'
session = onnxruntime.InferenceSession(model_path, None)
input_name1 = session.get_inputs()[0].name
input_name2 = session.get_inputs()[1].name
data = np.random.random_sample(original_shape).astype(np.float32)
data_shape = np.array([4, 2, 3], dtype=np.int64)
raw_result = session.run(None, {input_name1: data, input_name2: data_shape})
output_data = np.array(raw_result[0])
print("data:")
print(data)
print("output data:")
print(output_data)

You can see that I can create the model with output shape [1, 1, 24] is: new_shape=[1, 1, 24] the model will define output of reshaped node is [1, 1, 24]

But when I feed the model with output shape [4, 2, 3]: data_shape = np.array([4, 2, 3], dtype=np.int64) The model create output with shape [4, 2, 3]

this mean, the output shape define in [onnx.helper.make_tensor_value_info(‘reshaped’, onnx.TensorProto.FLOAT, new_shape)]) doesn’t impact to the output shape of model. So, why we need to define the new_shape=[1, 1, 24] when create the model?

2/ Please get below models from my git repo: https://github.com/hoaquocphan/shared_files.git

my_model.onnx mobilenetv2-1.0.onnx

You can see that the reshape node in my_model.onnx (created by above code snippet) have 2 inputs but the reshape node in mobilenetv2-1.0.onnx model have only one input (the reshape_attr_tensor421 input is not output of any other node)

So How to create the model have reshape node as mobilenetv2-1.0.onnx (we fix the input shape to the model 1x1000, don’t need to feed the input shape 1x1000)?

0reactions
hoaquocphancommented, Apr 20, 2021

Hi @jcwchen

I can create the reshape node with fix shape input in the model.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating and Modifying ONNX Model Using ONNX Python API
ONNX model is represented using protocol buffers. ... It contains the node information, node initializers, and IO tensors in the model.
Read more >
Creating ONNX from scratch - Towards Data Science
ONNX provides an extremely flexible format to store AI/ML models and pipelines. To learn how, it's instructive to build an ONNX graph by...
Read more >
onnx/Operators.md at main - GitHub
Generate a tensor with given value and shape. Version. This version of the operator has been available since version 9 of the default...
Read more >
Play with ONNX operators — sklearn-onnx 1.11 documentation
Let's try the example given by ONNX documentation: ONNX Model Using Helper ... Preprocessing: create a model with two nodes, Y's shape is...
Read more >
ONNX with Python — onnxcustom - Xavier Dupré
The linear regression is the most simple model in machine learning described by ... make_node : creates a node defined by an operation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found