caddn.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/perception/paddle3d/caddn/caddn.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. namespace perception {
  18. Caddn::Caddn(const std::string &model_file, const std::string &params_file,
  19. const std::string &config_file, const RuntimeOption &custom_option,
  20. const ModelFormat &model_format)
  21. : preprocessor_(config_file) {
  22. valid_gpu_backends = {Backend::PDINFER};
  23. runtime_option = custom_option;
  24. runtime_option.model_format = model_format;
  25. runtime_option.model_file = model_file;
  26. runtime_option.params_file = params_file;
  27. initialized = Initialize();
  28. }
  29. bool Caddn::Initialize() {
  30. if (!InitRuntime()) {
  31. FDERROR << "Failed to initialize ultra_infer backend." << std::endl;
  32. return false;
  33. }
  34. return true;
  35. }
  36. bool Caddn::Predict(const cv::Mat &im, std::vector<float> &input_cam_data,
  37. std::vector<float> &input_lidar_data,
  38. PerceptionResult *result) {
  39. std::vector<PerceptionResult> results;
  40. if (!BatchPredict({im}, input_cam_data, input_lidar_data, &results)) {
  41. return false;
  42. }
  43. if (results.size()) {
  44. *result = std::move(results[0]);
  45. }
  46. return true;
  47. }
  48. bool Caddn::BatchPredict(const std::vector<cv::Mat> &images,
  49. std::vector<float> &input_cam_data,
  50. std::vector<float> &input_lidar_data,
  51. std::vector<PerceptionResult> *results) {
  52. std::vector<FDMat> fd_images = WrapMat(images);
  53. if (!preprocessor_.Run(&fd_images, input_cam_data, input_lidar_data,
  54. &reused_input_tensors_)) {
  55. FDERROR << "Failed to preprocess the input image." << std::endl;
  56. return false;
  57. }
  58. reused_input_tensors_[0].name = "images";
  59. reused_input_tensors_[1].name = "trans_cam_to_img";
  60. reused_input_tensors_[2].name = "trans_lidar_to_cam";
  61. if (!Infer(reused_input_tensors_, &reused_output_tensors_)) {
  62. FDERROR << "Failed to inference by runtime." << std::endl;
  63. return false;
  64. }
  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 perception
  73. } // namespace vision
  74. } // namespace ultra_infer