// 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) 2019-2021 Intel Corporation #ifndef OPENCV_GAPI_INFER_HPP #define OPENCV_GAPI_INFER_HPP // FIXME: Inference API is currently only available in full mode #if !defined(GAPI_STANDALONE) #include #include // string #include // tuple #include // is_same, false_type #include // all_satisfy #include // any<> #include // GKernelType[M], GBackend #include // GArg #include // CompileArgTag #include // GMetaArg namespace cv { template class GNetworkType; namespace detail { // Infer /////////////////////////////////////////////////////////////////////// template struct accepted_infer_types { static constexpr const auto value = std::is_same::type, cv::GMat>::value || std::is_same::type, cv::GFrame>::value; }; template using valid_infer_types = all_satisfy; // Infer2 ////////////////////////////////////////////////////////////////////// template struct valid_infer2_types; // Terminal case 1 (50/50 success) template struct valid_infer2_types< std::tuple, std::tuple > { // By default, Nets are limited to GMat argument types only // for infer2, every GMat argument may translate to either // GArray or GArray. GArray<> part is stripped // already at this point. static constexpr const auto value = std::is_same::type, cv::GMat>::value || std::is_same::type, cv::Rect>::value; }; // Terminal case 2 (100% failure) template struct valid_infer2_types< std::tuple<>, std::tuple > : public std::false_type { }; // Terminal case 3 (100% failure) template struct valid_infer2_types< std::tuple, std::tuple<> > : public std::false_type { }; // Recursion -- generic template struct valid_infer2_types< std::tuple, std::tuple > { static constexpr const auto value = valid_infer2_types< std::tuple, std::tuple >::value && valid_infer2_types< std::tuple, std::tuple >::value; }; // Struct stores network input/output names. // Used by infer struct InOutInfo { std::vector in_names; std::vector out_names; }; template class GInferOutputsTyped { public: GInferOutputsTyped() = default; GInferOutputsTyped(std::shared_ptr call) : m_priv(std::make_shared(std::move(call))) { } OutT at(const std::string& name) { auto it = m_priv->blobs.find(name); if (it == m_priv->blobs.end()) { // FIXME: Avoid modifying GKernel auto shape = cv::detail::GTypeTraits::shape; m_priv->call->kernel().outShapes.push_back(shape); m_priv->call->kernel().outCtors.emplace_back(cv::detail::GObtainCtor::get()); auto out_idx = static_cast(m_priv->blobs.size()); it = m_priv->blobs.emplace(name, cv::detail::Yield::yield(*(m_priv->call), out_idx)).first; m_priv->info->out_names.push_back(name); } return it->second; } private: struct Priv { Priv(std::shared_ptr c) : call(std::move(c)), info(cv::util::any_cast(&call->params())) { } std::shared_ptr call; InOutInfo* info = nullptr; std::unordered_map blobs; }; std::shared_ptr m_priv; }; template class GInferInputsTyped { public: GInferInputsTyped() : m_priv(std::make_shared()) { } template GInferInputsTyped& setInput(const std::string& name, U in) { m_priv->blobs.emplace(std::piecewise_construct, std::forward_as_tuple(name), std::forward_as_tuple(in)); return *this; } using StorageT = cv::util::variant; StorageT& operator[](const std::string& name) { return m_priv->blobs[name]; } using Map = std::unordered_map; const Map& getBlobs() const { return m_priv->blobs; } private: struct Priv { std::unordered_map blobs; }; std::shared_ptr m_priv; }; template std::shared_ptr makeCall(const std::string &tag, std::vector &&args, std::vector &&names, cv::GKinds &&kinds) { auto call = std::make_shared(GKernel{ InferT::id(), tag, InferT::getOutMeta, {}, // outShape will be filled later std::move(kinds), {}, // outCtors will be filled later }); call->setArgs(std::move(args)); call->params() = cv::detail::InOutInfo{std::move(names), {}}; return call; } } // namespace detail // TODO: maybe tuple_wrap_helper from util.hpp may help with this. // Multiple-return-value network definition (specialized base class) template class GNetworkType(Args...)> > { public: using InArgs = std::tuple; using OutArgs = std::tuple; using Result = OutArgs; using API = std::function; using ResultL = std::tuple< cv::GArray... >; }; // Single-return-value network definition (specialized base class) template class GNetworkType > { public: using InArgs = std::tuple; using OutArgs = std::tuple; using Result = R; using API = std::function; using ResultL = cv::GArray; }; // InferAPI: Accepts either GMat or GFrame for very individual network's input template struct InferAPI { using type = typename std::enable_if < detail::valid_infer_types::value && std::tuple_size::value == sizeof...(Ts) , std::function >::type; }; // InferAPIRoi: Accepts a rectangle and either GMat or GFrame template struct InferAPIRoi { using type = typename std::enable_if < detail::valid_infer_types::value && std::tuple_size::value == 1u , std::function, T)> >::type; }; // InferAPIList: Accepts a list of rectangles and list of GMat/GFrames; // crops every input. template struct InferAPIList { using type = typename std::enable_if < detail::valid_infer_types::value && std::tuple_size::value == sizeof...(Ts) , std::function, Ts...)> >::type; }; // APIList2 is also template to allow different calling options // (GArray vs GArray per input) template struct InferAPIList2 { using type = typename std::enable_if < detail::valid_infer_types::value && cv::detail::valid_infer2_types< typename Net::InArgs , std::tuple >::value, std::function...)> >::type; }; // Base "Infer" kernel. Note - for whatever network, kernel ID // is always the same. Different inference calls are distinguished by // network _tag_ (an extra field in GCall) // // getOutMeta is a stub callback collected by G-API kernel subsystem // automatically. This is a rare case when this callback is defined by // a particular backend, not by a network itself. struct GInferBase { static constexpr const char * id() { return "org.opencv.dnn.infer"; // Universal stub } static GMetaArgs getOutMeta(const GMetaArgs &, const GArgs &) { return GMetaArgs{}; // One more universal stub } }; // Base "InferROI" kernel. // All notes from "Infer" kernel apply here as well. struct GInferROIBase { static constexpr const char * id() { return "org.opencv.dnn.infer-roi"; // Universal stub } static GMetaArgs getOutMeta(const GMetaArgs &, const GArgs &) { return GMetaArgs{}; // One more universal stub } }; // Base "Infer list" kernel. // All notes from "Infer" kernel apply here as well. struct GInferListBase { static constexpr const char * id() { return "org.opencv.dnn.infer-roi-list-1"; // Universal stub } static GMetaArgs getOutMeta(const GMetaArgs &, const GArgs &) { return GMetaArgs{}; // One more universal stub } }; // Base "Infer list 2" kernel. // All notes from "Infer" kernel apply here as well. struct GInferList2Base { static constexpr const char * id() { return "org.opencv.dnn.infer-roi-list-2"; // Universal stub } static GMetaArgs getOutMeta(const GMetaArgs &, const GArgs &) { return GMetaArgs{}; // One more universal stub } }; // A generic inference kernel. API (::on()) is fully defined by the Net // template parameter. // Acts as a regular kernel in graph (via KernelTypeMedium). template struct GInfer final : public GInferBase , public detail::KernelTypeMedium< GInfer , typename InferAPI::type > { using GInferBase::getOutMeta; // FIXME: name lookup conflict workaround? static constexpr const char* tag() { return Net::tag(); } }; // A specific roi-inference kernel. API (::on()) is fixed here and // verified against Net. template struct GInferROI final : public GInferROIBase , public detail::KernelTypeMedium< GInferROI , typename InferAPIRoi::type > { using GInferROIBase::getOutMeta; // FIXME: name lookup conflict workaround? static constexpr const char* tag() { return Net::tag(); } }; // A generic roi-list inference kernel. API (::on()) is derived from // the Net template parameter (see more in infer<> overload). template struct GInferList final : public GInferListBase , public detail::KernelTypeMedium< GInferList , typename InferAPIList::type > { using GInferListBase::getOutMeta; // FIXME: name lookup conflict workaround? static constexpr const char* tag() { return Net::tag(); } }; // An even more generic roi-list inference kernel. API (::on()) is // derived from the Net template parameter (see more in infer<> // overload). // Takes an extra variadic template list to reflect how this network // was called (with Rects or GMats as array parameters) template struct GInferList2 final : public GInferList2Base , public detail::KernelTypeMedium< GInferList2 , typename InferAPIList2::type > { using GInferList2Base::getOutMeta; // FIXME: name lookup conflict workaround? static constexpr const char* tag() { return Net::tag(); } }; /** * @brief G-API object used to collect network inputs */ using GInferInputs = cv::detail::GInferInputsTyped; /** * @brief G-API object used to collect the list of network inputs */ using GInferListInputs = cv::detail::GInferInputsTyped, cv::GArray>; /** * @brief G-API object used to collect network outputs */ using GInferOutputs = cv::detail::GInferOutputsTyped; /** * @brief G-API object used to collect the list of network outputs */ using GInferListOutputs = cv::detail::GInferOutputsTyped>; namespace detail { void inline unpackBlobs(const cv::GInferInputs::Map& blobs, std::vector& args, std::vector& names, cv::GKinds& kinds) { for (auto&& p : blobs) { names.emplace_back(p.first); switch (p.second.index()) { case cv::GInferInputs::StorageT::index_of(): args.emplace_back(cv::util::get(p.second)); kinds.emplace_back(cv::detail::OpaqueKind::CV_MAT); break; case cv::GInferInputs::StorageT::index_of(): args.emplace_back(cv::util::get(p.second)); kinds.emplace_back(cv::detail::OpaqueKind::CV_UNKNOWN); break; default: GAPI_Assert(false); } } } template struct InferROITraits; template <> struct InferROITraits { using outType = cv::GInferOutputs; using inType = cv::GOpaque; }; template <> struct InferROITraits { using outType = cv::GInferListOutputs; using inType = cv::GArray; }; template typename InferROITraits::outType inferGenericROI(const std::string& tag, const typename InferROITraits::inType& in, const cv::GInferInputs& inputs) { std::vector args; std::vector names; cv::GKinds kinds; args.emplace_back(in); kinds.emplace_back(cv::detail::OpaqueKind::CV_RECT); unpackBlobs(inputs.getBlobs(), args, names, kinds); auto call = cv::detail::makeCall(tag, std::move(args), std::move(names), std::move(kinds)); return {std::move(call)}; } } // namespace detail } // namespace cv // FIXME: Probably the signature makes a function/tuple/function round-trip #define G_API_NET(Class, API, Tag) \ struct Class final: public cv::GNetworkType { \ static constexpr const char * tag() { return Tag; } \ } namespace cv { namespace gapi { /** @brief Calculates response for the specified network (template * parameter) for the specified region in the source image. * Currently expects a single-input network only. * * @tparam A network type defined with G_API_NET() macro. * @param in input image where to take ROI from. * @param roi an object describing the region of interest * in the source image. May be calculated in the same graph dynamically. * @return an object of return type as defined in G_API_NET(). * If a network has multiple return values (defined with a tuple), a tuple of * objects of appropriate type is returned. * @sa G_API_NET() */ template typename Net::Result infer(cv::GOpaque roi, T in) { return GInferROI::on(roi, in); } /** @brief Calculates responses for the specified network (template * parameter) for every region in the source image. * * @tparam A network type defined with G_API_NET() macro. * @param roi a list of rectangles describing regions of interest * in the source image. Usually an output of object detector or tracker. * @param args network's input parameters as specified in G_API_NET() macro. * NOTE: verified to work reliably with 1-input topologies only. * @return a list of objects of return type as defined in G_API_NET(). * If a network has multiple return values (defined with a tuple), a tuple of * GArray<> objects is returned with the appropriate types inside. * @sa G_API_NET() */ template typename Net::ResultL infer(cv::GArray roi, Args&&... args) { return GInferList::on(roi, std::forward(args)...); } /** @brief Calculates responses for the specified network (template * parameter) for every region in the source image, extended version. * * @tparam A network type defined with G_API_NET() macro. * @param image A source image containing regions of interest * @param args GArray<> objects of cv::Rect or cv::GMat, one per every * network input: * - If a cv::GArray is passed, the appropriate * regions are taken from `image` and preprocessed to this particular * network input; * - If a cv::GArray is passed, the underlying data traited * as tensor (no automatic preprocessing happen). * @return a list of objects of return type as defined in G_API_NET(). * If a network has multiple return values (defined with a tuple), a tuple of * GArray<> objects is returned with the appropriate types inside. * @sa G_API_NET() */ template typename Net::ResultL infer2(T image, cv::GArray... args) { // FIXME: Declared as "2" because in the current form it steals // overloads from the regular infer return GInferList2::on(image, args...); } /** * @brief Calculates response for the specified network (template * parameter) given the input data. * * @tparam A network type defined with G_API_NET() macro. * @param args network's input parameters as specified in G_API_NET() macro. * @return an object of return type as defined in G_API_NET(). * If a network has multiple return values (defined with a tuple), a tuple of * objects of appropriate type is returned. * @sa G_API_NET() */ template typename Net::Result infer(Args&&... args) { return GInfer::on(std::forward(args)...); } /** * @brief Generic network type: input and output layers are configured dynamically at runtime * * Unlike the network types defined with G_API_NET macro, this one * doesn't fix number of network inputs and outputs at the compilation stage * thus providing user with an opportunity to program them in runtime. */ struct Generic { }; /** * @brief Calculates response for generic network * * @param tag a network tag * @param inputs networks's inputs * @return a GInferOutputs */ template cv::GInferOutputs infer(const std::string& tag, const cv::GInferInputs& inputs) { std::vector args; std::vector names; cv::GKinds kinds; cv::detail::unpackBlobs(inputs.getBlobs(), args, names, kinds); auto call = cv::detail::makeCall(tag, std::move(args), std::move(names), std::move(kinds)); return cv::GInferOutputs{std::move(call)}; } /** @brief Calculates response for the generic network * for the specified region in the source image. * Currently expects a single-input network only. * * @param tag a network tag * @param roi a an object describing the region of interest * in the source image. May be calculated in the same graph dynamically. * @param inputs networks's inputs * @return a cv::GInferOutputs */ template cv::GInferOutputs infer(const std::string& tag, const cv::GOpaque& roi, const cv::GInferInputs& inputs) { return cv::detail::inferGenericROI(tag, roi, inputs); } /** @brief Calculates responses for the specified network * for every region in the source image. * * @param tag a network tag * @param rois a list of rectangles describing regions of interest * in the source image. Usually an output of object detector or tracker. * @param inputs networks's inputs * @return a cv::GInferListOutputs */ template cv::GInferListOutputs infer(const std::string& tag, const cv::GArray& rois, const cv::GInferInputs& inputs) { return cv::detail::inferGenericROI(tag, rois, inputs); } /** @brief Calculates responses for the specified network * for every region in the source image, extended version. * * @param tag a network tag * @param in a source image containing regions of interest. * @param inputs networks's inputs * @return a cv::GInferListOutputs */ template typename std::enable_if::value, cv::GInferListOutputs>::type infer2(const std::string& tag, const Input& in, const cv::GInferListInputs& inputs) { std::vector args; std::vector names; cv::GKinds kinds; args.emplace_back(in); auto k = cv::detail::GOpaqueTraits::kind; kinds.emplace_back(k); for (auto&& p : inputs.getBlobs()) { names.emplace_back(p.first); switch (p.second.index()) { case cv::GInferListInputs::StorageT::index_of>(): args.emplace_back(cv::util::get>(p.second)); kinds.emplace_back(cv::detail::OpaqueKind::CV_MAT); break; case cv::GInferListInputs::StorageT::index_of>(): args.emplace_back(cv::util::get>(p.second)); kinds.emplace_back(cv::detail::OpaqueKind::CV_RECT); break; default: GAPI_Assert(false); } } auto call = cv::detail::makeCall(tag, std::move(args), std::move(names), std::move(kinds)); return cv::GInferListOutputs{std::move(call)}; } } // namespace gapi } // namespace cv #endif // GAPI_STANDALONE namespace cv { namespace gapi { // Note: the below code _is_ part of STANDALONE build, // just to make our compiler code compileable. // A type-erased form of network parameters. // Similar to how a type-erased GKernel is represented and used. /// @private struct GAPI_EXPORTS_W_SIMPLE GNetParam { std::string tag; // FIXME: const? GBackend backend; // Specifies the execution model util::any params; // Backend-interpreted parameter structure }; /** \addtogroup gapi_compile_args * @{ */ /** * @brief A container class for network configurations. Similar to * GKernelPackage. Use cv::gapi::networks() to construct this object. * * @sa cv::gapi::networks */ struct GAPI_EXPORTS_W_SIMPLE GNetPackage { GAPI_WRAP GNetPackage() = default; GAPI_WRAP explicit GNetPackage(std::vector nets); explicit GNetPackage(std::initializer_list ii); std::vector backends() const; std::vector networks; }; /** @} gapi_compile_args */ } // namespace gapi namespace detail { template gapi::GNetParam strip(T&& t) { return gapi::GNetParam { t.tag() , t.backend() , t.params() }; } template<> struct CompileArgTag { static const char* tag() { return "gapi.net_package"; } }; } // namespace cv::detail namespace gapi { template cv::gapi::GNetPackage networks(Args&&... args) { return cv::gapi::GNetPackage({ cv::detail::strip(args)... }); } inline cv::gapi::GNetPackage& operator += ( cv::gapi::GNetPackage& lhs, const cv::gapi::GNetPackage& rhs) { lhs.networks.reserve(lhs.networks.size() + rhs.networks.size()); lhs.networks.insert(lhs.networks.end(), rhs.networks.begin(), rhs.networks.end()); return lhs; } } // namespace gapi } // namespace cv #endif // OPENCV_GAPI_INFER_HPP