keypoint_postprocess.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright (c) 2021 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 "include/keypoint_postprocess.h"
  15. #define PI 3.1415926535
  16. #define HALF_CIRCLE_DEGREE 180
  17. cv::Point2f get_3rd_point(cv::Point2f& a, cv::Point2f& b) {
  18. cv::Point2f direct{a.x - b.x, a.y - b.y};
  19. return cv::Point2f(a.x - direct.y, a.y + direct.x);
  20. }
  21. std::vector<float> get_dir(float src_point_x,
  22. float src_point_y,
  23. float rot_rad) {
  24. float sn = sin(rot_rad);
  25. float cs = cos(rot_rad);
  26. std::vector<float> src_result{0.0, 0.0};
  27. src_result[0] = src_point_x * cs - src_point_y * sn;
  28. src_result[1] = src_point_x * sn + src_point_y * cs;
  29. return src_result;
  30. }
  31. void affine_tranform(
  32. float pt_x, float pt_y, cv::Mat& trans, std::vector<float>& preds, int p) {
  33. double new1[3] = {pt_x, pt_y, 1.0};
  34. cv::Mat new_pt(3, 1, trans.type(), new1);
  35. cv::Mat w = trans * new_pt;
  36. preds[p * 3 + 1] = static_cast<float>(w.at<double>(0, 0));
  37. preds[p * 3 + 2] = static_cast<float>(w.at<double>(1, 0));
  38. }
  39. void get_affine_transform(std::vector<float>& center,
  40. std::vector<float>& scale,
  41. float rot,
  42. std::vector<int>& output_size,
  43. cv::Mat& trans,
  44. int inv) {
  45. float src_w = scale[0];
  46. float dst_w = static_cast<float>(output_size[0]);
  47. float dst_h = static_cast<float>(output_size[1]);
  48. float rot_rad = rot * PI / HALF_CIRCLE_DEGREE;
  49. std::vector<float> src_dir = get_dir(-0.5 * src_w, 0, rot_rad);
  50. std::vector<float> dst_dir{static_cast<float>(-0.5) * dst_w, 0.0};
  51. cv::Point2f srcPoint2f[3], dstPoint2f[3];
  52. srcPoint2f[0] = cv::Point2f(center[0], center[1]);
  53. srcPoint2f[1] = cv::Point2f(center[0] + src_dir[0], center[1] + src_dir[1]);
  54. srcPoint2f[2] = get_3rd_point(srcPoint2f[0], srcPoint2f[1]);
  55. dstPoint2f[0] = cv::Point2f(dst_w * 0.5, dst_h * 0.5);
  56. dstPoint2f[1] =
  57. cv::Point2f(dst_w * 0.5 + dst_dir[0], dst_h * 0.5 + dst_dir[1]);
  58. dstPoint2f[2] = get_3rd_point(dstPoint2f[0], dstPoint2f[1]);
  59. if (inv == 0) {
  60. trans = cv::getAffineTransform(srcPoint2f, dstPoint2f);
  61. } else {
  62. trans = cv::getAffineTransform(dstPoint2f, srcPoint2f);
  63. }
  64. }
  65. void transform_preds(std::vector<float>& coords,
  66. std::vector<float>& center,
  67. std::vector<float>& scale,
  68. std::vector<int>& output_size,
  69. std::vector<int64_t>& dim,
  70. std::vector<float>& target_coords) {
  71. cv::Mat trans(2, 3, CV_64FC1);
  72. get_affine_transform(center, scale, 0, output_size, trans, 1);
  73. for (int p = 0; p < dim[1]; ++p) {
  74. affine_tranform(coords[p * 2], coords[p * 2 + 1], trans, target_coords, p);
  75. }
  76. }
  77. // only for batchsize == 1
  78. void get_max_preds(std::vector<float>& heatmap,
  79. std::vector<int>& dim,
  80. std::vector<float>& preds,
  81. std::vector<float>& maxvals,
  82. int batchid,
  83. int joint_idx) {
  84. int num_joints = dim[1];
  85. int width = dim[3];
  86. std::vector<int> idx;
  87. idx.resize(num_joints * 2);
  88. for (int j = 0; j < dim[1]; j++) {
  89. float* index = &(
  90. heatmap[batchid * num_joints * dim[2] * dim[3] + j * dim[2] * dim[3]]);
  91. float* end = index + dim[2] * dim[3];
  92. float* max_dis = std::max_element(index, end);
  93. auto max_id = std::distance(index, max_dis);
  94. maxvals[j] = *max_dis;
  95. if (*max_dis > 0) {
  96. preds[j * 2] = static_cast<float>(max_id % width);
  97. preds[j * 2 + 1] = static_cast<float>(max_id / width);
  98. }
  99. }
  100. }
  101. void dark_parse(std::vector<float>& heatmap,
  102. std::vector<int64_t>& dim,
  103. std::vector<float>& coords,
  104. int px,
  105. int py,
  106. int index,
  107. int ch){
  108. /*DARK postpocessing, Zhang et al. Distribution-Aware Coordinate
  109. Representation for Human Pose Estimation (CVPR 2020).
  110. 1) offset = - hassian.inv() * derivative
  111. 2) dx = (heatmap[x+1] - heatmap[x-1])/2.
  112. 3) dxx = (dx[x+1] - dx[x-1])/2.
  113. 4) derivative = Mat([dx, dy])
  114. 5) hassian = Mat([[dxx, dxy], [dxy, dyy]])
  115. */
  116. std::vector<float>::const_iterator first1 = heatmap.begin() + index;
  117. std::vector<float>::const_iterator last1 = heatmap.begin() + index + dim[2] * dim[3];
  118. std::vector<float> heatmap_ch(first1, last1);
  119. cv::Mat heatmap_mat = cv::Mat(heatmap_ch).reshape(0,dim[2]);
  120. heatmap_mat.convertTo(heatmap_mat, CV_32FC1);
  121. cv::GaussianBlur(heatmap_mat, heatmap_mat, cv::Size(3, 3), 0, 0);
  122. heatmap_mat = heatmap_mat.reshape(1,1);
  123. heatmap_ch = std::vector<float>(heatmap_mat.reshape(1,1));
  124. float epsilon = 1e-10;
  125. //sample heatmap to get values in around target location
  126. float xy = log(fmax(heatmap_ch[py * dim[3] + px], epsilon));
  127. float xr = log(fmax(heatmap_ch[py * dim[3] + px + 1], epsilon));
  128. float xl = log(fmax(heatmap_ch[py * dim[3] + px - 1], epsilon));
  129. float xr2 = log(fmax(heatmap_ch[py * dim[3] + px + 2], epsilon));
  130. float xl2 = log(fmax(heatmap_ch[py * dim[3] + px - 2], epsilon));
  131. float yu = log(fmax(heatmap_ch[(py + 1) * dim[3] + px], epsilon));
  132. float yd = log(fmax(heatmap_ch[(py - 1) * dim[3] + px], epsilon));
  133. float yu2 = log(fmax(heatmap_ch[(py + 2) * dim[3] + px], epsilon));
  134. float yd2 = log(fmax(heatmap_ch[(py - 2) * dim[3] + px], epsilon));
  135. float xryu = log(fmax(heatmap_ch[(py + 1) * dim[3] + px + 1], epsilon));
  136. float xryd = log(fmax(heatmap_ch[(py - 1) * dim[3] + px + 1], epsilon));
  137. float xlyu = log(fmax(heatmap_ch[(py + 1) * dim[3] + px - 1], epsilon));
  138. float xlyd = log(fmax(heatmap_ch[(py - 1) * dim[3] + px - 1], epsilon));
  139. //compute dx/dy and dxx/dyy with sampled values
  140. float dx = 0.5 * (xr - xl);
  141. float dy = 0.5 * (yu - yd);
  142. float dxx = 0.25 * (xr2 - 2*xy + xl2);
  143. float dxy = 0.25 * (xryu - xryd - xlyu + xlyd);
  144. float dyy = 0.25 * (yu2 - 2*xy + yd2);
  145. //finally get offset by derivative and hassian, which combined by dx/dy and dxx/dyy
  146. if(dxx * dyy - dxy*dxy != 0){
  147. float M[2][2] = {dxx, dxy, dxy, dyy};
  148. float D[2] = {dx, dy};
  149. cv::Mat hassian(2,2,CV_32F,M);
  150. cv::Mat derivative(2,1,CV_32F,D);
  151. cv::Mat offset = - hassian.inv() * derivative;
  152. coords[ch * 2] += offset.at<float>(0,0);
  153. coords[ch * 2 + 1] += offset.at<float>(1,0);
  154. }
  155. }
  156. void get_final_preds(std::vector<float>& heatmap,
  157. std::vector<int64_t>& dim,
  158. std::vector<int64_t>& idxout,
  159. std::vector<int64_t>& idxdim,
  160. std::vector<float>& center,
  161. std::vector<float> scale,
  162. std::vector<float>& preds,
  163. int batchid,
  164. bool DARK) {
  165. std::vector<float> coords;
  166. coords.resize(dim[1] * 2);
  167. int heatmap_height = dim[2];
  168. int heatmap_width = dim[3];
  169. for (int j = 0; j < dim[1]; ++j) {
  170. int index = (batchid * dim[1] + j) * dim[2] * dim[3];
  171. int idx = idxout[batchid * dim[1] + j];
  172. preds[j * 3] = heatmap[index + idx];
  173. coords[j * 2] = idx % heatmap_width;
  174. coords[j * 2 + 1] = idx / heatmap_width;
  175. int px = int(coords[j * 2] + 0.5);
  176. int py = int(coords[j * 2 + 1] + 0.5);
  177. if(DARK && px > 1 && px < heatmap_width - 2){
  178. dark_parse(heatmap, dim, coords, px, py, index, j);
  179. }
  180. else{
  181. if (px > 0 && px < heatmap_width - 1) {
  182. float diff_x = heatmap[index + py * dim[3] + px + 1] -
  183. heatmap[index + py * dim[3] + px - 1];
  184. coords[j * 2] += diff_x > 0 ? 1 : -1 * 0.25;
  185. }
  186. if (py > 0 && py < heatmap_height - 1) {
  187. float diff_y = heatmap[index + (py + 1) * dim[3] + px] -
  188. heatmap[index + (py - 1) * dim[3] + px];
  189. coords[j * 2 + 1] += diff_y > 0 ? 1 : -1 * 0.25;
  190. }
  191. }
  192. }
  193. std::vector<int> img_size{heatmap_width, heatmap_height};
  194. transform_preds(coords, center, scale, img_size, dim, preds);
  195. }