c++ - Mask RCNN OpenVino - C++ API
问题描述
我想使用 MaskRCNN 实现自定义图像分类器。
为了提高网络的速度,我想优化推理。
我已经使用过 OpenCV DNN 库,但我想用 OpenVINO 向前迈出一步。
我成功使用 OpenVINO 模型优化器 (python) 来构建代表我的网络的 .xml 和 .bin 文件。
我使用 Visual Studio 2017 成功构建了 OpenVINO 示例目录并运行了 MaskRCNNDemo 项目。
mask_rcnn_demo.exe -m .\Release\frozen_inference_graph.xml -i .\Release\input.jpg
InferenceEngine:
API version ............ 1.4
Build .................. 19154
[ INFO ] Parsing input parameters
[ INFO ] Files were added: 1
[ INFO ] .\Release\input.jpg
[ INFO ] Loading plugin
API version ............ 1.5
Build .................. win_20181005
Description ....... MKLDNNPlugin
[ INFO ] Loading network files
[ INFO ] Preparing input blobs
[ WARNING ] Image is resized from (4288, 2848) to (800, 800)
[ INFO ] Batch size is 1
[ INFO ] Preparing output blobs
[ INFO ] Loading model to the plugin
[ INFO ] Start inference (1 iterations)
Average running time of one iteration: 2593.81 ms
[ INFO ] Processing output blobs
[ INFO ] Detected class 16 with probability 0.986519: [2043.3, 1104.9], [2412.87, 1436.52]
[ INFO ] Image out.png created!
[ INFO ] Execution successful
然后我试图在一个单独的项目中重现这个项目......首先我必须观察依赖关系......
<MaskRCNNDemo>
//References
<format_reader/> => Open CV Images, resize it and get uchar data
<ie_cpu_extension/> => CPU extension for un-managed layers (?)
//Linker
format_reader.lib => Format Reader Lib (VINO Samples Compiled)
cpu_extension.lib => CPU extension Lib (VINO Samples Compiled)
inference_engined.lib => Inference Engine lib (VINO)
opencv_world401d.lib => OpenCV Lib
libiomp5md.lib => Dependancy
... (other libs)
有了它,我用我自己的类和打开图像的方式(多帧 tiff)构建了一个新项目。这项工作没有问题,我不会描述(我使用 CV DNN 推理引擎没有问题)。
我想构建与 MaskRCNNDemo 相同的项目:CustomIA
<CustomIA>
//References
None => I use my own libtiff way to open image and i resize with OpenCV
None => I will just add include to cpu_extension source code.
//Linker
opencv_world345d.lib => OpenCV 3.4.5 library
tiffd.lib => Libtiff Library
cpu_extension.lib => CPU extension compiled with sample
inference_engined.lib => Inference engine lib.
我将以下 dll 添加到项目目标目录:
cpu_extension.dll
inference_engined.dll
libiomp5md.dll
mkl_tiny_omp.dll
MKLDNNPlugind.dll
opencv_world345d.dll
tiffd.dll
tiffxxd.dll
我成功编译并执行,但我遇到了两个问题:
旧代码:
slog::info << "Loading plugin" << slog::endl;
InferencePlugin plugin = PluginDispatcher({ FLAGS_pp, "../../../lib/intel64" , "" }).getPluginByDevice(FLAGS_d);
/** Loading default extensions **/
if (FLAGS_d.find("CPU") != std::string::npos) {
/**
* cpu_extensions library is compiled from "extension" folder containing
* custom MKLDNNPlugin layer implementations. These layers are not supported
* by mkldnn, but they can be useful for inferring custom topologies.
**/
plugin.AddExtension(std::make_shared<Extensions::Cpu::CpuExtensions>());
}
/** Printing plugin version **/
printPluginVersion(plugin, std::cout);
输出 :
[ INFO ] Loading plugin
API version ............ 1.5
Build .................. win_20181005
Description ....... MKLDNNPlugin
新代码:
VINOEngine::VINOEngine()
{
// Loading Plugin
std::cout << std::endl;
std::cout << "[INFO] - Loading VINO Plugin..." << std::endl;
this->plugin= PluginDispatcher({ "", "../../../lib/intel64" , "" }).getPluginByDevice("CPU");
this->plugin.AddExtension(std::make_shared<Extensions::Cpu::CpuExtensions>());
printPluginVersion(this->plugin, std::cout);
输出 :
[INFO] - Loading VINO Plugin...
000001A242280A18 // Like memory adress ???
第二期:
当我尝试从新代码中提取我的 ROI 和掩码时,如果我有一个“匹配”,我总是有:
- 分数 =1.0
- x1=x2=0.0
- y1=y2=1.0
但是面具看起来很好提取......
新代码:
float score = box_info[2];
if (score > this->Conf_Threshold)
{
// On reconstruit les coordonnées de la box..
float x1 = std::min(std::max(0.0f, box_info[3] * Image.cols), static_cast<float>(Image.cols));
float y1 = std::min(std::max(0.0f, box_info[4] * Image.rows), static_cast<float>(Image.rows));
float x2 = std::min(std::max(0.0f, box_info[5] * Image.cols), static_cast<float>(Image.cols));
float y2 = std::min(std::max(0.0f, box_info[6] * Image.rows), static_cast<float>(Image.rows));
int box_width = std::min(static_cast<int>(std::max(0.0f, x2 - x1)), Image.cols);
int box_height = std::min(static_cast<int>(std::max(0.0f, y2 - y1)), Image.rows);
Image is resized from (4288, 2848) to (800, 800)
Detected class 62 with probability 1: [4288, 0], [4288, 0]
然后,当我没有正确的 bbox 坐标时,我不可能将蒙版放置在图像中并调整其大小......
有人知道我做得不好吗?
如何使用 cpu_extension 正确创建和链接 OpenVINO 项目?
谢谢 !
解决方案
版本的第一个问题:查看 printPluginVersion 函数,您将看到 InferenceEngine 和插件版本信息的重载 std::ostream 运算符。
第二:您可以尝试通过比较原始框架和 OV 的第一个卷积和输出层后的输出来调试模型。确保它是逐个元素相等的。
在 OV 中,您可以使用 network.addOutput("layer_name") 将任何层添加到输出。然后使用以下命令读取输出: const Blob::Ptr debug_blob = infer_request.GetBlob("layer_name")。
大多数时候遇到这样的问题,我发现缺少输入预处理(均值、归一化等)
cpu_extensions 是一个动态库,但您仍然可以更改 cmake 脚本以使其成为静态并将其与您的应用程序链接。之后,您需要使用应用程序路径来调用 IExtensionPtr extension_ptr = make_so_pointer(argv[0])
推荐阅读
- python - 如何使用远程 Firefox webdriver 下载文件?
- azure - 如何故意死信一条消息是 azure functions v2
- c# - 为什么 EF Core 不允许我创建没有主键的属性?
- r - 在 glm() 函数中编辑“术语”对象
- sql-server - 报表生成器:如何在文本框之间有统一的水平间隙?
- elasticsearch - Solr eDismax Lucene 查询问题
- r - 在 R 中解释奇怪的 Bland-Altman 情节
- python - 如何使用另一个脚本在不同目录中执行一个 python 脚本?
- javascript - 不考虑整个 dom 的 querySelector
- architecture - 干净的独立架构