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.4 kB

Fix warnings on Visual Studio (#1431) * Fix warnings C4244, C4267 in src/layer/yolov3detectionoutput.cpp C4244: '=': conversion from 'int' to 'float', possible loss of data C4244: 'initializing': conversion from 'float' to 'int', possible loss of data C4244: 'initializing': conversion from 'double' to 'float', possible loss of data C4244: 'return': conversion from 'double' to 'float', 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 warnings C4244, C4267 in src/layer/yolodetectionoutput.cpp C4244: '=': conversion from 'int' to 'float', possible loss of data C4244: 'initializing': conversion from 'float' to 'int', possible loss of data C4244: 'initializing': conversion from 'double' to 'float', possible loss of data C4244: 'return': conversion from 'double' to 'float', 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/quantize.cpp C4244: 'initializing': conversion from 'double' to 'int', possible loss of data * Fix warnings C4244, C4267 in src/layer/detectionoutput.cpp C4244: '=': conversion from 'int' to 'float', possible loss of data C4244: 'initializing': conversion from 'double' to 'float', 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/roipooling.cpp C4244: 'initializing': conversion from 'double' to 'int', possible loss of data * Fix warning C4244 in src/layer/sigmoid.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4267 in src/layer/slice.cpp C4267: '=': conversion from 'size_t' to 'int', possible loss of data C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4267 in src/layer/softmax.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/interp.cpp C4244: '=': conversion from 'float' to 'int', possible loss of data C4244: 'initializing': conversion from 'double' to 'int', possible loss of data * Fix warning C4244 in src/layer/instancenorm.cpp C4244: 'initializing': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/deconvolutiondepthwise.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/convolutiondepthwise.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/net.cpp C4244: 'return': conversion from '__int64' 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 C4267: 'return': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/bnll.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4267 in src/layer/concat.cpp C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4267 in tools/mxnet/mxnet2ncnn.cpp C4244: 'initializing': conversion from 'double' to 'float', possible loss of data C4267: '=': conversion from 'size_t' to 'int', possible loss of data C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data C4305: 'initializing': truncation from 'double' to 'float'
6 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include <math.h>
  16. namespace ncnn {
  17. InstanceNorm::InstanceNorm()
  18. {
  19. one_blob_only = true;
  20. support_inplace = true;
  21. }
  22. int InstanceNorm::load_param(const ParamDict& pd)
  23. {
  24. channels = pd.get(0, 0);
  25. eps = pd.get(1, 0.001f);
  26. return 0;
  27. }
  28. int InstanceNorm::load_model(const ModelBin& mb)
  29. {
  30. gamma_data = mb.load(channels, 1);
  31. if (gamma_data.empty())
  32. return -100;
  33. beta_data = mb.load(channels, 1);
  34. if (beta_data.empty())
  35. return -100;
  36. return 0;
  37. }
  38. int InstanceNorm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
  39. {
  40. // x = (x - mean) / (sqrt(var) + eps) * gamma + beta
  41. int w = bottom_top_blob.w;
  42. int h = bottom_top_blob.h;
  43. int size = w * h;
  44. #pragma omp parallel for num_threads(opt.num_threads)
  45. for (int q = 0; q < channels; q++)
  46. {
  47. float* ptr = bottom_top_blob.channel(q);
  48. // mean and var
  49. float sum = 0.f;
  50. float sqsum = 0.f;
  51. for (int i = 0; i < size; i++)
  52. {
  53. sum += ptr[i];
  54. //sqsum += ptr[i] * ptr[i];
  55. }
  56. float mean = sum / size;
  57. float tmp = 0.f;
  58. for (int i = 0; i < size; i++)
  59. {
  60. tmp = ptr[i] - mean;
  61. sqsum += tmp * tmp;
  62. }
  63. float var = sqsum / size;
  64. // the var maybe minus due to accuracy
  65. //float var = sqsum / size - mean * mean;
  66. float gamma = gamma_data[q];
  67. float beta = beta_data[q];
  68. float a = static_cast<float>(gamma / (sqrt(var + eps)));
  69. float b = -mean * a + beta;
  70. for (int i = 0; i < size; i++)
  71. {
  72. ptr[i] = ptr[i] * a + b;
  73. }
  74. }
  75. return 0;
  76. }
  77. } // namespace ncnn