cnpostproc.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 EDK_SAMPLES_CNPOSTPROC_H_
  21. #define EDK_SAMPLES_CNPOSTPROC_H_
  22. #include <string>
  23. #include <utility>
  24. #include <vector>
  25. #include "cxxutil/exception.h"
  26. #include "easytrack/easy_track.h"
  27. namespace edk {
  28. class CnPostproc {
  29. public:
  30. virtual ~CnPostproc() {}
  31. void set_threshold(const float threshold);
  32. /*********************************************************
  33. * @brief post proc
  34. * @param
  35. * net_outputs[in]: net_outputs[index].first is the neuron
  36. * network's output. net_outputs[index].second is length.
  37. *********************************************************/
  38. std::vector<DetectObject> Execute(const std::vector<std::pair<float*, uint64_t>>& net_outputs);
  39. protected:
  40. /*********************************************************
  41. * @brief called by Execute
  42. *********************************************************/
  43. virtual std::vector<DetectObject> Postproc(const std::vector<std::pair<float*, uint64_t>>& net_outputs) = 0;
  44. float threshold_ = 0;
  45. }; // class CnPostproc
  46. class ClassificationPostproc : public CnPostproc {
  47. protected:
  48. std::vector<DetectObject> Postproc(const std::vector<std::pair<float*, uint64_t>>& net_outputs) override;
  49. }; // class ClassificationPostproc
  50. class SsdPostproc : public CnPostproc {
  51. protected:
  52. std::vector<DetectObject> Postproc(const std::vector<std::pair<float*, uint64_t>>& net_outputs) override;
  53. }; // class SsdPostproc
  54. class Yolov3Postproc : public CnPostproc {
  55. public:
  56. inline void set_padl_ratio(float ratio) { padl_ratio_ = ratio; }
  57. inline void set_padb_ratio(float ratio) { padb_ratio_ = ratio; }
  58. inline void set_padr_ratio(float ratio) { padr_ratio_ = ratio; }
  59. inline void set_padt_ratio(float ratio) { padt_ratio_ = ratio; }
  60. inline float padl_ratio() const { return padl_ratio_; }
  61. inline float padb_ratio() const { return padb_ratio_; }
  62. inline float padr_ratio() const { return padr_ratio_; }
  63. inline float padt_ratio() const { return padt_ratio_; }
  64. protected:
  65. std::vector<DetectObject> Postproc(const std::vector<std::pair<float*, uint64_t>>& net_outputs) override;
  66. private:
  67. /*******************************************************************
  68. * padl_ratio_: left pad / width in preprocessing.
  69. * padb_ratio_: bottom pad / width in preprocessing.
  70. * padr_ratio_: right pad / width in preprocrssing.
  71. * padt_ratio_: top pad / width in preprocessing.
  72. *******************************************************************/
  73. float padl_ratio_ = 0, padb_ratio_ = 0, padr_ratio_ = 0, padt_ratio_ = 0;
  74. }; // class Yolov3Postproc
  75. } // namespace edk
  76. #endif // EDK_SAMPLES_CNPOSTPROC_H_