def forward(self, x):
# x = x.copy() # for profiling
z = [] # inference output
self.training |= self.export
for i in range(self.nl):
x[i] = self.m[i](x[i]).sigmoid() # conv
return x[0], x[1], x[2]
import os
import torch
import onnx
from onnxsim import simplify
import onnxoptimizer
import argparse
from models.yolo import Detect, Model
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--weights', type=str, default='./weights/yolov7.pt', help='initial weights path')
parser.add_argument('--cfg', type=str, default='./cfg/deploy/yolov7.yaml', help='initial weights path')
#================================================================
opt = parser.parse_args()
print(opt)
#Save Only weights
ckpt = torch.load(opt.weights, map_location=torch.device('cpu'))
torch.save(ckpt['model'].state_dict(), opt.weights.replace(".pt", "-model.pt"))
#Load model without postprocessing
new_model = Model(opt.cfg)
new_model.load_state_dict(torch.load(opt.weights.replace(".pt", "-model.pt"), map_location=torch.device('cpu')), False)
new_model.eval()
#save to JIT script
example = torch.rand(1, 3, 640, 640)
traced_script_module = torch.jit.trace(new_model, example)
traced_script_module.save(opt.weights.replace(".pt", "-jit.pt"))
#save to onnx
f = opt.weights.replace(".pt", ".onnx")
torch.onnx.export(new_model, example, f, verbose=False, opset_version=12,
training=torch.onnx.TrainingMode.EVAL,
do_constant_folding=True,
input_names=['data'],
output_names=['out0','out1','out2'])
#onnxsim
model_simp, check = simplify(f)
assert check, "Simplified ONNX model could not be validated"
onnx.save(model_simp, opt.weights.replace(".pt", "-sim.onnx"))
#optimize onnx
passes = ["extract_constant_to_initializer", "eliminate_unused_initializer"]
optimized_model = onnxoptimizer.optimize(model_simp, passes)
onnx.checker.check_model(optimized_model)
onnx.save(optimized_model, opt.weights.replace(".pt", "-op.onnx"))
print('finished exporting onnx')
Namespace(cfg='./cfg/deploy/yolov7.yaml', weights='./weights/yolov7.pt')
finished exporting onnx
import onnx
model = onnx.load("./weights/yolov7-op.onnx")
graph = model.graph
# print(onnx.helper.printable_graph(graph)) # 输出onnx的计算图
for index,n in enumerate(graph.node):
if n.name in ["Add_291","Add_298","Add_305"]:
graph.node.remove(n)
if n.name == "Add_289":
n.output[0] = "946"
if n.name == "Add_296":
n.output[0] = "955"
if n.name == "Add_303":
n.output[0] = "964"
for index,n in enumerate(graph.node):
print(n.name)
onnx.checker.check_model(model)
onnx.save(model,"./weights/yolov7-op.onnx")
节点中Add(B=0)在转换caffe时会报错,直接删除节点, 并将下层输入替换为上层输出。