Blame view

3rdparty/opencv-4.5.4/modules/gapi/src/backends/ie/giebackend/giewrapper.cpp 4.04 KB
f4334277   Hu Chunming   提交3rdparty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  // This file is part of OpenCV project.
  // It is subject to the license terms in the LICENSE file found in the top-level directory
  // of this distribution and at http://opencv.org/license.html.
  //
  // Copyright (C) 2020 Intel Corporation
  
  #ifdef HAVE_INF_ENGINE
  
  #include <vector>
  #include <string>
  #include <tuple>
  
  #include "backends/ie/giebackend/giewrapper.hpp"
  
  #include <ade/util/range.hpp>
  #include <ade/util/zip_range.hpp>
  
  #include <opencv2/core/utility.hpp>
  #include <opencv2/core/utils/logger.hpp>
  
  namespace IE = InferenceEngine;
  namespace giewrap = cv::gimpl::ie::wrap;
  using GIEParam = cv::gapi::ie::detail::ParamDesc;
  
  #if INF_ENGINE_RELEASE < 2020000000  // < 2020.1
  // Load extensions (taken from DNN module)
  std::vector<std::string> giewrap::getExtensions(const GIEParam& params) {
      std::vector<std::string> candidates;
      if (params.device_id == "CPU" || params.device_id == "FPGA")
      {
          const std::string suffixes[] = { "_avx2", "_sse4", ""};
          const bool haveFeature[] = {
              cv::checkHardwareSupport(CPU_AVX2),
              cv::checkHardwareSupport(CPU_SSE4_2),
              true
          };
          for (auto &&it : ade::util::zip(ade::util::toRange(suffixes),
                                          ade::util::toRange(haveFeature)))
          {
              std::string suffix;
              bool available = false;
              std::tie(suffix, available) = it;
              if (!available) continue;
  #ifdef _WIN32
              candidates.push_back("cpu_extension" + suffix + ".dll");
  #elif defined(__APPLE__)
              candidates.push_back("libcpu_extension" + suffix + ".so");  // built as loadable module
              candidates.push_back("libcpu_extension" + suffix + ".dylib");  // built as shared library
  #else
              candidates.push_back("libcpu_extension" + suffix + ".so");
  #endif  // _WIN32
          }
      }
      return candidates;
  }
  
  IE::CNNNetwork giewrap::readNetwork(const GIEParam& params) {
      IE::CNNNetReader reader;
      reader.ReadNetwork(params.model_path);
      reader.ReadWeights(params.weights_path);
      return reader.getNetwork();
  }
  #else // >= 2020.1
  std::vector<std::string> giewrap::getExtensions(const GIEParam&) {
      return std::vector<std::string>();
  }
  
  IE::CNNNetwork giewrap::readNetwork(const GIEParam& params) {
      auto core = giewrap::getCore();
      return core.ReadNetwork(params.model_path, params.weights_path);
  }
  #endif // INF_ENGINE_RELEASE < 2020000000
  
  #if INF_ENGINE_RELEASE < 2019020000  // < 2019.R2
  IE::InferencePlugin giewrap::getPlugin(const GIEParam& params) {
      auto plugin = IE::PluginDispatcher().getPluginByDevice(params.device_id);
      if (params.device_id == "CPU" || params.device_id == "FPGA")
      {
          for (auto &&extlib : giewrap::getExtensions(params))
          {
              try
              {
                  plugin.AddExtension(IE::make_so_pointer<IE::IExtension>(extlib));
                  CV_LOG_INFO(NULL, "DNN-IE: Loaded extension plugin: " << extlib);
                  break;
              }
              catch(...)
              {
                  CV_LOG_INFO(NULL, "Failed to load IE extension: " << extlib);
              }
          }
      }
      return plugin;
  }
  #else // >= 2019.R2
  IE::Core giewrap::getCore() {
      static IE::Core core;
      return core;
  }
  
  IE::Core giewrap::getPlugin(const GIEParam& params) {
      auto plugin = giewrap::getCore();
      if (params.device_id == "CPU" || params.device_id == "FPGA")
      {
          for (auto &&extlib : giewrap::getExtensions(params))
          {
              try
              {
  #if INF_ENGINE_RELEASE >= 2021040000
                  plugin.AddExtension(std::make_shared<IE::Extension>(extlib), params.device_id);
  #else
                  plugin.AddExtension(IE::make_so_pointer<IE::IExtension>(extlib), params.device_id);
  #endif
                  CV_LOG_INFO(NULL, "DNN-IE: Loaded extension plugin: " << extlib);
                  break;
              }
              catch(...)
              {
                  CV_LOG_INFO(NULL, "Failed to load IE extension: " << extlib);
              }
          }
      }
      return plugin;
  }
  #endif // INF_ENGINE_RELEASE < 2019020000
  
  #endif //HAVE_INF_ENGINE