import onnx model = onnx.load( "./test_dssm_part.onnx" ) graph = model.graph node = graph.node # initializer = graph.initializer # print("============ initializer[1] ============") # print(initializer[1]) # print("============ initializer[2]============") # print(initializer[2]) # old_const_fold_opt__23 = initializer[1] # old_const_fold_opt__22 = initializer[2] # """ # make_tensor(name, data_type, dims, vals, raw=False) # Make a TensorProto with specified arguments. If raw is False, this # function will choose the corresponding proto field to store the # values based on data_type. If raw is True, use "raw_data" proto # field to store the values, and values should be of type bytes in # this case. # """ # new_const_fold_opt__23 = onnx.helper.make_tensor( # name="const_fold_opt__23", # data_type=onnx.TensorProto.INT64, # dims=[4], # vals=b"\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\031\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000" # raw=True, # ) # graph.node.remove(old_const_fold_opt__23) # graph.node.insert(1, new_const_fold_opt__23) print ( "============ node[12] ============" ) print (node[ 12 ]) print ( "============ node[34] ============" ) print (node[ 34 ]) old_tile_1 = node[ 12 ] old_tile = node[ 34 ] """ op_type (string): The name of the operator to construct inputs (list of string): list of input names outputs (list of string): list of output names name (string, default None): optional unique identifier for NodeProto doc_string (string, default None): optional documentation string for NodeProto domain (string, default None): optional domain for NodeProto. If it's None, we will just use default domain (which is empty) **kwargs (dict): the attributes of the node. The acceptable values are documented in :func:`make_attribute`. """ new_tile_const = onnx.helper.make_node( name = "tile_const" , op_type = "Constant" , inputs = [], outputs = [ "tile_const:0" ], value = onnx.helper.make_tensor( 'value' , onnx.TensorProto.INT64, [ 4 ], b "\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\031\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000" , raw = True ) ) new_tile = onnx.helper.make_node( name = "Tile" , op_type = "Tile" , inputs = [ "Reshape_1:0" , "tile_const:0" ], outputs = [ "Tile:0" ], ) graph.node.remove(old_tile) graph.node.insert( 34 , new_tile) graph.node.insert( 34 , new_tile_const) new_tile_1_const = onnx.helper.make_node( name = "tile_1_const" , op_type = "Constant" , inputs = [], outputs = [ "tile_1_const:0" ], value = onnx.helper.make_tensor( 'value' , onnx.TensorProto.INT64, [ 3 ], b "\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000" , raw = True ) ) new_tile_1 = onnx.helper.make_node( name = "Tile_1" , op_type = "Tile" , inputs = [ "ExpandDims:0" , "tile_1_const:0" ], outputs = [ "Tile_1:0" ], ) graph.node.remove(old_tile_1) graph.node.insert( 12 , new_tile_1) graph.node.insert( 12 , new_tile_1_const) onnx.checker.check_model(model) onnx.save(model, "./test_dssm_part_modified.onnx" ) |