preproc.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_PREPROC_HPP_
  21. #define MODULES_INFERENCE_INCLUDE_PREPROC_HPP_
  22. /**
  23. * \file preproc.hpp
  24. *
  25. * This file contains a declaration of class Preproc
  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 "cnstream_frame.hpp"
  34. #include "cnstream_frame_va.hpp"
  35. #include "reflex_object.h"
  36. namespace cnstream {
  37. /**
  38. * @class Preproc
  39. *
  40. * @brief Preproc is the base class of neural network preprocessing for inference module.
  41. */
  42. class Preproc : virtual public ReflexObjectEx<Preproc> {
  43. public:
  44. /**
  45. * @brief Destructs an object.
  46. *
  47. * @return No return value.
  48. */
  49. virtual ~Preproc() {}
  50. /**
  51. * @brief Creates a preprocess object with the given preprocess's class name.
  52. *
  53. * @param[in] proc_name The preprocess class name.
  54. *
  55. * @return Returns the pointer to preprocess object.
  56. */
  57. static Preproc* Create(const std::string& proc_name);
  58. /**
  59. * @brief Initializes preprocessing parameters.
  60. *
  61. * @param[in] params The preprocessing 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 Executes preprocess on neural network inputs.
  68. *
  69. * @param[out] net_inputs Neural network inputs.
  70. * @param[in] model Model information including input shape and output shape.
  71. * @param[in] package Smart pointer of ``CNFrameInfo`` which stores origin data.
  72. *
  73. * @return Returns 0 if successful, otherwise returns -1.
  74. */
  75. virtual int Execute(const std::vector<float*>& net_inputs, const std::shared_ptr<edk::ModelLoader>& model,
  76. const CNFrameInfoPtr& package) = 0;
  77. }; // class Preproc
  78. /**
  79. * @class ObjPreproc
  80. *
  81. * @brief ObjPreproc is the base class of preprocess for object.
  82. */
  83. class ObjPreproc : virtual public ReflexObjectEx<ObjPreproc> {
  84. public:
  85. /**
  86. * @brief Destructs an object.
  87. *
  88. * @return No return value.
  89. */
  90. virtual ~ObjPreproc() {}
  91. /**
  92. * @brief Creates a preprocess object with the given preprocess's class name.
  93. *
  94. * @param[in] proc_name The preprocess class name.
  95. *
  96. * @return Returns the pointer to preprocess object.
  97. */
  98. static ObjPreproc* Create(const std::string& proc_name);
  99. /**
  100. * @brief Initializes preprocessing parameters.
  101. *
  102. * @param[in] params The preprocessing parameters.
  103. *
  104. * @return Returns ture for success, otherwise returns false.
  105. **/
  106. virtual bool Init(const std::unordered_map<std::string, std::string> &params) { return true; }
  107. /**
  108. * @brief Executes preprocess on neural network inputs.
  109. *
  110. * @param[out] net_inputs Neural network inputs.
  111. * @param[in] model Model information including input shape and output shape.
  112. * @param[in] finfo Smart pointer of ``CNFrameInfo`` which stores origin data.
  113. * @param[in] obj The deduced object information.
  114. *
  115. * @return Returns 0 if successful, otherwise returns -1.
  116. */
  117. virtual int Execute(const std::vector<float*>& net_inputs, const std::shared_ptr<edk::ModelLoader>& model,
  118. const CNFrameInfoPtr& finfo, const std::shared_ptr<CNInferObject>& pobj) = 0;
  119. }; // class ObjPreproc
  120. } // namespace cnstream
  121. #endif // ifndef MODULES_INFERENCE_INCLUDE_PREPROC_HPP_