【啟動AI Maker世代 】2024 MAI 開發者社群大會(5/16-17)
|

實作 OpenVINO 加速 YOLOv7 推論

   
作者:Felix

今年七月物件偵測模型 YOLO(You Only Look Once)官方終於釋出承接 YOLOv4 的後繼版本 YOLOv7,由中研院王建堯(Chien-Yao Wang)博士等人共同發表論文與實作程式碼。相關的技術原理已經有眾多大神的文章作說明,筆者就不在此丟人現眼了。然而比較特別的是此版本使用了 pyTorch 框架實作 YOLO 架構,並且導出模型,這就讓使用上更加方便彈性了!本篇文章將使用 OpenVINO 轉換 YOLOv7 模型,使其提高在邊緣裝置上的 AI 推論效能!

OpenVINO 2022.1 環境準備

筆者執行開發環境的筆電 CPU 為 Intel Core i7-8565U,作業系統為 Ubuntu 20.04 ,本篇後續的操作均在此電腦執行。OpenVINO 版本使用 2022.1 Python 開發環境的建置可以參照 OpenVINO 官方說明文件分別安裝 runtime 執行環境dev開發工具,過程在此就不多作贅述了,留意安裝後OpenVINO版本為 2022.1即可。完成後執行以下指令進入 OpenVINO python虛擬環境開始進行本次實作。(虛擬環境路徑依據建置過程可能會有所不同)

source ~/openvino_env/bin/activate

下載與測試 YOLOv7

首先當然是到 YOLOv7 官方 github 下載專案:

cd ~

git clone https://github.com/WongKinYiu/yolov7.git

cd yolov7

接著要取得預訓練好的權重,根據不同的輸入影像大小以及參數量有不同的預訓練模型,如yolov7xyolov7-w6yolov7-d6等,在此筆者就以推論速度為考量下載yolov7.pt,各位有興趣也可以都下載進行測試。

wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt

安裝所需的python 套件,預計會花費十分鐘左右的時間。

pip install -r requirements.txt

完成後就可以來測試YOLOv7的即時推論,帶入參數 weight 指定剛剛下載的預訓練權重,參數 source 則是輸入資料,可以帶入照片或影片的檔案路徑,這邊帶入 0 意思為編號0號的影像擷取裝置,也就是筆者電腦內建的 webcam。

python3 detect.py --weight yolov7.pt --source 0

載入完成後就會看到即時預覽的畫面,同時在終端機上會顯示偵測到的物件以及所花費的時間。可以看在筆者電腦上跑一張推論需要花費將近半秒鐘的時間,以一台商務筆電的性能來說,也只能說是中規中矩。

除此之外也測試一下圖片的推論,帶入權重、信心度、影像資料、以及影像路徑,這裡使用 yolo 專案內的影像進行推論:

python detect.py --weights yolov7.pt --conf 0.25 --img-size 640 --source inference/images/horses.jpg

推論完成的輸出資料會放置於 runs/detect/exp* 路徑下,每次執行都會產生一個新的資料夾,留意輸出訊息中的路徑。執行以下指令開啟推論完成的照片,可以看到結果相當不錯,連被遮擋的物件也都被順利偵測出來!

eog runs/detect/exp2/horses.jpg

轉換到 ONNX 與 IR 模型格式

接下來為了將預訓練模型 yolov7.pt 使用 OpenVINO 加速,需要將其轉為 ONNX 模型格式,再丟到模型優化器 mo。為此確保開發環境已安裝好 OpenVINO ONNX 套件,執行以下指令進行安裝:

pip install openvino-dev==2022.1.0

接著我們使用 YOLOv7 專案的程式將模型匯出為 ONNX 格式:

python export.py --weight ../yolov7.pt

執行完成則會產生 yolov7.onnx 檔案。若在匯出時產生以下錯誤訊息 『 ONNX export failure: Exporting the operator silu to ONNX opset version 12 is not supported. Please open a bug to request ONNX export support for the missing operator.』 代表目前開發環境上的 pyTorch 版本過舊,無法支援新的運算子,可以使輸入以下指令來更新 pyTroch:(此問題也有被提出在github 上的 issue#89,提供給各位參考)

pip install --upgrade torch torchvision

在進行模型最佳化前,先使用 benchmark_app 進行模型的效能評估,同時查看模型相關資訊。

benchmark_app -m yolov7.onnx

觀察輸出如下,在 Configuring input of the model 顯示模型的輸入與輸出資訊,input 為 images, output 為 ‘output’、’516’、’528’,在接下來模型優化器會使用到此作為參數。

(openvino_env) felix@felix-ThinkPad-T490:~/yolov7$ benchmark_app -m yolov7.onnx

Parsing and validating input arguments

-nstreams default value is determined automatically for a device. Although the automatic selection usually provides a reasonable performance, but it still may be non-optimal for some cases, for more information look at README.

Loading OpenVINO

PerformanceMode was not explicitly specified in command line. Device CPU performance hint will be set to THROUGHPUT.

OpenVINO:

API version............. 2022.1.0-7019-cdb9bec7210-releases/2022/1

Device info

CPU

openvino_intel_cpu_plugin version 2022.1

Build................... 2022.1.0-7019-cdb9bec7210-releases/2022/1

Setting device configuration

-nstreams default value is determined automatically for CPU device. Although the automatic selection usually provides a reasonable performance, but it still may be non-optimal for some cases, for more information look at README.

Reading network files

Read model took 301.17 ms

Resizing network to match image sizes and given batch

Network batch size: 1

Configuring input of the model

Model input 'images' precision u8, dimensions (): 1 3 640 640

Model output 'output' precision f32, dimensions (): 1 3 80 80 85

Model output '516' precision f32, dimensions (): 1 3 40 40 85

Model output '528' precision f32, dimensions (): 1 3 20 20 85

Loading the model to the device

Compile model took 384.81 ms

Querying optimal runtime parameters

DEVICE: CPU

AVAILABLE_DEVICES ,

RANGE_FOR_ASYNC_INFER_REQUESTS , (1, 1, 1)

RANGE_FOR_STREAMS , (1, 8)

FULL_DEVICE_NAME , Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz

OPTIMIZATION_CAPABILITIES ,

CACHE_DIR ,

NUM_STREAMS , 4

INFERENCE_NUM_THREADS , 0

PERF_COUNT , False

PERFORMANCE_HINT_NUM_REQUESTS , 0

Creating infer requests and preparing input data

Create 4 infer requests took 0.78 ms

No input files were given for input 'images'!. This input will be filled with random values!

Fill input 'images' with random values

Measuring performance (Start inference asynchronously, 4 inference requests using 4 streams for CPU, inference only: True, limits: 60000 ms duration)

Benchmarking in inference only mode (inputs filling are not included in measurement loop).

First inference took 860.03 ms

Dumping statistics report

Count: 96 iterations

Duration: 64118.68 ms

Latency:

Median: 2184.34 ms

AVG: 2645.58 ms

MIN: 2076.72 ms

MAX: 3766.81 ms

Throughput: 1.50 FPS

執行以下模型優化器指令,分別帶入輸入模型 yolov7.onnx、輸入層 images、輸出層 output,516,528 以及資料格式 FP16 等參數,這邊使用半精度的浮點數 FP16 來加速推論。

mo --input_model yolov7.onnx --input images --output output,516,528 --data_type FP16

優化完成的提示訊息也顯示此 IR 模型為 v11 版本,建議使用推論引擎 2.0 的 API 來達到較佳的效果,也就是 OpenVINO 2022.1 之後版本。

本文為會員限定文章

立即加入會員! 全站文章無限看~

                               

已經是會員? 按此登入

只需不到短短一分鐘...

輸入您的信箱與ID註冊即可享有一切福利!

會員福利
1

免費電子報

2

會員搶先看

3

主題訂閱

4

好文收藏

Lin Felix

Author: Lin Felix

長期出沒在南部地區的Maker社群,致力於推廣從實作中學習的精神。熱愛胡搞瞎搞,喜歡嘗試新事物。現職為亞堤教育團隊講師與創客閣樓召集人。

Share This Post On
468 ad

Submit a Comment

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *