PandA-2024.02
04_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])
10 
11 W_info = helper.make_tensor_value_info('W', TensorProto.FLOAT, [1,8])
12 W = np.ones((1,8)).astype(np.float32)
13 W = numpy_helper.from_array(W, 'W')
14 
15 B_info = helper.make_tensor_value_info('B', TensorProto.FLOAT, [8])
16 B = np.ones((8)).astype(np.float32)
17 B = numpy_helper.from_array(B, 'B')
18 
19 # Create graph output Y
20 Z = helper.make_tensor_value_info('Z', TensorProto.FLOAT, [8])
21 
22 matmul1 = helper.make_node(
23  'MatMul', # name
24  ['X', 'W'], # inputs
25  ['Y'], # outputs
26  )
27 
28 bias1 = helper.make_node(
29  'Add',
30  ['Y', 'B'],
31  ['Z'],
32  )
33 
34 
35 graph_def = helper.make_graph(
36  nodes=[matmul1, bias1], # graph nodes
37  name= 'dense_a_model', # graph name
38  inputs = [X, W_info, B_info], # graph inputs
39  outputs = [Z], # graph outputs
40  initializer = [W, B],
41  )
42 
43 model_def = helper.make_model(graph_def, producer_name='benchmarks')
44 
45 onnx.checker.check_model(model_def)
46 model_def = shape_inference.infer_shapes(model_def)
47 onnx.checker.check_model(model_def)
48 model_def = optimizer.optimize(model_def)
49 onnx.checker.check_model(model_def)
50 
51 onnx.save_model(model_def, '04_dense_a.onnx')

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