gcompiled.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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) 2018 Intel Corporation
  6. #ifndef OPENCV_GAPI_GCOMPILED_HPP
  7. #define OPENCV_GAPI_GCOMPILED_HPP
  8. #include <vector>
  9. #include "opencv2/gapi/opencv_includes.hpp"
  10. #include "opencv2/gapi/own/assert.hpp"
  11. #include "opencv2/gapi/garg.hpp"
  12. namespace cv {
  13. // This class represents a compiled computation.
  14. // In theory (and ideally), it can be used w/o the rest of APIs.
  15. // In theory (and ideally), it can be serialized/deserialized.
  16. // It can enable scenarious like deployment to an autonomous devince, FuSa, etc.
  17. //
  18. // Currently GCompiled assumes all GMats you used to pass data to G-API
  19. // are valid and not destroyed while you use a GCompiled object.
  20. //
  21. // FIXME: In future, there should be a way to name I/O objects and specify it
  22. // to GCompiled externally (for example, when it is loaded on the target system).
  23. /**
  24. * \addtogroup gapi_main_classes
  25. * @{
  26. */
  27. /**
  28. * @brief Represents a compiled computation (graph). Can only be used
  29. * with image / data formats & resolutions it was compiled for, with
  30. * some exceptions.
  31. *
  32. * This class represents a product of graph compilation (calling
  33. * cv::GComputation::compile()). Objects of this class actually do
  34. * data processing, and graph execution is incapsulated into objects
  35. * of this class. Execution model itself depends on kernels and
  36. * backends which were using during the compilation, see @ref
  37. * gapi_compile_args for details.
  38. *
  39. * In a general case, GCompiled objects can be applied to data only in
  40. * that formats/resolutions they were compiled for (see @ref
  41. * gapi_meta_args). However, if the underlying backends allow, a
  42. * compiled object can be _reshaped_ to handle data (images) of
  43. * different resolution, though formats and types must remain the same.
  44. *
  45. * GCompiled is very similar to `std::function<>` in its semantics --
  46. * running it looks like a function call in the user code.
  47. *
  48. * At the moment, GCompiled objects are not reentrant -- generally,
  49. * the objects are stateful since graph execution itself is a stateful
  50. * process and this state is now maintained in GCompiled's own memory
  51. * (not on the process stack).
  52. *
  53. * At the same time, two different GCompiled objects produced from the
  54. * single cv::GComputation are completely independent and can be used
  55. * concurrently.
  56. */
  57. class GAPI_EXPORTS GCompiled
  58. {
  59. public:
  60. /// @private
  61. class GAPI_EXPORTS Priv;
  62. /**
  63. * @brief Constructs an empty object
  64. */
  65. GCompiled();
  66. /**
  67. * @brief Run the compiled computation, a generic version.
  68. *
  69. * @param ins vector of inputs to process.
  70. * @param outs vector of outputs to produce.
  71. *
  72. * Input/output vectors must have the same number of elements as
  73. * defined in the cv::GComputation protocol (at the moment of its
  74. * construction). Shapes of elements also must conform to protocol
  75. * (e.g. cv::Mat needs to be passed where cv::GMat has been
  76. * declared as input, and so on). Run-time exception is generated
  77. * otherwise.
  78. *
  79. * Objects in output vector may remain empty (like cv::Mat) --
  80. * G-API will automatically initialize output objects to proper formats.
  81. *
  82. * @note Don't construct GRunArgs/GRunArgsP objects manually, use
  83. * cv::gin()/cv::gout() wrappers instead.
  84. */
  85. void operator() (GRunArgs &&ins, GRunArgsP &&outs); // Generic arg-to-arg
  86. #if !defined(GAPI_STANDALONE)
  87. /**
  88. * @brief Execute an unary computation
  89. *
  90. * @overload
  91. * @param in input cv::Mat for unary computation
  92. * @param out output cv::Mat for unary computation
  93. * process.
  94. */
  95. void operator() (cv::Mat in, cv::Mat &out); // Unary overload
  96. /**
  97. * @brief Execute an unary computation
  98. *
  99. * @overload
  100. * @param in input cv::Mat for unary computation
  101. * @param out output cv::Scalar for unary computation
  102. * process.
  103. */
  104. void operator() (cv::Mat in, cv::Scalar &out); // Unary overload (scalar)
  105. /**
  106. * @brief Execute a binary computation
  107. *
  108. * @overload
  109. * @param in1 first input cv::Mat for binary computation
  110. * @param in2 second input cv::Mat for binary computation
  111. * @param out output cv::Mat for binary computation
  112. * process.
  113. */
  114. void operator() (cv::Mat in1, cv::Mat in2, cv::Mat &out); // Binary overload
  115. /**
  116. * @brief Execute an binary computation
  117. *
  118. * @overload
  119. * @param in1 first input cv::Mat for binary computation
  120. * @param in2 second input cv::Mat for binary computation
  121. * @param out output cv::Scalar for binary computation
  122. * process.
  123. */
  124. void operator() (cv::Mat in1, cv::Mat in2, cv::Scalar &out); // Binary overload (scalar)
  125. /**
  126. * @brief Execute a computation with arbitrary number of
  127. * inputs/outputs.
  128. *
  129. * @overload
  130. * @param ins vector of input cv::Mat objects to process by the
  131. * computation.
  132. * @param outs vector of output cv::Mat objects to produce by the
  133. * computation.
  134. *
  135. * Numbers of elements in ins/outs vectos must match numbers of
  136. * inputs/outputs which were used to define the source GComputation.
  137. */
  138. void operator() (const std::vector<cv::Mat> &ins, // Compatibility overload
  139. const std::vector<cv::Mat> &outs);
  140. #endif // !defined(GAPI_STANDALONE)
  141. /// @private
  142. Priv& priv();
  143. /**
  144. * @brief Check if compiled object is valid (non-empty)
  145. *
  146. * @return true if the object is runnable (valid), false otherwise
  147. */
  148. explicit operator bool () const;
  149. /**
  150. * @brief Vector of metadata this graph was compiled for.
  151. *
  152. * @return Unless _reshape_ is not supported, return value is the
  153. * same vector which was passed to cv::GComputation::compile() to
  154. * produce this compiled object. Otherwise, it is the latest
  155. * metadata vector passed to reshape() (if that call was
  156. * successful).
  157. */
  158. const GMetaArgs& metas() const; // Meta passed to compile()
  159. /**
  160. * @brief Vector of metadata descriptions of graph outputs
  161. *
  162. * @return vector with formats/resolutions of graph's output
  163. * objects, auto-inferred from input metadata vector by
  164. * operations which form this computation.
  165. *
  166. * @note GCompiled objects produced from the same
  167. * cv::GComputiation graph with different input metas may return
  168. * different values in this vector.
  169. */
  170. const GMetaArgs& outMetas() const;
  171. /**
  172. * @brief Check if the underlying backends support reshape or not.
  173. *
  174. * @return true if supported, false otherwise.
  175. */
  176. bool canReshape() const;
  177. /**
  178. * @brief Reshape a compiled graph to support new image
  179. * resolutions.
  180. *
  181. * Throws an exception if an error occurs.
  182. *
  183. * @param inMetas new metadata to reshape on. Vector size and
  184. * metadata shapes must match the computation's protocol.
  185. * @param args compilation arguments to use.
  186. */
  187. // FIXME: Why it requires compile args?
  188. void reshape(const GMetaArgs& inMetas, const GCompileArgs& args);
  189. protected:
  190. /// @private
  191. std::shared_ptr<Priv> m_priv;
  192. };
  193. /** @} */
  194. }
  195. #endif // OPENCV_GAPI_GCOMPILED_HPP