preprocessor.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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/detection/ppdet/preprocessor.h"
  15. #include "ultra_infer/function/concat.h"
  16. #include "ultra_infer/function/pad.h"
  17. #include "yaml-cpp/yaml.h"
  18. namespace ultra_infer {
  19. namespace vision {
  20. namespace detection {
  21. PaddleDetPreprocessor::PaddleDetPreprocessor(const std::string &config_file) {
  22. this->config_file_ = config_file;
  23. FDASSERT(BuildPreprocessPipelineFromConfig(),
  24. "Failed to create PaddleDetPreprocessor.");
  25. initialized_ = true;
  26. }
  27. bool PaddleDetPreprocessor::BuildPreprocessPipelineFromConfig() {
  28. processors_.clear();
  29. YAML::Node cfg;
  30. try {
  31. cfg = YAML::LoadFile(config_file_);
  32. } catch (YAML::BadFile &e) {
  33. FDERROR << "Failed to load yaml file " << config_file_
  34. << ", maybe you should check this file." << std::endl;
  35. return false;
  36. }
  37. // read for postprocess
  38. if (cfg["arch"].IsDefined()) {
  39. arch_ = cfg["arch"].as<std::string>();
  40. } else {
  41. FDERROR << "Please set model arch,"
  42. << "support value : SOLOv2, YOLO, SSD, RetinaNet, RCNN, Face."
  43. << std::endl;
  44. return false;
  45. }
  46. // read for preprocess
  47. processors_.push_back(std::make_shared<BGR2RGB>());
  48. bool has_permute = false;
  49. for (const auto &op : cfg["Preprocess"]) {
  50. std::string op_name = op["type"].as<std::string>();
  51. if (op_name == "NormalizeImage") {
  52. if (!disable_normalize_) {
  53. auto mean = op["mean"].as<std::vector<float>>();
  54. auto std = op["std"].as<std::vector<float>>();
  55. bool is_scale = true;
  56. if (op["is_scale"]) {
  57. is_scale = op["is_scale"].as<bool>();
  58. }
  59. std::string norm_type = "mean_std";
  60. if (op["norm_type"]) {
  61. norm_type = op["norm_type"].as<std::string>();
  62. }
  63. if (norm_type != "mean_std") {
  64. std::fill(mean.begin(), mean.end(), 0.0);
  65. std::fill(std.begin(), std.end(), 1.0);
  66. }
  67. processors_.push_back(std::make_shared<Normalize>(mean, std, is_scale));
  68. }
  69. } else if (op_name == "Resize") {
  70. bool keep_ratio = op["keep_ratio"].as<bool>();
  71. auto target_size = op["target_size"].as<std::vector<int>>();
  72. int interp = op["interp"].as<int>();
  73. FDASSERT(target_size.size() == 2,
  74. "Require size of target_size be 2, but now it's %lu.",
  75. target_size.size());
  76. if (!keep_ratio) {
  77. int width = target_size[1];
  78. int height = target_size[0];
  79. processors_.push_back(
  80. std::make_shared<Resize>(width, height, -1.0, -1.0, interp, false));
  81. } else {
  82. int min_target_size = std::min(target_size[0], target_size[1]);
  83. int max_target_size = std::max(target_size[0], target_size[1]);
  84. std::vector<int> max_size;
  85. if (max_target_size > 0) {
  86. max_size.push_back(max_target_size);
  87. max_size.push_back(max_target_size);
  88. }
  89. processors_.push_back(std::make_shared<ResizeByShort>(
  90. min_target_size, interp, true, max_size));
  91. }
  92. } else if (op_name == "Permute") {
  93. // Do nothing, do permute as the last operation
  94. has_permute = true;
  95. continue;
  96. } else if (op_name == "Pad") {
  97. auto size = op["size"].as<std::vector<int>>();
  98. auto value = op["fill_value"].as<std::vector<float>>();
  99. processors_.push_back(
  100. std::make_shared<PadToSize>(size[1], size[0], value));
  101. } else if (op_name == "PadStride") {
  102. auto stride = op["stride"].as<int>();
  103. processors_.push_back(
  104. std::make_shared<StridePad>(stride, std::vector<float>(3, 0)));
  105. } else {
  106. FDERROR << "Unexcepted preprocess operator: " << op_name << "."
  107. << std::endl;
  108. return false;
  109. }
  110. }
  111. if (!disable_permute_) {
  112. if (has_permute) {
  113. // permute = cast<float> + HWC2CHW
  114. processors_.push_back(std::make_shared<Cast>("float"));
  115. processors_.push_back(std::make_shared<HWC2CHW>());
  116. }
  117. }
  118. // Fusion will improve performance
  119. FuseTransforms(&processors_);
  120. return true;
  121. }
  122. bool PaddleDetPreprocessor::Apply(FDMatBatch *image_batch,
  123. std::vector<FDTensor> *outputs) {
  124. if (!initialized_) {
  125. FDERROR << "The preprocessor is not initialized." << std::endl;
  126. return false;
  127. }
  128. if (image_batch->mats->empty()) {
  129. FDERROR << "The size of input images should be greater than 0."
  130. << std::endl;
  131. return false;
  132. }
  133. // There are 3 outputs, image, scale_factor, im_shape
  134. // But im_shape is not used for all the PaddleDetection models
  135. // So preprocessor will output the 3 FDTensors, and how to use `im_shape`
  136. // is decided by the model itself
  137. outputs->resize(3);
  138. int batch = static_cast<int>(image_batch->mats->size());
  139. // Allocate memory for scale_factor
  140. (*outputs)[1].Resize({batch, 2}, FDDataType::FP32);
  141. // Allocate memory for im_shape
  142. (*outputs)[2].Resize({batch, 2}, FDDataType::FP32);
  143. // Record the max size for a batch of input image
  144. // All the tensor will pad to the max size to compose a batched tensor
  145. std::vector<int> max_hw({-1, -1});
  146. auto *scale_factor_ptr =
  147. reinterpret_cast<float *>((*outputs)[1].MutableData());
  148. auto *im_shape_ptr = reinterpret_cast<float *>((*outputs)[2].MutableData());
  149. for (size_t i = 0; i < image_batch->mats->size(); ++i) {
  150. FDMat *mat = &(image_batch->mats->at(i));
  151. int origin_w = mat->Width();
  152. int origin_h = mat->Height();
  153. scale_factor_ptr[2 * i] = 1.0;
  154. scale_factor_ptr[2 * i + 1] = 1.0;
  155. for (size_t j = 0; j < processors_.size(); ++j) {
  156. if (!(*(processors_[j].get()))(mat)) {
  157. FDERROR << "Failed to process image:" << i << " in "
  158. << processors_[j]->Name() << "." << std::endl;
  159. return false;
  160. }
  161. if (processors_[j]->Name().find("Resize") != std::string::npos) {
  162. scale_factor_ptr[2 * i] = mat->Height() * 1.0 / origin_h;
  163. scale_factor_ptr[2 * i + 1] = mat->Width() * 1.0 / origin_w;
  164. }
  165. }
  166. if (mat->Height() > max_hw[0]) {
  167. max_hw[0] = mat->Height();
  168. }
  169. if (mat->Width() > max_hw[1]) {
  170. max_hw[1] = mat->Width();
  171. }
  172. im_shape_ptr[2 * i] = max_hw[0];
  173. im_shape_ptr[2 * i + 1] = max_hw[1];
  174. }
  175. // if the size of image less than max_hw, pad to max_hw
  176. for (size_t i = 0; i < image_batch->mats->size(); ++i) {
  177. FDMat *mat = &(image_batch->mats->at(i));
  178. if (mat->Height() < max_hw[0] || mat->Width() < max_hw[1]) {
  179. pad_op_->SetWidthHeight(max_hw[1], max_hw[0]);
  180. (*pad_op_)(mat);
  181. }
  182. }
  183. // Get the NCHW tensor
  184. FDTensor *tensor = image_batch->Tensor();
  185. (*outputs)[0].SetExternalData(tensor->Shape(), tensor->Dtype(),
  186. tensor->Data(), tensor->device,
  187. tensor->device_id);
  188. return true;
  189. }
  190. void PaddleDetPreprocessor::DisableNormalize() {
  191. this->disable_normalize_ = true;
  192. // the DisableNormalize function will be invalid if the configuration file is
  193. // loaded during preprocessing
  194. if (!BuildPreprocessPipelineFromConfig()) {
  195. FDERROR << "Failed to build preprocess pipeline from configuration file."
  196. << std::endl;
  197. }
  198. }
  199. void PaddleDetPreprocessor::DisablePermute() {
  200. this->disable_permute_ = true;
  201. // the DisablePermute function will be invalid if the configuration file is
  202. // loaded during preprocessing
  203. if (!BuildPreprocessPipelineFromConfig()) {
  204. FDERROR << "Failed to build preprocess pipeline from configuration file."
  205. << std::endl;
  206. }
  207. }
  208. } // namespace detection
  209. } // namespace vision
  210. } // namespace ultra_infer