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_arm.cpp 4.9 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 "convolution_arm.h"
  15. namespace ncnn {
  16. #include "convolution_1x1.h"
  17. #include "convolution_2x2.h"
  18. #include "convolution_3x3.h"
  19. #include "convolution_4x4.h"
  20. #include "convolution_5x5.h"
  21. #include "convolution_7x7.h"
  22. DEFINE_LAYER_CREATOR(Convolution_arm)
  23. int Convolution_arm::load_param(const ParamDict& pd)
  24. {
  25. int ret = Convolution::load_param(pd);
  26. if (ret != 0)
  27. return ret;
  28. use_winograd3x3 = false;
  29. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  30. {
  31. int num_input = weight_data_size / 9 / num_output;
  32. // winograd is slow on small channel count
  33. if (num_input >= 16 && num_output >= 16)
  34. use_winograd3x3 = true;
  35. }
  36. return 0;
  37. }
  38. int Convolution_arm::load_model(const ModelBin& mb)
  39. {
  40. int ret = Convolution::load_model(mb);
  41. if (ret != 0)
  42. return ret;
  43. if (use_winograd3x3)
  44. {
  45. int num_input = weight_data_size / 9 / num_output;
  46. conv3x3s1_winograd64_transform_kernel_neon(weight_data, weight_3x3_winograd64_data, num_input, num_output);
  47. }
  48. return 0;
  49. }
  50. int Convolution_arm::forward(const Mat& bottom_blob, Mat& top_blob) const
  51. {
  52. // convolv with NxN kernel
  53. // value = value + bias
  54. if (kernel_w != kernel_h || stride_w != stride_h)
  55. {
  56. return Convolution::forward(bottom_blob, top_blob);
  57. }
  58. const int kernel_size = kernel_w;
  59. const int stride = stride_w;
  60. if (kernel_size > 7 || stride > 4 || dilation_w != 1 || dilation_h != 1)
  61. {
  62. return Convolution::forward(bottom_blob, top_blob);
  63. }
  64. typedef void (*conv_func)(const Mat&, Mat&, const Mat&, const Mat&);
  65. // kernel_size x stride
  66. conv_func conv_func_table[7][4] =
  67. {
  68. {
  69. conv1x1s1_neon,
  70. conv1x1s2_neon,
  71. 0,
  72. 0
  73. }, // kernel_size = 1
  74. {
  75. conv2x2s1_neon,
  76. 0,
  77. 0,
  78. 0
  79. }, // kernel_size = 2
  80. {
  81. conv3x3s1_neon,
  82. conv3x3s2_neon,
  83. 0,
  84. 0
  85. }, // kernel_size = 3
  86. {
  87. 0,
  88. 0,
  89. 0,
  90. conv4x4s4_neon
  91. }, // kernel_size = 4
  92. {
  93. conv5x5s1_neon,
  94. conv5x5s2_neon,
  95. 0,
  96. 0
  97. }, // kernel_size = 5
  98. {
  99. 0,
  100. 0,
  101. 0,
  102. 0
  103. }, // kernel_size = 6
  104. {
  105. conv7x7s1_neon,
  106. conv7x7s2_neon,
  107. 0,
  108. 0
  109. } // kernel_size = 7
  110. };
  111. conv_func conv = conv_func_table[kernel_size-1][stride-1];
  112. if (!conv)
  113. {
  114. return Convolution::forward(bottom_blob, top_blob);
  115. }
  116. int w = bottom_blob.w;
  117. int h = bottom_blob.h;
  118. int channels = bottom_blob.c;
  119. Mat bottom_blob_bordered = bottom_blob;
  120. if (pad_w > 0 || pad_h > 0)
  121. {
  122. copy_make_border(bottom_blob, bottom_blob_bordered, pad_h, pad_h, pad_w, pad_w, BORDER_CONSTANT, 0.f);
  123. if (bottom_blob_bordered.empty())
  124. return -100;
  125. w = bottom_blob_bordered.w;
  126. h = bottom_blob_bordered.h;
  127. }
  128. else if (pad_w == -233 && pad_h == -233)
  129. {
  130. int wpad = kernel_size + (w - 1) / stride * stride - w;
  131. int hpad = kernel_size + (h - 1) / stride * stride - h;
  132. if (wpad > 0 || hpad > 0)
  133. {
  134. copy_make_border(bottom_blob, bottom_blob_bordered, hpad / 2, hpad - hpad / 2, wpad / 2, wpad - wpad / 2, BORDER_CONSTANT, 0.f);
  135. if (bottom_blob_bordered.empty())
  136. return -100;
  137. }
  138. w = bottom_blob_bordered.w;
  139. h = bottom_blob_bordered.h;
  140. }
  141. int outw = (w - kernel_size) / stride + 1;
  142. int outh = (h - kernel_size) / stride + 1;
  143. top_blob.create(outw, outh, num_output);
  144. if (top_blob.empty())
  145. return -100;
  146. if (use_winograd3x3 && w <= 120 && h <= 120)
  147. {
  148. conv3x3s1_winograd64_neon4(bottom_blob_bordered, top_blob, weight_3x3_winograd64_data, bias_data);
  149. }
  150. else
  151. conv(bottom_blob_bordered, top_blob, weight_data, bias_data);
  152. return 0;
  153. }
  154. } // namespace ncnn