fsanet.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/headpose/contrib/fsanet.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 headpose {
  20. FSANet::FSANet(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 FSANet::Initialize() {
  37. // parameters for preprocess
  38. size = {64, 64};
  39. if (!InitRuntime()) {
  40. FDERROR << "Failed to initialize ultra_infer backend." << std::endl;
  41. return false;
  42. }
  43. return true;
  44. }
  45. bool FSANet::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 / 128.0f, 1.0f / 128.0f, 1.0f / 128.0f};
  55. std::vector<float> beta = {-127.5f / 128.0f, -127.5f / 128.0f,
  56. -127.5f / 128.0f};
  57. Convert::Run(mat, alpha, beta);
  58. // Record output shape of preprocessed image
  59. (*im_info)["output_shape"] = {mat->Height(), mat->Width()};
  60. HWC2CHW::Run(mat);
  61. Cast::Run(mat, "float");
  62. mat->ShareWithTensor(output);
  63. output->shape.insert(output->shape.begin(), 1); // reshape to n, c, h, w
  64. return true;
  65. }
  66. bool FSANet::Postprocess(
  67. FDTensor &infer_result, HeadPoseResult *result,
  68. const std::map<std::string, std::array<int, 2>> &im_info) {
  69. FDASSERT(infer_result.shape[0] == 1, "Only support batch = 1 now.");
  70. if (infer_result.dtype != FDDataType::FP32) {
  71. FDERROR << "Only support post process with float32 data." << std::endl;
  72. return false;
  73. }
  74. auto iter_in = im_info.find("input_shape");
  75. FDASSERT(iter_in != im_info.end(), "Cannot find input_shape from im_info.");
  76. int in_h = iter_in->second[0];
  77. int in_w = iter_in->second[1];
  78. result->Clear();
  79. float *data = static_cast<float *>(infer_result.Data());
  80. for (size_t i = 0; i < 3; ++i) {
  81. result->euler_angles.emplace_back(data[i]);
  82. }
  83. return true;
  84. }
  85. bool FSANet::Predict(cv::Mat *im, HeadPoseResult *result) {
  86. Mat mat(*im);
  87. std::vector<FDTensor> input_tensors(1);
  88. std::map<std::string, std::array<int, 2>> im_info;
  89. // Record the shape of image and the shape of preprocessed image
  90. im_info["input_shape"] = {mat.Height(), mat.Width()};
  91. im_info["output_shape"] = {mat.Height(), mat.Width()};
  92. if (!Preprocess(&mat, &input_tensors[0], &im_info)) {
  93. FDERROR << "Failed to preprocess input image." << std::endl;
  94. return false;
  95. }
  96. input_tensors[0].name = InputInfoOfRuntime(0).name;
  97. std::vector<FDTensor> output_tensors;
  98. if (!Infer(input_tensors, &output_tensors)) {
  99. FDERROR << "Failed to inference." << std::endl;
  100. return false;
  101. }
  102. if (!Postprocess(output_tensors[0], result, im_info)) {
  103. FDERROR << "Failed to post process." << std::endl;
  104. return false;
  105. }
  106. return true;
  107. }
  108. } // namespace headpose
  109. } // namespace vision
  110. } // namespace ultra_infer