|
| 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) 2020 Intel Corporation |
| 6 | + |
| 7 | +#ifndef OPENCV_GAPI_INFER_ONNX_HPP |
| 8 | +#define OPENCV_GAPI_INFER_ONNX_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 | +namespace onnx { |
| 24 | + |
| 25 | +GAPI_EXPORTS cv::gapi::GBackend backend(); |
| 26 | + |
| 27 | +enum class TraitAs: int { |
| 28 | + TENSOR, //!< G-API traits an associated cv::Mat as a raw tensor |
| 29 | + // and passes dimensions as-is |
| 30 | + IMAGE //!< G-API traits an associated cv::Mat as an image so |
| 31 | + // creates an "image" blob (NCHW/NHWC, etc) |
| 32 | +}; |
| 33 | + |
| 34 | +using PostProc = std::function<void(const std::unordered_map<std::string, cv::Mat> &, |
| 35 | + std::unordered_map<std::string, cv::Mat> &)>; |
| 36 | + |
| 37 | + |
| 38 | +namespace detail { |
| 39 | +struct ParamDesc { |
| 40 | + std::string model_path; |
| 41 | + |
| 42 | + // NB: nun_* may differ from topology's real input/output port numbers |
| 43 | + // (e.g. topology's partial execution) |
| 44 | + std::size_t num_in; // How many inputs are defined in the operation |
| 45 | + std::size_t num_out; // How many outputs are defined in the operation |
| 46 | + |
| 47 | + // NB: Here order follows the `Net` API |
| 48 | + std::vector<std::string> input_names; |
| 49 | + std::vector<std::string> output_names; |
| 50 | + |
| 51 | + using ConstInput = std::pair<cv::Mat, TraitAs>; |
| 52 | + std::unordered_map<std::string, ConstInput> const_inputs; |
| 53 | + |
| 54 | + std::vector<cv::Scalar> mean; |
| 55 | + std::vector<cv::Scalar> stdev; |
| 56 | + |
| 57 | + std::vector<cv::GMatDesc> out_metas; |
| 58 | + PostProc custom_post_proc; |
| 59 | + |
| 60 | + std::vector<bool> normalize; |
| 61 | +}; |
| 62 | +} // namespace detail |
| 63 | + |
| 64 | +template<typename Net> |
| 65 | +struct PortCfg { |
| 66 | + using In = std::array |
| 67 | + < std::string |
| 68 | + , std::tuple_size<typename Net::InArgs>::value >; |
| 69 | + using Out = std::array |
| 70 | + < std::string |
| 71 | + , std::tuple_size<typename Net::OutArgs>::value >; |
| 72 | + using NormCoefs = std::array |
| 73 | + < cv::Scalar |
| 74 | + , std::tuple_size<typename Net::InArgs>::value >; |
| 75 | + using Normalize = std::array |
| 76 | + < bool |
| 77 | + , std::tuple_size<typename Net::InArgs>::value >; |
| 78 | +}; |
| 79 | + |
| 80 | +template<typename Net> class Params { |
| 81 | +public: |
| 82 | + Params(const std::string &model) { |
| 83 | + desc.model_path = model; |
| 84 | + desc.num_in = std::tuple_size<typename Net::InArgs>::value; |
| 85 | + desc.num_out = std::tuple_size<typename Net::OutArgs>::value; |
| 86 | + }; |
| 87 | + |
| 88 | + // BEGIN(G-API's network parametrization API) |
| 89 | + GBackend backend() const { return cv::gapi::onnx::backend(); } |
| 90 | + std::string tag() const { return Net::tag(); } |
| 91 | + cv::util::any params() const { return { desc }; } |
| 92 | + // END(G-API's network parametrization API) |
| 93 | + |
| 94 | + Params<Net>& cfgInputLayers(const typename PortCfg<Net>::In &ll) { |
| 95 | + desc.input_names.assign(ll.begin(), ll.end()); |
| 96 | + return *this; |
| 97 | + } |
| 98 | + |
| 99 | + Params<Net>& cfgOutputLayers(const typename PortCfg<Net>::Out &ll) { |
| 100 | + desc.output_names.assign(ll.begin(), ll.end()); |
| 101 | + return *this; |
| 102 | + } |
| 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 | + |
| 111 | + Params<Net>& cfgMeanStd(const typename PortCfg<Net>::NormCoefs &m, |
| 112 | + const typename PortCfg<Net>::NormCoefs &s) { |
| 113 | + desc.mean.assign(m.begin(), m.end()); |
| 114 | + desc.stdev.assign(s.begin(), s.end()); |
| 115 | + return *this; |
| 116 | + } |
| 117 | + |
| 118 | + Params<Net>& cfgPostProc(const std::vector<cv::GMatDesc> &outs, |
| 119 | + const PostProc &pp) { |
| 120 | + desc.out_metas = outs; |
| 121 | + desc.custom_post_proc = pp; |
| 122 | + return *this; |
| 123 | + } |
| 124 | + |
| 125 | + Params<Net>& cfgNormalize(const typename PortCfg<Net>::Normalize &n) { |
| 126 | + desc.normalize.assign(n.begin(), n.end()); |
| 127 | + return *this; |
| 128 | + } |
| 129 | + |
| 130 | +protected: |
| 131 | + detail::ParamDesc desc; |
| 132 | +}; |
| 133 | + |
| 134 | +} // namespace onnx |
| 135 | +} // namespace gapi |
| 136 | +} // namespace cv |
| 137 | + |
| 138 | +#endif // OPENCV_GAPI_INFER_HPP |
0 commit comments