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_arm.cpp 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_arm.h"
  15. #ifdef _OPENMP
  16. #include <omp.h>
  17. #endif
  18. namespace ncnn {
  19. #include "deconvolution_3x3.h"
  20. #include "deconvolution_4x4.h"
  21. DEFINE_LAYER_CREATOR(DeconvolutionDepthWise_arm)
  22. int DeconvolutionDepthWise_arm::forward(const Mat& bottom_blob, Mat& top_blob) const
  23. {
  24. // convolv with NxN kernel
  25. // value = value + bias
  26. if (kernel_w != kernel_h || stride_w != stride_h)
  27. {
  28. return DeconvolutionDepthWise::forward(bottom_blob, top_blob);
  29. }
  30. const int kernel_size = kernel_w;
  31. const int stride = stride_w;
  32. if ((kernel_size != 3 && kernel_size != 4) || stride > 2 || dilation_w != 1 || dilation_h != 1)
  33. {
  34. return DeconvolutionDepthWise::forward(bottom_blob, top_blob);
  35. }
  36. typedef void (*deconv_func)(const Mat&, Mat&, const Mat&, const Mat&);
  37. // kernel_size x stride
  38. deconv_func deconv_func_table[2][2] =
  39. {
  40. {
  41. deconv3x3s1_neon,
  42. deconv3x3s2_neon
  43. }, // kernel_size = 3
  44. {
  45. deconv4x4s1_neon,
  46. deconv4x4s2_neon
  47. } // kernel_size = 4
  48. };
  49. deconv_func deconv = deconv_func_table[kernel_size-3][stride-1];
  50. if (!deconv)
  51. {
  52. return DeconvolutionDepthWise::forward(bottom_blob, top_blob);
  53. }
  54. int w = bottom_blob.w;
  55. int h = bottom_blob.h;
  56. int channels = bottom_blob.c;
  57. int outw = (w - 1) * stride + kernel_size;
  58. int outh = (h - 1) * stride + kernel_size;
  59. Mat top_blob_bordered(outw, outh, num_output);
  60. if (top_blob_bordered.empty())
  61. return -100;
  62. const int maxk = kernel_size * kernel_size;
  63. // depth-wise
  64. if (channels == group && group == num_output)
  65. {
  66. #ifdef _OPENMP
  67. int nested_current = omp_get_nested();
  68. omp_set_nested(0);
  69. #endif
  70. #pragma omp parallel for
  71. for (int g=0; g<group; g++)
  72. {
  73. Mat top_blob_bordered_g = top_blob_bordered.channel(g);
  74. Mat bottom_blob_g = bottom_blob.channel(g);
  75. Mat weight_data_g(maxk, (void*)((const float*)weight_data + maxk * g));
  76. Mat bias_data_g;
  77. if (bias_term)
  78. bias_data_g = Mat(1, (void*)((const float*)bias_data + g));
  79. deconv(bottom_blob_g, top_blob_bordered_g, weight_data_g, bias_data_g);
  80. }
  81. #ifdef _OPENMP
  82. omp_set_nested(nested_current);
  83. #endif
  84. } else {
  85. const int channels_g = channels / group;
  86. const int num_output_g = num_output / group;
  87. for (int g=0; g<group; g++)
  88. {
  89. Mat top_blob_bordered_g(outw, outh, num_output_g, top_blob_bordered.channel(num_output_g * g));
  90. Mat bottom_blob_g(w, h, channels_g, bottom_blob.channel(channels_g * g).data);
  91. Mat weight_data_g(maxk * channels_g * num_output_g, (void*)((const float*)weight_data + maxk * channels_g * num_output_g * g));
  92. Mat bias_data_g;
  93. if (bias_term)
  94. bias_data_g = Mat(num_output_g, (void*)((const float*)bias_data + num_output_g * g));
  95. deconv(bottom_blob_g, top_blob_bordered_g, weight_data_g, bias_data_g);
  96. }
  97. }
  98. top_blob = top_blob_bordered;
  99. if (pad_w > 0 || pad_h > 0)
  100. {
  101. copy_cut_border(top_blob_bordered, top_blob, pad_h, pad_h, pad_w, pad_w);
  102. if (top_blob.empty())
  103. return -100;
  104. outw = top_blob.w;
  105. outh = top_blob.h;
  106. }
  107. return 0;
  108. }
  109. }// namespace ncnn