preprocessor.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/centerpoint/preprocessor.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. namespace perception {
  18. CenterpointPreprocessor::CenterpointPreprocessor(
  19. const std::string &config_file) {
  20. initialized_ = true;
  21. }
  22. bool CenterpointPreprocessor::ReadPoint(const std::string &file_path,
  23. const int64_t num_point_dim,
  24. std::vector<float> &data,
  25. int64_t *num_points) {
  26. std::ifstream file_in(file_path, std::ios::in | std::ios::binary);
  27. if (num_point_dim < 4) {
  28. FDERROR << "Point dimension must not be less than 4, but received "
  29. << "num_point_dim is " << num_point_dim << std::endl;
  30. }
  31. if (!file_in) {
  32. FDERROR << "Failed to read file: " << file_path << std::endl;
  33. return false;
  34. }
  35. std::streampos file_size;
  36. file_in.seekg(0, std::ios::end);
  37. file_size = file_in.tellg();
  38. file_in.seekg(0, std::ios::beg);
  39. data.resize(file_size / sizeof(float));
  40. file_in.read(reinterpret_cast<char *>(data.data()), file_size);
  41. file_in.close();
  42. if (file_size / sizeof(float) % num_point_dim != 0) {
  43. FDERROR << "Loaded file size (" << file_size
  44. << ") is not evenly divisible by num_point_dim (" << num_point_dim
  45. << ")\n";
  46. return false;
  47. }
  48. *num_points = file_size / sizeof(float) / num_point_dim;
  49. return true;
  50. }
  51. bool CenterpointPreprocessor::InsertTimeToPoints(const int64_t num_points,
  52. const int64_t num_point_dim,
  53. float *points) {
  54. for (int64_t i = 0; i < num_points; ++i) {
  55. *(points + i * num_point_dim + 4) = 0.;
  56. }
  57. return true;
  58. }
  59. bool CenterpointPreprocessor::Apply(std::vector<std::string> &points_dir,
  60. const int64_t num_point_dim,
  61. const int with_timelag,
  62. std::vector<FDTensor> &outputs) {
  63. for (int index = 0; index < points_dir.size(); ++index) {
  64. std::string file_path = points_dir[index];
  65. std::vector<int64_t> points_shape;
  66. std::vector<float> data;
  67. int64_t num_points;
  68. if (!ReadPoint(file_path, num_point_dim, data, &num_points)) {
  69. return false;
  70. }
  71. float *points = data.data();
  72. if (!with_timelag && num_point_dim == 5 || num_point_dim > 5) {
  73. InsertTimeToPoints(num_points, num_point_dim, points);
  74. }
  75. points_shape.push_back(num_points);
  76. points_shape.push_back(num_point_dim);
  77. FDTensor tensor;
  78. tensor.SetData(points_shape, FDDataType::FP32, points, true);
  79. outputs.push_back(tensor);
  80. }
  81. return true;
  82. }
  83. bool CenterpointPreprocessor::Run(std::vector<std::string> &points_dir,
  84. const int64_t num_point_dim,
  85. const int with_timelag,
  86. std::vector<FDTensor> &outputs) {
  87. bool ret = Apply(points_dir, num_point_dim, with_timelag, outputs);
  88. return ret;
  89. }
  90. } // namespace perception
  91. } // namespace vision
  92. } // namespace ultra_infer