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.
5 // Copyright (C) 2019 Intel Corporation
7 #ifndef OPENCV_GAPI_INFER_IE_HPP
8 #define OPENCV_GAPI_INFER_IE_HPP
10 #include <unordered_map>
13 #include <tuple> // tuple, tuple_size
15 #include <opencv2/gapi/opencv_includes.hpp>
16 #include <opencv2/gapi/util/any.hpp>
18 #include <opencv2/core/cvdef.h> // GAPI_EXPORTS
19 #include <opencv2/gapi/gkernel.hpp> // GKernelPackage
23 // FIXME: introduce a new sub-namespace for NN?
26 GAPI_EXPORTS cv::gapi::GBackend backend();
29 * Specify how G-API and IE should trait input data
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.
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).
38 enum class TraitAs: int
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)
46 std::string model_path;
47 std::string weights_path;
48 std::string device_id;
50 // NB: Here order follows the `Net` API
51 std::vector<std::string> input_names;
52 std::vector<std::string> output_names;
54 using ConstInput = std::pair<cv::Mat, TraitAs>;
55 std::unordered_map<std::string, ConstInput> const_inputs;
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
64 // FIXME: this is probably a shared (reusable) thing
65 template<typename Net>
69 , std::tuple_size<typename Net::InArgs>::value >;
70 using Out = std::array
72 , std::tuple_size<typename Net::OutArgs>::value >;
75 template<typename Net> class Params {
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
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));
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));
102 Params<Net>& constInput(const std::string &layer_name,
104 TraitAs hint = TraitAs::TENSOR) {
105 desc.const_inputs[layer_name] = {data, hint};
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)
116 detail::ParamDesc desc;
123 #endif // OPENCV_GAPI_INFER_HPP