PandA-2024.02
e2_onnx_build.py
Go to the documentation of this file.
1 import numpy as np
2 
3 import onnx
4 from onnx import helper, shape_inference, optimizer
5 from onnx import numpy_helper
6 from onnx import AttributeProto, TensorProto, GraphProto
7 
8 # Create graph input X
9 X = helper.make_tensor_value_info('X', TensorProto.FLOAT, [1, 1, 32, 32])
10 
11 W_info = helper.make_tensor_value_info('W', TensorProto.FLOAT, [5, 1, 3, 3])
12 W = np.array([[[[-1,-1,-1],[-1,8.1,-1],[-1,-1,-1]]],[[[0,-1,0],[-1,5,-1],[0,-1,0]]],[[[-2,-1,0],[-1,1,1],[0,1,2]]],[[[1,2,1],[0,0,0],[-1,-2,-1]]],[[[1,0,-1],[2,0,-2],[1,0,-1]]]]).astype(np.float32)
13 W = numpy_helper.from_array(W, 'W')
14 
15 
16 # Create graph output Y
17 Y = helper.make_tensor_value_info('Y', TensorProto.FLOAT, [1, 5, 32, 32])
18 
19 conv1 = helper.make_node(
20  'Conv', # name
21  ['X', 'W'], # inputs
22  ['Y'], # outputs
23 
24  kernel_shape=[3, 3],
25  strides=[1, 1],
26  pads=[1, 1, 1, 1],
27  )
28 
29 
30 graph_def = helper.make_graph(
31  nodes=[conv1], # graph nodes
32  name= 'conv_model', # graph name
33  inputs = [X, W_info], # graph inputs
34  outputs = [Y], # graph outputs
35  initializer = [W],
36  )
37 
38 model_def = helper.make_model(graph_def, producer_name='benchmarks')
39 
40 onnx.checker.check_model(model_def)
41 model_def = shape_inference.infer_shapes(model_def)
42 onnx.checker.check_model(model_def)
43 model_def = optimizer.optimize(model_def)
44 onnx.checker.check_model(model_def)
45 
46 onnx.save_model(model_def, 'e2_conv.onnx')

Generated on Mon Feb 12 2024 13:02:50 for PandA-2024.02 by doxygen 1.8.13