preprocessor.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/petr/preprocessor.h"
  15. #include <iostream>
  16. #include "ultra_infer/function/concat.h"
  17. #include "yaml-cpp/yaml.h"
  18. namespace ultra_infer {
  19. namespace vision {
  20. namespace perception {
  21. PetrPreprocessor::PetrPreprocessor(const std::string &config_file) {
  22. config_file_ = config_file;
  23. FDASSERT(BuildPreprocessPipelineFromConfig(),
  24. "Failed to create Paddle3DDetPreprocessor.");
  25. initialized_ = true;
  26. }
  27. bool PetrPreprocessor::BuildPreprocessPipelineFromConfig() {
  28. processors_.clear();
  29. processors_.push_back(std::make_shared<Resize>(800, 450));
  30. processors_.push_back(std::make_shared<Crop>(0, 130, 800, 320));
  31. std::vector<float> mean{103.530, 116.280, 123.675};
  32. std::vector<float> std{57.375, 57.120, 58.395};
  33. bool scale = false;
  34. processors_.push_back(std::make_shared<Normalize>(mean, std, scale));
  35. processors_.push_back(std::make_shared<Cast>("float"));
  36. processors_.push_back(std::make_shared<HWC2CHW>());
  37. // Fusion will improve performance
  38. FuseTransforms(&processors_);
  39. return true;
  40. }
  41. bool PetrPreprocessor::Apply(FDMatBatch *image_batch,
  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, k_data, timestamp
  53. outputs->resize(3);
  54. int num_cams = static_cast<int>(image_batch->mats->size());
  55. // Allocate memory for k_data
  56. (*outputs)[1].Resize({1, num_cams, 4, 4}, FDDataType::FP32);
  57. // Allocate memory for image_data
  58. (*outputs)[0].Resize({1, num_cams, 3, 320, 800}, FDDataType::FP32);
  59. // Allocate memory for timestamp
  60. (*outputs)[2].Resize({1, num_cams}, FDDataType::FP32);
  61. auto *image_ptr = reinterpret_cast<float *>((*outputs)[0].MutableData());
  62. auto *k_data_ptr = reinterpret_cast<float *>((*outputs)[1].MutableData());
  63. auto *timestamp_ptr = reinterpret_cast<float *>((*outputs)[2].MutableData());
  64. for (size_t i = 0; i < image_batch->mats->size(); ++i) {
  65. FDMat *mat = &(image_batch->mats->at(i));
  66. for (size_t j = 0; j < processors_.size(); ++j) {
  67. if (!(*(processors_[j].get()))(mat)) {
  68. FDERROR << "Failed to process image:" << i << " in "
  69. << processors_[j]->Name() << "." << std::endl;
  70. return false;
  71. }
  72. }
  73. }
  74. for (int i = 0; i < num_cams / 2 * 4 * 4; ++i) {
  75. input_k_data_.push_back(input_k_data_[i]);
  76. }
  77. memcpy(k_data_ptr, input_k_data_.data(), num_cams * 16 * sizeof(float));
  78. std::vector<float> timestamp(num_cams, 0.0f);
  79. for (int i = num_cams / 2; i < num_cams; ++i) {
  80. timestamp[i] = 1.0f;
  81. }
  82. memcpy(timestamp_ptr, timestamp.data(), num_cams * sizeof(float));
  83. FDTensor *tensor = image_batch->Tensor(); // [num_cams,3,320,800]
  84. tensor->ExpandDim(0); // [num_cams,3,320,800] -> [1,num_cams,3,320,800]
  85. (*outputs)[0].SetExternalData(tensor->Shape(), tensor->Dtype(),
  86. tensor->Data(), tensor->device,
  87. tensor->device_id);
  88. return true;
  89. }
  90. } // namespace perception
  91. } // namespace vision
  92. } // namespace ultra_infer