// 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 #ifndef OPENCV_GAPI_INFER_ONNX_HPP #define OPENCV_GAPI_INFER_ONNX_HPP #include #include #include #include // tuple, tuple_size #include #include #include // GAPI_EXPORTS #include // GKernelPackage namespace cv { namespace gapi { namespace onnx { GAPI_EXPORTS cv::gapi::GBackend backend(); enum class TraitAs: int { TENSOR, //!< G-API traits an associated cv::Mat as a raw tensor // and passes dimensions as-is IMAGE //!< G-API traits an associated cv::Mat as an image so // creates an "image" blob (NCHW/NHWC, etc) }; using PostProc = std::function &, std::unordered_map &)>; namespace detail { struct ParamDesc { std::string model_path; // NB: nun_* may differ from topology's real input/output port numbers // (e.g. topology's partial execution) std::size_t num_in; // How many inputs are defined in the operation std::size_t num_out; // How many outputs are defined in the operation // NB: Here order follows the `Net` API std::vector input_names; std::vector output_names; using ConstInput = std::pair; std::unordered_map const_inputs; std::vector mean; std::vector stdev; std::vector out_metas; PostProc custom_post_proc; std::vector normalize; }; } // namespace detail template struct PortCfg { using In = std::array < std::string , std::tuple_size::value >; using Out = std::array < std::string , std::tuple_size::value >; using NormCoefs = std::array < cv::Scalar , std::tuple_size::value >; using Normalize = std::array < bool , std::tuple_size::value >; }; template class Params { public: Params(const std::string &model) { desc.model_path = model; desc.num_in = std::tuple_size::value; desc.num_out = std::tuple_size::value; }; // BEGIN(G-API's network parametrization API) GBackend backend() const { return cv::gapi::onnx::backend(); } std::string tag() const { return Net::tag(); } cv::util::any params() const { return { desc }; } // END(G-API's network parametrization API) Params& cfgInputLayers(const typename PortCfg::In &ll) { desc.input_names.assign(ll.begin(), ll.end()); return *this; } Params& cfgOutputLayers(const typename PortCfg::Out &ll) { desc.output_names.assign(ll.begin(), ll.end()); return *this; } Params& constInput(const std::string &layer_name, const cv::Mat &data, TraitAs hint = TraitAs::TENSOR) { desc.const_inputs[layer_name] = {data, hint}; return *this; } Params& cfgMeanStd(const typename PortCfg::NormCoefs &m, const typename PortCfg::NormCoefs &s) { desc.mean.assign(m.begin(), m.end()); desc.stdev.assign(s.begin(), s.end()); return *this; } Params& cfgPostProc(const std::vector &outs, const PostProc &pp) { desc.out_metas = outs; desc.custom_post_proc = pp; return *this; } Params& cfgNormalize(const typename PortCfg::Normalize &n) { desc.normalize.assign(n.begin(), n.end()); return *this; } protected: detail::ParamDesc desc; }; } // namespace onnx } // namespace gapi } // namespace cv #endif // OPENCV_GAPI_INFER_HPP