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.

deconvolutiondepthwise.cpp 8.8 kB

7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "deconvolutiondepthwise.h"
  15. #include "fused_activation.h"
  16. namespace ncnn {
  17. DeconvolutionDepthWise::DeconvolutionDepthWise()
  18. {
  19. one_blob_only = true;
  20. support_inplace = false;
  21. }
  22. int DeconvolutionDepthWise::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_left = pd.get(4, 0);
  32. pad_right = pd.get(15, pad_left);
  33. pad_top = pd.get(14, pad_left);
  34. pad_bottom = pd.get(16, pad_top);
  35. output_pad_right = pd.get(18, 0);
  36. output_pad_bottom = pd.get(19, output_pad_right);
  37. output_w = pd.get(20, 0);
  38. output_h = pd.get(21, output_w);
  39. bias_term = pd.get(5, 0);
  40. weight_data_size = pd.get(6, 0);
  41. group = pd.get(7, 1);
  42. activation_type = pd.get(9, 0);
  43. activation_params = pd.get(10, Mat());
  44. return 0;
  45. }
  46. int DeconvolutionDepthWise::load_model(const ModelBin& mb)
  47. {
  48. weight_data = mb.load(weight_data_size, 0);
  49. if (weight_data.empty())
  50. return -100;
  51. if (bias_term)
  52. {
  53. bias_data = mb.load(num_output, 1);
  54. if (bias_data.empty())
  55. return -100;
  56. }
  57. return 0;
  58. }
  59. static int deconvolutiondepthwise(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data, const Mat& bias_data, int kernel_w, int kernel_h, int stride_w, int stride_h, int dilation_w, int dilation_h, int group, int activation_type, const Mat& activation_params, const Option& opt)
  60. {
  61. const int inch = bottom_blob.c;
  62. const int outw = top_blob.w;
  63. const int outch = top_blob.c;
  64. const int bias_term = bias_data.empty() ? 0 : 1;
  65. const int maxk = kernel_w * kernel_h;
  66. // kernel offsets
  67. std::vector<int> _space_ofs(maxk);
  68. int* space_ofs = &_space_ofs[0];
  69. {
  70. int p1 = 0;
  71. int p2 = 0;
  72. int gap = outw * dilation_h - kernel_w * dilation_w;
  73. for (int i = 0; i < kernel_h; i++)
  74. {
  75. for (int j = 0; j < kernel_w; j++)
  76. {
  77. space_ofs[p1] = p2;
  78. p1++;
  79. p2 += dilation_w;
  80. }
  81. p2 += gap;
  82. }
  83. }
  84. // depth-wise
  85. if (inch == group && group == outch)
  86. {
  87. #pragma omp parallel for num_threads(opt.num_threads)
  88. for (int g = 0; g < group; g++)
  89. {
  90. const float* inptr = bottom_blob.channel(g);
  91. const float* kptr = (const float*)weight_data + maxk * g;
  92. Mat out = top_blob.channel(g);
  93. const float bias = bias_term ? bias_data[g] : 0.f;
  94. out.fill(bias);
  95. // shadowed variable for less openmp task args
  96. const int w = bottom_blob.w;
  97. const int h = bottom_blob.h;
  98. const int outw = top_blob.w;
  99. const int outh = top_blob.h;
  100. for (int i = 0; i < h; i++)
  101. {
  102. for (int j = 0; j < w; j++)
  103. {
  104. float* outptr = out.row(i * stride_h) + j * stride_w;
  105. const float val = inptr[i * w + j];
  106. for (int k = 0; k < maxk; k++)
  107. {
  108. float w = kptr[k];
  109. outptr[space_ofs[k]] += val * w;
  110. }
  111. }
  112. }
  113. {
  114. float* outptr = out;
  115. int size = outw * outh;
  116. for (int i = 0; i < size; i++)
  117. {
  118. outptr[i] = activation_ss(outptr[i], activation_type, activation_params);
  119. }
  120. }
  121. }
  122. }
  123. else
  124. {
  125. const int inch_g = inch / group;
  126. const int outch_g = outch / group;
  127. #ifdef _WIN32
  128. #pragma omp parallel for num_threads(opt.num_threads)
  129. #else
  130. #pragma omp parallel for collapse(2) num_threads(opt.num_threads)
  131. #endif
  132. for (int g = 0; g < group; g++)
  133. {
  134. for (int p = 0; p < outch_g; p++)
  135. {
  136. Mat out = top_blob.channel(g * outch_g + p);
  137. const float* weight_data_ptr = (const float*)weight_data + maxk * inch_g * outch_g * g;
  138. const float bias = bias_term ? bias_data[g * outch_g + p] : 0.f;
  139. out.fill(bias);
  140. // shadowed variable for less openmp task args
  141. const int w = bottom_blob.w;
  142. const int h = bottom_blob.h;
  143. const int outw = top_blob.w;
  144. const int outh = top_blob.h;
  145. for (int i = 0; i < h; i++)
  146. {
  147. for (int j = 0; j < w; j++)
  148. {
  149. float* outptr = out.row(i * stride_h) + j * stride_w;
  150. const float* kptr = weight_data_ptr + maxk * inch_g * p;
  151. for (int q = 0; q < inch_g; q++)
  152. {
  153. const float val = bottom_blob.channel(inch_g * g + q).row(i)[j];
  154. for (int k = 0; k < maxk; k++)
  155. {
  156. outptr[space_ofs[k]] += val * kptr[k];
  157. }
  158. kptr += maxk;
  159. }
  160. }
  161. }
  162. {
  163. float* outptr = out;
  164. int size = outw * outh;
  165. for (int i = 0; i < size; i++)
  166. {
  167. outptr[i] = activation_ss(outptr[i], activation_type, activation_params);
  168. }
  169. }
  170. }
  171. }
  172. }
  173. return 0;
  174. }
  175. int DeconvolutionDepthWise::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  176. {
  177. int w = bottom_blob.w;
  178. int h = bottom_blob.h;
  179. size_t elemsize = bottom_blob.elemsize;
  180. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  181. const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
  182. int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right;
  183. int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom;
  184. Mat top_blob_bordered;
  185. if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0))
  186. {
  187. top_blob_bordered.create(outw, outh, num_output, elemsize, opt.workspace_allocator);
  188. }
  189. else
  190. {
  191. top_blob_bordered = top_blob;
  192. top_blob_bordered.create(outw, outh, num_output, elemsize, opt.blob_allocator);
  193. }
  194. if (top_blob_bordered.empty())
  195. return -100;
  196. int ret = deconvolutiondepthwise(bottom_blob, top_blob_bordered, weight_data, bias_data, kernel_w, kernel_h, stride_w, stride_h, dilation_w, dilation_h, group, activation_type, activation_params, opt);
  197. if (ret != 0)
  198. return ret;
  199. cut_padding(top_blob_bordered, top_blob, opt);
  200. if (top_blob.empty())
  201. return -100;
  202. return 0;
  203. }
  204. void DeconvolutionDepthWise::cut_padding(const Mat& top_blob_bordered, Mat& top_blob, const Option& opt) const
  205. {
  206. if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0)
  207. {
  208. copy_cut_border(top_blob_bordered, top_blob, pad_top, pad_bottom, pad_left, pad_right, opt);
  209. }
  210. else if (output_w > 0 && output_h > 0)
  211. {
  212. int wcut = top_blob_bordered.w - output_w;
  213. int hcut = top_blob_bordered.h - output_h;
  214. if (pad_left == -233 || pad_right == -233 || pad_top == -233 || pad_bottom == -233)
  215. {
  216. // onnx padding=SAME_UPPER
  217. copy_cut_border(top_blob_bordered, top_blob, hcut / 2, hcut - hcut / 2, wcut / 2, wcut - wcut / 2, opt);
  218. }
  219. else if (pad_left == -234 || pad_right == -234 || pad_top == -234 || pad_bottom == -234)
  220. {
  221. // onnx padding=SAME_LOWER
  222. copy_cut_border(top_blob_bordered, top_blob, hcut - hcut / 2, hcut / 2, wcut - wcut / 2, wcut / 2, opt);
  223. }
  224. }
  225. else
  226. {
  227. top_blob = top_blob_bordered;
  228. }
  229. }
  230. } // namespace ncnn