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.

mvn.cpp 4.8 kB

8 years ago
8 years ago
Fix warnings on Visual Studio (#1456) * Fix warning C4244 in src/layer/convolution.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/convolution_sgemm_int8.h C4244: 'initializing': conversion from 'double' to 'int', possible loss of data * Fix warning C4244 in src/layer/deconvolution.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/elu.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4267 in src/layer/embed.cpp C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/exp.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/innerproduct.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/log.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data C4244: 'initializing': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/lrn.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/mvn.cp C4244: 'initializing': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/power.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warnings C4244 and C4267 in src/layer/proposal.cpp C4244: 'initializing': conversion from 'double' to 'float', possible loss of data C4244: 'initializing': conversion from 'double' to 'int', possible loss of data C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/reduction.cpp C4244: 'return': conversion from 'double' to 'T', possible loss of data * Fix warning C4244 in src/layer/tanh.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/binaryop.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warnings C4244 and C4267 in src/layer/unaryop.cpp C4244: 'return': conversion from 'double' to 'T', possible loss of data C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/x86/convolutiondepthwise_3x3_int8.h C4244: 'initializing': conversion from 'double' to 'int', possible loss of data
6 years ago
8 years ago
Fix warnings on Visual Studio (#1456) * Fix warning C4244 in src/layer/convolution.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/convolution_sgemm_int8.h C4244: 'initializing': conversion from 'double' to 'int', possible loss of data * Fix warning C4244 in src/layer/deconvolution.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/elu.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4267 in src/layer/embed.cpp C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/exp.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/innerproduct.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/log.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data C4244: 'initializing': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/lrn.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/mvn.cp C4244: 'initializing': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/power.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warnings C4244 and C4267 in src/layer/proposal.cpp C4244: 'initializing': conversion from 'double' to 'float', possible loss of data C4244: 'initializing': conversion from 'double' to 'int', possible loss of data C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/reduction.cpp C4244: 'return': conversion from 'double' to 'T', possible loss of data * Fix warning C4244 in src/layer/tanh.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/binaryop.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warnings C4244 and C4267 in src/layer/unaryop.cpp C4244: 'return': conversion from 'double' to 'T', possible loss of data C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/x86/convolutiondepthwise_3x3_int8.h C4244: 'initializing': conversion from 'double' to 'int', possible loss of data
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "mvn.h"
  15. #include <math.h>
  16. namespace ncnn {
  17. MVN::MVN()
  18. {
  19. one_blob_only = true;
  20. support_inplace = false;
  21. }
  22. int MVN::load_param(const ParamDict& pd)
  23. {
  24. normalize_variance = pd.get(0, 0);
  25. across_channels = pd.get(1, 0);
  26. eps = pd.get(2, 0.0001f);
  27. return 0;
  28. }
  29. int MVN::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  30. {
  31. int w = bottom_blob.w;
  32. int h = bottom_blob.h;
  33. int channels = bottom_blob.c;
  34. size_t elemsize = bottom_blob.elemsize;
  35. int size = w * h;
  36. top_blob.create(w, h, channels, elemsize, opt.blob_allocator);
  37. if (top_blob.empty())
  38. return -100;
  39. // prepare sum per channel
  40. Mat sum(channels, elemsize, opt.workspace_allocator);
  41. if (sum.empty())
  42. return -100;
  43. #pragma omp parallel for num_threads(opt.num_threads)
  44. for (int q = 0; q < channels; q++)
  45. {
  46. const float* ptr = bottom_blob.channel(q);
  47. float s = 0.f;
  48. for (int i = 0; i < size; i++)
  49. {
  50. s += ptr[i];
  51. }
  52. sum[q] = s;
  53. }
  54. if (across_channels)
  55. {
  56. // compute mean across channels
  57. float mean = 0.f;
  58. for (int q = 0; q < channels; q++)
  59. {
  60. mean += sum[q];
  61. }
  62. mean = mean / (channels * size);
  63. // subtract mean
  64. #pragma omp parallel for num_threads(opt.num_threads)
  65. for (int q = 0; q < channels; q++)
  66. {
  67. const float* ptr = bottom_blob.channel(q);
  68. float* outptr = top_blob.channel(q);
  69. for (int i = 0; i < size; i++)
  70. {
  71. outptr[i] = ptr[i] - mean;
  72. }
  73. }
  74. }
  75. else
  76. {
  77. // subtract mean
  78. #pragma omp parallel for num_threads(opt.num_threads)
  79. for (int q = 0; q < channels; q++)
  80. {
  81. const float* ptr = bottom_blob.channel(q);
  82. float* outptr = top_blob.channel(q);
  83. float mean = sum[q] / size;
  84. for (int i = 0; i < size; i++)
  85. {
  86. outptr[i] = ptr[i] - mean;
  87. }
  88. }
  89. }
  90. if (normalize_variance)
  91. {
  92. // prepare squared sum per channel
  93. Mat sqsum(channels, elemsize, opt.workspace_allocator);
  94. if (sqsum.empty())
  95. return -100;
  96. #pragma omp parallel for num_threads(opt.num_threads)
  97. for (int q = 0; q < channels; q++)
  98. {
  99. const float* ptr = top_blob.channel(q);
  100. float s = 0.f;
  101. for (int i = 0; i < size; i++)
  102. {
  103. s += ptr[i] * ptr[i];
  104. }
  105. sqsum[q] = s;
  106. }
  107. if (across_channels)
  108. {
  109. // compute squared mean across channels
  110. float sqmean = 0.f;
  111. for (int q = 0; q < channels; q++)
  112. {
  113. sqmean += sqsum[q];
  114. }
  115. sqmean = sqmean / (channels * size);
  116. // normalize variance
  117. float norm_var = static_cast<float>(sqrt(sqmean) + eps);
  118. float norm_var_inv = 1.f / norm_var;
  119. // apply normalize_variance
  120. #pragma omp parallel for num_threads(opt.num_threads)
  121. for (int q = 0; q < channels; q++)
  122. {
  123. float* outptr = top_blob.channel(q);
  124. for (int i = 0; i < size; i++)
  125. {
  126. outptr[i] = outptr[i] * norm_var_inv;
  127. }
  128. }
  129. }
  130. else
  131. {
  132. // apply normalize_variance
  133. #pragma omp parallel for num_threads(opt.num_threads)
  134. for (int q = 0; q < channels; q++)
  135. {
  136. float* outptr = top_blob.channel(q);
  137. float sqmean = sqsum[q] / size;
  138. float norm_var = static_cast<float>(sqrt(sqmean) + eps);
  139. float norm_var_inv = 1.f / norm_var;
  140. for (int i = 0; i < size; i++)
  141. {
  142. outptr[i] = outptr[i] * norm_var_inv;
  143. }
  144. }
  145. }
  146. }
  147. return 0;
  148. }
  149. } // namespace ncnn