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 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. int Scale::load_model(const ModelBin& mb)
  31. {
  32. if (scale_data_size != -233)
  33. {
  34. scale_data = mb.load(scale_data_size, 1);
  35. if (scale_data.empty())
  36. return -100;
  37. }
  38. if (bias_term)
  39. {
  40. bias_data = mb.load(scale_data_size, 1);
  41. if (bias_data.empty())
  42. return -100;
  43. }
  44. return 0;
  45. }
  46. int Scale::forward_inplace(std::vector<Mat>& bottom_top_blobs) const
  47. {
  48. Mat& bottom_top_blob = bottom_top_blobs[0];
  49. const Mat& scale_blob = bottom_top_blobs[1];
  50. int w = bottom_top_blob.w;
  51. int h = bottom_top_blob.h;
  52. int channels = bottom_top_blob.c;
  53. int size = w * h;
  54. if (bias_term)
  55. {
  56. #pragma omp parallel for
  57. for (int q=0; q<channels; q++)
  58. {
  59. float* ptr = bottom_top_blob.channel(q);
  60. float s = scale_blob[q];
  61. float bias = bias_data[q];
  62. for (int i=0; i<size; i++)
  63. {
  64. ptr[i] = ptr[i] * s + bias;
  65. }
  66. }
  67. }
  68. else
  69. {
  70. #pragma omp parallel for
  71. for (int q=0; q<channels; q++)
  72. {
  73. float* ptr = bottom_top_blob.channel(q);
  74. float s = scale_blob[q];
  75. for (int i=0; i<size; i++)
  76. {
  77. ptr[i] *= s;
  78. }
  79. }
  80. }
  81. return 0;
  82. }
  83. int Scale::forward_inplace(Mat& bottom_top_blob) const
  84. {
  85. int w = bottom_top_blob.w;
  86. int h = bottom_top_blob.h;
  87. int channels = bottom_top_blob.c;
  88. int size = w * h;
  89. if (bias_term)
  90. {
  91. #pragma omp parallel for
  92. for (int q=0; q<channels; q++)
  93. {
  94. float* ptr = bottom_top_blob.channel(q);
  95. float s = scale_data[q];
  96. float bias = bias_data[q];
  97. for (int i=0; i<size; i++)
  98. {
  99. ptr[i] = ptr[i] * s + bias;
  100. }
  101. }
  102. }
  103. else
  104. {
  105. #pragma omp parallel for
  106. for (int q=0; q<channels; q++)
  107. {
  108. float* ptr = bottom_top_blob.channel(q);
  109. float s = scale_data[q];
  110. for (int i=0; i<size; i++)
  111. {
  112. ptr[i] *= s;
  113. }
  114. }
  115. }
  116. return 0;
  117. }
  118. } // namespace ncnn