postproc.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #ifndef MODULES_INFERENCE_INCLUDE_POSTPROC_HPP_
  21. #define MODULES_INFERENCE_INCLUDE_POSTPROC_HPP_
  22. /**
  23. * \file postproc.hpp
  24. *
  25. * This file contains a declaration of class Postproc
  26. */
  27. #include <memory>
  28. #include <string>
  29. #include <unordered_map>
  30. #include <utility>
  31. #include <vector>
  32. #include "easyinfer/model_loader.h"
  33. #include "reflex_object.h"
  34. #include "cnstream_frame.hpp"
  35. #include "cnstream_frame_va.hpp"
  36. namespace cnstream {
  37. /**
  38. * @class Postproc
  39. *
  40. * @brief Postproc is the base class of post process.
  41. */
  42. class Postproc : virtual public ReflexObjectEx<Postproc> {
  43. public:
  44. /**
  45. * @brief Destructs an object.
  46. *
  47. * @return No return value.
  48. */
  49. virtual ~Postproc() = 0;
  50. /**
  51. * @brief Creates a postprocess object with the given postprocess's class name.
  52. *
  53. * @param[in] proc_name The postprocess class name.
  54. *
  55. * @return The pointer to postprocess object.
  56. */
  57. static Postproc* Create(const std::string& proc_name);
  58. /**
  59. * @brief Initializes postprocessing parameters.
  60. *
  61. * @param[in] params The postprocessing parameters.
  62. *
  63. * @return Returns ture for success, otherwise returns false.
  64. **/
  65. virtual bool Init(const std::unordered_map<std::string, std::string> &params) { return true; }
  66. /**
  67. * @brief Sets threshold.
  68. *
  69. * @param[in] threshold The value between 0 and 1.
  70. *
  71. * @return No return value.
  72. */
  73. void SetThreshold(const float threshold);
  74. /**
  75. * @brief Executes postproc on neural network outputs.
  76. *
  77. * @param[in] net_outputs Neural network outputs, and the data is stored on the host.
  78. * @param[in] model Model information including input shape and output shape.
  79. * @param[in,out] package Smart pointer of ``CNFrameInfo`` to store processed data.
  80. *
  81. * @return Returns 0 if successful, otherwise returns -1.
  82. *
  83. * @note
  84. * - This function is called by the Inferencer module when the parameter `mem_on_mlu_for_postproc`
  85. is set to false and `obj_infer` is set to false. See the Inferencer parameter description for details.
  86. */
  87. virtual int Execute(const std::vector<float*>& net_outputs, const std::shared_ptr<edk::ModelLoader>& model,
  88. const CNFrameInfoPtr& package) { return 0; }
  89. /**
  90. * @brief Execute post processing on neural network outputs.
  91. *
  92. * @param[in] net_outputs Neural network outputs, and the data is stored on the MLU.
  93. * @param[in] model Model information including input shape and output shape.
  94. * @param[in,out] packages The batched frames's result of postprocessing.
  95. *
  96. * @return Returns 0 if successful, otherwise returns -1.
  97. *
  98. * @note
  99. * - This function is called by the Inferencer module when the parameter ``mem_on_mlu_for_postproc``
  100. is set to true and ``obj_infer`` is set to false.
  101. See the Inferencer parameter description for details.
  102. */
  103. virtual int Execute(const std::vector<void*>& net_outputs, const std::shared_ptr<edk::ModelLoader>& model,
  104. const std::vector<CNFrameInfoPtr> &packages) { return 0; }
  105. protected:
  106. float threshold_ = 0;
  107. }; // class Postproc
  108. /**
  109. * @class ObjPostproc
  110. *
  111. * @brief ObjPostproc is the base class of object post processing.
  112. */
  113. class ObjPostproc : virtual public ReflexObjectEx<ObjPostproc> {
  114. public:
  115. /**
  116. * @brief Destructs an object.
  117. *
  118. * @return No return value.
  119. */
  120. virtual ~ObjPostproc() = 0;
  121. /**
  122. * @brief Creates a postprocess object with the given postprocess's class name.
  123. *
  124. * @param[in] proc_name The postprocess class name.
  125. *
  126. * @return The pointer to postprocess object.
  127. */
  128. static ObjPostproc* Create(const std::string& proc_name);
  129. /**
  130. * @brief Initializes postprocessing parameters.
  131. *
  132. * @param[in] params The postprocessing parameters.
  133. *
  134. * @return Returns ture for success, otherwise returns false.
  135. **/
  136. virtual bool Init(const std::unordered_map<std::string, std::string> &params) { return true; }
  137. /**
  138. * @brief Sets threshold.
  139. *
  140. * @param[in] threshold The value between 0 and 1.
  141. *
  142. * @return No return value.
  143. */
  144. void SetThreshold(const float threshold);
  145. /**
  146. * @brief Executes post processing on neural network outputs.
  147. *
  148. * @param[in] net_outputs Neural network outputs, and the data is stored on the host.
  149. * @param[in] model Model information including input shape and output shape.
  150. * @param[in,out] finfo Smart pointer of ``CNFrameInfo`` to store processed data.
  151. * @param[in] pobj The deduced object information.
  152. *
  153. * @return Returns 0 if successful, otherwise returns -1.
  154. *
  155. * @note
  156. * - This function is called by the Inferencer module when the parameter
  157. ``mem_on_mlu_for_postproc`` is set to false and ``obj_infer`` is set to true.
  158. See the Inferencer parameter description for details.
  159. */
  160. virtual int Execute(const std::vector<float*>& net_outputs, const std::shared_ptr<edk::ModelLoader>& model,
  161. const CNFrameInfoPtr& finfo, const std::shared_ptr<CNInferObject>& pobj) { return 0; }
  162. /**
  163. * @brief Execute post processing on neural network outputs.
  164. *
  165. * @param[in] net_outputs Neural network outputs, and the data is stored on the MLU.
  166. * @param[in] model Model information including input shape and output shape.
  167. * @param[in,out] obj_infos The batched frames's result of postprocessing.
  168. *
  169. * @return Returns 0 if successful, otherwise returns -1.
  170. *
  171. * @note
  172. * - This function is called by the Inferencer module when the parameter
  173. ``mem_on_mlu_for_postproc`` is set to true and ``obj_infer`` is set to true.
  174. See the Inferencer parameter description for details.
  175. */
  176. virtual int Execute(const std::vector<void*>& net_outputs, const std::shared_ptr<edk::ModelLoader>& model,
  177. const std::vector<std::pair<CNFrameInfoPtr, std::shared_ptr<CNInferObject>>>& obj_infos) {
  178. return 0;
  179. }
  180. protected:
  181. float threshold_ = 0;
  182. }; // class ObjPostproc
  183. } // namespace cnstream
  184. #endif // MODULES_INFERENCE_INCLUDE_POSTPROC_HPP_