Update the Weights in the ONNX Model
作者:XD / 发表: 2022年3月30日 22:30 / 更新: 2022年3月31日 00:46 / 编程笔记 / 阅读量:1859
Update the Weights in the ONNX Model
import onnx
onnx_path = "test.onnx"
onnx_model = onnx.load(onnx_path)
graph = onnx_model.graph
node = graph.node
for i in range(len(node)):
# get the node and check the node type
if node[i].op_type == 'Constant':
att = node[i].attribute[0]
# based on the node structure and data type to find the target
# It is better to print the structure, and then load it step by step.
if att and att.t and att.t.float_data:
temp = att.t.float_data[0]
node[i].attribute[0].t.float_data[0] = temp + 0.5
onnx.save(onnx_model, 'new_onnx.onnx')