yolov5.cc 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (c) 2022 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. #include "ultra_infer/vision/detection/contrib/yolov5/yolov5.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. namespace detection {
  18. YOLOv5::YOLOv5(const std::string &model_file, const std::string &params_file,
  19. const RuntimeOption &custom_option,
  20. const ModelFormat &model_format) {
  21. if (model_format == ModelFormat::ONNX) {
  22. valid_cpu_backends = {Backend::OPENVINO, Backend::ORT};
  23. valid_gpu_backends = {Backend::ORT, Backend::TRT};
  24. } else if (model_format == ModelFormat::SOPHGO) {
  25. valid_sophgonpu_backends = {Backend::SOPHGOTPU};
  26. } else {
  27. valid_cpu_backends = {Backend::PDINFER, Backend::ORT, Backend::LITE};
  28. valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
  29. valid_kunlunxin_backends = {Backend::LITE};
  30. valid_timvx_backends = {Backend::LITE};
  31. valid_ascend_backends = {Backend::LITE};
  32. }
  33. runtime_option = custom_option;
  34. runtime_option.model_format = model_format;
  35. runtime_option.model_file = model_file;
  36. runtime_option.params_file = params_file;
  37. initialized = Initialize();
  38. }
  39. bool YOLOv5::Initialize() {
  40. if (!InitRuntime()) {
  41. FDERROR << "Failed to initialize ultra_infer backend." << std::endl;
  42. return false;
  43. }
  44. return true;
  45. }
  46. bool YOLOv5::Predict(cv::Mat *im, DetectionResult *result, float conf_threshold,
  47. float nms_threshold) {
  48. postprocessor_.SetConfThreshold(conf_threshold);
  49. postprocessor_.SetNMSThreshold(nms_threshold);
  50. if (!Predict(*im, result)) {
  51. return false;
  52. }
  53. return true;
  54. }
  55. bool YOLOv5::Predict(const cv::Mat &im, DetectionResult *result) {
  56. std::vector<DetectionResult> results;
  57. if (!BatchPredict({im}, &results)) {
  58. return false;
  59. }
  60. *result = std::move(results[0]);
  61. return true;
  62. }
  63. bool YOLOv5::BatchPredict(const std::vector<cv::Mat> &images,
  64. std::vector<DetectionResult> *results) {
  65. std::vector<std::map<std::string, std::array<float, 2>>> ims_info;
  66. std::vector<FDMat> fd_images = WrapMat(images);
  67. if (!preprocessor_.Run(&fd_images, &reused_input_tensors_, &ims_info)) {
  68. FDERROR << "Failed to preprocess the input image." << std::endl;
  69. return false;
  70. }
  71. reused_input_tensors_[0].name = InputInfoOfRuntime(0).name;
  72. if (!Infer(reused_input_tensors_, &reused_output_tensors_)) {
  73. FDERROR << "Failed to inference by runtime." << std::endl;
  74. return false;
  75. }
  76. if (!postprocessor_.Run(reused_output_tensors_, results, ims_info)) {
  77. FDERROR << "Failed to postprocess the inference results by runtime."
  78. << std::endl;
  79. return false;
  80. }
  81. return true;
  82. }
  83. } // namespace detection
  84. } // namespace vision
  85. } // namespace ultra_infer