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.

scale.cpp 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "scale.h"
  15. namespace ncnn {
  16. DEFINE_LAYER_CREATOR(Scale)
  17. Scale::Scale()
  18. {
  19. one_blob_only = true;
  20. support_inplace = true;
  21. }
  22. int Scale::load_param(const ParamDict& pd)
  23. {
  24. scale_data_size = pd.get(0, 0);
  25. bias_term = pd.get(1, 0);
  26. if (scale_data_size == -233)
  27. one_blob_only = false;
  28. return 0;
  29. }
  30. #if NCNN_STDIO
  31. int Scale::load_model(FILE* binfp)
  32. {
  33. int nread;
  34. if (scale_data_size != -233)
  35. {
  36. scale_data.create(scale_data_size);
  37. if (scale_data.empty())
  38. return -100;
  39. nread = fread(scale_data, scale_data_size * sizeof(float), 1, binfp);
  40. if (nread != 1)
  41. {
  42. fprintf(stderr, "Scale read scale_data failed %d\n", nread);
  43. return -1;
  44. }
  45. }
  46. if (bias_term)
  47. {
  48. bias_data.create(scale_data_size);
  49. if (bias_data.empty())
  50. return -100;
  51. nread = fread(bias_data, scale_data_size * sizeof(float), 1, binfp);
  52. if (nread != 1)
  53. {
  54. fprintf(stderr, "Scale read bias_data failed %d\n", nread);
  55. return -1;
  56. }
  57. }
  58. return 0;
  59. }
  60. #endif // NCNN_STDIO
  61. int Scale::load_model(const unsigned char*& mem)
  62. {
  63. if (scale_data_size != -233)
  64. {
  65. scale_data = Mat(scale_data_size, (float*)mem);
  66. mem += scale_data_size * sizeof(float);
  67. }
  68. if (bias_term)
  69. {
  70. bias_data = Mat(scale_data_size, (float*)mem);
  71. mem += scale_data_size * sizeof(float);
  72. }
  73. return 0;
  74. }
  75. int Scale::forward_inplace(std::vector<Mat>& bottom_top_blobs) const
  76. {
  77. Mat& bottom_top_blob = bottom_top_blobs[0];
  78. const Mat& scale_blob = bottom_top_blobs[1];
  79. int w = bottom_top_blob.w;
  80. int h = bottom_top_blob.h;
  81. int channels = bottom_top_blob.c;
  82. int size = w * h;
  83. if (bias_term)
  84. {
  85. const float* bias_ptr = bias_data;
  86. #pragma omp parallel for
  87. for (int q=0; q<channels; q++)
  88. {
  89. float* ptr = bottom_top_blob.channel(q);
  90. float s = scale_blob.channel(q)[0];
  91. float bias = bias_ptr[q];
  92. for (int i=0; i<size; i++)
  93. {
  94. ptr[i] = ptr[i] * s + bias;
  95. }
  96. }
  97. }
  98. else
  99. {
  100. #pragma omp parallel for
  101. for (int q=0; q<channels; q++)
  102. {
  103. float* ptr = bottom_top_blob.channel(q);
  104. float s = scale_blob.channel(q)[0];
  105. for (int i=0; i<size; i++)
  106. {
  107. ptr[i] *= s;
  108. }
  109. }
  110. }
  111. return 0;
  112. }
  113. int Scale::forward_inplace(Mat& bottom_top_blob) const
  114. {
  115. int w = bottom_top_blob.w;
  116. int h = bottom_top_blob.h;
  117. int channels = bottom_top_blob.c;
  118. int size = w * h;
  119. if (bias_term)
  120. {
  121. const float* scale_ptr = scale_data;
  122. const float* bias_ptr = bias_data;
  123. #pragma omp parallel for
  124. for (int q=0; q<channels; q++)
  125. {
  126. float* ptr = bottom_top_blob.channel(q);
  127. float s = scale_ptr[q];
  128. float bias = bias_ptr[q];
  129. for (int i=0; i<size; i++)
  130. {
  131. ptr[i] = ptr[i] * s + bias;
  132. }
  133. }
  134. }
  135. else
  136. {
  137. const float* scale_ptr = scale_data;
  138. #pragma omp parallel for
  139. for (int q=0; q<channels; q++)
  140. {
  141. float* ptr = bottom_top_blob.channel(q);
  142. float s = scale_ptr[q];
  143. for (int i=0; i<size; i++)
  144. {
  145. ptr[i] *= s;
  146. }
  147. }
  148. }
  149. return 0;
  150. }
  151. } // namespace ncnn