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

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