当前位置:   article > 正文

python ONNXRuntime的使用例子_onnxruntime python推理实例

onnxruntime python推理实例
import onnx
import numpy as np
import onnxruntime as rt
import cv2
import time

model_path = './lite_hrnet_30_384x288_coco.onnx'
onnx_model = onnx.load(model_path)
onnx.checker.check_model(onnx_model)

sess = rt.InferenceSession(model_path)
# sess.set_providers(["TensorrtExecutionProvider"])
sess.set_providers(["CPUExecutionProvider"])
# sess.set_providers(["CUDAExecutionProvider"])

image = cv2.imread("hrnet_demo.jpg")
image = cv2.resize(image, (288,384))
image = image.astype(np.float32)/255.0

image = image.transpose(2,0,1)
image = np.array(image)[np.newaxis, :, :, :]
print(image.shape)

input_name_1 = sess.get_inputs()[0].name
output_name_1 = sess.get_outputs()[0].name
output_name_2 = sess.get_outputs()[1].name

print("input_name_1:",input_name_1)
print("output_name_1:",output_name_1)
print("output_name_2:",output_name_2)

i=0
while i<10:
    start = time.time()
    output = sess.run([output_name_1,output_name_2], {(input_name_1): image})
    print('spend time:',(time.time()-start)*1000.0)

    i+=1





  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/850043
推荐阅读
相关标签
  

闽ICP备14008679号