ie.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. // Copyright (C) 2019 Intel Corporation
  6. #ifndef OPENCV_GAPI_INFER_IE_HPP
  7. #define OPENCV_GAPI_INFER_IE_HPP
  8. #include <unordered_map>
  9. #include <string>
  10. #include <array>
  11. #include <tuple> // tuple, tuple_size
  12. #include <map>
  13. #include <opencv2/gapi/opencv_includes.hpp>
  14. #include <opencv2/gapi/util/any.hpp>
  15. #include <opencv2/core/cvdef.h> // GAPI_EXPORTS
  16. #include <opencv2/gapi/gkernel.hpp> // GKernelPackage
  17. #include <opencv2/gapi/infer.hpp> // Generic
  18. namespace cv {
  19. namespace gapi {
  20. // FIXME: introduce a new sub-namespace for NN?
  21. namespace ie {
  22. GAPI_EXPORTS cv::gapi::GBackend backend();
  23. /**
  24. * Specify how G-API and IE should trait input data
  25. *
  26. * In OpenCV, the same cv::Mat is used to represent both
  27. * image and tensor data. Sometimes those are hardly distinguishable,
  28. * so this extra parameter is used to give G-API a hint.
  29. *
  30. * This hint controls how G-API reinterprets the data when converting
  31. * it to IE Blob format (and which layout/etc is assigned to this data).
  32. */
  33. enum class TraitAs: int
  34. {
  35. TENSOR, //!< G-API traits an associated cv::Mat as a raw tensor and passes dimensions as-is
  36. IMAGE //!< G-API traits an associated cv::Mat as an image so creates an "image" blob (NCHW/NHWC, etc)
  37. };
  38. using IEConfig = std::map<std::string, std::string>;
  39. namespace detail {
  40. struct ParamDesc {
  41. std::string model_path;
  42. std::string weights_path;
  43. std::string device_id;
  44. // NB: Here order follows the `Net` API
  45. std::vector<std::string> input_names;
  46. std::vector<std::string> output_names;
  47. using ConstInput = std::pair<cv::Mat, TraitAs>;
  48. std::unordered_map<std::string, ConstInput> const_inputs;
  49. // NB: nun_* may differ from topology's real input/output port numbers
  50. // (e.g. topology's partial execution)
  51. std::size_t num_in; // How many inputs are defined in the operation
  52. std::size_t num_out; // How many outputs are defined in the operation
  53. enum class Kind { Load, Import };
  54. Kind kind;
  55. bool is_generic;
  56. IEConfig config;
  57. };
  58. } // namespace detail
  59. // FIXME: this is probably a shared (reusable) thing
  60. template<typename Net>
  61. struct PortCfg {
  62. using In = std::array
  63. < std::string
  64. , std::tuple_size<typename Net::InArgs>::value >;
  65. using Out = std::array
  66. < std::string
  67. , std::tuple_size<typename Net::OutArgs>::value >;
  68. };
  69. template<typename Net> class Params {
  70. public:
  71. Params(const std::string &model,
  72. const std::string &weights,
  73. const std::string &device)
  74. : desc{ model, weights, device, {}, {}, {}
  75. , std::tuple_size<typename Net::InArgs>::value // num_in
  76. , std::tuple_size<typename Net::OutArgs>::value // num_out
  77. , detail::ParamDesc::Kind::Load
  78. , false
  79. , {}} {
  80. };
  81. Params(const std::string &model,
  82. const std::string &device)
  83. : desc{ model, {}, device, {}, {}, {}
  84. , std::tuple_size<typename Net::InArgs>::value // num_in
  85. , std::tuple_size<typename Net::OutArgs>::value // num_out
  86. , detail::ParamDesc::Kind::Import
  87. , false
  88. , {}} {
  89. };
  90. Params<Net>& cfgInputLayers(const typename PortCfg<Net>::In &ll) {
  91. desc.input_names.clear();
  92. desc.input_names.reserve(ll.size());
  93. std::copy(ll.begin(), ll.end(),
  94. std::back_inserter(desc.input_names));
  95. return *this;
  96. }
  97. Params<Net>& cfgOutputLayers(const typename PortCfg<Net>::Out &ll) {
  98. desc.output_names.clear();
  99. desc.output_names.reserve(ll.size());
  100. std::copy(ll.begin(), ll.end(),
  101. std::back_inserter(desc.output_names));
  102. return *this;
  103. }
  104. Params<Net>& constInput(const std::string &layer_name,
  105. const cv::Mat &data,
  106. TraitAs hint = TraitAs::TENSOR) {
  107. desc.const_inputs[layer_name] = {data, hint};
  108. return *this;
  109. }
  110. Params& pluginConfig(IEConfig&& cfg) {
  111. desc.config = std::move(cfg);
  112. return *this;
  113. }
  114. Params& pluginConfig(const IEConfig& cfg) {
  115. desc.config = cfg;
  116. return *this;
  117. }
  118. // BEGIN(G-API's network parametrization API)
  119. GBackend backend() const { return cv::gapi::ie::backend(); }
  120. std::string tag() const { return Net::tag(); }
  121. cv::util::any params() const { return { desc }; }
  122. // END(G-API's network parametrization API)
  123. protected:
  124. detail::ParamDesc desc;
  125. };
  126. template<>
  127. class Params<cv::gapi::Generic> {
  128. public:
  129. Params(const std::string &tag,
  130. const std::string &model,
  131. const std::string &weights,
  132. const std::string &device)
  133. : desc{ model, weights, device, {}, {}, {}, 0u, 0u, detail::ParamDesc::Kind::Load, true, {}}, m_tag(tag) {
  134. };
  135. Params(const std::string &tag,
  136. const std::string &model,
  137. const std::string &device)
  138. : desc{ model, {}, device, {}, {}, {}, 0u, 0u, detail::ParamDesc::Kind::Import, true, {}}, m_tag(tag) {
  139. };
  140. Params& pluginConfig(IEConfig&& cfg) {
  141. desc.config = std::move(cfg);
  142. return *this;
  143. }
  144. Params& pluginConfig(const IEConfig& cfg) {
  145. desc.config = cfg;
  146. return *this;
  147. }
  148. // BEGIN(G-API's network parametrization API)
  149. GBackend backend() const { return cv::gapi::ie::backend(); }
  150. std::string tag() const { return m_tag; }
  151. cv::util::any params() const { return { desc }; }
  152. // END(G-API's network parametrization API)
  153. protected:
  154. detail::ParamDesc desc;
  155. std::string m_tag;
  156. };
  157. } // namespace ie
  158. } // namespace gapi
  159. } // namespace cv
  160. #endif // OPENCV_GAPI_INFER_IE_HPP