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.

normalize.cpp 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 "normalize.h"
  15. #include <math.h>
  16. namespace ncnn {
  17. DEFINE_LAYER_CREATOR(Normalize)
  18. Normalize::Normalize()
  19. {
  20. one_blob_only = true;
  21. support_inplace = false;
  22. }
  23. #if NCNN_STDIO
  24. #if NCNN_STRING
  25. int Normalize::load_param(FILE* paramfp)
  26. {
  27. int nscan = fscanf(paramfp, "%d %d %f %d",
  28. &across_spatial, &channel_shared, &eps, &scale_data_size);
  29. if (nscan != 4)
  30. {
  31. fprintf(stderr, "Normalize load_param failed %d\n", nscan);
  32. return -1;
  33. }
  34. return 0;
  35. }
  36. #endif // NCNN_STRING
  37. int Normalize::load_param_bin(FILE* paramfp)
  38. {
  39. fread(&across_spatial, sizeof(int), 1, paramfp);
  40. fread(&channel_shared, sizeof(int), 1, paramfp);
  41. fread(&eps, sizeof(float), 1, paramfp);
  42. fread(&scale_data_size, sizeof(int), 1, paramfp);
  43. return 0;
  44. }
  45. int Normalize::load_model(FILE* binfp)
  46. {
  47. int nread;
  48. scale_data.create(1, scale_data_size);
  49. nread = fread(scale_data, scale_data_size * sizeof(float), 1, binfp);
  50. if (nread != 1)
  51. {
  52. fprintf(stderr, "Normalize read scale_data failed %d\n", nread);
  53. return -1;
  54. }
  55. return 0;
  56. }
  57. #endif // NCNN_STDIO
  58. int Normalize::load_param(const unsigned char*& mem)
  59. {
  60. across_spatial = *(int*)(mem);
  61. mem += 4;
  62. channel_shared = *(int*)(mem);
  63. mem += 4;
  64. eps = *(float*)(mem);
  65. mem += 4;
  66. scale_data_size = *(float*)(mem);
  67. mem += 4;
  68. return 0;
  69. }
  70. int Normalize::load_model(const unsigned char*& mem)
  71. {
  72. scale_data = Mat(1, scale_data_size, (float*)mem);
  73. mem += scale_data_size * sizeof(float);
  74. return 0;
  75. }
  76. int Normalize::forward(const Mat& bottom_blob, Mat& top_blob) const
  77. {
  78. int w = bottom_blob.w;
  79. int h = bottom_blob.h;
  80. int channels = bottom_blob.c;
  81. int size = w * h;
  82. top_blob.create(w, h, channels);
  83. if (top_blob.empty())
  84. return -100;
  85. if (across_spatial)
  86. {
  87. // square
  88. Mat square_sum_blob;
  89. square_sum_blob.create(channels);
  90. if (square_sum_blob.empty())
  91. return -100;
  92. float* square_sum_ptr = square_sum_blob;
  93. #pragma omp parallel for
  94. for (int q=0; q<channels; q++)
  95. {
  96. const float* ptr = bottom_blob.channel(q);
  97. float ssum = 0.f;
  98. for (int i=0; i<size; i++)
  99. {
  100. ssum += ptr[i] * ptr[i];
  101. }
  102. square_sum_ptr[q] = ssum;
  103. }
  104. // sum + eps
  105. float ssum = eps;
  106. for (int q=0; q<channels; q++)
  107. {
  108. ssum += square_sum_ptr[q];
  109. }
  110. // 1 / sqrt(ssum)
  111. float a = 1.f / sqrt(ssum);
  112. if (channel_shared)
  113. {
  114. float scale = a * scale_data.data[0];
  115. #pragma omp parallel for
  116. for (int q=0; q<channels; q++)
  117. {
  118. const float* ptr = bottom_blob.channel(q);
  119. float* outptr = top_blob.channel(q);
  120. for (int i=0; i<size; i++)
  121. {
  122. outptr[i] = ptr[i] * scale;
  123. }
  124. }
  125. }
  126. else
  127. {
  128. #pragma omp parallel for
  129. for (int q=0; q<channels; q++)
  130. {
  131. const float* ptr = bottom_blob.channel(q);
  132. float* outptr = top_blob.channel(q);
  133. float scale = a * scale_data.data[q];
  134. for (int i=0; i<size; i++)
  135. {
  136. outptr[i] = ptr[i] * scale;
  137. }
  138. }
  139. }
  140. }
  141. else
  142. {
  143. // square sum, 1 / sqrt(ssum)
  144. Mat square_sum_blob;
  145. square_sum_blob.create(w, h);
  146. if (square_sum_blob.empty())
  147. return -100;
  148. float* ssptr = square_sum_blob;
  149. if (channel_shared)
  150. {
  151. float scale = scale_data.data[0];
  152. #pragma omp parallel for
  153. for (int i=0; i<size; i++)
  154. {
  155. float ssum = eps;
  156. for (int q=0; q<channels; q++)
  157. {
  158. const float* ptr = bottom_blob.channel(q);
  159. ssum += ptr[i] * ptr[i];
  160. }
  161. ssptr[i] = 1.f / sqrt(ssum) * scale;
  162. }
  163. #pragma omp parallel for
  164. for (int q=0; q<channels; q++)
  165. {
  166. const float* ptr = bottom_blob.channel(q);
  167. float* outptr = top_blob.channel(q);
  168. for (int i=0; i<size; i++)
  169. {
  170. outptr[i] = ptr[i] * ssptr[i];
  171. }
  172. }
  173. }
  174. else
  175. {
  176. #pragma omp parallel for
  177. for (int i=0; i<size; i++)
  178. {
  179. float ssum = eps;
  180. for (int q=0; q<channels; q++)
  181. {
  182. const float* ptr = bottom_blob.channel(q);
  183. ssum += ptr[i] * ptr[i];
  184. }
  185. ssptr[i] = 1.f / sqrt(ssum);
  186. }
  187. #pragma omp parallel for
  188. for (int q=0; q<channels; q++)
  189. {
  190. const float* ptr = bottom_blob.channel(q);
  191. float* outptr = top_blob.channel(q);
  192. float scale = scale_data.data[q];
  193. for (int i=0; i<size; i++)
  194. {
  195. outptr[i] = ptr[i] * ssptr[i] * scale;
  196. }
  197. }
  198. }
  199. }
  200. return 0;
  201. }
  202. } // namespace ncnn