dark_parse.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/utils/utils.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. namespace utils {
  18. void DarkParse(const std::vector<float> &heatmap, const std::vector<int> &dim,
  19. std::vector<float> *coords, const int px, const int py,
  20. const int index, const int ch) {
  21. /*DARK postprocessing, Zhang et al. Distribution-Aware Coordinate
  22. Representation for Human Pose Estimation (CVPR 2020).
  23. 1) offset = - hassian.inv() * derivative
  24. 2) dx = (heatmap[x+1] - heatmap[x-1])/2.
  25. 3) dxx = (dx[x+1] - dx[x-1])/2.
  26. 4) derivative = Mat([dx, dy])
  27. 5) hassian = Mat([[dxx, dxy], [dxy, dyy]])
  28. */
  29. std::vector<float>::const_iterator first1 = heatmap.begin() + index;
  30. std::vector<float>::const_iterator last1 =
  31. heatmap.begin() + index + dim[2] * dim[3];
  32. std::vector<float> heatmap_ch(first1, last1);
  33. cv::Mat heatmap_mat = cv::Mat(heatmap_ch).reshape(0, dim[2]);
  34. heatmap_mat.convertTo(heatmap_mat, CV_32FC1);
  35. cv::GaussianBlur(heatmap_mat, heatmap_mat, cv::Size(3, 3), 0, 0);
  36. heatmap_mat = heatmap_mat.reshape(1, 1);
  37. heatmap_ch = std::vector<float>(heatmap_mat.reshape(1, 1));
  38. float epsilon = 1e-10;
  39. // sample heatmap to get values in around target location
  40. float xy = log(fmax(heatmap_ch[py * dim[3] + px], epsilon));
  41. float xr = log(fmax(heatmap_ch[py * dim[3] + px + 1], epsilon));
  42. float xl = log(fmax(heatmap_ch[py * dim[3] + px - 1], epsilon));
  43. float xr2 = log(fmax(heatmap_ch[py * dim[3] + px + 2], epsilon));
  44. float xl2 = log(fmax(heatmap_ch[py * dim[3] + px - 2], epsilon));
  45. float yu = log(fmax(heatmap_ch[(py + 1) * dim[3] + px], epsilon));
  46. float yd = log(fmax(heatmap_ch[(py - 1) * dim[3] + px], epsilon));
  47. float yu2 = log(fmax(heatmap_ch[(py + 2) * dim[3] + px], epsilon));
  48. float yd2 = log(fmax(heatmap_ch[(py - 2) * dim[3] + px], epsilon));
  49. float xryu = log(fmax(heatmap_ch[(py + 1) * dim[3] + px + 1], epsilon));
  50. float xryd = log(fmax(heatmap_ch[(py - 1) * dim[3] + px + 1], epsilon));
  51. float xlyu = log(fmax(heatmap_ch[(py + 1) * dim[3] + px - 1], epsilon));
  52. float xlyd = log(fmax(heatmap_ch[(py - 1) * dim[3] + px - 1], epsilon));
  53. // compute dx/dy and dxx/dyy with sampled values
  54. float dx = 0.5 * (xr - xl);
  55. float dy = 0.5 * (yu - yd);
  56. float dxx = 0.25 * (xr2 - 2 * xy + xl2);
  57. float dxy = 0.25 * (xryu - xryd - xlyu + xlyd);
  58. float dyy = 0.25 * (yu2 - 2 * xy + yd2);
  59. // finally get offset by derivative and hassian, which combined by dx/dy and
  60. // dxx/dyy
  61. if (dxx * dyy - dxy * dxy != 0) {
  62. float M[2][2] = {dxx, dxy, dxy, dyy};
  63. float D[2] = {dx, dy};
  64. cv::Mat hassian(2, 2, CV_32F, M);
  65. cv::Mat derivative(2, 1, CV_32F, D);
  66. cv::Mat offset = -hassian.inv() * derivative;
  67. (*coords)[ch * 2] += offset.at<float>(0, 0);
  68. (*coords)[ch * 2 + 1] += offset.at<float>(1, 0);
  69. }
  70. }
  71. } // namespace utils
  72. } // namespace vision
  73. } // namespace ultra_infer