ppyoloe_crn_s_300e_coco.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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/ppyoloe_crn_s_300e_coco.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 ppyoloe_crn_s_300e_coco::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. // scale_factor
  107. std::vector<float> scale_factor{scale_factor_h, scale_factor_w};
  108. databuf_size = 2 * sizeof(float);
  109. databuf_data = MempoolWrapper::instance().malloc(databuf_size);
  110. if (!databuf_data) {
  111. LOG(ERROR) << "Malloc failed, size: " << databuf_size;
  112. return -1;
  113. }
  114. memcpy(databuf_data, scale_factor.data(), databuf_size);
  115. databuf_char = reinterpret_cast<char *>(databuf_data);
  116. paddle::PaddleBuf paddleBuf_2(databuf_char, databuf_size);
  117. paddle::PaddleTensor tensor_in_2;
  118. tensor_in_2.name = "scale_factor";
  119. tensor_in_2.dtype = paddle::PaddleDType::FLOAT32;
  120. tensor_in_2.shape = {1, 2};
  121. tensor_in_2.lod = in->at(0).lod;
  122. tensor_in_2.data = paddleBuf_2;
  123. real_in->push_back(tensor_in_2);
  124. if (InferManager::instance().infer(engine_name().c_str(), real_in, out,
  125. batch_size)) {
  126. LOG(ERROR) << "(logid=" << log_id
  127. << ") Failed do infer in fluid model: " << engine_name().c_str();
  128. return -1;
  129. }
  130. int64_t end = timeline.TimeStampUS();
  131. CopyBlobInfo(input_blob, output_blob);
  132. AddBlobInfo(output_blob, start);
  133. AddBlobInfo(output_blob, end);
  134. return 0;
  135. }
  136. void ppyoloe_crn_s_300e_coco::preprocess_det(const cv::Mat &img, float *data,
  137. float &scale_factor_h,
  138. float &scale_factor_w,
  139. int im_shape_h, int im_shape_w,
  140. const std::vector<float> &mean,
  141. const std::vector<float> &scale,
  142. const bool is_scale) {
  143. // scale_factor
  144. scale_factor_h =
  145. static_cast<float>(im_shape_h) / static_cast<float>(img.rows);
  146. scale_factor_w =
  147. static_cast<float>(im_shape_w) / static_cast<float>(img.cols);
  148. // Resize
  149. cv::Mat resize_img;
  150. cv::resize(img, resize_img, cv::Size(im_shape_w, im_shape_h), 0, 0, 2);
  151. // Normalize
  152. double e = 1.0;
  153. if (is_scale) {
  154. e /= 255.0;
  155. }
  156. cv::Mat img_fp;
  157. (resize_img).convertTo(img_fp, CV_32FC3, e);
  158. for (int h = 0; h < im_shape_h; h++) {
  159. for (int w = 0; w < im_shape_w; w++) {
  160. img_fp.at<cv::Vec3f>(h, w)[0] =
  161. (img_fp.at<cv::Vec3f>(h, w)[0] - mean[0]) / scale[0];
  162. img_fp.at<cv::Vec3f>(h, w)[1] =
  163. (img_fp.at<cv::Vec3f>(h, w)[1] - mean[1]) / scale[1];
  164. img_fp.at<cv::Vec3f>(h, w)[2] =
  165. (img_fp.at<cv::Vec3f>(h, w)[2] - mean[2]) / scale[2];
  166. }
  167. }
  168. // Permute
  169. int rh = img_fp.rows;
  170. int rw = img_fp.cols;
  171. int rc = img_fp.channels();
  172. for (int i = 0; i < rc; ++i) {
  173. cv::extractChannel(img_fp, cv::Mat(rh, rw, CV_32FC1, data + i * rh * rw),
  174. i);
  175. }
  176. }
  177. cv::Mat ppyoloe_crn_s_300e_coco::Base2Mat(std::string &base64_data) {
  178. cv::Mat img;
  179. std::string s_mat;
  180. s_mat = base64Decode(base64_data.data(), base64_data.size());
  181. std::vector<char> base64_img(s_mat.begin(), s_mat.end());
  182. img = cv::imdecode(base64_img, cv::IMREAD_COLOR); // CV_LOAD_IMAGE_COLOR
  183. return img;
  184. }
  185. std::string ppyoloe_crn_s_300e_coco::base64Decode(const char *Data,
  186. int DataByte) {
  187. const char DecodeTable[] = {
  188. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  189. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  190. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  191. 62, // '+'
  192. 0, 0, 0,
  193. 63, // '/'
  194. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
  195. 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  196. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'
  197. 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
  198. 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'
  199. };
  200. std::string strDecode;
  201. int nValue;
  202. int i = 0;
  203. while (i < DataByte) {
  204. if (*Data != '\r' && *Data != '\n') {
  205. nValue = DecodeTable[*Data++] << 18;
  206. nValue += DecodeTable[*Data++] << 12;
  207. strDecode += (nValue & 0x00FF0000) >> 16;
  208. if (*Data != '=') {
  209. nValue += DecodeTable[*Data++] << 6;
  210. strDecode += (nValue & 0x0000FF00) >> 8;
  211. if (*Data != '=') {
  212. nValue += DecodeTable[*Data++];
  213. strDecode += nValue & 0x000000FF;
  214. }
  215. }
  216. i += 4;
  217. } else // 回车换行,跳过
  218. {
  219. Data++;
  220. i++;
  221. }
  222. }
  223. return strDecode;
  224. }
  225. DEFINE_OP(ppyoloe_crn_s_300e_coco);
  226. } // namespace serving
  227. } // namespace paddle_serving
  228. } // namespace baidu