preprocessor.cc 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/classification/contrib/yolov5cls/preprocessor.h"
  15. #include "ultra_infer/function/concat.h"
  16. namespace ultra_infer {
  17. namespace vision {
  18. namespace classification {
  19. YOLOv5ClsPreprocessor::YOLOv5ClsPreprocessor() {
  20. size_ = {224, 224}; //{h,w}
  21. }
  22. bool YOLOv5ClsPreprocessor::Preprocess(
  23. FDMat *mat, FDTensor *output,
  24. std::map<std::string, std::array<float, 2>> *im_info) {
  25. // Record the shape of image and the shape of preprocessed image
  26. (*im_info)["input_shape"] = {static_cast<float>(mat->Height()),
  27. static_cast<float>(mat->Width())};
  28. // process after image load
  29. double ratio = (size_[0] * 1.0) / std::max(static_cast<float>(mat->Height()),
  30. static_cast<float>(mat->Width()));
  31. // yolov5cls's preprocess steps
  32. // 1. CenterCrop
  33. // 2. Normalize
  34. // CenterCrop
  35. int crop_size = std::min(mat->Height(), mat->Width());
  36. CenterCrop::Run(mat, crop_size, crop_size);
  37. Resize::Run(mat, size_[0], size_[1], -1, -1, cv::INTER_LINEAR);
  38. // Normalize
  39. BGR2RGB::Run(mat);
  40. std::vector<float> alpha = {1.0f / 255.0f, 1.0f / 255.0f, 1.0f / 255.0f};
  41. std::vector<float> beta = {0.0f, 0.0f, 0.0f};
  42. Convert::Run(mat, alpha, beta);
  43. std::vector<float> mean = {0.485f, 0.456f, 0.406f};
  44. std::vector<float> std = {0.229f, 0.224f, 0.225f};
  45. NormalizeAndPermute::Run(mat, mean, std, false);
  46. // Record output shape of preprocessed image
  47. (*im_info)["output_shape"] = {static_cast<float>(mat->Height()),
  48. static_cast<float>(mat->Width())};
  49. mat->ShareWithTensor(output);
  50. output->ExpandDim(0); // reshape to n, c, h, w
  51. return true;
  52. }
  53. bool YOLOv5ClsPreprocessor::Run(
  54. std::vector<FDMat> *images, std::vector<FDTensor> *outputs,
  55. std::vector<std::map<std::string, std::array<float, 2>>> *ims_info) {
  56. if (images->size() == 0) {
  57. FDERROR << "The size of input images should be greater than 0."
  58. << std::endl;
  59. return false;
  60. }
  61. ims_info->resize(images->size());
  62. outputs->resize(1);
  63. // Concat all the preprocessed data to a batch tensor
  64. std::vector<FDTensor> tensors(images->size());
  65. for (size_t i = 0; i < images->size(); ++i) {
  66. if (!Preprocess(&(*images)[i], &tensors[i], &(*ims_info)[i])) {
  67. FDERROR << "Failed to preprocess input image." << std::endl;
  68. return false;
  69. }
  70. }
  71. if (tensors.size() == 1) {
  72. (*outputs)[0] = std::move(tensors[0]);
  73. } else {
  74. function::Concat(tensors, &((*outputs)[0]), 0);
  75. }
  76. return true;
  77. }
  78. } // namespace classification
  79. } // namespace vision
  80. } // namespace ultra_infer