ppmsvsr.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/sr/ppsr/ppmsvsr.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. namespace sr {
  18. PPMSVSR::PPMSVSR(const std::string &model_file, const std::string &params_file,
  19. const RuntimeOption &custom_option,
  20. const ModelFormat &model_format) {
  21. // unsupported ORT backend
  22. valid_cpu_backends = {Backend::PDINFER, Backend::ORT, Backend::OPENVINO};
  23. valid_gpu_backends = {Backend::PDINFER, Backend::TRT, Backend::ORT};
  24. runtime_option = custom_option;
  25. runtime_option.model_format = model_format;
  26. runtime_option.model_file = model_file;
  27. runtime_option.params_file = params_file;
  28. initialized = Initialize();
  29. }
  30. bool PPMSVSR::Initialize() {
  31. if (!InitRuntime()) {
  32. FDERROR << "Failed to initialize ultra_infer backend." << std::endl;
  33. return false;
  34. }
  35. mean_ = {0., 0., 0.};
  36. scale_ = {1., 1., 1.};
  37. return true;
  38. }
  39. bool PPMSVSR::Preprocess(Mat *mat, std::vector<float> &output) {
  40. BGR2RGB::Run(mat);
  41. Normalize::Run(mat, mean_, scale_, true);
  42. HWC2CHW::Run(mat);
  43. // Csat float
  44. float *ptr = static_cast<float *>(mat->Data());
  45. size_t size = mat->Width() * mat->Height() * mat->Channels();
  46. output = std::vector<float>(ptr, ptr + size);
  47. return true;
  48. }
  49. bool PPMSVSR::Predict(std::vector<cv::Mat> &imgs,
  50. std::vector<cv::Mat> &results) {
  51. // Theoretically, the more frame nums there are, the better the result will
  52. // be, but it will lead to a significant increase in memory
  53. int frame_num = imgs.size();
  54. int rows = imgs[0].rows;
  55. int cols = imgs[0].cols;
  56. int channels = imgs[0].channels();
  57. std::vector<FDTensor> input_tensors;
  58. input_tensors.resize(1);
  59. std::vector<float> all_data_temp;
  60. for (int i = 0; i < frame_num; i++) {
  61. Mat mat(imgs[i]);
  62. std::vector<float> data_temp;
  63. Preprocess(&mat, data_temp);
  64. all_data_temp.insert(all_data_temp.end(), data_temp.begin(),
  65. data_temp.end());
  66. }
  67. // share memory in order to avoid memory copy, data type must be float32
  68. input_tensors[0].SetExternalData({1, frame_num, channels, rows, cols},
  69. FDDataType::FP32, all_data_temp.data());
  70. input_tensors[0].shape = {1, frame_num, channels, rows, cols};
  71. input_tensors[0].name = InputInfoOfRuntime(0).name;
  72. std::vector<FDTensor> output_tensors;
  73. if (!Infer(input_tensors, &output_tensors)) {
  74. FDERROR << "Failed to inference." << std::endl;
  75. return false;
  76. }
  77. if (!Postprocess(output_tensors, results)) {
  78. FDERROR << "Failed to post process." << std::endl;
  79. return false;
  80. }
  81. return true;
  82. }
  83. bool PPMSVSR::Postprocess(std::vector<FDTensor> &infer_results,
  84. std::vector<cv::Mat> &results) {
  85. // group to image
  86. // output_shape is [b, n, c, h, w] n = frame_nums b=1(default)
  87. // b and n is dependence export model shape
  88. // see
  89. // https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md
  90. auto output_shape = infer_results[0].shape;
  91. // PP-MSVSR
  92. int h_ = output_shape[3];
  93. int w_ = output_shape[4];
  94. int c_ = output_shape[2];
  95. int frame_num = output_shape[1];
  96. float *out_data = static_cast<float *>(infer_results[0].Data());
  97. cv::Mat temp = cv::Mat::zeros(h_, w_, CV_32FC3); // RGB image
  98. int pix_num = h_ * w_;
  99. int frame_pix_num = pix_num * c_;
  100. for (int frame = 0; frame < frame_num; frame++) {
  101. int index = 0;
  102. for (int h = 0; h < h_; ++h) {
  103. for (int w = 0; w < w_; ++w) {
  104. temp.at<cv::Vec3f>(h, w) = {
  105. out_data[2 * pix_num + index + frame_pix_num * frame],
  106. out_data[pix_num + index + frame_pix_num * frame],
  107. out_data[index + frame_pix_num * frame]};
  108. index += 1;
  109. }
  110. }
  111. // tmp data type is float[0-1.0],convert to uint type
  112. cv::Mat res = cv::Mat::zeros(temp.size(), CV_8UC3);
  113. temp.convertTo(res, CV_8UC3, 255);
  114. results.push_back(res);
  115. }
  116. return true;
  117. }
  118. } // namespace sr
  119. } // namespace vision
  120. } // namespace ultra_infer