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.

instancenorm.cpp 2.7 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "instancenorm.h"
  15. namespace ncnn {
  16. InstanceNorm::InstanceNorm()
  17. {
  18. one_blob_only = true;
  19. support_inplace = true;
  20. }
  21. int InstanceNorm::load_param(const ParamDict& pd)
  22. {
  23. channels = pd.get(0, 0);
  24. eps = pd.get(1, 0.001f);
  25. affine = pd.get(2, 1);
  26. return 0;
  27. }
  28. int InstanceNorm::load_model(const ModelBin& mb)
  29. {
  30. if (affine == 0)
  31. return 0;
  32. gamma_data = mb.load(channels, 1);
  33. if (gamma_data.empty())
  34. return -100;
  35. beta_data = mb.load(channels, 1);
  36. if (beta_data.empty())
  37. return -100;
  38. return 0;
  39. }
  40. int InstanceNorm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
  41. {
  42. // x = (x - mean) / (sqrt(var + eps)) * gamma + beta
  43. int w = bottom_top_blob.w;
  44. int h = bottom_top_blob.h;
  45. int c = bottom_top_blob.c;
  46. int size = w * h;
  47. #pragma omp parallel for num_threads(opt.num_threads)
  48. for (int q = 0; q < c; q++)
  49. {
  50. float* ptr = bottom_top_blob.channel(q);
  51. // mean and var
  52. float sum = 0.f;
  53. float sqsum = 0.f;
  54. for (int i = 0; i < size; i++)
  55. {
  56. sum += ptr[i];
  57. //sqsum += ptr[i] * ptr[i];
  58. }
  59. float mean = sum / size;
  60. float tmp = 0.f;
  61. for (int i = 0; i < size; i++)
  62. {
  63. tmp = ptr[i] - mean;
  64. sqsum += tmp * tmp;
  65. }
  66. float var = sqsum / size;
  67. // the var maybe minus due to accuracy
  68. //float var = sqsum / size - mean * mean;
  69. float a;
  70. float b;
  71. if (affine)
  72. {
  73. float gamma = gamma_data[q];
  74. float beta = beta_data[q];
  75. a = gamma / (sqrtf(var + eps));
  76. b = -mean * a + beta;
  77. }
  78. else
  79. {
  80. a = 1.f / (sqrtf(var + eps));
  81. b = -mean * a;
  82. }
  83. for (int i = 0; i < size; i++)
  84. {
  85. ptr[i] = ptr[i] * a + b;
  86. }
  87. }
  88. return 0;
  89. }
  90. } // namespace ncnn