picodet_mnn.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef __PicoDet_H__
  15. #define __PicoDet_H__
  16. #pragma once
  17. #include "Interpreter.hpp"
  18. #include "ImageProcess.hpp"
  19. #include "MNNDefine.h"
  20. #include "Tensor.hpp"
  21. #include <algorithm>
  22. #include <chrono>
  23. #include <iostream>
  24. #include <memory>
  25. #include <opencv2/opencv.hpp>
  26. #include <string>
  27. #include <vector>
  28. typedef struct NonPostProcessHeadInfo_ {
  29. std::string cls_layer;
  30. std::string dis_layer;
  31. int stride;
  32. } NonPostProcessHeadInfo;
  33. typedef struct BoxInfo_ {
  34. float x1;
  35. float y1;
  36. float x2;
  37. float y2;
  38. float score;
  39. int label;
  40. } BoxInfo;
  41. class PicoDet {
  42. public:
  43. PicoDet(const std::string &mnn_path, int input_width, int input_length,
  44. int num_thread_ = 4, float score_threshold_ = 0.5,
  45. float nms_threshold_ = 0.3);
  46. ~PicoDet();
  47. int detect(cv::Mat &img, std::vector<BoxInfo> &result_list,
  48. bool has_postprocess);
  49. private:
  50. void decode_infer(MNN::Tensor *cls_pred, MNN::Tensor *dis_pred, int stride,
  51. float threshold,
  52. std::vector<std::vector<BoxInfo>> &results);
  53. BoxInfo disPred2Bbox(const float *&dfl_det, int label, float score, int x,
  54. int y, int stride);
  55. void nms(std::vector<BoxInfo> &input_boxes, float NMS_THRESH);
  56. private:
  57. std::shared_ptr<MNN::Interpreter> PicoDet_interpreter;
  58. MNN::Session *PicoDet_session = nullptr;
  59. MNN::Tensor *input_tensor = nullptr;
  60. int num_thread;
  61. int image_w;
  62. int image_h;
  63. int in_w = 320;
  64. int in_h = 320;
  65. float score_threshold;
  66. float nms_threshold;
  67. const float mean_vals[3] = {103.53f, 116.28f, 123.675f};
  68. const float norm_vals[3] = {0.017429f, 0.017507f, 0.017125f};
  69. const int num_class = 80;
  70. const int reg_max = 7;
  71. std::vector<float> bbox_output_data_;
  72. std::vector<float> class_output_data_;
  73. std::vector<std::string> nms_heads_info{"tmp_16", "concat_4.tmp_0"};
  74. // If not export post-process, will use non_postprocess_heads_info
  75. std::vector<NonPostProcessHeadInfo> non_postprocess_heads_info{
  76. // cls_pred|dis_pred|stride
  77. {"transpose_0.tmp_0", "transpose_1.tmp_0", 8},
  78. {"transpose_2.tmp_0", "transpose_3.tmp_0", 16},
  79. {"transpose_4.tmp_0", "transpose_5.tmp_0", 32},
  80. {"transpose_6.tmp_0", "transpose_7.tmp_0", 64},
  81. };
  82. };
  83. template <typename _Tp>
  84. int activation_function_softmax(const _Tp *src, _Tp *dst, int length);
  85. inline float fast_exp(float x);
  86. inline float sigmoid(float x);
  87. #endif