【问题标题】:OpenVINO - Image classificationOpenVINO - 图像分类
【发布时间】:2021-09-03 00:19:48
【问题描述】:

我尝试使用 OpenVINO 推理引擎来加速我的深度学习推理。它适用于一张图片。但我想创建一批两张图像,然后进行推理。 这是我的代码:

InferenceEngine::Core core;
InferenceEngine::CNNNetwork network = core.ReadNetwork("path/to/model.xml");
    
InferenceEngine::InputInfo::Ptr input_info = network.getInputsInfo().begin()->second;
std::string input_name = network.getInputsInfo().begin()->first;
    
InferenceEngine::DataPtr output_info = network.getOutputsInfo().begin()->second;
std::string output_name = network.getOutputsInfo().begin()->first;
    
InferenceEngine::ExecutableNetwork executableNetwork = core.LoadNetwork(network, "CPU");
    
InferenceEngine::InferRequest inferRequest = executableNetwork.CreateInferRequest();

std::string input_image_01 = "path/to/image_01.png";
cv::Mat image_01 = cv::imread(input_image_01 );
InferenceEngine::Blob::Ptr imgBlob_01 = wrapMat2Blob(image_01);

std::string input_image_02 = "path/to/image_02.png";
cv::Mat image_02 = cv::imread(input_image_02 );
InferenceEngine::Blob::Ptr imgBlob_02 = wrapMat2Blob(image_02);

InferenceEngine::BlobMap imgBlobMap;
std::pair<std::string, InferenceEngine::Blob::Ptr> pair01(input_image_01, imgBlob_01);
imgBlobMap.insert(pair01);
std::pair<std::string, InferenceEngine::Blob::Ptr> pair02(input_image_02, imgBlob_02);
imgBlobMap.insert(pair02);
inferRequest.SetInput(imgBlobMap);

inferRequest.StartAsync();
inferRequest.Wait(InferenceEngine::IInferRequest::WaitMode::RESULT_READY);

InferenceEngine::Blob::Ptr output = inferRequest.GetBlob(output_name);
std::vector<unsigned> class_results;
ClassificationResult cls(output, {"x", "y"}, 2, 3);
class_results = cls.getResults();

不幸的是,我从命令中收到以下错误消息

inferRequest.SetInput(imgBlobMap);

[NOT_FOUND] 找不到名称为“path/to/image_02.png”的输入或输出 C:\j\workspace\private-ci\ie\build-windows-vs2019@2\b\repos\openvino\inference-engine\src\plugin_api\cpp_interfaces/impl/ie_infer_request_internal.hpp:303 C:\Program Files (x86)\Intel\openvino_2021.3.394\inference_engine\include\details/ie_exception_conversion.hpp:66

如何创建一批图像,进行推理并获取分类类别和置信度的信息?置信度和类别是否位于 GetBlob() 的接收变量中?我是否需要调用 ClassificationResult cls(output, {"x", "y"}, 2, 3);?

【问题讨论】:

    标签: c++ classification openvino


    【解决方案1】:

    我建议您查看 OpenVINO 在线文档中的 Using Shape Inference 文章,以了解使用批处理的限制。它还引用了 Open Model Zoo smart_classroom_demo,其中动态批处理用于处理多个先前检测到的人脸。基本上,当您在模型中启用批处理时,您的输入 blob 的内存缓冲区将被分配为所有批次的图​​像留出空间,您的责任是从您的数据中批量填充每个图像的输入 blob 中的数据。您可以查看 smart_classroom_demo 的函数 CnnDLSDKBase::InferBatch,该函数位于文件 smart_classroom_demo/cpp/src/cnn.cpp 第 51 行。如您所见,在 num_imgs 的循环中,辅助函数 matU8ToBlob 填充了输入包含图像 current_batch_size 数据的 blob,然后为推断请求设置批量大小并运行推断。

    for (size_t batch_i = 0; batch_i < num_imgs; batch_i += batch_size) {
        const size_t current_batch_size = std::min(batch_size, num_imgs - batch_i);
        for (size_t b = 0; b < current_batch_size; b++) {
            matU8ToBlob<uint8_t>(frames[batch_i + b], input, b);
        }
    
        if (config_.max_batch_size != 1)
            infer_request_.SetBatch(current_batch_size);
        infer_request_.Infer();
    

    【讨论】:

      【解决方案2】:

      有一个类似的示例使用批量输入作为 OpenVINO 中模型的输入。您可以参考以下链接。

      https://github.com/openvinotoolkit/openvino/blob/ae2913d3b5970ce0d3112cc880d03be1708f13eb/inference-engine/samples/hello_nv12_input_classification/main.cpp#L236

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-23
        • 2020-05-23
        • 1970-01-01
        • 2019-09-12
        • 1970-01-01
        • 2019-09-25
        • 1970-01-01
        • 2020-12-22
        相关资源
        最近更新 更多