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.

lrn.cpp 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "lrn.h"
  15. #include <math.h>
  16. namespace ncnn {
  17. DEFINE_LAYER_CREATOR(LRN)
  18. LRN::LRN()
  19. {
  20. one_blob_only = true;
  21. support_inplace = true;
  22. }
  23. int LRN::load_param(const ParamDict& pd)
  24. {
  25. region_type = pd.get(0, 0);
  26. local_size = pd.get(1, 5);
  27. alpha = pd.get(2, 1.f);
  28. beta = pd.get(3, 0.75f);
  29. bias = pd.get(4, 1.f);
  30. return 0;
  31. }
  32. int LRN::forward_inplace(Mat& bottom_top_blob) const
  33. {
  34. int w = bottom_top_blob.w;
  35. int h = bottom_top_blob.h;
  36. int channels = bottom_top_blob.c;
  37. int size = w * h;
  38. // squared values with local_size padding
  39. Mat square_blob;
  40. square_blob.create(w, h, channels);
  41. if (square_blob.empty())
  42. return -100;
  43. #pragma omp parallel for
  44. for (int q=0; q<channels; q++)
  45. {
  46. const float* ptr = bottom_top_blob.channel(q);
  47. float* outptr = square_blob.channel(q);
  48. for (int i=0; i<size; i++)
  49. {
  50. outptr[i] = ptr[i] * ptr[i];
  51. }
  52. }
  53. if (region_type == NormRegion_ACROSS_CHANNELS)
  54. {
  55. Mat square_sum;
  56. square_sum.create(w, h, channels);
  57. if (square_sum.empty())
  58. return -100;
  59. square_sum.fill(0.f);
  60. const float alpha_div_size = alpha / local_size;
  61. #pragma omp parallel for
  62. for (int q=0; q<channels; q++)
  63. {
  64. // square sum
  65. float* ssptr = square_sum.channel(q);
  66. for (int p=q - local_size / 2; p<=q + local_size / 2; p++)
  67. {
  68. if (p < 0 || p >= channels)
  69. continue;
  70. const float* sptr = square_blob.channel(p);
  71. for (int i=0; i<size; i++)
  72. {
  73. ssptr[i] += sptr[i];
  74. }
  75. }
  76. float* ptr = bottom_top_blob.channel(q);
  77. for (int i=0; i<size; i++)
  78. {
  79. ptr[i] = ptr[i] * pow(bias + alpha_div_size * ssptr[i], -beta);
  80. }
  81. }
  82. }
  83. else if (region_type == NormRegion_WITHIN_CHANNEL)
  84. {
  85. int outw = w;
  86. int outh = h;
  87. Mat square_blob_bordered = square_blob;
  88. int pad = local_size / 2;
  89. if (pad > 0)
  90. {
  91. copy_make_border(square_blob, square_blob_bordered, pad, local_size - pad - 1, pad, local_size - pad - 1, BORDER_CONSTANT, 0.f);
  92. if (square_blob_bordered.empty())
  93. return -100;
  94. w = square_blob_bordered.w;
  95. h = square_blob_bordered.h;
  96. }
  97. const int maxk = local_size * local_size;
  98. const float alpha_div_size = alpha / maxk;
  99. // norm window offsets
  100. std::vector<int> _space_ofs(maxk);
  101. int* space_ofs = &_space_ofs[0];
  102. {
  103. int p1 = 0;
  104. int p2 = 0;
  105. int gap = w - local_size;
  106. for (int i = 0; i < local_size; i++)
  107. {
  108. for (int j = 0; j < local_size; j++)
  109. {
  110. space_ofs[p1] = p2;
  111. p1++;
  112. p2++;
  113. }
  114. p2 += gap;
  115. }
  116. }
  117. #pragma omp parallel for
  118. for (int q=0; q<channels; q++)
  119. {
  120. float* ptr = bottom_top_blob.channel(q);
  121. const Mat m = square_blob_bordered.channel(q);
  122. for (int i = 0; i < outh; i++)
  123. {
  124. for (int j = 0; j < outw; j++)
  125. {
  126. const float* sptr = m.row(i) + j;
  127. float ss = 0.f;
  128. for (int k = 0; k < maxk; k++)
  129. {
  130. float val = sptr[ space_ofs[k] ];
  131. ss += val;
  132. }
  133. ptr[j] = ptr[j] * pow(bias + alpha_div_size * ss, -beta);
  134. }
  135. ptr += outw;
  136. }
  137. }
  138. }
  139. return 0;
  140. }
  141. } // namespace ncnn