onnx、openvino和mnn推理速度對比
阿新 • • 發佈:2021-01-22
技術標籤:深度學習mnnopenvinoonnxonnxruntime
onnx、openvino和mnn都可以作為cpu推理的框架,推理速度這裡進行對比一下
模型使用的python版openvino使用這篇 文章onnx檔案
注意:這裡使用的mnn和onnx檔案均未量化等操作,是直接轉換而來的,都是直接進行讀取的
對比程式碼如下:
from openvino.inference_engine import IECore import onnxruntime import MNN import numpy as np import cv2 import time model="ctdet_coco_dlav0_512.onnx" mnnmodel="ctdet_coco_dlav0_512.mnn" #openvino ie = IECore() net = ie.read_network(model=model) input_blob = next(iter(net.input_info)) out_blob = next(iter(net.outputs)) net.batch_size=1#batchsize n, c, h, w = net.input_info[input_blob].input_data.shape print(n, c, h, w) images = np.ndarray(shape=(n, c, h, w)) for i in range(n): image = cv2.imread("123.jpg") if image.shape[:-1] != (h, w): image = cv2.resize(image, (w, h)) image = image.transpose((2, 0, 1)) images[i] = image images=images.astype(np.float32) exec_net = ie.load_network(network=net, device_name="CPU") #onnxruntime session = onnxruntime.InferenceSession(model) inputs = {session.get_inputs()[0].name: images.astype(np.float32)} #mnn interpreter = MNN.Interpreter(mnnmodel) mnn_session = interpreter.createSession() input_tensor = interpreter.getSessionInput(mnn_session) tmp_input = MNN.Tensor((1, 3, 512, 512),\ MNN.Halide_Type_Float, images[0], MNN.Tensor_DimensionType_Tensorflow) input_tensor.copyFrom(tmp_input) #onnxruntime infer start=time.time() out=session.run(None, inputs) print('onnxruntime infer total time is %.4f s'%(time.time()-start)) #openvino infer start=time.time() res = exec_net.infer(inputs={input_blob: images}) #print(res) print('openvino infer total time is %.4f s'%(time.time()-start)) #mnn infer start=time.time() interpreter.runSession(mnn_session) print('mnn infer total time is %.4f s'%(time.time()-start))
執行結果:
結論:可以看到openvino確實優化速度很多,提升大於30%,mnn速度提升不明顯