You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

statisticspooling.cpp 2.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2016 SoundAI Technology Co., Ltd. (author: Charles Wang)
  2. //
  3. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  4. // in compliance with the License. You may obtain a copy of the License at
  5. //
  6. // https://opensource.org/licenses/BSD-3-Clause
  7. //
  8. // Unless required by applicable law or agreed to in writing, software distributed
  9. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  10. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. // specific language governing permissions and limitations under the License.
  12. #include "statisticspooling.h"
  13. #include <float.h>
  14. #include <limits.h>
  15. namespace ncnn {
  16. StatisticsPooling::StatisticsPooling()
  17. {
  18. one_blob_only = true;
  19. support_inplace = false;
  20. }
  21. int StatisticsPooling::load_param(const ParamDict& pd)
  22. {
  23. include_stddev = pd.get(0, 0);
  24. return 0;
  25. }
  26. int StatisticsPooling::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  27. {
  28. int w = bottom_blob.w;
  29. int h = bottom_blob.h;
  30. int channels = bottom_blob.c;
  31. int size = w * h;
  32. size_t elemsize = bottom_blob.elemsize;
  33. int out_channels = channels;
  34. if (include_stddev)
  35. {
  36. out_channels *= 2;
  37. }
  38. top_blob.create(out_channels, elemsize, opt.blob_allocator);
  39. #pragma omp parallel for num_threads(opt.num_threads)
  40. for (int q = 0; q < channels; q++)
  41. {
  42. const float* ptr = bottom_blob.channel(q);
  43. float mean = 0.f;
  44. for (int i = 0; i < size; i++)
  45. {
  46. mean += ptr[i];
  47. }
  48. top_blob[q] = mean / w / h;
  49. }
  50. #pragma omp parallel for num_threads(opt.num_threads)
  51. for (int q = channels; q < out_channels; q++)
  52. {
  53. const float* ptr = bottom_blob.channel(q - channels);
  54. float std = 0.f;
  55. for (int i = 0; i < size; i++)
  56. {
  57. std += powf((ptr[i] - top_blob[q - channels]), 2);
  58. }
  59. top_blob[q] = sqrtf(std / w / h);
  60. }
  61. return 0;
  62. }
  63. } // namespace ncnn