x = torch.randn(1, 3, 416, 416)
input_names = ["input"]
out_names = ["output"]
INPUT_DICT = ""#pth模型路径
OUT_ONNX = ""#onnx模型路径
#定义网络结构
model = Network()
#加载参数
model_dict = model.state_dict()
pretrained_dict = torch.load(INPUT_DICT, map_location=torch.device('cpu'))
pretrained_dict = {k: v for k, v in pretrained_dict.items() if np.shape(model_dict[k]) == np.shape(v)}
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
#gpu
# if torch.cuda.device_count() > 1:
# print("let's use", torch.cuda.device_count(), "Gpus!")
# model = DataParallel(model)
model.eval()
torch.onnx.export(model, x, OUT_ONNX, export_params=True, training=False, input_names=input_names, output_names=out_names, opset_version=11,do_constant_folding=False)
#print('please run: python3 -m onnxsim test.onnx ?test_sim.onnx\n')#??????????????????????????????????????
print('convert done!\n')