ppmatting.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/matting/ppmatting/ppmatting.h"
  15. #include "ultra_infer/vision/utils/utils.h"
  16. #include "yaml-cpp/yaml.h"
  17. namespace ultra_infer {
  18. namespace vision {
  19. namespace matting {
  20. PPMatting::PPMatting(const std::string &model_file,
  21. const std::string &params_file,
  22. const std::string &config_file,
  23. const RuntimeOption &custom_option,
  24. const ModelFormat &model_format) {
  25. config_file_ = config_file;
  26. valid_cpu_backends = {Backend::ORT, Backend::PDINFER, Backend::LITE};
  27. valid_gpu_backends = {Backend::PDINFER, Backend::TRT};
  28. valid_kunlunxin_backends = {Backend::LITE};
  29. runtime_option = custom_option;
  30. runtime_option.model_format = model_format;
  31. runtime_option.model_file = model_file;
  32. runtime_option.params_file = params_file;
  33. initialized = Initialize();
  34. }
  35. bool PPMatting::Initialize() {
  36. if (!BuildPreprocessPipelineFromConfig()) {
  37. FDERROR << "Failed to build preprocess pipeline from configuration file."
  38. << std::endl;
  39. return false;
  40. }
  41. if (!InitRuntime()) {
  42. FDERROR << "Failed to initialize ultra_infer backend." << std::endl;
  43. return false;
  44. }
  45. return true;
  46. }
  47. bool PPMatting::BuildPreprocessPipelineFromConfig() {
  48. processors_.clear();
  49. YAML::Node cfg;
  50. processors_.push_back(std::make_shared<BGR2RGB>());
  51. try {
  52. cfg = YAML::LoadFile(config_file_);
  53. } catch (YAML::BadFile &e) {
  54. FDERROR << "Failed to load yaml file " << config_file_
  55. << ", maybe you should check this file." << std::endl;
  56. return false;
  57. }
  58. FDASSERT((cfg["Deploy"]["input_shape"]),
  59. "The yaml file should include input_shape parameters");
  60. // input_shape
  61. // b c h w
  62. auto input_shape = cfg["Deploy"]["input_shape"].as<std::vector<int>>();
  63. FDASSERT(input_shape.size() == 4,
  64. "The input_shape in yaml file need to be 4-dimensions, but now its "
  65. "dimension is %zu.",
  66. input_shape.size());
  67. is_fixed_input_shape_ = false;
  68. if (input_shape[2] > 0 && input_shape[3] > 0) {
  69. is_fixed_input_shape_ = true;
  70. }
  71. if (input_shape[2] < 0 || input_shape[3] < 0) {
  72. FDWARNING << "Detected dynamic input shape of your model, only Paddle "
  73. "Inference / OpenVINO support this model now."
  74. << std::endl;
  75. }
  76. if (cfg["Deploy"]["transforms"]) {
  77. auto preprocess_cfg = cfg["Deploy"]["transforms"];
  78. int long_size = -1;
  79. for (const auto &op : preprocess_cfg) {
  80. FDASSERT(op.IsMap(),
  81. "Require the transform information in yaml be Map type.");
  82. if (op["type"].as<std::string>() == "LimitShort") {
  83. int max_short = op["max_short"] ? op["max_short"].as<int>() : -1;
  84. int min_short = op["min_short"] ? op["min_short"].as<int>() : -1;
  85. if (is_fixed_input_shape_) {
  86. // if the input shape is fixed, will resize by scale, and the max
  87. // shape will not exceed input_shape
  88. long_size = max_short;
  89. std::vector<int> max_size = {input_shape[2], input_shape[3]};
  90. processors_.push_back(
  91. std::make_shared<ResizeByShort>(long_size, 1, true, max_size));
  92. } else {
  93. processors_.push_back(
  94. std::make_shared<LimitShort>(max_short, min_short));
  95. }
  96. } else if (op["type"].as<std::string>() == "ResizeToIntMult") {
  97. if (is_fixed_input_shape_) {
  98. std::vector<int> max_size = {input_shape[2], input_shape[3]};
  99. processors_.push_back(
  100. std::make_shared<ResizeByShort>(long_size, 1, true, max_size));
  101. } else {
  102. int mult_int = op["mult_int"] ? op["mult_int"].as<int>() : 32;
  103. processors_.push_back(std::make_shared<LimitByStride>(mult_int));
  104. }
  105. } else if (op["type"].as<std::string>() == "Normalize") {
  106. std::vector<float> mean = {0.5, 0.5, 0.5};
  107. std::vector<float> std = {0.5, 0.5, 0.5};
  108. if (op["mean"]) {
  109. mean = op["mean"].as<std::vector<float>>();
  110. }
  111. if (op["std"]) {
  112. std = op["std"].as<std::vector<float>>();
  113. }
  114. processors_.push_back(std::make_shared<Normalize>(mean, std));
  115. } else if (op["type"].as<std::string>() == "ResizeByShort") {
  116. long_size = op["short_size"].as<int>();
  117. if (is_fixed_input_shape_) {
  118. std::vector<int> max_size = {input_shape[2], input_shape[3]};
  119. processors_.push_back(
  120. std::make_shared<ResizeByShort>(long_size, 1, true, max_size));
  121. } else {
  122. processors_.push_back(std::make_shared<ResizeByShort>(long_size));
  123. }
  124. }
  125. }
  126. // the default padding value is {127.5,127.5,127.5} so after normalizing,
  127. // ((127.5/255)-0.5)/0.5 = 0.0
  128. std::vector<float> value = {0.0, 0.0, 0.0};
  129. processors_.push_back(std::make_shared<Cast>("float"));
  130. processors_.push_back(
  131. std::make_shared<PadToSize>(input_shape[3], input_shape[2], value));
  132. processors_.push_back(std::make_shared<HWC2CHW>());
  133. }
  134. return true;
  135. }
  136. bool PPMatting::Preprocess(Mat *mat, FDTensor *output,
  137. std::map<std::string, std::array<int, 2>> *im_info) {
  138. (*im_info)["input_shape"] = {mat->Height(), mat->Width()};
  139. for (size_t i = 0; i < processors_.size(); ++i) {
  140. if (!(*(processors_[i].get()))(mat)) {
  141. FDERROR << "Failed to process image data in " << processors_[i]->Name()
  142. << "." << std::endl;
  143. return false;
  144. }
  145. }
  146. (*im_info)["output_shape"] = {mat->Height(), mat->Width()};
  147. mat->ShareWithTensor(output);
  148. output->shape.insert(output->shape.begin(), 1);
  149. output->name = InputInfoOfRuntime(0).name;
  150. return true;
  151. }
  152. bool PPMatting::Postprocess(
  153. std::vector<FDTensor> &infer_result, MattingResult *result,
  154. const std::map<std::string, std::array<int, 2>> &im_info) {
  155. FDASSERT((infer_result.size() == 1),
  156. "The default number of output tensor must be 1 ");
  157. FDTensor &alpha_tensor = infer_result.at(0); // (1, 1, h, w)
  158. FDASSERT((alpha_tensor.shape[0] == 1), "Only support batch = 1 now.");
  159. if (alpha_tensor.dtype != FDDataType::FP32) {
  160. FDERROR << "Only support post process with float32 data." << std::endl;
  161. return false;
  162. }
  163. std::vector<int64_t> dim{0, 2, 3, 1};
  164. function::Transpose(alpha_tensor, &alpha_tensor, dim);
  165. alpha_tensor.Squeeze(0);
  166. Mat mat = Mat::Create(alpha_tensor);
  167. auto iter_ipt = im_info.find("input_shape");
  168. auto iter_out = im_info.find("output_shape");
  169. if (is_fixed_input_shape_) {
  170. double scale_h = static_cast<double>(iter_out->second[0]) /
  171. static_cast<double>(iter_ipt->second[0]);
  172. double scale_w = static_cast<double>(iter_out->second[1]) /
  173. static_cast<double>(iter_ipt->second[1]);
  174. double actual_scale = std::min(scale_h, scale_w);
  175. int size_before_pad_h = round(actual_scale * iter_ipt->second[0]);
  176. int size_before_pad_w = round(actual_scale * iter_ipt->second[1]);
  177. Crop::Run(&mat, 0, 0, size_before_pad_w, size_before_pad_h);
  178. }
  179. Resize::Run(&mat, iter_ipt->second[1], iter_ipt->second[0], -1.0f, -1.0f, 1,
  180. false, ProcLib::OPENCV);
  181. result->Clear();
  182. // note: must be setup shape before Resize
  183. result->contain_foreground = false;
  184. result->shape = {iter_ipt->second[0], iter_ipt->second[1]};
  185. int numel = iter_ipt->second[0] * iter_ipt->second[1];
  186. int nbytes = numel * sizeof(float);
  187. result->Resize(numel);
  188. std::memcpy(result->alpha.data(), mat.Data(), nbytes);
  189. return true;
  190. }
  191. bool PPMatting::Predict(cv::Mat *im, MattingResult *result) {
  192. Mat mat(*im);
  193. std::vector<FDTensor> processed_data(1);
  194. std::map<std::string, std::array<int, 2>> im_info;
  195. if (!Preprocess(&mat, &(processed_data[0]), &im_info)) {
  196. FDERROR << "Failed to preprocess input data while using model:"
  197. << ModelName() << "." << std::endl;
  198. return false;
  199. }
  200. std::vector<FDTensor> infer_result(1);
  201. if (!Infer(processed_data, &infer_result)) {
  202. FDERROR << "Failed to inference while using model:" << ModelName() << "."
  203. << std::endl;
  204. return false;
  205. }
  206. if (!Postprocess(infer_result, result, im_info)) {
  207. FDERROR << "Failed to postprocess while using model:" << ModelName() << "."
  208. << std::endl;
  209. return false;
  210. }
  211. return true;
  212. }
  213. } // namespace matting
  214. } // namespace vision
  215. } // namespace ultra_infer