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 12 kB

7 years ago
Fix warnings on Visual Studio (#1431) * Fix warnings C4244, C4267 in src/layer/yolov3detectionoutput.cpp C4244: '=': conversion from 'int' to 'float', possible loss of data C4244: 'initializing': conversion from 'float' to 'int', possible loss of data C4244: 'initializing': conversion from 'double' to 'float', possible loss of data C4244: 'return': conversion from 'double' to 'float', 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 warnings C4244, C4267 in src/layer/yolodetectionoutput.cpp C4244: '=': conversion from 'int' to 'float', possible loss of data C4244: 'initializing': conversion from 'float' to 'int', possible loss of data C4244: 'initializing': conversion from 'double' to 'float', possible loss of data C4244: 'return': conversion from 'double' to 'float', 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/quantize.cpp C4244: 'initializing': conversion from 'double' to 'int', possible loss of data * Fix warnings C4244, C4267 in src/layer/detectionoutput.cpp C4244: '=': conversion from 'int' to 'float', possible loss of data C4244: 'initializing': conversion from 'double' to 'float', 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/roipooling.cpp C4244: 'initializing': conversion from 'double' to 'int', possible loss of data * Fix warning C4244 in src/layer/sigmoid.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4267 in src/layer/slice.cpp C4267: '=': conversion from 'size_t' to 'int', possible loss of data C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4267 in src/layer/softmax.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/interp.cpp C4244: '=': conversion from 'float' to 'int', possible loss of data C4244: 'initializing': conversion from 'double' to 'int', possible loss of data * Fix warning C4244 in src/layer/instancenorm.cpp C4244: 'initializing': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/deconvolutiondepthwise.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/convolutiondepthwise.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/net.cpp C4244: 'return': conversion from '__int64' 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 C4267: 'return': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/bnll.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4267 in src/layer/concat.cpp C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4267 in tools/mxnet/mxnet2ncnn.cpp C4244: 'initializing': conversion from 'double' to 'float', possible loss of data C4267: '=': conversion from 'size_t' to 'int', possible loss of data C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data C4305: 'initializing': truncation from 'double' to 'float'
6 years ago
7 years ago
7 years ago
Fix warnings on Visual Studio (#1431) * Fix warnings C4244, C4267 in src/layer/yolov3detectionoutput.cpp C4244: '=': conversion from 'int' to 'float', possible loss of data C4244: 'initializing': conversion from 'float' to 'int', possible loss of data C4244: 'initializing': conversion from 'double' to 'float', possible loss of data C4244: 'return': conversion from 'double' to 'float', 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 warnings C4244, C4267 in src/layer/yolodetectionoutput.cpp C4244: '=': conversion from 'int' to 'float', possible loss of data C4244: 'initializing': conversion from 'float' to 'int', possible loss of data C4244: 'initializing': conversion from 'double' to 'float', possible loss of data C4244: 'return': conversion from 'double' to 'float', 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/quantize.cpp C4244: 'initializing': conversion from 'double' to 'int', possible loss of data * Fix warnings C4244, C4267 in src/layer/detectionoutput.cpp C4244: '=': conversion from 'int' to 'float', possible loss of data C4244: 'initializing': conversion from 'double' to 'float', 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/roipooling.cpp C4244: 'initializing': conversion from 'double' to 'int', possible loss of data * Fix warning C4244 in src/layer/sigmoid.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4267 in src/layer/slice.cpp C4267: '=': conversion from 'size_t' to 'int', possible loss of data C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4267 in src/layer/softmax.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/interp.cpp C4244: '=': conversion from 'float' to 'int', possible loss of data C4244: 'initializing': conversion from 'double' to 'int', possible loss of data * Fix warning C4244 in src/layer/instancenorm.cpp C4244: 'initializing': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/deconvolutiondepthwise.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/convolutiondepthwise.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/net.cpp C4244: 'return': conversion from '__int64' 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 C4267: 'return': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/bnll.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4267 in src/layer/concat.cpp C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4267 in tools/mxnet/mxnet2ncnn.cpp C4244: 'initializing': conversion from 'double' to 'float', possible loss of data C4267: '=': conversion from 'size_t' to 'int', possible loss of data C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data C4305: 'initializing': truncation from 'double' to 'float'
6 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 <algorithm>
  16. #include "layer_type.h"
  17. namespace ncnn {
  18. DEFINE_LAYER_CREATOR(DeconvolutionDepthWise)
  19. DeconvolutionDepthWise::DeconvolutionDepthWise()
  20. {
  21. one_blob_only = true;
  22. support_inplace = false;
  23. }
  24. int DeconvolutionDepthWise::load_param(const ParamDict& pd)
  25. {
  26. num_output = pd.get(0, 0);
  27. kernel_w = pd.get(1, 0);
  28. kernel_h = pd.get(11, kernel_w);
  29. dilation_w = pd.get(2, 1);
  30. dilation_h = pd.get(12, dilation_w);
  31. stride_w = pd.get(3, 1);
  32. stride_h = pd.get(13, stride_w);
  33. pad_left = pd.get(4, 0);
  34. pad_right = pd.get(15, pad_left);
  35. pad_top = pd.get(14, pad_left);
  36. pad_bottom = pd.get(16, pad_top);
  37. output_pad_right = pd.get(18, 0);
  38. output_pad_bottom = pd.get(19, output_pad_right);
  39. output_w = pd.get(20, 0);
  40. output_h = pd.get(21, output_w);
  41. bias_term = pd.get(5, 0);
  42. weight_data_size = pd.get(6, 0);
  43. group = pd.get(7, 1);
  44. activation_type = pd.get(9, 0);
  45. activation_params = pd.get(10, Mat());
  46. return 0;
  47. }
  48. int DeconvolutionDepthWise::load_model(const ModelBin& mb)
  49. {
  50. weight_data = mb.load(weight_data_size, 0);
  51. if (weight_data.empty())
  52. return -100;
  53. if (bias_term)
  54. {
  55. bias_data = mb.load(num_output, 1);
  56. if (bias_data.empty())
  57. return -100;
  58. }
  59. return 0;
  60. }
  61. int DeconvolutionDepthWise::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  62. {
  63. // deconvolv with NxN kernel
  64. // value = value + bias
  65. int w = bottom_blob.w;
  66. int h = bottom_blob.h;
  67. int channels = bottom_blob.c;
  68. size_t elemsize = bottom_blob.elemsize;
  69. if (channels % group != 0 || num_output % group != 0)
  70. {
  71. // reject invalid group
  72. return -100;
  73. }
  74. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  75. const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
  76. int outw = (w - 1) * stride_w + kernel_extent_w;
  77. int outh = (h - 1) * stride_h + kernel_extent_h;
  78. Mat top_blob_bordered;
  79. 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))
  80. {
  81. top_blob_bordered.create(outw, outh, num_output, elemsize, opt.workspace_allocator);
  82. }
  83. else
  84. {
  85. top_blob_bordered = top_blob;
  86. top_blob_bordered.create(outw, outh, num_output, elemsize, opt.blob_allocator);
  87. }
  88. if (top_blob_bordered.empty())
  89. return -100;
  90. const int maxk = kernel_w * kernel_h;
  91. // kernel offsets
  92. std::vector<int> _space_ofs(maxk);
  93. int* space_ofs = &_space_ofs[0];
  94. {
  95. int p1 = 0;
  96. int p2 = 0;
  97. int gap = outw * dilation_h - kernel_w * dilation_w;
  98. for (int i = 0; i < kernel_h; i++)
  99. {
  100. for (int j = 0; j < kernel_w; j++)
  101. {
  102. space_ofs[p1] = p2;
  103. p1++;
  104. p2 += dilation_w;
  105. }
  106. p2 += gap;
  107. }
  108. }
  109. // depth-wise
  110. if (channels == group && group == num_output)
  111. {
  112. #pragma omp parallel for num_threads(opt.num_threads)
  113. for (int g=0; g<group; g++)
  114. {
  115. const float* inptr = bottom_blob.channel(g);
  116. const float* kptr = (const float*)weight_data + maxk * g;
  117. Mat m = top_blob_bordered.channel(g);
  118. const float bias = bias_term ? bias_data[g] : 0.f;
  119. m.fill(bias);
  120. for (int i = 0; i < h; i++)
  121. {
  122. for (int j = 0; j < w; j++)
  123. {
  124. float* outptr = m.row(i*stride_h) + j*stride_w;
  125. for (int k = 0; k < maxk; k++)
  126. {
  127. float val = inptr[i*w + j];
  128. float w = kptr[k];
  129. outptr[ space_ofs[k] ] += val * w;
  130. }
  131. }
  132. }
  133. if (activation_type == 1)
  134. {
  135. float* outptr = m;
  136. int size = outw * outh;
  137. for (int i = 0; i < size; i++)
  138. {
  139. outptr[i] = std::max(outptr[i], 0.f);
  140. }
  141. }
  142. else if (activation_type == 2)
  143. {
  144. float* outptr = m;
  145. int size = outw * outh;
  146. float slope = activation_params[0];
  147. for (int i = 0; i < size; i++)
  148. {
  149. outptr[i] = outptr[i] > 0.f ? outptr[i] : outptr[i] * slope;
  150. }
  151. }
  152. else if (activation_type == 3)
  153. {
  154. float* outptr = m;
  155. int size = outw * outh;
  156. float min = activation_params[0];
  157. float max = activation_params[1];
  158. for (int i = 0; i < size; i++)
  159. {
  160. if (outptr[i] < min)
  161. outptr[i] = min;
  162. if (outptr[i] > max)
  163. outptr[i] = max;
  164. }
  165. }
  166. else if (activation_type == 4)
  167. {
  168. float* outptr = m;
  169. int size = outw * outh;
  170. for (int i = 0; i < size; i++)
  171. {
  172. outptr[i] = static_cast<float>(1.f / (1.f + exp(-outptr[i])));
  173. }
  174. }
  175. }
  176. }
  177. else
  178. {
  179. // num_output
  180. const int channels_g = channels / group;
  181. const int num_output_g = num_output / group;
  182. #ifdef _WIN32
  183. #pragma omp parallel for num_threads(opt.num_threads)
  184. #else // _WIN32
  185. #pragma omp parallel for collapse(2) num_threads(opt.num_threads)
  186. #endif // _WIN32
  187. for (int g = 0; g < group; g++)
  188. {
  189. for (int p = 0; p < num_output_g; p++)
  190. {
  191. Mat out = top_blob_bordered.channel(g * num_output_g + p);
  192. const float* weight_data_ptr = (const float*)weight_data + maxk * channels_g * num_output_g * g;
  193. const float bias = bias_term ? bias_data[g * num_output_g + p] : 0.f;
  194. out.fill(bias);
  195. for (int i = 0; i < h; i++)
  196. {
  197. for (int j = 0; j < w; j++)
  198. {
  199. float* outptr = out.row(i*stride_h) + j*stride_w;
  200. const float* kptr = weight_data_ptr + maxk * channels_g * p;
  201. // channels_g
  202. for (int q = 0; q < channels_g; q++)
  203. {
  204. const Mat m = bottom_blob.channel(channels_g * g + q);
  205. float val = *(m.row(i) + j);
  206. for (int k = 0; k < maxk; k++)
  207. {
  208. outptr[ space_ofs[k] ] += val * kptr[k];
  209. }
  210. kptr += maxk;
  211. }
  212. }
  213. }
  214. if (activation_type == 1)
  215. {
  216. float* outptr = out;
  217. int size = outw * outh;
  218. for (int i = 0; i < size; i++)
  219. {
  220. outptr[i] = std::max(outptr[i], 0.f);
  221. }
  222. }
  223. else if (activation_type == 2)
  224. {
  225. float* outptr = out;
  226. int size = outw * outh;
  227. float slope = activation_params[0];
  228. for (int i = 0; i < size; i++)
  229. {
  230. outptr[i] = outptr[i] > 0.f ? outptr[i] : outptr[i] * slope;
  231. }
  232. }
  233. else if (activation_type == 3)
  234. {
  235. float* outptr = out;
  236. int size = outw * outh;
  237. float min = activation_params[0];
  238. float max = activation_params[1];
  239. for (int i = 0; i < size; i++)
  240. {
  241. if (outptr[i] < min)
  242. outptr[i] = min;
  243. if (outptr[i] > max)
  244. outptr[i] = max;
  245. }
  246. }
  247. else if (activation_type == 4)
  248. {
  249. float* outptr = out;
  250. int size = outw * outh;
  251. for (int i = 0; i < size; i++)
  252. {
  253. outptr[i] = static_cast<float>(1.f / (1.f + exp(-outptr[i])));
  254. }
  255. }
  256. }
  257. }
  258. }
  259. if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0)
  260. {
  261. Mat top_blob_bordered_adj = top_blob_bordered;
  262. if (output_pad_right > 0 || output_pad_bottom > 0)
  263. {
  264. Option opt_b = opt;
  265. opt_b.blob_allocator = opt.workspace_allocator;
  266. copy_make_border(top_blob_bordered, top_blob_bordered_adj, 0, output_pad_bottom, 0, output_pad_right, BORDER_CONSTANT, 0.f, opt_b);
  267. if (top_blob_bordered_adj.empty())
  268. return -100;
  269. }
  270. copy_cut_border(top_blob_bordered_adj, top_blob, pad_top, pad_bottom, pad_left, pad_right, opt);
  271. if (top_blob.empty())
  272. return -100;
  273. outw = top_blob.w;
  274. outh = top_blob.h;
  275. }
  276. else if (output_w > 0 && output_h > 0)
  277. {
  278. Mat top_blob_bordered_adj = top_blob_bordered;
  279. if (output_pad_right > 0 || output_pad_bottom > 0)
  280. {
  281. Option opt_b = opt;
  282. opt_b.blob_allocator = opt.workspace_allocator;
  283. copy_make_border(top_blob_bordered, top_blob_bordered_adj, 0, output_pad_bottom, 0, output_pad_right, BORDER_CONSTANT, 0.f, opt_b);
  284. if (top_blob_bordered_adj.empty())
  285. return -100;
  286. }
  287. int wcut = top_blob_bordered_adj.w - output_w;
  288. int hcut = top_blob_bordered_adj.h - output_h;
  289. if (pad_left == -233 || pad_right == -233 || pad_top == -233 || pad_bottom == -233)
  290. {
  291. // onnx padding=SAME_UPPER
  292. copy_cut_border(top_blob_bordered_adj, top_blob, hcut / 2, hcut - hcut / 2, wcut / 2, wcut - wcut / 2, opt);
  293. }
  294. else if (pad_left == -234 || pad_right == -234 || pad_top == -234 || pad_bottom == -234)
  295. {
  296. // onnx padding=SAME_LOWER
  297. copy_cut_border(top_blob_bordered_adj, top_blob, hcut - hcut / 2, hcut / 2, wcut - wcut / 2, wcut / 2, opt);
  298. }
  299. if (top_blob.empty())
  300. return -100;
  301. outw = top_blob.w;
  302. outh = top_blob.h;
  303. }
  304. else
  305. {
  306. if (output_pad_right > 0 || output_pad_bottom > 0)
  307. {
  308. copy_make_border(top_blob_bordered, top_blob, 0, output_pad_bottom, 0, output_pad_right, BORDER_CONSTANT, 0.f, opt);
  309. if (top_blob.empty())
  310. return -100;
  311. }
  312. else
  313. {
  314. top_blob = top_blob_bordered;
  315. }
  316. }
  317. return 0;
  318. }
  319. } // namespace ncnn