目錄
Openvino簡(jiǎn)介
如何使用它?
構(gòu)建源代碼
Openvino IR模型
第一個(gè)Openvino示例
C語言示例
C++示例
使用OpenVino跑Yolo模型
Openvino簡(jiǎn)介
Openvino是由Intel開發(fā)的專門用于優(yōu)化和部署人工智能推理的半開源的工具包,主要用于對(duì)深度推理做優(yōu)化。
Openvino內(nèi)部集成了Opencv、TensorFlow模塊,除此之外它還具有強(qiáng)大的Plugin開發(fā)框架,允許開發(fā)者在Openvino之上對(duì)推理過程做優(yōu)化。
Openvino整體框架為:Openvino前端→ Plugin中間層→ Backend后端
?Openvino的優(yōu)點(diǎn)在于它屏蔽了后端接口,提供了統(tǒng)一操作的前端API,開發(fā)者可以無需關(guān)心后端的實(shí)現(xiàn),例如后端可以是TensorFlow、Keras、ARM-NN,通過Plugin提供給前端接口調(diào)用,也就意味著一套代碼在Openvino之上可以運(yùn)行在多個(gè)推理引擎之上,Openvino像是類似聚合一樣的開發(fā)包。
如何使用它?
構(gòu)建源代碼
1.?首先你需要在github上下載到它的源代碼
git clone https://github.com/openvinotoolkit/openvino
2.?下載完成之后進(jìn)入到openvino的目錄,然后拉取openvino的子模塊源代碼
cd openvino
git submodule update --init --recursive
NOTE:如果你的網(wǎng)絡(luò)不好,可以使用gitee方式拉取
chmod +x scripts/submodule_update_with_gitee.sh ./scripts/submodule_update_with_gitee.sh
3.?使用Openvino自帶的腳本安裝依賴
chmod +x install_build_dependencies.sh
sudo ./install_build_dependencies.sh
4.?從源碼構(gòu)建Openvino
首先創(chuàng)建一個(gè)build目錄并進(jìn)入
mkdir build && cd build
通過Cmake構(gòu)建Openvino
cmake -DCMAKE_BUILD_TYPE=Release ..
make --jobs=$(nproc --all)
Openvino項(xiàng)目比較大,構(gòu)建過程可能需要一定時(shí)間。
5.?環(huán)境配置
編譯完成之后執(zhí)行make install命令進(jìn)行腳本自動(dòng)化配置
安裝完成之后會(huì)存放到/usr/local/runtime目錄下
make install
Openvino IR模型
Openvino使用IR文件格式作為神經(jīng)網(wǎng)絡(luò)數(shù)據(jù)處理格式,IR文件格式是Openvino官方自己定義的,IR模型由兩個(gè)文件組成:
XML文件
描述網(wǎng)絡(luò)拓?fù)浣Y(jié)構(gòu)
BIN文件
包含網(wǎng)絡(luò)權(quán)重?cái)?shù)據(jù),以IR文件格式存儲(chǔ)
Openvino提供了工具來將任意模型格式轉(zhuǎn)化為IR文件格式:mo.py
你可以在Openvino的源代碼目錄里找到:
tools/mo/openvino/tools/mo/mo.py
使用示例
tools/mo/openvino/tools/mo/mo.py --input_model your_model.pb --output_dir output_dir
執(zhí)行之后會(huì)在輸出目錄下生成兩個(gè)文件:xml、bin文件,需要值得注意的是Openvino不支持動(dòng)態(tài)輸入維度,你可以通過模型check工具來查看你的模型是否是動(dòng)態(tài)輸入
如果維度里顯示?,代表是動(dòng)態(tài)輸入,大小是可變的,Openvino不支持動(dòng)態(tài)輸入,有兩種方法可以修改,第一種是通過mo.py或IR文件格式指定,第二種是在代碼里指定。
在使用mo.py時(shí)指定它的維度:
--input:?指定輸入端口名稱
--input_shape:?指定維度
tools/mo/openvino/tools/mo/mo.py --input_model your_model.pb --input Input:0 --input_shape [1,1]--output_dir output_dir
如果你的模型已經(jīng)轉(zhuǎn)化為IR文件格式則可以直接在xml里修改:
<layers>
<layer id="0" name="Input" type="Parameter" version="opset1">
<data shape="?,1" element_type="f32" /> #修改這里將?改成1
<output>
<port id="0" precision="FP32" names="Input:0">
<dim>-1</dim>
<dim>1</dim>
</port>
</output>
</layer>
</layers>
除此之外在xml里你還可以修改輸入維度的類型,這些配置在compile模型時(shí)會(huì)Openvino會(huì)根據(jù)xml描述來分配不同的類型存儲(chǔ)以及運(yùn)算。
Openvino本身已經(jīng)支持PB、ONNX格式,你可以不用轉(zhuǎn)換成IR文件格式在Openvino里直接使用,在Compile階段Openvino會(huì)自動(dòng)將其轉(zhuǎn)換成IR文件格式。
第一個(gè)Openvino示例
Openvino底層是使用C語言實(shí)現(xiàn)的,所以它可以提供給多個(gè)語言的使用。
C語言示例
首先包含Openvino基礎(chǔ)頭文件
#include <openvino/c/openvino.h>
初始化ov
ov_core_t* core = NULL;
ov_core_create(&core);
if(core == NULL) {
printf("can't create core object\n");
return -1;
}
讀取模型到內(nèi)存
ov_model_t* model = NULL;
ov_core_read_model(core, "test.xml", NULL, &model);
if (model == NULL) {
printf("can't read a model.\n");
return -1;
}
編譯模型,這一步需要指定要使用的Plugin
ov_compiled_model_t* compiled_model = NULL;
ov_core_compile_model(core, model, "CPU", 0, &compiled_model);
if (compiled_model == NULL) {
printf("can't compile model.\n");
return -1;
}
創(chuàng)建推理引擎
ov_infer_request_t* infer_request = NULL;
ov_compiled_model_create_infer_request(compiled_model, &infer_request);
if(infer_request == NULL) {
printf("can't create infer request.\n");
return -1;
}
獲取輸入Input層
ov_tensor_t* input_tensor1 = NULL;
ov_infer_request_get_tensor(infer_request, "Input:0", &input_tensor1);
if (input_tensor1 == NULL) {
printf("can't get tensor.\n");
return -1;
}
獲取輸入tensor
void* data = NULL;
ov_tensor_data(input_tensor1, &data);
if (data == NULL) {
printf("can't i642i32.\n");
return -1;
}
float* data1 = (float*)data;
輸入數(shù)據(jù)
data1[0] = 20.f;
開始推理
ov_infer_request_infer(infer_request);
獲取推理結(jié)果并打印
ov_tensor_t* output_tensor = NULL;
void* data_out = NULL;
ov_infer_request_get_output_tensor(infer_request, &output_tensor);
if(output_tensor == NULL) {
printf("can't get output tensor.\n");
return -1;
}
ov_tensor_data(output_tensor, &data_out);
if(data_out == NULL) {
printf("can't out i642i32.\n");
return -1;
}
float* out_data = (float*)data_out;
printf("%f\n", out_data[0]);
C++示例
初始化OpenVINO運(yùn)行時(shí)核心
ov::Core core;
讀取模型
std::shared_ptr<ov::Model> model = core.read_model("./Face.xml");
加載模型到內(nèi)存并指定Plugin
ov::CompiledModel compiled_model = core.compile_model(model, "cpu");
獲取模型的輸入端口
auto input_port = compiled_model.input();
創(chuàng)建推理引擎
ov::InferRequest infer_request = compiled_model.create_infer_request();
輸入數(shù)據(jù)
ov::Tensor input_tensor1 = infer_request.get_input_tensor(0);
auto data1 = input_tensor1.data<float>();
data1[0] = 60.f
開始推理
infer_request.infer();
獲取輸出并打印
auto output_tensor = infer_request.get_output_tensor(0);
const float *result = output_tensor_p8.data<const float>();
cout << result[0] << endl;
使用OpenVino跑Yolo模型
Openvino官方提供了YoloV7模型的示例,可以直接在Github上下載
git clone https://github.com/OpenVINO-dev-contest/YOLOv7_OpenVINO_cpp-python.git
除此之外你需要一個(gè)YoloV7的模型,你可以在這里下載到:YoloV7
下載一個(gè)你需要的模型,然后在將Yolo源碼拉下來
git clone https://github.com/WongKinYiu/yolov7.git
OpenVino支持onnx模型,使用yolov7自帶的導(dǎo)出py代碼,將pt文件導(dǎo)出成onnx
python export.py --weights yolov7.pt
會(huì)在當(dāng)前目錄下生成一個(gè)yolov7.onnx的文件
然后進(jìn)入到YOLOv7_OpenVINO_cpp-python目錄下編譯CPP代碼
cd YOLOv7_OpenVINO_cpp-python
cd cpp
mkdir build
cmake ..
make
將yolov7.onnxCopy到build目錄下然后執(zhí)行:
yolov7 yolov7.onnx ../data/horses.jpg 'CPU'
運(yùn)行結(jié)果:文章來源:http://www.zghlxwxcb.cn/news/detail-425685.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-425685.html
到了這里,關(guān)于什么是OpenVino?以及如何使用OpenVino運(yùn)行yolo的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!