ppyolo_mbv3_large_coco.cpp 9.3 KB

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