私的AI研究会 > OpenVINO3
4つのステップのプログラムからの呼び出しで簡単に使うことができる。
Inference Engine の中の IECore というクラスを使用する。
# モジュール読み込み from openvino.inference_engine import IECore
ターゲットデバイス(プロセッサ)の種類
NCS(Neural Compute Stick)は「MYRIAD」を引数に渡す。
指定は次のモデルの読み込みで行う。
modelファイル名がxxx.xmlでweightsファイル名がxxx.binだった場合、以下のコードで、読み込むことができる。
ie = IECore() net = ie.read_network(model='xxx.xml', weights='xxx.bin') exec_net = ie.load_network(network=net, device_name="MYRIAD")
推論実行は、ExecutableNetworkのメソッド infer を呼び出すだけで行われ、戻り値に結果が入る。
out = exec_net.infer(inputs)
emotions-recognition-retail-0003
pi@raspberrypi:~ $ mkdir workspace/FP16 pi@raspberrypi:~ $ cd workspace/FP16 pi@raspberrypi:~/workspace/FP16 $ wget --no-check-certificate https://download.01.org/opencv/2021/openvinotoolkit/2021.2/open_model_zoo/models_bin/3/emotions-recognition-retail-0003/FP16/emotions-recognition-retail-0003.bin --2020-12-28 13:36:28-- https://download.01.org/opencv/2021/openvinotoolkit/2021.2/open_model_zoo/models_bin/3/emotions-recognition-retail-0003/FP16/emotions-recognition-retail-0003.bin download.01.org (download.01.org) をDNSに問いあわせています... 2600:140b:d400:188::4b21, 2600:140b:d400:1ad::4b21, 104.118.81.176 : 2020-12-28 13:36:31 (1.78 MB/s) - `emotions-recognition-retail-0003.bin' へ保存完了 [4965014/4965014] pi@raspberrypi:~/workspace/FP16 $ wget --no-check-certificate https://download.01.org/opencv/2021/openvinotoolkit/2021.2/open_model_zoo/models_bin/3/emotions-recognition-retail-0003/FP16/emotions-recognition-retail-0003.xml --2020-12-28 13:36:54-- https://download.01.org/opencv/2021/openvinotoolkit/2021.2/open_model_zoo/models_bin/3/emotions-recognition-retail-0003/FP16/emotions-recognition-retail-0003.xml download.01.org (download.01.org) をDNSに問いあわせています... 2600:140b:d400:1ad::4b21, 2600:140b:d400:188::4b21, 104.118.81.176 : 2020-12-28 13:36:55 (1.35 MB/s) - `emotions-recognition-retail-0003.xml' へ保存完了 [37985/37985] pi@raspberrypi:~/workspace/FP16 $ ls -l 合計 4892 -rw-r--r-- 1 pi pi 4965014 12月 11 02:12 emotions-recognition-retail-0003.bin -rw-r--r-- 1 pi pi 37985 12月 11 02:12 emotions-recognition-retail-0003.xml
Inputs Name: input, shape: [1x3x64x64] - An input image in [1xCxHxW] format. Expected color order is BGR.
# 画像サイズを64x64にする img = cv2.resize(img, (64, 64)) # HWCからCHWに変更 img = img.transpose((2, 0, 1)) # 大きさ1の次元を追加し4次元にする。省略OK img = np.expand_dims(img, axis=0)
Outputs name: "prob", shape: [1, 5, 1, 1] - Softmax output across five emotions ('neutral', 'happy', 'sad', 'surprise', 'anger').
# 次元の削減 out = np.squeeze(out)
vi emotion1.py # -*- coding: utf-8 -*- import cv2 import numpy as np # モジュール読み込み from openvino.inference_engine import IECore # モデルの読み込み ie = IECore() net = ie.read_network(model='FP16/emotions-recognition-retail-0003.xml', weights='FP16/emotions-recognition-retail-0003.bin') exec_net = ie.load_network(network=net, device_name="MYRIAD") # 入力画像読み込み img = cv2.imread('./image/face.jpg') # 入力データフォーマットへ変換 img = cv2.resize(img, (64, 64)) # サイズ変更 img = img.transpose((2, 0, 1)) # HWC > CHW img = np.expand_dims(img, axis=0) # 次元合せ # 推論実行 out = exec_net.infer(inputs={'data': img}) # 出力 print(out)
pi@raspberrypi:~/workspace $ python3 emotion1.py {'prob_emotion': array([[[[0.8222656 ]], [[0.09936523]], [[0.04391479]], [[0.00532913]], [[0.02854919]]]], dtype=float32)}
: # 推論実行 out = exec_net.infer(inputs={'data': img}) # 出力から必要なデータのみ取り出し out = out['prob_emotion'] out = np.squeeze(out) #不要な次元の削 # 出力 print(out)
pi@raspberrypi:~/workspace $ python3 emotion1.py [0.8222656 0.09936523 0.04391479 0.00532913 0.02854919]
vi emotion2.py # -*- coding: utf-8 -*- import cv2 import numpy as np # モジュール読み込み from openvino.inference_engine import IECore # モデルの読み込み ie = IECore() net = ie.read_network(model='FP16/emotions-recognition-retail-0003.xml', weights='FP16/emotions-recognition-retail-0003.bin') exec_net = ie.load_network(network=net, device_name="MYRIAD") # 入力画像読み込み img_face = cv2.imread('./image/face.jpg') # 入力データフォーマットへ変換 img = cv2.resize(img_face, (64, 64)) # サイズ変更 img = img.transpose((2, 0, 1)) # HWC > CHW img = np.expand_dims(img, axis=0) # 次元合せ # 推論実行 out = exec_net.infer(inputs={'data': img}) # 出力から必要なデータのみ取り出し out = out['prob_emotion'] out = np.squeeze(out) #不要な次元の削減 # 出力 print(out) # 出力値が最大のインデックスを得る index_max = np.argmax(out) # 各感情の文字列をリスト化 list_emotion = ['neutral', 'happy', 'sad', 'surprise', 'anger'] # 文字列描画 cv2.putText(img_face, list_emotion[index_max], (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) # 画像表示 cv2.imshow('image', img_face) # キーが押されたら終了 cv2.waitKey(0) cv2.destroyAllWindows()
face7 → pi@raspberrypi:~/workspace $ python3 emotion2.py [0.03646851 0.8496094 0.06646729 0.03634644 0.0107193 ] E: [xLink] [ 816950] [python3] DispatcherWaitEventComplete:355 Assertion Failed: curr != NULL E: [global] [ 816951] [python3] XLinkResetRemote:254 Condition failed: DispatcherWaitEventComplete(&link->deviceHandle) face1 → pi@raspberrypi:~/workspace $ python3 emotion2.py [1.7944336e-02 6.7758560e-04 3.9405823e-03 7.1830750e-03 9.7070312e-01] E: [global] [ 1978] [python3] XLink_sem_wait:94 XLink_sem_inc(sem) method call failed with an error: -1 E: [global] [ 1978] [python3] XLinkResetRemote:257 can't wait dispatcherClosedSem face5 → pi@raspberrypi:~/workspace $ python3 emotion2.py [0.00666046 0.5209961 0.02537537 0.4404297 0.00688171] E: [global] [ 282280] [python3] XLink_sem_wait:94 XLink_sem_inc(sem) method call failed with an error: -1 E: [global] [ 282281] [python3] XLinkResetRemote:257 can't wait dispatcherClosedSem