OpenVINO可以获得模型的输入节点信息:
-
输入节点的名字
- Model Optimizer转换后的模型精度选择:对于IR模型来说,FP16 使用最普遍且性能最高 对于IR模型来说,FP16 使用最普遍且性能最高
- 输入节点数据的精度选择:U8使用最普遍 输入节点数据的精度选择:U8
- 输出节点数据的精度选择:FP32使用最普遍 输出节点数据的精度选择:FP32
- 输入节点数据的形状(shape)
- 输入节点数据的布局(layout),默认情况下,输入节点的layout是Layout::NCHW;输出节点由维度定义 输出节点的layout由数据维度决定
layout的代码定义如下:
#include <ie_common.h>
enum Layout
{
ANY = 0,
NCHW = 1,
NHWC = 2,
NCDHW = 3,
NDHWC = 4,
OIHW = 64,
GOIHW = 65,
OIDHW = 66,
GOIDHW = 67,
SCALAR = 95,
C = 96,
CHW = 128,
HWC = 129,
HW = 192,
NC = 193,
CN = 194,
BLOCKED = 200,
};
- 输入节点是否支持Resize算法,默认是NO_RESIZE,即不会自动缩放输入数据。使用下面的代码设置是否需要支持Resize。
input_data->getPreProcess().setResizeAlgorithm(InferenceEngine::RESIZE_BILINEAR);
Resize算法种类的代码定义如下
#include <ie_preprocess.hpp>
enum ResizeAlgorithm
{
NO_RESIZE = 0,
RESIZE_BILINEAR,
RESIZE_AREA,
};
- 数据的色彩模式(Color format):默认情况下,Inference Engine假定输入数据的色彩模式是BGR,并且禁止色彩模式,即ColorFormat::RAW。注意:BGR是OpenCV的默认色彩模式。
ColorFormat代码定义如下:
#include <ie_common.h>
enum ColorFormat
{
RAW = 0u,
RGB,
BGR,
RGBX,
BGRX,
NV12,
I420,
};
- 输入节点是否支持平均,默认是不支持对输入数据做平均。MeanVariant代码定义如下:
#include <ie_preprocess.hpp>
enum MeanVariant
{
MEAN_IMAGE, //mean value is specified for each input pixel
MEAN_VALUE, //mean value is specified for each input channel
NONE, //no mean value specified
};
整套范例程序如下所示:
// OpenVINO Sample code for PPYOLOv2
#include<string>
#include<iostream>
#include<map>
#include<inference_engine.hpp>
#include<ngraph/ngraph.hpp>
#include "ocv_common.hpp"
using namespace InferenceEngine;
using namespace std;
//配置推理计算设备,IR文件路径,图片路径,阈值和标签
string DEVICE = "CPU";
string IR_FileXML = "D:/pd/ov_model/ppyolov2.xml";
string imageFile = "road554.png";
float confidence_threshold = 0.7; //取值0~1
vector<string> labels = { "speedlimit","crosswalk","trafficlight","stop" }; //标签输入
int main()
{
// --------------------------- 1. 创建Core对象 --------------------------------------
cout << "1.Create Core Object." << endl;
Core ie; // 创建Core对象
cout << "InferenceEngine: " << GetInferenceEngineVersion() << endl;//输出IE版本信息
cout << ie.GetVersions(DEVICE) << std::endl; //输出插件版本信息, “<<”运算符重载代码在common.hpp中
// ------------------- 2. 将模型文件载入推理设备 ------------------------------------
cout << "2.Load the Model to the Device..." <<endl;
CNNNetwork network = ie.ReadNetwork(IR_FileXML); //The CNNNetwork class contains all the information about the Neural Network
network.setBatchSize(1); //Set the inference batch size
cout << "The network's name: " << network.getName() <<std::endl;
cout << "The number of layers in the network : " << network.layerCount() << std::endl;
cout << "Collect all input nodes informations : " << std::endl;
auto inputNodes = network.getInputsInfo();
for (auto& i : inputNodes)
{
cout << "node name: " << i.first << "; the shape:";
for (auto& item : i.second->getTensorDesc().getDims())
{
cout << item << ",";
}
cout << "the precision: " << i.second->getPrecision() << "; ";
cout << "the layout: " << i.second->getLayout() << "; ";
cout << "the color format: " << i.second->getPreProcess().getColorFormat() << "; ";
cout << "the ResizeAlgorithm: " << i.second->getPreProcess().getResizeAlgorithm()<< endl;
i.second->getPreProcess().setResizeAlgorithm(InferenceEngine::RESIZE_BILINEAR);
cout << "Set the ResizeAlgorithm: " << i.second->getPreProcess().getResizeAlgorithm() << endl;
cout << "The Default mean variant: " << i.second->getPreProcess().getMeanVariant() << endl;
return 0;
}
运行结果如下所示:
OpenVINO获取模型输入节点信息
结论:
- 模型精度设置为FP16
- 模型的图像数据输入节点数据精度设置为U8,其余辅助信息节点保持默认
- 模型的图像数据输入节点的color format保持默认BGR,不做自动转换;layout保持默认:NCHW。
- 模型的输出数据精度为FP32
- OpenVINO的color format自动转换,图像尺寸自动放缩Resize和图像数据自动平均功能保持禁用;color format转换为BGR在图像采集时完成;Resize和Normalize功能手动写一个preprocess函数来实现
网友评论