rkyolo.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. //NOLINT
  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. #include "ultra_infer/vision/detection/contrib/rknpu2/rkyolo.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. namespace detection {
  18. RKYOLO::RKYOLO(const std::string &model_file,
  19. const ultra_infer::RuntimeOption &custom_option,
  20. const ultra_infer::ModelFormat &model_format) {
  21. if (model_format == ModelFormat::RKNN) {
  22. valid_cpu_backends = {};
  23. valid_gpu_backends = {};
  24. valid_rknpu_backends = {Backend::RKNPU2};
  25. } else {
  26. FDERROR << "RKYOLO Only Support run in RKNPU2" << std::endl;
  27. }
  28. runtime_option = custom_option;
  29. runtime_option.model_format = model_format;
  30. runtime_option.model_file = model_file;
  31. initialized = Initialize();
  32. }
  33. bool RKYOLO::Initialize() {
  34. if (!InitRuntime()) {
  35. FDERROR << "Failed to initialize ultra_infer backend." << std::endl;
  36. return false;
  37. }
  38. auto size = GetPreprocessor().GetSize();
  39. GetPostprocessor().SetHeightAndWeight(size[0], size[1]);
  40. return true;
  41. }
  42. bool RKYOLO::Predict(const cv::Mat &im, DetectionResult *result) {
  43. std::vector<DetectionResult> results;
  44. if (!BatchPredict({im}, &results)) {
  45. return false;
  46. }
  47. *result = std::move(results[0]);
  48. return true;
  49. }
  50. bool RKYOLO::BatchPredict(const std::vector<cv::Mat> &images,
  51. std::vector<DetectionResult> *results) {
  52. std::vector<FDMat> fd_images = WrapMat(images);
  53. if (!preprocessor_.Run(&fd_images, &reused_input_tensors_)) {
  54. FDERROR << "Failed to preprocess the input image." << std::endl;
  55. return false;
  56. }
  57. reused_input_tensors_[0].name = InputInfoOfRuntime(0).name;
  58. if (!Infer(reused_input_tensors_, &reused_output_tensors_)) {
  59. FDERROR << "Failed to inference by runtime." << std::endl;
  60. return false;
  61. }
  62. auto pad_hw_values_ = preprocessor_.GetPadHWValues();
  63. postprocessor_.SetPadHWValues(preprocessor_.GetPadHWValues());
  64. postprocessor_.SetScale(preprocessor_.GetScale());
  65. if (!postprocessor_.Run(reused_output_tensors_, results)) {
  66. FDERROR << "Failed to postprocess the inference results by runtime."
  67. << std::endl;
  68. return false;
  69. }
  70. return true;
  71. }
  72. } // namespace detection
  73. } // namespace vision
  74. } // namespace ultra_infer