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.cpp 8.3 kB

7 years ago
Fix warnings on Visual Studio (#1456) * Fix warning C4244 in src/layer/convolution.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/convolution_sgemm_int8.h C4244: 'initializing': conversion from 'double' to 'int', possible loss of data * Fix warning C4244 in src/layer/deconvolution.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/elu.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4267 in src/layer/embed.cpp C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/exp.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/innerproduct.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/log.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data C4244: 'initializing': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/lrn.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/mvn.cp C4244: 'initializing': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/power.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warnings C4244 and C4267 in src/layer/proposal.cpp C4244: 'initializing': conversion from 'double' to 'float', possible loss of data C4244: 'initializing': conversion from 'double' to 'int', possible loss of data C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/reduction.cpp C4244: 'return': conversion from 'double' to 'T', possible loss of data * Fix warning C4244 in src/layer/tanh.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/binaryop.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warnings C4244 and C4267 in src/layer/unaryop.cpp C4244: 'return': conversion from 'double' to 'T', possible loss of data C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/x86/convolutiondepthwise_3x3_int8.h C4244: 'initializing': conversion from 'double' to 'int', possible loss of data
6 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.h"
  15. #include "layer_type.h"
  16. #include <algorithm>
  17. namespace ncnn {
  18. Deconvolution::Deconvolution()
  19. {
  20. one_blob_only = true;
  21. support_inplace = false;
  22. }
  23. int Deconvolution::load_param(const ParamDict& pd)
  24. {
  25. num_output = pd.get(0, 0);
  26. kernel_w = pd.get(1, 0);
  27. kernel_h = pd.get(11, kernel_w);
  28. dilation_w = pd.get(2, 1);
  29. dilation_h = pd.get(12, dilation_w);
  30. stride_w = pd.get(3, 1);
  31. stride_h = pd.get(13, stride_w);
  32. pad_left = pd.get(4, 0);
  33. pad_right = pd.get(15, pad_left);
  34. pad_top = pd.get(14, pad_left);
  35. pad_bottom = pd.get(16, pad_top);
  36. output_pad_right = pd.get(18, 0);
  37. output_pad_bottom = pd.get(19, output_pad_right);
  38. output_w = pd.get(20, 0);
  39. output_h = pd.get(21, output_w);
  40. bias_term = pd.get(5, 0);
  41. weight_data_size = pd.get(6, 0);
  42. activation_type = pd.get(9, 0);
  43. activation_params = pd.get(10, Mat());
  44. return 0;
  45. }
  46. int Deconvolution::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. int Deconvolution::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  60. {
  61. // backward strided convolv with NxN kernel
  62. // value = value + bias
  63. int w = bottom_blob.w;
  64. int h = bottom_blob.h;
  65. int channels = bottom_blob.c;
  66. size_t elemsize = bottom_blob.elemsize;
  67. // NCNN_LOGE("Deconvolution input %d x %d pad = %d %d ksize=%d %d stride=%d %d", w, h, pad_w, pad_h, kernel_w, kernel_h, stride_w, stride_h);
  68. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  69. const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
  70. int outw = (w - 1) * stride_w + kernel_extent_w;
  71. int outh = (h - 1) * stride_h + kernel_extent_h;
  72. Mat top_blob_bordered;
  73. if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || output_pad_right > 0 || output_pad_bottom > 0 || (output_w > 0 && output_h > 0))
  74. {
  75. top_blob_bordered.create(outw, outh, num_output, elemsize, opt.workspace_allocator);
  76. }
  77. else
  78. {
  79. top_blob_bordered = top_blob;
  80. top_blob_bordered.create(outw, outh, num_output, elemsize, opt.blob_allocator);
  81. }
  82. if (top_blob_bordered.empty())
  83. return -100;
  84. const int maxk = kernel_w * kernel_h;
  85. // kernel offsets
  86. std::vector<int> _space_ofs(maxk);
  87. int* space_ofs = &_space_ofs[0];
  88. {
  89. int p1 = 0;
  90. int p2 = 0;
  91. int gap = outw * dilation_h - kernel_w * dilation_w;
  92. for (int i = 0; i < kernel_h; i++)
  93. {
  94. for (int j = 0; j < kernel_w; j++)
  95. {
  96. space_ofs[p1] = p2;
  97. p1++;
  98. p2 += dilation_w;
  99. }
  100. p2 += gap;
  101. }
  102. }
  103. // num_output
  104. #pragma omp parallel for num_threads(opt.num_threads)
  105. for (int p = 0; p < num_output; p++)
  106. {
  107. Mat out = top_blob_bordered.channel(p);
  108. const float bias = bias_term ? bias_data[p] : 0.f;
  109. out.fill(bias);
  110. for (int i = 0; i < h; i++)
  111. {
  112. for (int j = 0; j < w; j++)
  113. {
  114. float* outptr = out.row(i * stride_h) + j * stride_w;
  115. const float* kptr = (const float*)weight_data + maxk * channels * p;
  116. // channels
  117. for (int q = 0; q < channels; q++)
  118. {
  119. const Mat m = bottom_blob.channel(q);
  120. float val = *(m.row(i) + j);
  121. for (int k = 0; k < maxk; k++)
  122. {
  123. float w = kptr[k];
  124. outptr[space_ofs[k]] += val * w;
  125. }
  126. kptr += maxk;
  127. }
  128. }
  129. }
  130. if (activation_type == 1)
  131. {
  132. float* outptr = out;
  133. int size = outw * outh;
  134. for (int i = 0; i < size; i++)
  135. {
  136. outptr[i] = std::max(outptr[i], 0.f);
  137. }
  138. }
  139. else if (activation_type == 2)
  140. {
  141. float* outptr = out;
  142. int size = outw * outh;
  143. float slope = activation_params[0];
  144. for (int i = 0; i < size; i++)
  145. {
  146. outptr[i] = outptr[i] > 0.f ? outptr[i] : outptr[i] * slope;
  147. }
  148. }
  149. else if (activation_type == 3)
  150. {
  151. float* outptr = out;
  152. int size = outw * outh;
  153. float min = activation_params[0];
  154. float max = activation_params[1];
  155. for (int i = 0; i < size; i++)
  156. {
  157. if (outptr[i] < min)
  158. outptr[i] = min;
  159. if (outptr[i] > max)
  160. outptr[i] = max;
  161. }
  162. }
  163. else if (activation_type == 4)
  164. {
  165. float* outptr = out;
  166. int size = outw * outh;
  167. for (int i = 0; i < size; i++)
  168. {
  169. outptr[i] = static_cast<float>(1.f / (1.f + exp(-outptr[i])));
  170. }
  171. }
  172. }
  173. cut_padding(top_blob_bordered, top_blob, opt);
  174. if (top_blob.empty())
  175. return -100;
  176. return 0;
  177. }
  178. void Deconvolution::cut_padding(const Mat& top_blob_bordered, Mat& top_blob, const Option& opt) const
  179. {
  180. if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0)
  181. {
  182. Mat top_blob_bordered_adj = top_blob_bordered;
  183. if (output_pad_right > 0 || output_pad_bottom > 0)
  184. {
  185. Option opt_b = opt;
  186. opt_b.blob_allocator = opt.workspace_allocator;
  187. copy_make_border(top_blob_bordered, top_blob_bordered_adj, 0, output_pad_bottom, 0, output_pad_right, BORDER_CONSTANT, 0.f, opt_b);
  188. if (top_blob_bordered_adj.empty())
  189. return;
  190. }
  191. copy_cut_border(top_blob_bordered_adj, top_blob, pad_top, pad_bottom, pad_left, pad_right, opt);
  192. }
  193. else if (output_w > 0 && output_h > 0)
  194. {
  195. Mat top_blob_bordered_adj = top_blob_bordered;
  196. if (output_pad_right > 0 || output_pad_bottom > 0)
  197. {
  198. Option opt_b = opt;
  199. opt_b.blob_allocator = opt.workspace_allocator;
  200. copy_make_border(top_blob_bordered, top_blob_bordered_adj, 0, output_pad_bottom, 0, output_pad_right, BORDER_CONSTANT, 0.f, opt_b);
  201. if (top_blob_bordered_adj.empty())
  202. return;
  203. }
  204. int wcut = top_blob_bordered_adj.w - output_w;
  205. int hcut = top_blob_bordered_adj.h - output_h;
  206. if (pad_left == -233 || pad_right == -233 || pad_top == -233 || pad_bottom == -233)
  207. {
  208. // onnx padding=SAME_UPPER
  209. copy_cut_border(top_blob_bordered_adj, top_blob, hcut / 2, hcut - hcut / 2, wcut / 2, wcut - wcut / 2, opt);
  210. }
  211. else if (pad_left == -234 || pad_right == -234 || pad_top == -234 || pad_bottom == -234)
  212. {
  213. // onnx padding=SAME_LOWER
  214. copy_cut_border(top_blob_bordered_adj, top_blob, hcut - hcut / 2, hcut / 2, wcut - wcut / 2, wcut / 2, opt);
  215. }
  216. }
  217. else
  218. {
  219. if (output_pad_right > 0 || output_pad_bottom > 0)
  220. {
  221. copy_make_border(top_blob_bordered, top_blob, 0, output_pad_bottom, 0, output_pad_right, BORDER_CONSTANT, 0.f, opt);
  222. }
  223. else
  224. {
  225. top_blob = top_blob_bordered;
  226. }
  227. }
  228. }
  229. } // namespace ncnn