preprocessor.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/preprocessor.h"
  15. #include "ultra_infer/function/concat.h"
  16. #include "yaml-cpp/yaml.h"
  17. namespace ultra_infer {
  18. namespace vision {
  19. namespace perception {
  20. CaddnPreprocessor::CaddnPreprocessor(const std::string &config_file) {
  21. config_file_ = config_file;
  22. FDASSERT(BuildPreprocessPipeline(),
  23. "Failed to create Paddle3DDetPreprocessor.");
  24. initialized_ = true;
  25. }
  26. bool CaddnPreprocessor::BuildPreprocessPipeline() {
  27. processors_.clear();
  28. // preprocess
  29. processors_.push_back(std::make_shared<BGR2RGB>());
  30. std::vector<float> alpha = {1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0};
  31. std::vector<float> beta = {0.0, 0.0, 0.0};
  32. processors_.push_back(std::make_shared<Convert>(alpha, beta));
  33. processors_.push_back(std::make_shared<Cast>("float"));
  34. processors_.push_back(std::make_shared<HWC2CHW>());
  35. // Fusion will improve performance
  36. FuseTransforms(&processors_);
  37. return true;
  38. }
  39. bool CaddnPreprocessor::Apply(FDMatBatch *image_batch,
  40. std::vector<float> &input_cam_data,
  41. std::vector<float> &input_lidar_data,
  42. std::vector<FDTensor> *outputs) {
  43. if (image_batch->mats->empty()) {
  44. FDERROR << "The size of input images should be greater than 0."
  45. << std::endl;
  46. return false;
  47. }
  48. if (!initialized_) {
  49. FDERROR << "The preprocessor is not initialized." << std::endl;
  50. return false;
  51. }
  52. // There are 3 outputs, image, cam_data, lidar_data
  53. outputs->resize(3);
  54. int batch = static_cast<int>(image_batch->mats->size());
  55. // Allocate memory for cam_data
  56. (*outputs)[1].Resize({batch, 3, 4}, FDDataType::FP32);
  57. // Allocate memory for lidar_data
  58. (*outputs)[2].Resize({batch, 4, 4}, FDDataType::FP32);
  59. auto *cam_data_ptr = reinterpret_cast<float *>((*outputs)[1].MutableData());
  60. auto *lidar_data_ptr = reinterpret_cast<float *>((*outputs)[2].MutableData());
  61. for (size_t i = 0; i < image_batch->mats->size(); ++i) {
  62. FDMat *mat = &(image_batch->mats->at(i));
  63. for (size_t j = 0; j < processors_.size(); ++j) {
  64. if (!(*(processors_[j].get()))(mat)) {
  65. FDERROR << "Failed to process image:" << i << " in "
  66. << processors_[j]->Name() << "." << std::endl;
  67. return false;
  68. }
  69. }
  70. memcpy(cam_data_ptr + i * 12, input_cam_data.data(), 12 * sizeof(float));
  71. memcpy(lidar_data_ptr + i * 16, input_lidar_data.data(),
  72. 16 * sizeof(float));
  73. }
  74. FDTensor *tensor = image_batch->Tensor();
  75. (*outputs)[0].SetExternalData(tensor->Shape(), tensor->Dtype(),
  76. tensor->Data(), tensor->device,
  77. tensor->device_id);
  78. return true;
  79. }
  80. bool CaddnPreprocessor::Run(std::vector<FDMat> *images,
  81. std::vector<float> &input_cam_data,
  82. std::vector<float> &input_lidar_data,
  83. std::vector<FDTensor> *outputs) {
  84. FDMatBatch image_batch(images);
  85. PreApply(&image_batch);
  86. bool ret = Apply(&image_batch, input_cam_data, input_lidar_data, outputs);
  87. PostApply();
  88. return ret;
  89. }
  90. } // namespace perception
  91. } // namespace vision
  92. } // namespace ultra_infer