github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/opencv4/include/opencv2/gapi/infer/onnx.hpp (about)

     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-2021 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  
    24  /**
    25   * @brief This namespace contains G-API ONNX Runtime backend functions, structures, and symbols.
    26   */
    27  namespace onnx {
    28  
    29  GAPI_EXPORTS cv::gapi::GBackend backend();
    30  
    31  enum class TraitAs: int {
    32      TENSOR, //!< G-API traits an associated cv::Mat as a raw tensor
    33              // and passes dimensions as-is
    34      IMAGE   //!< G-API traits an associated cv::Mat as an image so
    35              // creates an "image" blob (NCHW/NHWC, etc)
    36  };
    37  
    38  using PostProc = std::function<void(const std::unordered_map<std::string, cv::Mat> &,
    39                                            std::unordered_map<std::string, cv::Mat> &)>;
    40  
    41  namespace detail {
    42  /**
    43  * @brief This structure contains description of inference parameters
    44  * which is specific to ONNX models.
    45  */
    46  struct ParamDesc {
    47      std::string model_path; //!< Path to model.
    48  
    49      // NB: nun_* may differ from topology's real input/output port numbers
    50      // (e.g. topology's partial execution)
    51      std::size_t num_in;  //!< How many inputs are defined in the operation
    52      std::size_t num_out; //!< How many outputs are defined in the operation
    53  
    54      // NB: Here order follows the `Net` API
    55      std::vector<std::string> input_names; //!< Names of input network layers.
    56      std::vector<std::string> output_names; //!< Names of output network layers.
    57  
    58      using ConstInput = std::pair<cv::Mat, TraitAs>;
    59      std::unordered_map<std::string, ConstInput> const_inputs; //!< Map with pair of name of network layer and ConstInput which will be associated with this.
    60  
    61      std::vector<cv::Scalar> mean; //!< Mean values for preprocessing.
    62      std::vector<cv::Scalar> stdev; //!< Standard deviation values for preprocessing.
    63  
    64      std::vector<cv::GMatDesc> out_metas; //!< Out meta information about your output (type, dimension).
    65      PostProc custom_post_proc; //!< Post processing function.
    66  
    67      std::vector<bool> normalize; //!< Vector of bool values that enabled or disabled normalize of input data.
    68  
    69      std::vector<std::string> names_to_remap; //!< Names of output layers that will be processed in PostProc function.
    70  };
    71  } // namespace detail
    72  
    73  template<typename Net>
    74  struct PortCfg {
    75      using In = std::array
    76          < std::string
    77          , std::tuple_size<typename Net::InArgs>::value >;
    78      using Out = std::array
    79          < std::string
    80          , std::tuple_size<typename Net::OutArgs>::value >;
    81      using NormCoefs = std::array
    82          < cv::Scalar
    83          , std::tuple_size<typename Net::InArgs>::value >;
    84      using Normalize = std::array
    85          < bool
    86          , std::tuple_size<typename Net::InArgs>::value >;
    87  };
    88  
    89  /**
    90   * Contains description of inference parameters and kit of functions that
    91   * fill this parameters.
    92   */
    93  template<typename Net> class Params {
    94  public:
    95      /** @brief Class constructor.
    96  
    97      Constructs Params based on model information and sets default values for other
    98      inference description parameters.
    99  
   100      @param model Path to model (.onnx file).
   101      */
   102      Params(const std::string &model) {
   103          desc.model_path = model;
   104          desc.num_in  = std::tuple_size<typename Net::InArgs>::value;
   105          desc.num_out = std::tuple_size<typename Net::OutArgs>::value;
   106      };
   107  
   108      /** @brief Specifies sequence of network input layers names for inference.
   109  
   110      The function is used to associate data of graph inputs with input layers of
   111      network topology. Number of names has to match the number of network inputs. If a network
   112      has only one input layer, there is no need to call it as the layer is
   113      associated with input automatically but this doesn't prevent you from
   114      doing it yourself. Count of names has to match to number of network inputs.
   115  
   116      @param layer_names std::array<std::string, N> where N is the number of inputs
   117      as defined in the @ref G_API_NET. Contains names of input layers.
   118      @return the reference on modified object.
   119      */
   120      Params<Net>& cfgInputLayers(const typename PortCfg<Net>::In &layer_names) {
   121          desc.input_names.assign(layer_names.begin(), layer_names.end());
   122          return *this;
   123      }
   124  
   125      /** @brief Specifies sequence of output layers names for inference.
   126  
   127       The function is used to associate data of graph outputs with output layers of
   128      network topology. If a network has only one output layer, there is no need to call it
   129      as the layer is associated with ouput automatically but this doesn't prevent
   130      you from doing it yourself. Count of names has to match to number of network
   131      outputs or you can set your own output but for this case you have to
   132      additionally use @ref cfgPostProc function.
   133  
   134      @param layer_names std::array<std::string, N> where N is the number of outputs
   135      as defined in the @ref G_API_NET. Contains names of output layers.
   136      @return the reference on modified object.
   137      */
   138      Params<Net>& cfgOutputLayers(const typename PortCfg<Net>::Out &layer_names) {
   139          desc.output_names.assign(layer_names.begin(), layer_names.end());
   140          return *this;
   141      }
   142  
   143      /** @brief Sets a constant input.
   144  
   145      The function is used to set constant input. This input has to be
   146      a prepared tensor since preprocessing is disabled for this case. You should
   147      provide name of network layer which will receive provided data.
   148  
   149      @param layer_name Name of network layer.
   150      @param data cv::Mat that contains data which will be associated with network layer.
   151      @param hint Type of input (TENSOR).
   152      @return the reference on modified object.
   153      */
   154      Params<Net>& constInput(const std::string &layer_name,
   155                              const cv::Mat &data,
   156                              TraitAs hint = TraitAs::TENSOR) {
   157          desc.const_inputs[layer_name] = {data, hint};
   158          return *this;
   159      }
   160  
   161      /** @brief Specifies mean value and standard deviation for preprocessing.
   162  
   163      The function is used to set mean value and standard deviation for preprocessing
   164      of input data.
   165  
   166      @param m std::array<cv::Scalar, N> where N is the number of inputs
   167      as defined in the @ref G_API_NET. Contains mean values.
   168      @param s std::array<cv::Scalar, N> where N is the number of inputs
   169      as defined in the @ref G_API_NET. Contains standard deviation values.
   170      @return the reference on modified object.
   171      */
   172      Params<Net>& cfgMeanStd(const typename PortCfg<Net>::NormCoefs &m,
   173                              const typename PortCfg<Net>::NormCoefs &s) {
   174          desc.mean.assign(m.begin(), m.end());
   175          desc.stdev.assign(s.begin(), s.end());
   176          return *this;
   177      }
   178  
   179      /** @brief Configures graph output and provides the post processing function from user.
   180  
   181      The function is used when you work with networks with dynamic outputs.
   182      Since we can't know dimensions of inference result needs provide them for
   183      construction of graph output. This dimensions can differ from inference result.
   184      So you have to provide @ref PostProc function that gets information from inference
   185      result and fill output which is constructed by dimensions from out_metas.
   186  
   187      @param out_metas Out meta information about your output (type, dimension).
   188      @param remap_function Post processing function, which has two parameters. First is onnx
   189      result, second is graph output. Both parameters is std::map that contain pair of
   190      layer's name and cv::Mat.
   191      @return the reference on modified object.
   192      */
   193      Params<Net>& cfgPostProc(const std::vector<cv::GMatDesc> &out_metas,
   194                               const PostProc &remap_function) {
   195          desc.out_metas        = out_metas;
   196          desc.custom_post_proc = remap_function;
   197          return *this;
   198      }
   199  
   200      /** @overload
   201      Function with a rvalue parameters.
   202  
   203      @param out_metas rvalue out meta information about your output (type, dimension).
   204      @param remap_function rvalue post processing function, which has two parameters. First is onnx
   205      result, second is graph output. Both parameters is std::map that contain pair of
   206      layer's name and cv::Mat.
   207      @return the reference on modified object.
   208      */
   209      Params<Net>& cfgPostProc(std::vector<cv::GMatDesc> &&out_metas,
   210                               PostProc &&remap_function) {
   211          desc.out_metas        = std::move(out_metas);
   212          desc.custom_post_proc = std::move(remap_function);
   213          return *this;
   214      }
   215  
   216      /** @overload
   217      The function has additional parameter names_to_remap. This parameter provides
   218      information about output layers which will be used for inference and post
   219      processing function.
   220  
   221      @param out_metas Out meta information.
   222      @param remap_function Post processing function.
   223      @param names_to_remap Names of output layers. network's inference will
   224      be done on these layers. Inference's result will be processed in post processing
   225      function using these names.
   226      @return the reference on modified object.
   227      */
   228      Params<Net>& cfgPostProc(const std::vector<cv::GMatDesc> &out_metas,
   229                               const PostProc &remap_function,
   230                               const std::vector<std::string> &names_to_remap) {
   231          desc.out_metas        = out_metas;
   232          desc.custom_post_proc = remap_function;
   233          desc.names_to_remap   = names_to_remap;
   234          return *this;
   235      }
   236  
   237      /** @overload
   238      Function with a rvalue parameters and additional parameter names_to_remap.
   239  
   240      @param out_metas rvalue out meta information.
   241      @param remap_function rvalue post processing function.
   242      @param names_to_remap rvalue names of output layers. network's inference will
   243      be done on these layers. Inference's result will be processed in post processing
   244      function using these names.
   245      @return the reference on modified object.
   246      */
   247      Params<Net>& cfgPostProc(std::vector<cv::GMatDesc> &&out_metas,
   248                               PostProc &&remap_function,
   249                               std::vector<std::string> &&names_to_remap) {
   250          desc.out_metas        = std::move(out_metas);
   251          desc.custom_post_proc = std::move(remap_function);
   252          desc.names_to_remap   = std::move(names_to_remap);
   253          return *this;
   254      }
   255  
   256      /** @brief Specifies normalize parameter for preprocessing.
   257  
   258      The function is used to set normalize parameter for preprocessing of input data.
   259  
   260      @param normalizations std::array<cv::Scalar, N> where N is the number of inputs
   261      as defined in the @ref G_API_NET. Сontains bool values that enabled or disabled
   262      normalize of input data.
   263      @return the reference on modified object.
   264      */
   265      Params<Net>& cfgNormalize(const typename PortCfg<Net>::Normalize &normalizations) {
   266          desc.normalize.assign(normalizations.begin(), normalizations.end());
   267          return *this;
   268      }
   269  
   270      // BEGIN(G-API's network parametrization API)
   271      GBackend      backend() const { return cv::gapi::onnx::backend(); }
   272      std::string   tag()     const { return Net::tag(); }
   273      cv::util::any params()  const { return { desc }; }
   274      // END(G-API's network parametrization API)
   275  
   276  protected:
   277      detail::ParamDesc desc;
   278  };
   279  
   280  } // namespace onnx
   281  } // namespace gapi
   282  } // namespace cv
   283  
   284  #endif // OPENCV_GAPI_INFER_HPP