c6d7f272a8cb1aa4c1f9f1e96cd3b6c67ef7511f
[platform/upstream/opencv.git] / modules / gapi / include / opencv2 / gapi / infer / ie.hpp
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
7 #ifndef OPENCV_GAPI_INFER_IE_HPP
8 #define OPENCV_GAPI_INFER_IE_HPP
9
10 #include <unordered_map>
11 #include <string>
12 #include <array>
13 #include <tuple> // tuple, tuple_size
14
15 #include <opencv2/gapi/opencv_includes.hpp>
16 #include <opencv2/gapi/util/any.hpp>
17
18 #include <opencv2/core/cvdef.h>     // GAPI_EXPORTS
19 #include <opencv2/gapi/gkernel.hpp> // GKernelPackage
20
21 namespace cv {
22 namespace gapi {
23 // FIXME: introduce a new sub-namespace for NN?
24 namespace ie {
25
26 GAPI_EXPORTS cv::gapi::GBackend backend();
27
28 /**
29  * Specify how G-API and IE should trait input data
30  *
31  * In OpenCV, the same cv::Mat is used to represent both
32  * image and tensor data. Sometimes those are hardly distinguishable,
33  * so this extra parameter is used to give G-API a hint.
34  *
35  * This hint controls how G-API reinterprets the data when converting
36  * it to IE Blob format (and which layout/etc is assigned to this data).
37  */
38 enum class TraitAs: int
39 {
40     TENSOR, //!< G-API traits an associated cv::Mat as a raw tensor and passes dimensions as-is
41     IMAGE   //!< G-API traits an associated cv::Mat as an image so creates an "image" blob (NCHW/NHWC, etc)
42 };
43
44 namespace detail {
45     struct ParamDesc {
46         std::string model_path;
47         std::string weights_path;
48         std::string device_id;
49
50         // NB: Here order follows the `Net` API
51         std::vector<std::string> input_names;
52         std::vector<std::string> output_names;
53
54         using ConstInput = std::pair<cv::Mat, TraitAs>;
55         std::unordered_map<std::string, ConstInput> const_inputs;
56
57         // NB: nun_* may differ from topology's real input/output port numbers
58         // (e.g. topology's partial execution)
59         std::size_t num_in;  // How many inputs are defined in the operation
60         std::size_t num_out; // How many outputs are defined in the operation
61     };
62 } // namespace detail
63
64 // FIXME: this is probably a shared (reusable) thing
65 template<typename Net>
66 struct PortCfg {
67     using In = std::array
68         < std::string
69         , std::tuple_size<typename Net::InArgs>::value >;
70     using Out = std::array
71         < std::string
72         , std::tuple_size<typename Net::OutArgs>::value >;
73 };
74
75 template<typename Net> class Params {
76 public:
77     Params(const std::string &model,
78            const std::string &weights,
79            const std::string &device)
80         : desc{ model, weights, device, {}, {}, {}
81               , std::tuple_size<typename Net::InArgs>::value  // num_in
82               , std::tuple_size<typename Net::OutArgs>::value // num_out
83               } {
84     };
85
86     Params<Net>& cfgInputLayers(const typename PortCfg<Net>::In &ll) {
87         desc.input_names.clear();
88         desc.input_names.reserve(ll.size());
89         std::copy(ll.begin(), ll.end(),
90                   std::back_inserter(desc.input_names));
91         return *this;
92     }
93
94     Params<Net>& cfgOutputLayers(const typename PortCfg<Net>::Out &ll) {
95         desc.output_names.clear();
96         desc.output_names.reserve(ll.size());
97         std::copy(ll.begin(), ll.end(),
98                   std::back_inserter(desc.output_names));
99         return *this;
100     }
101
102     Params<Net>& constInput(const std::string &layer_name,
103                             const cv::Mat &data,
104                             TraitAs hint = TraitAs::TENSOR) {
105         desc.const_inputs[layer_name] = {data, hint};
106         return *this;
107     }
108
109     // BEGIN(G-API's network parametrization API)
110     GBackend      backend() const { return cv::gapi::ie::backend();  }
111     std::string   tag()     const { return Net::tag(); }
112     cv::util::any params()  const { return { desc }; }
113     // END(G-API's network parametrization API)
114
115 protected:
116     detail::ParamDesc desc;
117 };
118
119 } // namespace ie
120 } // namespace gapi
121 } // namespace cv
122
123 #endif // OPENCV_GAPI_INFER_HPP