Create onnx model with reshape node
See original GitHub issueHi 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:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
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
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)?
Hi @jcwchen
I can create the reshape node with fix shape input in the model.