【DFRobot行业AI开发者大赛】OpenVINO 模型浮点数据输入方法
OpenVINO 将 Intel 的各种计算设备整合在一起,方便地部署人工智能推理模型。但在官方的开发环境中,模型输入只给出了 Precision::U8 类型的数据转换函数:matU8ToBlob(),没有给出 Precision::FP32 类型输入数据的实现方法,使得其它浮点数输入模型转换为 IR 模型后不能准确推理。这里给出一种实现 Precision::FP32 类型数据输入的方法,以方便大家使用。
首先将模型的输入精度设置为 FP32 :
InputInfo::Ptr& inputInfoFirst = inputInfo.begin()->second;
inputInfoFirst->setPrecision(Precision::FP32);
input = inputInfo.begin()->first;
然后设置完成模型的其它参数,并将输入数据推入处理队列:
Blob::Ptr inputBlob = request->GetBlob(input);
matF32ToBlob(face, inputBlob, enquedFaces);
这里的 matF32ToBlob() 函数实现了 Precision::FP32 类型数据输入,具体定义如下:
void matFP32ToBlob(const cv::Mat& orig_image, InferenceEngine::Blob::Ptr& blob, int batchIndex = 0) {
InferenceEngine::SizeVector blobSize = blob->getTensorDesc().getDims();
const size_t width = blobSize;
const size_t height = blobSize;
const size_t channels = blobSize;
InferenceEngine::MemoryBlob::Ptr mblob = InferenceEngine::as<InferenceEngine::MemoryBlob>(blob);
if (!mblob) {
THROW_IE_EXCEPTION << "We expect blob to be inherited from MemoryBlob in matF32ToBlob, "
<< "but by fact we were not able to cast inputBlob to MemoryBlob";
}
// locked memory holder should be alive all time while access to its buffer happens
auto mblobHolder = mblob->wmap();
float *blob_data = mblobHolder.as<float *>();
cv::Mat resized_image(orig_image);
if (static_cast<int>(width) != orig_image.size().width ||
static_cast<int>(height) != orig_image.size().height) {
cv::resize(orig_image, resized_image, cv::Size(width, height));
}
int batchOffset = batchIndex * width * height * channels;
for (size_t c = 0; c < channels; c++) {
for (size_th = 0; h < height; h++) {
for (size_t w = 0; w < width; w++) {
blob_data =
resized_image.at<float>(h, w, c);
}
}
}
}
以上就是一种便捷的浮点数输入方法,可以广泛应用于各种 IR 模型。
页:
[1]