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.

convolution.cpp 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "convolution.h"
  15. #include "layer_type.h"
  16. namespace ncnn {
  17. DEFINE_LAYER_CREATOR(Convolution)
  18. Convolution::Convolution()
  19. {
  20. one_blob_only = true;
  21. support_inplace = false;
  22. }
  23. int Convolution::load_param(const ParamDict& pd)
  24. {
  25. num_output = pd.get(0, 0);
  26. kernel_w = pd.get(1, 0);
  27. kernel_h = pd.get(11, kernel_w);
  28. dilation_w = pd.get(2, 1);
  29. dilation_h = pd.get(12, dilation_w);
  30. stride_w = pd.get(3, 1);
  31. stride_h = pd.get(13, stride_w);
  32. pad_w = pd.get(4, 0);
  33. pad_h = pd.get(14, pad_w);
  34. bias_term = pd.get(5, 0);
  35. weight_data_size = pd.get(6, 0);
  36. return 0;
  37. }
  38. int Convolution::load_model(const ModelBin& mb)
  39. {
  40. weight_data = mb.load(weight_data_size, 0);
  41. if (weight_data.empty())
  42. return -100;
  43. if (bias_term)
  44. {
  45. bias_data = mb.load(num_output, 1);
  46. if (bias_data.empty())
  47. return -100;
  48. }
  49. return 0;
  50. }
  51. int Convolution::forward(const Mat& bottom_blob, Mat& top_blob) const
  52. {
  53. // convolv with NxN kernel
  54. // value = value + bias
  55. // flattened blob, implement as InnerProduct
  56. if (bottom_blob.dims == 1 && kernel_w == 1 && kernel_h == 1)
  57. {
  58. int num_input = weight_data_size / num_output;
  59. if (bottom_blob.w == num_input)
  60. {
  61. // call InnerProduct
  62. ncnn::Layer* op = ncnn::create_layer(ncnn::LayerType::InnerProduct);
  63. // set param
  64. ncnn::ParamDict pd;
  65. pd.set(0, num_output);
  66. pd.set(1, bias_term);
  67. pd.set(2, weight_data_size);
  68. op->load_param(pd);
  69. // set weights
  70. ncnn::Mat weights[2];
  71. weights[0] = weight_data;
  72. weights[1] = bias_data;
  73. op->load_model(ModelBinFromMatArray(weights));
  74. // forward
  75. op->forward(bottom_blob, top_blob);
  76. delete op;
  77. return 0;
  78. }
  79. }
  80. int w = bottom_blob.w;
  81. int h = bottom_blob.h;
  82. int channels = bottom_blob.c;
  83. // fprintf(stderr, "Convolution input %d x %d pad = %d %d ksize=%d %d stride=%d %d\n", w, h, pad_w, pad_h, kernel_w, kernel_h, stride_w, stride_h);
  84. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  85. const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
  86. Mat bottom_blob_bordered = bottom_blob;
  87. if (pad_w > 0 || pad_h > 0)
  88. {
  89. copy_make_border(bottom_blob, bottom_blob_bordered, pad_h, pad_h, pad_w, pad_w, BORDER_CONSTANT, 0.f);
  90. if (bottom_blob_bordered.empty())
  91. return -100;
  92. w = bottom_blob_bordered.w;
  93. h = bottom_blob_bordered.h;
  94. }
  95. else if (pad_w == -233 && pad_h == -233)
  96. {
  97. int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
  98. int hpad = kernel_extent_h + (h - 1) / stride_h * stride_h - h;
  99. if (wpad > 0 || hpad > 0)
  100. {
  101. copy_make_border(bottom_blob, bottom_blob_bordered, hpad / 2, hpad - hpad / 2, wpad / 2, wpad - wpad / 2, BORDER_CONSTANT, 0.f);
  102. if (bottom_blob_bordered.empty())
  103. return -100;
  104. }
  105. w = bottom_blob_bordered.w;
  106. h = bottom_blob_bordered.h;
  107. }
  108. int outw = (w - kernel_extent_w) / stride_w + 1;
  109. int outh = (h - kernel_extent_h) / stride_h + 1;
  110. top_blob.create(outw, outh, num_output);
  111. if (top_blob.empty())
  112. return -100;
  113. const int maxk = kernel_w * kernel_h;
  114. // kernel offsets
  115. std::vector<int> _space_ofs(maxk);
  116. int* space_ofs = &_space_ofs[0];
  117. {
  118. int p1 = 0;
  119. int p2 = 0;
  120. int gap = w * dilation_h - kernel_w * dilation_w;
  121. for (int i = 0; i < kernel_h; i++)
  122. {
  123. for (int j = 0; j < kernel_w; j++)
  124. {
  125. space_ofs[p1] = p2;
  126. p1++;
  127. p2 += dilation_w;
  128. }
  129. p2 += gap;
  130. }
  131. }
  132. // num_output
  133. #pragma omp parallel for
  134. for (int p=0; p<num_output; p++)
  135. {
  136. float* outptr = top_blob.channel(p);
  137. for (int i = 0; i < outh; i++)
  138. {
  139. for (int j = 0; j < outw; j++)
  140. {
  141. float sum = 0.f;
  142. if (bias_term)
  143. sum = bias_data[p];
  144. const float* kptr = (const float*)weight_data + maxk * channels * p;
  145. // channels
  146. for (int q=0; q<channels; q++)
  147. {
  148. const Mat m = bottom_blob_bordered.channel(q);
  149. const float* sptr = m.row(i*stride_h) + j*stride_w;
  150. for (int k = 0; k < maxk; k++) // 29.23
  151. {
  152. float val = sptr[ space_ofs[k] ]; // 20.72
  153. float w = kptr[k];
  154. sum += val * w; // 41.45
  155. }
  156. kptr += maxk;
  157. }
  158. outptr[j] = sum;
  159. }
  160. outptr += outw;
  161. }
  162. }
  163. return 0;
  164. }
  165. } // namespace ncnn