ソースを参照

openvino add segement module

syyxsxx 5 年 前
コミット
9fdf12b5ef

+ 5 - 14
deploy/openvino/python/converter.py

@@ -49,20 +49,11 @@ def arg_parser():
 
 
 def export_openvino_model(model, args):
-    if model.model_type == "detector" or model.__class__.__name__ == "FastSCNN":
-        print(
-            "Only image classifier models and semantic segmentation models(except FastSCNN) are supported to export to openvino"
-        )
-    try:
-        import x2paddle
-        if x2paddle.__version__ < '0.7.4':
-            logging.error("You need to upgrade x2paddle >= 0.7.4")
-    except:
-        print(
-            "You need to install x2paddle first, pip install x2paddle>=0.7.4")
 
-    import x2paddle.convert as x2pc
-    x2pc.paddle2onnx(args.model_dir, args.save_dir)
+    if model.__class__.__name__ == "YOLOv3":
+        pdx.converter.export_onnx_model(model, args.save_dir)
+    else:
+        pdx.converter.export_onnx_model(model, args.save_dir, 11)
 
     import mo.main as mo
     from mo.utils.cli_parser import get_onnx_cli_parser
@@ -81,7 +72,7 @@ def export_openvino_model(model, args):
         input_channel = info['_init_params']['input_channel']
     shape = '[1,{},' + shape_list[1] + ',' + shape_list[0] + ']'
     shape = shape.format(input_channel)
-    if model.__class__.__name__ == "YOLOV3":
+    if model.__class__.__name__ == "YOLOv3":
         shape = shape + ",[1,2]"
         inputs = "image,im_size"
         onnx_parser.set_defaults(input=inputs)

+ 29 - 22
deploy/openvino/src/paddlex.cpp

@@ -161,9 +161,17 @@ bool Model::predict(const cv::Mat& im, DetResult* result) {
 
   infer_request.Infer();
 
-  InferenceEngine::OutputsDataMap out_map = network_.getOutputsInfo();
-  auto iter = out_map.begin();
-  std::string outputName = iter->first;
+  InferenceEngine::OutputsDataMap out_maps = network_.getOutputsInfo();
+  std::string outputName;
+  for (const auto & output_map : out_maps) {
+    if (output_map.second->getTensorDesc().getDims().size() == 3) {
+      outputName = output_map.first;
+    }
+  }
+  if (outputName.empty()) {
+    std::cerr << "get result node failed!" << std::endl:
+    return false;
+  }
   InferenceEngine::Blob::Ptr output = infer_request.GetBlob(outputName);
   InferenceEngine::MemoryBlob::CPtr moutput =
     InferenceEngine::as<InferenceEngine::MemoryBlob>(output);
@@ -226,24 +234,7 @@ bool Model::predict(const cv::Mat& im, SegResult* result) {
   InferenceEngine::OutputsDataMap out_map = network_.getOutputsInfo();
   auto iter = out_map.begin();
   iter++;
-  std::string output_name_score = iter->first;
-  InferenceEngine::Blob::Ptr output_score =
-    infer_request.GetBlob(output_name_score);
-  InferenceEngine::MemoryBlob::CPtr moutput_score =
-    InferenceEngine::as<InferenceEngine::MemoryBlob>(output_score);
-  InferenceEngine::TensorDesc blob_score = moutput_score->getTensorDesc();
-  std::vector<size_t> output_score_shape = blob_score.getDims();
-  int size = 1;
-  for (auto& i : output_score_shape) {
-    size *= static_cast<int>(i);
-    result->score_map.shape.push_back(static_cast<int>(i));
-  }
-  result->score_map.data.resize(size);
-  auto moutputHolder_score = moutput_score->rmap();
-  float* score_data = moutputHolder_score.as<float *>();
-  memcpy(result->score_map.data.data(), score_data, moutput_score->byteSize());
 
-  iter++;
   std::string output_name_label = iter->first;
   InferenceEngine::Blob::Ptr output_label =
     infer_request.GetBlob(output_name_label);
@@ -251,7 +242,7 @@ bool Model::predict(const cv::Mat& im, SegResult* result) {
     InferenceEngine::as<InferenceEngine::MemoryBlob>(output_label);
   InferenceEngine::TensorDesc blob_label = moutput_label->getTensorDesc();
   std::vector<size_t> output_label_shape = blob_label.getDims();
-  size = 1;
+  int size = 1;
   for (auto& i : output_label_shape) {
     size *= static_cast<int>(i);
     result->label_map.shape.push_back(static_cast<int>(i));
@@ -261,7 +252,23 @@ bool Model::predict(const cv::Mat& im, SegResult* result) {
   int* label_data = moutputHolder_label.as<int *>();
   memcpy(result->label_map.data.data(), label_data, moutput_label->byteSize());
 
-
+  iter++;
+  std::string output_name_score = iter->first;
+  InferenceEngine::Blob::Ptr output_score =
+    infer_request.GetBlob(output_name_score);
+  InferenceEngine::MemoryBlob::CPtr moutput_score =
+    InferenceEngine::as<InferenceEngine::MemoryBlob>(output_score);
+  InferenceEngine::TensorDesc blob_score = moutput_score->getTensorDesc();
+  std::vector<size_t> output_score_shape = blob_score.getDims();
+  size = 1;
+  for (auto& i : output_score_shape) {
+    size *= static_cast<int>(i);
+    result->score_map.shape.push_back(static_cast<int>(i));
+  }
+  result->score_map.data.resize(size);
+  auto moutputHolder_score = moutput_score->rmap();
+  float* score_data = moutputHolder_score.as<float *>();
+  memcpy(result->score_map.data.data(), score_data, moutput_score->byteSize());
 
   std::vector<uint8_t> label_map(result->label_map.data.begin(),
                                  result->label_map.data.end());

+ 5 - 5
docs/deploy/openvino/export_openvino_model.md

@@ -3,11 +3,11 @@
 
 ## 环境依赖
 
-* ONNX 1.5.0+
-* PaddleX 1.0+
-* OpenVINO 2020.4
+* ONNX 1.6.0+
+* PaddleX 1.2+
+* OpenVINO 2021.1+
 
-**说明**:PaddleX安装请参考[PaddleX](https://paddlex.readthedocs.io/zh_CN/develop/install.html) , OpenVINO安装请参考[OpenVINO](https://docs.openvinotoolkit.org/latest/index.html),ONNX请安装1.5.0以上版本否则会出现转模型错误。
+**说明**:PaddleX安装请参考[PaddleX](https://paddlex.readthedocs.io/zh_CN/develop/install.html) , OpenVINO安装请参考[OpenVINO](https://docs.openvinotoolkit.org/latest/index.html),ONNX请安装1.6.0以上版本否则会出现转模型错误。
 
 请确保系统已经安装好上述基本软件,**下面所有示例以工作目录 `/root/projects/`演示**。
 
@@ -38,5 +38,5 @@ python converter.py --model_dir /path/to/inference_model --save_dir /path/to/ope
 | --data type(option)  | FP32、FP16,默认为FP32,VPU下的IR需要为FP16 |  
 
 **注意**:
-- 由于OpenVINO不支持ONNX的resize-11 OP的原因,目前还不支持Paddle的分割模型
+- 由于OpenVINO 从2021.1版本开始支持ONNX的resize-11 OP的原因,请下载OpenVINO 2021.1+的版本
 - YOLOv3在通过OpenVINO部署时,由于OpenVINO对ONNX OP的支持限制,我们在将YOLOv3的Paddle模型导出时,对最后一层multiclass_nms进行了特殊处理,导出的ONNX模型,最终输出的Box结果包括背景类别(而Paddle模型不包含),此处在OpenVINO的部署代码中,我们通过后处理过滤了背景类别。

+ 2 - 2
docs/deploy/openvino/introduction.md

@@ -6,11 +6,11 @@ PaddleX支持将训练好的Paddle模型通过OpenVINO实现模型的预测加
 
 |硬件平台|Linux|Windows|Raspbian OS|c++|python |分类|检测|分割|
 | ----|  ---- | ---- | ----|  ---- | ---- |---- | ---- |---- |
-|CPU|支持|支持|不支持|支持|支持|支持|支持|支持|
+|CPU|支持|支持|不支持|支持|支持|支持|支持|支持|
 |VPU|支持|支持|支持|支持|支持|支持|不支持|不支持|  
 
 
-**注意**:其中Raspbian OS为树莓派操作系统。检测模型仅支持YOLOV3,由于OpenVINO不支持ONNX的resize-11 OP的原因,目前还不支持Paddle的分割模型
+**注意**:其中Raspbian OS为树莓派操作系统。检测模型仅支持YOLOv3
 
 ## 部署流程
 **PaddleX到OpenVINO的部署流程可以分为如下两步**:

+ 4 - 2
docs/deploy/openvino/linux.md

@@ -59,7 +59,7 @@ ARCH=x86
 
 ### Step4: 预测
 
-编译成功后,分类任务的预测可执行程序为`classifier`,检测任务的预测可执行程序为`detector`,其主要命令参数说明如下:
+编译成功后,分类任务的预测可执行程序为`classifier`,检测任务的预测可执行程序为`detector`,分割任务的预测可执行程序为`segmenter`,其主要命令参数说明如下:
 
 |  参数   | 说明  |
 |  ----  | ----  |
@@ -114,7 +114,9 @@ linux系统在CPU下做多张图片的检测任务预测,并保存预测可视
 |---|---|---|---|
 |resnet-50 | 20.56 | 16.12 | 224*224 |
 |mobilenet-V2 | 5.16 | 2.31 |224*224|
-|yolov3-mobilnetv1 |76.63| 46.26|608*608 |  
+|yolov3-mobilnetv1 |76.63| 46.26|608*608 |
+|unet| 276.40| 211.49| 512*512|  
+
 
 `测试二`:
 在PC机上插入VPU架构的神经计算棒(NCS2),通过Openvino加速。

+ 1 - 1
docs/deploy/openvino/windows.md

@@ -74,7 +74,7 @@ D:
 cd D:\projects\PaddleX\deploy\openvino\out\build\x64-Release
 ```
 
-* 编译成功后,图片预测demo的入口程序为`detector.exe`,`classifier.exe`,用户可根据自己的模型类型选择,其主要命令参数说明如下:
+* 编译成功后,图片预测demo的入口程序为`detector.exe`,`classifier.exe`,`segmenter.exe`,用户可根据自己的模型类型选择,其主要命令参数说明如下:
 
 |  参数   | 说明  |
 |  ----  | ----  |