pfld.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/facealign/contrib/pfld.h"
  15. #include "ultra_infer/utils/perf.h"
  16. #include "ultra_infer/vision/utils/utils.h"
  17. namespace ultra_infer {
  18. namespace vision {
  19. namespace facealign {
  20. PFLD::PFLD(const std::string &model_file, const std::string &params_file,
  21. const RuntimeOption &custom_option,
  22. const ModelFormat &model_format) {
  23. if (model_format == ModelFormat::ONNX) {
  24. valid_cpu_backends = {Backend::OPENVINO, Backend::ORT};
  25. valid_gpu_backends = {Backend::ORT, Backend::TRT};
  26. } else {
  27. valid_cpu_backends = {Backend::PDINFER, Backend::ORT};
  28. valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
  29. }
  30. runtime_option = custom_option;
  31. runtime_option.model_format = model_format;
  32. runtime_option.model_file = model_file;
  33. runtime_option.params_file = params_file;
  34. initialized = Initialize();
  35. }
  36. bool PFLD::Initialize() {
  37. // parameters for preprocess
  38. size = {112, 112};
  39. if (!InitRuntime()) {
  40. FDERROR << "Failed to initialize ultra_infer backend." << std::endl;
  41. return false;
  42. }
  43. return true;
  44. }
  45. bool PFLD::Preprocess(Mat *mat, FDTensor *output,
  46. std::map<std::string, std::array<int, 2>> *im_info) {
  47. // Resize
  48. int resize_w = size[0];
  49. int resize_h = size[1];
  50. if (resize_h != mat->Height() || resize_w != mat->Width()) {
  51. Resize::Run(mat, resize_w, resize_h);
  52. }
  53. // Normalize
  54. std::vector<float> alpha = {1.0f / 255.0f, 1.0f / 255.0f, 1.0f / 255.0f};
  55. std::vector<float> beta = {0.0f, 0.0f, 0.0f};
  56. Convert::Run(mat, alpha, beta);
  57. // Record output shape of preprocessed image
  58. (*im_info)["output_shape"] = {mat->Height(), mat->Width()};
  59. HWC2CHW::Run(mat);
  60. Cast::Run(mat, "float");
  61. mat->ShareWithTensor(output);
  62. output->shape.insert(output->shape.begin(), 1); // reshape to n, c, h, w
  63. return true;
  64. }
  65. bool PFLD::Postprocess(
  66. FDTensor &infer_result, FaceAlignmentResult *result,
  67. const std::map<std::string, std::array<int, 2>> &im_info) {
  68. FDASSERT(infer_result.shape[0] == 1, "Only support batch = 1 now.");
  69. if (infer_result.dtype != FDDataType::FP32) {
  70. FDERROR << "Only support post process with float32 data." << std::endl;
  71. return false;
  72. }
  73. auto iter_in = im_info.find("input_shape");
  74. FDASSERT(iter_in != im_info.end(), "Cannot find input_shape from im_info.");
  75. int in_h = iter_in->second[0];
  76. int in_w = iter_in->second[1];
  77. result->Clear();
  78. float *data = static_cast<float *>(infer_result.Data());
  79. for (size_t i = 0; i < infer_result.shape[1]; i += 2) {
  80. float x = data[i];
  81. float y = data[i + 1];
  82. x = std::min(std::max(0.f, x), 1.0f);
  83. y = std::min(std::max(0.f, y), 1.0f);
  84. // decode landmarks (default 106 landmarks)
  85. result->landmarks.emplace_back(std::array<float, 2>{x * in_w, y * in_h});
  86. }
  87. return true;
  88. }
  89. bool PFLD::Predict(cv::Mat *im, FaceAlignmentResult *result) {
  90. Mat mat(*im);
  91. std::vector<FDTensor> input_tensors(1);
  92. std::map<std::string, std::array<int, 2>> im_info;
  93. // Record the shape of image and the shape of preprocessed image
  94. im_info["input_shape"] = {mat.Height(), mat.Width()};
  95. im_info["output_shape"] = {mat.Height(), mat.Width()};
  96. if (!Preprocess(&mat, &input_tensors[0], &im_info)) {
  97. FDERROR << "Failed to preprocess input image." << std::endl;
  98. return false;
  99. }
  100. input_tensors[0].name = InputInfoOfRuntime(0).name;
  101. std::vector<FDTensor> output_tensors;
  102. if (!Infer(input_tensors, &output_tensors)) {
  103. FDERROR << "Failed to inference." << std::endl;
  104. return false;
  105. }
  106. if (!Postprocess(output_tensors[1], result, im_info)) {
  107. FDERROR << "Failed to post process." << std::endl;
  108. return false;
  109. }
  110. return true;
  111. }
  112. } // namespace facealign
  113. } // namespace vision
  114. } // namespace ultra_infer