normalize.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/common/processors/normalize.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. Normalize::Normalize(const std::vector<float> &mean,
  18. const std::vector<float> &std, bool is_scale,
  19. const std::vector<float> &min,
  20. const std::vector<float> &max, bool swap_rb) {
  21. FDASSERT(mean.size() == std.size(),
  22. "Normalize: requires the size of mean equal to the size of std.");
  23. std::vector<double> mean_(mean.begin(), mean.end());
  24. std::vector<double> std_(std.begin(), std.end());
  25. std::vector<double> min_(mean.size(), 0.0);
  26. std::vector<double> max_(mean.size(), 255.0);
  27. if (min.size() != 0) {
  28. FDASSERT(
  29. min.size() == mean.size(),
  30. "Normalize: while min is defined, requires the size of min equal to "
  31. "the size of mean.");
  32. min_.assign(min.begin(), min.end());
  33. }
  34. if (max.size() != 0) {
  35. FDASSERT(
  36. min.size() == mean.size(),
  37. "Normalize: while max is defined, requires the size of max equal to "
  38. "the size of mean.");
  39. max_.assign(max.begin(), max.end());
  40. }
  41. for (auto c = 0; c < mean_.size(); ++c) {
  42. double alpha = 1.0;
  43. if (is_scale) {
  44. alpha /= (max_[c] - min_[c]);
  45. }
  46. double beta = -1.0 * (mean_[c] + min_[c] * alpha) / std_[c];
  47. alpha /= std_[c];
  48. alpha_.push_back(alpha);
  49. beta_.push_back(beta);
  50. }
  51. swap_rb_ = swap_rb;
  52. }
  53. bool Normalize::ImplByOpenCV(Mat *mat) {
  54. cv::Mat *im = mat->GetOpenCVMat();
  55. std::vector<cv::Mat> split_im;
  56. cv::split(*im, split_im);
  57. if (swap_rb_)
  58. std::swap(split_im[0], split_im[2]);
  59. for (int c = 0; c < im->channels(); c++) {
  60. split_im[c].convertTo(split_im[c], CV_32FC1, alpha_[c], beta_[c]);
  61. }
  62. cv::merge(split_im, *im);
  63. return true;
  64. }
  65. #ifdef ENABLE_FLYCV
  66. bool Normalize::ImplByFlyCV(Mat *mat) {
  67. fcv::Mat *im = mat->GetFlyCVMat();
  68. if (im->channels() != 3) {
  69. FDERROR << "Only supports 3-channels image in FlyCV, but now it's "
  70. << im->channels() << "." << std::endl;
  71. return false;
  72. }
  73. std::vector<float> mean(3, 0);
  74. std::vector<float> std(3, 0);
  75. for (size_t i = 0; i < 3; ++i) {
  76. std[i] = 1.0 / alpha_[i];
  77. mean[i] = -1 * beta_[i] * std[i];
  78. }
  79. std::vector<uint32_t> channel_reorder_index = {0, 1, 2};
  80. if (swap_rb_)
  81. std::swap(channel_reorder_index[0], channel_reorder_index[2]);
  82. fcv::Mat new_im(im->width(), im->height(), fcv::FCVImageType::PKG_BGR_F32);
  83. fcv::normalize_to_submean_to_reorder(*im, mean, std, channel_reorder_index,
  84. new_im, true);
  85. mat->SetMat(new_im);
  86. return true;
  87. }
  88. #endif
  89. bool Normalize::Run(Mat *mat, const std::vector<float> &mean,
  90. const std::vector<float> &std, bool is_scale,
  91. const std::vector<float> &min,
  92. const std::vector<float> &max, ProcLib lib, bool swap_rb) {
  93. auto n = Normalize(mean, std, is_scale, min, max, swap_rb);
  94. return n(mat, lib);
  95. }
  96. } // namespace vision
  97. } // namespace ultra_infer