1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| import onnx import onnx_graphsurgeon as gs import os import argparse
def modify_onnx(input_model, output_model=None): if output_model is None: filename, ext = os.path.splitext(input_model) output_model = f"{filename}_dla{ext}" graph = gs.import_onnx(onnx.load(input_model)) removed_reshape = False if len(graph.inputs) > 0 and len(graph.nodes) > 0: first_node = None for node in graph.nodes: if graph.inputs[0] in node.inputs: first_node = node break if first_node is not None and first_node.op == "Reshape": reshape_output = first_node.outputs[0] new_input = gs.Variable( name=graph.inputs[0].name, dtype=reshape_output.dtype, shape=reshape_output.shape ) for node in graph.nodes: for i, inp in enumerate(node.inputs): if inp == reshape_output: node.inputs[i] = new_input graph.inputs = [new_input] + graph.inputs[1:] graph.nodes.remove(first_node) removed_reshape = True print(f"已删除第一个Reshape节点,将输入shape设置为 {new_input.shape}") consecutive_reshape_removed = 0 reshape_nodes = [node for node in graph.nodes if node.op == "Reshape"] output_to_node = {} for node in graph.nodes: for inp in node.inputs: if inp.name: output_to_node[inp.name] = node nodes_to_remove = [] for reshape1 in reshape_nodes: if reshape1 in nodes_to_remove: continue reshape1_output = reshape1.outputs[0] if reshape1_output.name in output_to_node: reshape2 = output_to_node[reshape1_output.name] if reshape2.op == "Reshape" and reshape2 not in nodes_to_remove: reshape2_output = reshape2.outputs[0] reshape1_input = reshape1.inputs[0] if reshape1_input.shape == reshape2_output.shape: for node in graph.nodes: for i, inp in enumerate(node.inputs): if inp == reshape2_output: node.inputs[i] = reshape1_input nodes_to_remove.extend([reshape1, reshape2]) consecutive_reshape_removed += 1 print(f"找到并移除连续Reshape节点: {reshape1.name} -> {reshape2.name}") for node in nodes_to_remove: graph.nodes.remove(node) if consecutive_reshape_removed > 0: print(f"总共移除了 {consecutive_reshape_removed} 对连续的Reshape节点") resize_count = 0 target_attrs = { "coordinate_transformation_mode": "asymmetric", "cubic_coeff_a": -0.75, "mode": "nearest", "nearest_mode": "floor" } for node in graph.nodes: if node.op == "Resize": resize_count += 1 node.attrs.update(target_attrs) print(f"修改了Resize节点: {node.name}") if len(node.inputs) >= 4 and node.inputs[3] is not None: sizes = node.inputs[3] if isinstance(sizes, gs.Constant) and sizes.values is not None: sizes_values = sizes.values if len(sizes_values) > 0 and sizes_values[0] != 1: old_size = int(sizes_values[0]) sizes_values[0] = 6 sizes.values = sizes_values print(f" 修改了Resize节点 {node.name} 的目标尺寸batchsize从 {old_size} 到 6") print(f"总共修改的Resize节点数: {resize_count}")
batch_modified = False for i, inp in enumerate(graph.inputs): if inp.shape is not None and len(inp.shape) > 0: new_shape = list(inp.shape) if new_shape[0] is not None and new_shape[0] != 6: old_shape = new_shape[0] new_shape[0] = 6 inp.shape = tuple(new_shape) print(f"输入 '{inp.name}' 的batchsize从 {old_shape} 修改为 6") batch_modified = True for i, out in enumerate(graph.outputs): if out.shape is not None and len(out.shape) > 0: new_shape = list(out.shape) if new_shape[0] is not None and new_shape[0] != 1: old_shape = new_shape[0] new_shape[0] = 1 out.shape = tuple(new_shape) print(f"输出 '{out.name}' 的batchsize从 {old_shape} 修改为 6") batch_modified = True if batch_modified: print("已完成输入/输出batchsize修改为1") graph.cleanup() output_onnx = gs.export_onnx(graph) onnx.save(output_onnx, output_model) print(f"修改后的模型保存至: {output_model}") return resize_count, removed_reshape, consecutive_reshape_removed, batch_modified
if __name__ == "__main__": parser = argparse.ArgumentParser(description="修改ONNX模型") parser.add_argument("input_model", help="输入ONNX模型的路径") parser.add_argument("-o", "--output_model", help="保存修改后ONNX模型的路径", default=None) args = parser.parse_args() resize_count, removed_reshape, consecutive_reshape_removed, batch_modified = modify_onnx(args.input_model, args.output_model) if resize_count == 0 and not removed_reshape and consecutive_reshape_removed == 0 and not batch_modified: print("模型未进行任何修改。")
|