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.

deconvolution_arm.cpp 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "deconvolution_arm.h"
  15. namespace ncnn {
  16. #include "deconvolution_4x4.h"
  17. #include "deconvolution_3x3.h"
  18. DEFINE_LAYER_CREATOR(Deconvolution_arm)
  19. int Deconvolution_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  20. {
  21. // deconvolv with NxN kernel
  22. // value = value + bias
  23. if (kernel_w != kernel_h || stride_w != stride_h)
  24. {
  25. return Deconvolution::forward(bottom_blob, top_blob, opt);
  26. }
  27. const int kernel_size = kernel_w;
  28. const int stride = stride_w;
  29. if ((kernel_size != 3 && kernel_size != 4) || stride > 2 || dilation_w != 1 || dilation_h != 1)
  30. {
  31. return Deconvolution::forward(bottom_blob, top_blob, opt);
  32. }
  33. typedef void (*deconv_func)(const Mat&, Mat&, const Mat&, const Mat&, const Option&);
  34. // kernel_size x stride
  35. deconv_func deconv_func_table[2][2] =
  36. {
  37. {
  38. deconv3x3s1_neon,
  39. deconv3x3s2_neon
  40. }, // kernel_size = 3
  41. {
  42. deconv4x4s1_neon,
  43. deconv4x4s2_neon
  44. } // kernel_size = 4
  45. };
  46. deconv_func deconv = deconv_func_table[kernel_size-3][stride-1];
  47. if (!deconv)
  48. {
  49. return Deconvolution::forward(bottom_blob, top_blob, opt);
  50. }
  51. int w = bottom_blob.w;
  52. int h = bottom_blob.h;
  53. size_t elemsize = bottom_blob.elemsize;
  54. int outw = (w - 1) * stride + kernel_size;
  55. int outh = (h - 1) * stride + kernel_size;
  56. Mat top_blob_bordered;
  57. if (pad_w > 0 || pad_h > 0)
  58. {
  59. top_blob_bordered.create(outw, outh, num_output, elemsize, opt.workspace_allocator);
  60. if (top_blob_bordered.empty())
  61. return -100;
  62. }
  63. else
  64. {
  65. top_blob_bordered = top_blob;
  66. top_blob_bordered.create(outw, outh, num_output, elemsize, opt.blob_allocator);
  67. if (top_blob_bordered.empty())
  68. return -100;
  69. }
  70. deconv(bottom_blob, top_blob_bordered, weight_data, bias_data, opt);
  71. if (pad_w > 0 || pad_h > 0)
  72. {
  73. copy_cut_border(top_blob_bordered, top_blob, pad_h, pad_h, pad_w, pad_w, opt.blob_allocator, opt.num_threads);
  74. if (top_blob.empty())
  75. return -100;
  76. outw = top_blob.w;
  77. outh = top_blob.h;
  78. }
  79. else
  80. {
  81. top_blob = top_blob_bordered;
  82. }
  83. return 0;
  84. }
  85. } // namespace ncnn