openvinoのPythonバージョンを使用する

優れたCPU推論エンジンとしてのopenvino

ここでPythonバージョンのAPIを使用します

使用する前にopenvinoをコンパイルしてインストールする必要があります

https://docs.openvinotoolkit.org/latest/openvino_docs_get_started_get_started_windows.html

インストール後、openvinoのPythonバージョンをインストールするためにopenvinoを直接pipインストールします

実際、APIのPythonバージョンはC ++でコンパイルされたopenvinoとも呼ばれます。そのため、Pythonバージョンを使用し、openvinoをコンパイルしてインストールする必要もあります

次に使用できます。使用する前にopenvinoモデルファイルを転送する必要があります。もちろん、転送する必要はありません。Openvinoはonnxの読み取りをサポートしています。

python /opt/intel/openvino_2021/deployment_tools/model_optimizer/mo.py --input_model ctdet_coco_dlav0_512.onnx --output_dir ./ctdet_coco_dlav0_512  --data_type FP16

onnxモデルはここからですhttps://download.01.org/opencv/public_models/122019/ctdet_coco_dlav0/

これで推論を実行できます。最初にonnxファイルを使用して直接推論します

from openvino.inference_engine import IECore
import numpy as np
import cv2
import time

ie = IECore()
model="ctdet_coco_dlav0_512.onnx"
#model="ctdet_coco_dlav0_512/ctdet_coco_dlav0_512.xml"
net = ie.read_network(model=model)
input_blob = next(iter(net.input_info))
out_blob = next(iter(net.outputs))
net.batch_size=16#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
exec_net = ie.load_network(network=net, device_name="CPU")
start=time.time()
res = exec_net.infer(inputs={input_blob: images})
#print(res)
print('infer total time is %.4f s'%(time.time()-start))

演算結果:

 上記のコードで設定したバッチサイズは16です。ここで、batchsizeの結果を1として試して、net.batch_size = 1を変更するだけです。

 

次に、onnxによって転送されたファイルを推論に使用します。

from openvino.inference_engine import IECore
import numpy as np
import cv2
import time

ie = IECore()
#model="ctdet_coco_dlav0_512.onnx"
model="ctdet_coco_dlav0_512/ctdet_coco_dlav0_512.xml"
net = ie.read_network(model=model)
input_blob = next(iter(net.input_info))
out_blob = next(iter(net.outputs))
net.batch_size=16#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
exec_net = ie.load_network(network=net, device_name="CPU")
start=time.time()
res = exec_net.infer(inputs={input_blob: images})
#print(res)
print('infer total time is %.4f s'%(time.time()-start))

運転結果

同様に、net.batch_size = 1の操作結果:

onnxを使用した推論時間は、変換された推論時間と基本的に同じであることがわかります。

おすすめ

転載: blog.csdn.net/zhou_438/article/details/112846704