postprocess_yolov3.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include <algorithm>
  21. #include <cmath>
  22. #include <vector>
  23. #include <memory>
  24. #include "cnstream_frame_va.hpp"
  25. #include "postproc.hpp"
  26. #include "cnstream_logging.hpp"
  27. /**
  28. * @brief Postprocessing for YOLOv3 neural network
  29. * The input frame of the model should keep aspect ratio.
  30. */
  31. class PostprocYolov3 : public cnstream::Postproc {
  32. public:
  33. int Execute(const std::vector<float*>& net_outputs, const std::shared_ptr<edk::ModelLoader>& model,
  34. const std::shared_ptr<cnstream::CNFrameInfo>& package) {
  35. LOGF_IF(DEMO, model->InputNum() != 1);
  36. LOGF_IF(DEMO, model->OutputNum() != 1);
  37. LOGF_IF(DEMO, net_outputs.size() != 1);
  38. const auto input_sp = model->InputShape(0);
  39. const int img_w = package->collection.Get<cnstream::CNDataFramePtr>(cnstream::kCNDataFrameTag)->width;
  40. const int img_h = package->collection.Get<cnstream::CNDataFramePtr>(cnstream::kCNDataFrameTag)->height;
  41. const int model_input_w = static_cast<int>(input_sp.W());
  42. const int model_input_h = static_cast<int>(input_sp.H());
  43. const float* net_output = net_outputs[0];
  44. // scaling factors
  45. const float scaling_factors = std::min(1.0 * model_input_w / img_w, 1.0 * model_input_h / img_h);
  46. // The input frame of the model should keep aspect ratio.
  47. // If mlu resize and convert operator is used as preproc, parameter keep_aspect_ratio of Inferencer module
  48. // should be set to true in config json file.
  49. // If cpu preproc is used as preproc, please make sure keep aspect ratio in custom preproc.
  50. // Scaler does not support keep aspect ratio.
  51. // If the input frame does not keep aspect ratio, set scaled_w = model_input_w and scaled_h = model_input_h
  52. // scaled size
  53. const int scaled_w = scaling_factors * img_w;
  54. const int scaled_h = scaling_factors * img_h;
  55. // bboxes
  56. const int box_num = static_cast<int>(net_output[0]);
  57. int box_step = 7;
  58. auto range_0_1 = [](float num) { return std::max(.0f, std::min(1.0f, num)); };
  59. cnstream::CNInferObjsPtr objs_holder =
  60. package->collection.Get<cnstream::CNInferObjsPtr>(cnstream::kCNInferObjsTag);
  61. cnstream::CNObjsVec &objs = objs_holder->objs_;
  62. for (int box_idx = 0; box_idx < box_num; ++box_idx) {
  63. float left = range_0_1(net_output[64 + box_idx * box_step + 3]);
  64. float right = range_0_1(net_output[64 + box_idx * box_step + 5]);
  65. float top = range_0_1(net_output[64 + box_idx * box_step + 4]);
  66. float bottom = range_0_1(net_output[64 + box_idx * box_step + 6]);
  67. // rectify
  68. left = (left * model_input_w - (model_input_w - scaled_w) / 2) / scaled_w;
  69. right = (right * model_input_w - (model_input_w - scaled_w) / 2) / scaled_w;
  70. top = (top * model_input_h - (model_input_h - scaled_h) / 2) / scaled_h;
  71. bottom = (bottom * model_input_h - (model_input_h - scaled_h) / 2) / scaled_h;
  72. left = std::max(0.0f, left);
  73. right = std::max(0.0f, right);
  74. top = std::max(0.0f, top);
  75. bottom = std::max(0.0f, bottom);
  76. auto obj = std::make_shared<cnstream::CNInferObject>();
  77. obj->id = std::to_string(static_cast<int>(net_output[64 + box_idx * box_step + 1]));
  78. obj->score = net_output[64 + box_idx * box_step + 2];
  79. obj->bbox.x = left;
  80. obj->bbox.y = top;
  81. obj->bbox.w = std::min(1.0f - obj->bbox.x, right - left);
  82. obj->bbox.h = std::min(1.0f - obj->bbox.y, bottom - top);
  83. if (obj->bbox.h <= 0 || obj->bbox.w <= 0 || (obj->score < threshold_ && threshold_ > 0)) continue;
  84. std::lock_guard<std::mutex> objs_mutex(objs_holder->mutex_);
  85. objs.push_back(obj);
  86. }
  87. return 0;
  88. }
  89. private:
  90. DECLARE_REFLEX_OBJECT_EX(PostprocYolov3, cnstream::Postproc);
  91. }; // class PostprocessYolov3
  92. IMPLEMENT_REFLEX_OBJECT_EX(PostprocYolov3, cnstream::Postproc);