preprocessor.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/contrib/yolov5seg/preprocessor.h"
  15. #include "ultra_infer/function/concat.h"
  16. namespace ultra_infer {
  17. namespace vision {
  18. namespace detection {
  19. YOLOv5SegPreprocessor::YOLOv5SegPreprocessor() {
  20. size_ = {640, 640};
  21. padding_value_ = {114.0, 114.0, 114.0};
  22. is_mini_pad_ = false;
  23. is_no_pad_ = false;
  24. is_scale_up_ = true;
  25. stride_ = 32;
  26. max_wh_ = 7680.0;
  27. }
  28. void YOLOv5SegPreprocessor::LetterBox(FDMat *mat) {
  29. float scale =
  30. std::min(size_[1] * 1.0 / mat->Height(), size_[0] * 1.0 / mat->Width());
  31. if (!is_scale_up_) {
  32. scale = std::min(scale, 1.0f);
  33. }
  34. int resize_h = int(round(mat->Height() * scale));
  35. int resize_w = int(round(mat->Width() * scale));
  36. int pad_w = size_[0] - resize_w;
  37. int pad_h = size_[1] - resize_h;
  38. if (is_mini_pad_) {
  39. pad_h = pad_h % stride_;
  40. pad_w = pad_w % stride_;
  41. } else if (is_no_pad_) {
  42. pad_h = 0;
  43. pad_w = 0;
  44. resize_h = size_[1];
  45. resize_w = size_[0];
  46. }
  47. if (std::fabs(scale - 1.0f) > 1e-06) {
  48. Resize::Run(mat, resize_w, resize_h);
  49. }
  50. if (pad_h > 0 || pad_w > 0) {
  51. float half_h = pad_h * 1.0 / 2;
  52. int top = int(round(half_h - 0.1));
  53. int bottom = int(round(half_h + 0.1));
  54. float half_w = pad_w * 1.0 / 2;
  55. int left = int(round(half_w - 0.1));
  56. int right = int(round(half_w + 0.1));
  57. Pad::Run(mat, top, bottom, left, right, padding_value_);
  58. }
  59. }
  60. bool YOLOv5SegPreprocessor::Preprocess(
  61. FDMat *mat, FDTensor *output,
  62. std::map<std::string, std::array<float, 2>> *im_info) {
  63. // Record the shape of image and the shape of preprocessed image
  64. (*im_info)["input_shape"] = {static_cast<float>(mat->Height()),
  65. static_cast<float>(mat->Width())};
  66. // yolov5seg's preprocess steps
  67. // 1. letterbox
  68. // 2. convert_and_permute(swap_rb=true)
  69. LetterBox(mat);
  70. std::vector<float> alpha = {1.0f / 255.0f, 1.0f / 255.0f, 1.0f / 255.0f};
  71. std::vector<float> beta = {0.0f, 0.0f, 0.0f};
  72. ConvertAndPermute::Run(mat, alpha, beta, true);
  73. // Record output shape of preprocessed image
  74. (*im_info)["output_shape"] = {static_cast<float>(mat->Height()),
  75. static_cast<float>(mat->Width())};
  76. mat->ShareWithTensor(output);
  77. output->ExpandDim(0); // reshape to n, c, h, w
  78. return true;
  79. }
  80. bool YOLOv5SegPreprocessor::Run(
  81. std::vector<FDMat> *images, std::vector<FDTensor> *outputs,
  82. std::vector<std::map<std::string, std::array<float, 2>>> *ims_info) {
  83. if (images->size() == 0) {
  84. FDERROR << "The size of input images should be greater than 0."
  85. << std::endl;
  86. return false;
  87. }
  88. ims_info->resize(images->size());
  89. outputs->resize(1);
  90. // Concat all the preprocessed data to a batch tensor
  91. std::vector<FDTensor> tensors(images->size());
  92. for (size_t i = 0; i < images->size(); ++i) {
  93. if (!Preprocess(&(*images)[i], &tensors[i], &(*ims_info)[i])) {
  94. FDERROR << "Failed to preprocess input image." << std::endl;
  95. return false;
  96. }
  97. }
  98. if (tensors.size() == 1) {
  99. (*outputs)[0] = std::move(tensors[0]);
  100. } else {
  101. function::Concat(tensors, &((*outputs)[0]), 0);
  102. }
  103. return true;
  104. }
  105. } // namespace detection
  106. } // namespace vision
  107. } // namespace ultra_infer