yolov7.cc 3.1 KB

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