preprocessor.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/smoke/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. SmokePreprocessor::SmokePreprocessor(const std::string &config_file) {
  21. config_file_ = config_file;
  22. FDASSERT(BuildPreprocessPipelineFromConfig(),
  23. "Failed to create Paddle3DDetPreprocessor.");
  24. initialized_ = true;
  25. }
  26. bool SmokePreprocessor::BuildPreprocessPipelineFromConfig() {
  27. processors_.clear();
  28. YAML::Node cfg;
  29. try {
  30. cfg = YAML::LoadFile(config_file_);
  31. } catch (YAML::BadFile &e) {
  32. FDERROR << "Failed to load yaml file " << config_file_
  33. << ", maybe you should check this file." << std::endl;
  34. return false;
  35. }
  36. // read for preprocess
  37. processors_.push_back(std::make_shared<BGR2RGB>());
  38. bool has_permute = false;
  39. for (const auto &op : cfg["Preprocess"]) {
  40. std::string op_name = op["type"].as<std::string>();
  41. if (op_name == "NormalizeImage") {
  42. auto mean = op["mean"].as<std::vector<float>>();
  43. auto std = op["std"].as<std::vector<float>>();
  44. bool is_scale = true;
  45. if (op["is_scale"]) {
  46. is_scale = op["is_scale"].as<bool>();
  47. }
  48. std::string norm_type = "mean_std";
  49. if (op["norm_type"]) {
  50. norm_type = op["norm_type"].as<std::string>();
  51. }
  52. if (norm_type != "mean_std") {
  53. std::fill(mean.begin(), mean.end(), 0.0);
  54. std::fill(std.begin(), std.end(), 1.0);
  55. }
  56. processors_.push_back(std::make_shared<Normalize>(mean, std, is_scale));
  57. } else if (op_name == "Resize") {
  58. bool keep_ratio = op["keep_ratio"].as<bool>();
  59. auto target_size = op["target_size"].as<std::vector<int>>();
  60. int interp = op["interp"].as<int>();
  61. FDASSERT(target_size.size() == 2,
  62. "Require size of target_size be 2, but now it's %lu.",
  63. target_size.size());
  64. if (!keep_ratio) {
  65. int width = target_size[1];
  66. int height = target_size[0];
  67. processors_.push_back(
  68. std::make_shared<Resize>(width, height, -1.0, -1.0, interp, false));
  69. } else {
  70. int min_target_size = std::min(target_size[0], target_size[1]);
  71. int max_target_size = std::max(target_size[0], target_size[1]);
  72. std::vector<int> max_size;
  73. if (max_target_size > 0) {
  74. max_size.push_back(max_target_size);
  75. max_size.push_back(max_target_size);
  76. }
  77. processors_.push_back(std::make_shared<ResizeByShort>(
  78. min_target_size, interp, true, max_size));
  79. }
  80. } else if (op_name == "Permute") {
  81. // Do nothing, do permute as the last operation
  82. has_permute = true;
  83. continue;
  84. } else {
  85. FDERROR << "Unexcepted preprocess operator: " << op_name << "."
  86. << std::endl;
  87. return false;
  88. }
  89. }
  90. if (!disable_permute_) {
  91. if (has_permute) {
  92. // permute = cast<float> + HWC2CHW
  93. processors_.push_back(std::make_shared<Cast>("float"));
  94. processors_.push_back(std::make_shared<HWC2CHW>());
  95. }
  96. }
  97. // Fusion will improve performance
  98. FuseTransforms(&processors_);
  99. input_k_data_ = cfg["k_data"].as<std::vector<float>>();
  100. input_ratio_data_ = cfg["ratio_data"].as<std::vector<float>>();
  101. return true;
  102. }
  103. bool SmokePreprocessor::Apply(FDMatBatch *image_batch,
  104. std::vector<FDTensor> *outputs) {
  105. if (image_batch->mats->empty()) {
  106. FDERROR << "The size of input images should be greater than 0."
  107. << std::endl;
  108. return false;
  109. }
  110. if (!initialized_) {
  111. FDERROR << "The preprocessor is not initialized." << std::endl;
  112. return false;
  113. }
  114. // There are 3 outputs, image, k_data, ratio_data
  115. outputs->resize(3);
  116. int batch = static_cast<int>(image_batch->mats->size());
  117. // Allocate memory for k_data
  118. (*outputs)[2].Resize({batch, 3, 3}, FDDataType::FP32);
  119. // Allocate memory for ratio_data
  120. (*outputs)[0].Resize({batch, 2}, FDDataType::FP32);
  121. auto *k_data_ptr = reinterpret_cast<float *>((*outputs)[2].MutableData());
  122. auto *ratio_data_ptr = reinterpret_cast<float *>((*outputs)[0].MutableData());
  123. for (size_t i = 0; i < image_batch->mats->size(); ++i) {
  124. FDMat *mat = &(image_batch->mats->at(i));
  125. for (size_t j = 0; j < processors_.size(); ++j) {
  126. if (!(*(processors_[j].get()))(mat)) {
  127. FDERROR << "Failed to process image:" << i << " in "
  128. << processors_[j]->Name() << "." << std::endl;
  129. return false;
  130. }
  131. }
  132. memcpy(k_data_ptr + i * 9, input_k_data_.data(), 9 * sizeof(float));
  133. memcpy(ratio_data_ptr + i * 2, input_ratio_data_.data(), 2 * sizeof(float));
  134. }
  135. FDTensor *tensor = image_batch->Tensor();
  136. (*outputs)[1].SetExternalData(tensor->Shape(), tensor->Dtype(),
  137. tensor->Data(), tensor->device,
  138. tensor->device_id);
  139. return true;
  140. }
  141. } // namespace perception
  142. } // namespace vision
  143. } // namespace ultra_infer