)
Onnx模型前向验证说明下方代码对应的是模板代码onnx_path是对应onnx的路径data_path是pth模型前向保存的网络输入和网络输出输出的打印比较多可按照自己需求进行关闭importonnxruntimeimporttorchimportpickleimportnumpyasnpimportonnx onnx_path***.onnxdata_path***.pkl# 获取onnx网络的输出modelonnx.load(onnx_path)onnx.checker.check_model(model)outputmodel.graph.outputprint(output)# 使用onnxruntime进行前向推断importonnxruntime sessonnxruntime.InferenceSession(onnx_path)output_layers_name[aa,bb]# inputsinputspickle.load(open(data_path,rb))print(inputs.keys())dummy_input{key0:inputs[key0],key1:inputs[key1]}onnx_outputsess.run(output_layers_name,dummy_input)res_onnx{}print(**60)foriinrange(len(output_layers_name)):res_onnx[output_layers_name[i]]onnx_output[i]print(output_layers_name[i],res_onnx[output_layers_name[i]].shape)# compute diffprint(**60)print(begin to compute diff)foriinrange(len(output_layers_name)):res_onnx_tmpres_onnx[output_layers_name[i]].reshape(-1)res_pth_tmpinputs[output_layers_name[i]].cpu().numpy().reshape(-1)print(**60)print({} , onnx_res: {}, pth_res: {}.format(output_layers_name[i],res_onnx_tmp.shape,res_pth_tmp.shape))diffnp.sum(res_onnx_tmp.reshape(-1)-res_pth_tmp.reshape(-1))print({} diff: {}.format(output_layers_name[i],diff))