onnx.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #ifndef OPENCV_GAPI_INFER_ONNX_HPP
  7. #define OPENCV_GAPI_INFER_ONNX_HPP
  8. #include <unordered_map>
  9. #include <string>
  10. #include <array>
  11. #include <tuple> // tuple, tuple_size
  12. #include <opencv2/gapi/opencv_includes.hpp>
  13. #include <opencv2/gapi/util/any.hpp>
  14. #include <opencv2/core/cvdef.h> // GAPI_EXPORTS
  15. #include <opencv2/gapi/gkernel.hpp> // GKernelPackage
  16. namespace cv {
  17. namespace gapi {
  18. namespace onnx {
  19. GAPI_EXPORTS cv::gapi::GBackend backend();
  20. enum class TraitAs: int {
  21. TENSOR, //!< G-API traits an associated cv::Mat as a raw tensor
  22. // and passes dimensions as-is
  23. IMAGE //!< G-API traits an associated cv::Mat as an image so
  24. // creates an "image" blob (NCHW/NHWC, etc)
  25. };
  26. using PostProc = std::function<void(const std::unordered_map<std::string, cv::Mat> &,
  27. std::unordered_map<std::string, cv::Mat> &)>;
  28. namespace detail {
  29. struct ParamDesc {
  30. std::string model_path;
  31. // NB: nun_* may differ from topology's real input/output port numbers
  32. // (e.g. topology's partial execution)
  33. std::size_t num_in; // How many inputs are defined in the operation
  34. std::size_t num_out; // How many outputs are defined in the operation
  35. // NB: Here order follows the `Net` API
  36. std::vector<std::string> input_names;
  37. std::vector<std::string> output_names;
  38. using ConstInput = std::pair<cv::Mat, TraitAs>;
  39. std::unordered_map<std::string, ConstInput> const_inputs;
  40. std::vector<cv::Scalar> mean;
  41. std::vector<cv::Scalar> stdev;
  42. std::vector<cv::GMatDesc> out_metas;
  43. PostProc custom_post_proc;
  44. std::vector<bool> normalize;
  45. };
  46. } // namespace detail
  47. template<typename Net>
  48. struct PortCfg {
  49. using In = std::array
  50. < std::string
  51. , std::tuple_size<typename Net::InArgs>::value >;
  52. using Out = std::array
  53. < std::string
  54. , std::tuple_size<typename Net::OutArgs>::value >;
  55. using NormCoefs = std::array
  56. < cv::Scalar
  57. , std::tuple_size<typename Net::InArgs>::value >;
  58. using Normalize = std::array
  59. < bool
  60. , std::tuple_size<typename Net::InArgs>::value >;
  61. };
  62. template<typename Net> class Params {
  63. public:
  64. Params(const std::string &model) {
  65. desc.model_path = model;
  66. desc.num_in = std::tuple_size<typename Net::InArgs>::value;
  67. desc.num_out = std::tuple_size<typename Net::OutArgs>::value;
  68. };
  69. // BEGIN(G-API's network parametrization API)
  70. GBackend backend() const { return cv::gapi::onnx::backend(); }
  71. std::string tag() const { return Net::tag(); }
  72. cv::util::any params() const { return { desc }; }
  73. // END(G-API's network parametrization API)
  74. Params<Net>& cfgInputLayers(const typename PortCfg<Net>::In &ll) {
  75. desc.input_names.assign(ll.begin(), ll.end());
  76. return *this;
  77. }
  78. Params<Net>& cfgOutputLayers(const typename PortCfg<Net>::Out &ll) {
  79. desc.output_names.assign(ll.begin(), ll.end());
  80. return *this;
  81. }
  82. Params<Net>& constInput(const std::string &layer_name,
  83. const cv::Mat &data,
  84. TraitAs hint = TraitAs::TENSOR) {
  85. desc.const_inputs[layer_name] = {data, hint};
  86. return *this;
  87. }
  88. Params<Net>& cfgMeanStd(const typename PortCfg<Net>::NormCoefs &m,
  89. const typename PortCfg<Net>::NormCoefs &s) {
  90. desc.mean.assign(m.begin(), m.end());
  91. desc.stdev.assign(s.begin(), s.end());
  92. return *this;
  93. }
  94. Params<Net>& cfgPostProc(const std::vector<cv::GMatDesc> &outs,
  95. const PostProc &pp) {
  96. desc.out_metas = outs;
  97. desc.custom_post_proc = pp;
  98. return *this;
  99. }
  100. Params<Net>& cfgNormalize(const typename PortCfg<Net>::Normalize &n) {
  101. desc.normalize.assign(n.begin(), n.end());
  102. return *this;
  103. }
  104. protected:
  105. detail::ParamDesc desc;
  106. };
  107. } // namespace onnx
  108. } // namespace gapi
  109. } // namespace cv
  110. #endif // OPENCV_GAPI_INFER_HPP