tinypose_128x96.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "core/general-server/op/tinypose_128x96.h"
  15. #include "core/predictor/framework/infer.h"
  16. #include "core/predictor/framework/memory.h"
  17. #include "core/predictor/framework/resource.h"
  18. #include "core/util/include/timer.h"
  19. #include <algorithm>
  20. #include <iostream>
  21. #include <memory>
  22. #include <sstream>
  23. namespace baidu {
  24. namespace paddle_serving {
  25. namespace serving {
  26. using baidu::paddle_serving::Timer;
  27. using baidu::paddle_serving::predictor::InferManager;
  28. using baidu::paddle_serving::predictor::MempoolWrapper;
  29. using baidu::paddle_serving::predictor::PaddleGeneralModelConfig;
  30. using baidu::paddle_serving::predictor::general_model::Request;
  31. using baidu::paddle_serving::predictor::general_model::Response;
  32. using baidu::paddle_serving::predictor::general_model::Tensor;
  33. int tinypose_128x96::inference() {
  34. VLOG(2) << "Going to run inference";
  35. const std::vector<std::string> pre_node_names = pre_names();
  36. if (pre_node_names.size() != 1) {
  37. LOG(ERROR) << "This op(" << op_name()
  38. << ") can only have one predecessor op, but received "
  39. << pre_node_names.size();
  40. return -1;
  41. }
  42. const std::string pre_name = pre_node_names[0];
  43. const GeneralBlob *input_blob = get_depend_argument<GeneralBlob>(pre_name);
  44. if (!input_blob) {
  45. LOG(ERROR) << "input_blob is nullptr,error";
  46. return -1;
  47. }
  48. uint64_t log_id = input_blob->GetLogId();
  49. VLOG(2) << "(logid=" << log_id << ") Get precedent op name: " << pre_name;
  50. GeneralBlob *output_blob = mutable_data<GeneralBlob>();
  51. if (!output_blob) {
  52. LOG(ERROR) << "output_blob is nullptr,error";
  53. return -1;
  54. }
  55. output_blob->SetLogId(log_id);
  56. if (!input_blob) {
  57. LOG(ERROR) << "(logid=" << log_id
  58. << ") Failed mutable depended argument, op:" << pre_name;
  59. return -1;
  60. }
  61. const TensorVector *in = &input_blob->tensor_vector;
  62. TensorVector *out = &output_blob->tensor_vector;
  63. int batch_size = input_blob->_batch_size;
  64. output_blob->_batch_size = batch_size;
  65. VLOG(2) << "(logid=" << log_id << ") infer batch size: " << batch_size;
  66. Timer timeline;
  67. int64_t start = timeline.TimeStampUS();
  68. timeline.Start();
  69. // only support string type
  70. char *total_input_ptr = static_cast<char *>(in->at(0).data.data());
  71. std::string base64str = total_input_ptr;
  72. cv::Mat img = Base2Mat(base64str);
  73. cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
  74. // preprocess
  75. std::vector<float> input(1 * 3 * im_shape_h * im_shape_w, 0.0f);
  76. preprocess_det(img, input.data(), scale_factor_h, scale_factor_w, im_shape_h,
  77. im_shape_w, mean_, scale_, is_scale_);
  78. // create real_in
  79. TensorVector *real_in = new TensorVector();
  80. if (!real_in) {
  81. LOG(ERROR) << "real_in is nullptr,error";
  82. return -1;
  83. }
  84. int in_num = 0;
  85. size_t databuf_size = 0;
  86. void *databuf_data = NULL;
  87. char *databuf_char = NULL;
  88. // image
  89. in_num = 1 * 3 * im_shape_h * im_shape_w;
  90. databuf_size = in_num * sizeof(float);
  91. databuf_data = MempoolWrapper::instance().malloc(databuf_size);
  92. if (!databuf_data) {
  93. LOG(ERROR) << "Malloc failed, size: " << databuf_size;
  94. return -1;
  95. }
  96. memcpy(databuf_data, input.data(), databuf_size);
  97. databuf_char = reinterpret_cast<char *>(databuf_data);
  98. paddle::PaddleBuf paddleBuf(databuf_char, databuf_size);
  99. paddle::PaddleTensor tensor_in;
  100. tensor_in.name = "image";
  101. tensor_in.dtype = paddle::PaddleDType::FLOAT32;
  102. tensor_in.shape = {1, 3, im_shape_h, im_shape_w};
  103. tensor_in.lod = in->at(0).lod;
  104. tensor_in.data = paddleBuf;
  105. real_in->push_back(tensor_in);
  106. if (InferManager::instance().infer(engine_name().c_str(), real_in, out,
  107. batch_size)) {
  108. LOG(ERROR) << "(logid=" << log_id
  109. << ") Failed do infer in fluid model: " << engine_name().c_str();
  110. return -1;
  111. }
  112. int64_t end = timeline.TimeStampUS();
  113. CopyBlobInfo(input_blob, output_blob);
  114. AddBlobInfo(output_blob, start);
  115. AddBlobInfo(output_blob, end);
  116. return 0;
  117. }
  118. void tinypose_128x96::preprocess_det(const cv::Mat &img, float *data,
  119. float &scale_factor_h,
  120. float &scale_factor_w, int im_shape_h,
  121. int im_shape_w,
  122. const std::vector<float> &mean,
  123. const std::vector<float> &scale,
  124. const bool is_scale) {
  125. // Resize
  126. cv::Mat resize_img;
  127. cv::resize(img, resize_img, cv::Size(im_shape_w, im_shape_h), 0, 0, 1);
  128. // Normalize
  129. double e = 1.0;
  130. if (is_scale) {
  131. e /= 255.0;
  132. }
  133. cv::Mat img_fp;
  134. (resize_img).convertTo(img_fp, CV_32FC3, e);
  135. for (int h = 0; h < im_shape_h; h++) {
  136. for (int w = 0; w < im_shape_w; w++) {
  137. img_fp.at<cv::Vec3f>(h, w)[0] =
  138. (img_fp.at<cv::Vec3f>(h, w)[0] - mean[0]) / scale[0];
  139. img_fp.at<cv::Vec3f>(h, w)[1] =
  140. (img_fp.at<cv::Vec3f>(h, w)[1] - mean[1]) / scale[1];
  141. img_fp.at<cv::Vec3f>(h, w)[2] =
  142. (img_fp.at<cv::Vec3f>(h, w)[2] - mean[2]) / scale[2];
  143. }
  144. }
  145. // Permute
  146. int rh = img_fp.rows;
  147. int rw = img_fp.cols;
  148. int rc = img_fp.channels();
  149. for (int i = 0; i < rc; ++i) {
  150. cv::extractChannel(img_fp, cv::Mat(rh, rw, CV_32FC1, data + i * rh * rw),
  151. i);
  152. }
  153. }
  154. cv::Mat tinypose_128x96::Base2Mat(std::string &base64_data) {
  155. cv::Mat img;
  156. std::string s_mat;
  157. s_mat = base64Decode(base64_data.data(), base64_data.size());
  158. std::vector<char> base64_img(s_mat.begin(), s_mat.end());
  159. img = cv::imdecode(base64_img, cv::IMREAD_COLOR); // CV_LOAD_IMAGE_COLOR
  160. return img;
  161. }
  162. std::string tinypose_128x96::base64Decode(const char *Data, int DataByte) {
  163. const char DecodeTable[] = {
  164. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  165. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  166. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  167. 62, // '+'
  168. 0, 0, 0,
  169. 63, // '/'
  170. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
  171. 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  172. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'
  173. 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
  174. 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'
  175. };
  176. std::string strDecode;
  177. int nValue;
  178. int i = 0;
  179. while (i < DataByte) {
  180. if (*Data != '\r' && *Data != '\n') {
  181. nValue = DecodeTable[*Data++] << 18;
  182. nValue += DecodeTable[*Data++] << 12;
  183. strDecode += (nValue & 0x00FF0000) >> 16;
  184. if (*Data != '=') {
  185. nValue += DecodeTable[*Data++] << 6;
  186. strDecode += (nValue & 0x0000FF00) >> 8;
  187. if (*Data != '=') {
  188. nValue += DecodeTable[*Data++];
  189. strDecode += nValue & 0x000000FF;
  190. }
  191. }
  192. i += 4;
  193. } else // 回车换行,跳过
  194. {
  195. Data++;
  196. i++;
  197. }
  198. }
  199. return strDecode;
  200. }
  201. DEFINE_OP(tinypose_128x96);
  202. } // namespace serving
  203. } // namespace paddle_serving
  204. } // namespace baidu