postprocess_ssd.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <cstring>
  22. #include <iostream>
  23. #include <memory>
  24. #include <string>
  25. #include <utility>
  26. #include <vector>
  27. #include "cnstream_frame_va.hpp"
  28. #include "postproc.hpp"
  29. using std::cerr;
  30. using std::endl;
  31. using std::pair;
  32. using std::to_string;
  33. using std::vector;
  34. /**
  35. * @brief Post process for ssd
  36. */
  37. class PostprocSsd : public cnstream::Postproc {
  38. public:
  39. /**
  40. * @brief Execute postproc on neural ssd network outputs
  41. *
  42. * @param net_outputs: neural network outputs
  43. * @param model: model information(you can get input shape and output shape from model)
  44. * @param package: smart pointer of struct to store processed result
  45. *
  46. * @return return 0 if succeed
  47. */
  48. int Execute(const std::vector<float*>& net_outputs, const std::shared_ptr<edk::ModelLoader>& model,
  49. const cnstream::CNFrameInfoPtr& package) override;
  50. DECLARE_REFLEX_OBJECT_EX(PostprocSsd, cnstream::Postproc)
  51. }; // class PostprocSsd
  52. #define CLIP(x) ((x) < 0 ? 0 : ((x) > 1 ? 1 : (x)))
  53. IMPLEMENT_REFLEX_OBJECT_EX(PostprocSsd, cnstream::Postproc)
  54. int PostprocSsd::Execute(const std::vector<float*>& net_outputs, const std::shared_ptr<edk::ModelLoader>& model,
  55. const cnstream::CNFrameInfoPtr& package) {
  56. auto data = net_outputs[0];
  57. // auto len = net_outputs[0].second;
  58. auto box_num = data[0];
  59. data += 64;
  60. cnstream::CNInferObjsPtr objs_holder = package->collection.Get<cnstream::CNInferObjsPtr>(cnstream::kCNInferObjsTag);
  61. cnstream::CNObjsVec& objs = objs_holder->objs_;
  62. for (decltype(box_num) bi = 0; bi < box_num; ++bi) {
  63. if (data[1] == 0) continue;
  64. if (threshold_ > 0 && data[2] < threshold_) continue;
  65. std::shared_ptr<cnstream::CNInferObject> object = std::make_shared<cnstream::CNInferObject>();
  66. object->id = std::to_string(data[1] - 1);
  67. object->score = data[2];
  68. object->bbox.x = data[3];
  69. object->bbox.y = data[4];
  70. object->bbox.w = data[5] - object->bbox.x;
  71. object->bbox.h = data[6] - object->bbox.y;
  72. std::lock_guard<std::mutex> objs_mutex(objs_holder->mutex_);
  73. objs.push_back(object);
  74. data += 7;
  75. }
  76. return 0;
  77. }