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

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. #include "layer_type.h"
  16. #if __ARM_NEON
  17. #include <arm_neon.h>
  18. #include "neon_mathfun.h"
  19. #endif // __ARM_NEON
  20. #include "neon_activation.h"
  21. namespace ncnn {
  22. #include "deconvolution_3x3.h"
  23. #include "deconvolution_4x4.h"
  24. DEFINE_LAYER_CREATOR(Deconvolution_arm)
  25. Deconvolution_arm::Deconvolution_arm()
  26. {
  27. #if __ARM_NEON
  28. support_packing = true;
  29. #endif // __ARM_NEON
  30. activation = 0;
  31. }
  32. int Deconvolution_arm::create_pipeline(const Option& opt)
  33. {
  34. if (activation_type == 1)
  35. {
  36. activation = ncnn::create_layer(ncnn::LayerType::ReLU);
  37. ncnn::ParamDict pd;
  38. activation->load_param(pd);
  39. }
  40. else if (activation_type == 2)
  41. {
  42. activation = ncnn::create_layer(ncnn::LayerType::ReLU);
  43. ncnn::ParamDict pd;
  44. pd.set(0, activation_params[0]); // slope
  45. activation->load_param(pd);
  46. }
  47. else if (activation_type == 3)
  48. {
  49. activation = ncnn::create_layer(ncnn::LayerType::Clip);
  50. ncnn::ParamDict pd;
  51. pd.set(0, activation_params[0]); // min
  52. pd.set(1, activation_params[1]); // max
  53. activation->load_param(pd);
  54. }
  55. else if (activation_type == 4)
  56. {
  57. activation = ncnn::create_layer(ncnn::LayerType::Sigmoid);
  58. ncnn::ParamDict pd;
  59. activation->load_param(pd);
  60. }
  61. if (activation)
  62. {
  63. activation->create_pipeline(opt);
  64. }
  65. const int maxk = kernel_w * kernel_h;
  66. int num_input = weight_data_size / maxk / num_output;
  67. Mat weight_data_transposed(weight_data.w);
  68. {
  69. float* pt = weight_data_transposed;
  70. const float* p = weight_data;
  71. for (int i = 0; i < num_input * num_output; i++)
  72. {
  73. for (int k = 0; k < maxk; k++)
  74. {
  75. pt[maxk - 1 - k] = p[k];
  76. }
  77. p += maxk;
  78. pt += maxk;
  79. }
  80. }
  81. int elempack = (support_packing && opt.use_packing_layout && num_input % 4 == 0) ? 4 : 1;
  82. int out_elempack = (support_packing && opt.use_packing_layout && num_output % 4 == 0) ? 4 : 1;
  83. #if __ARM_NEON
  84. // pack4
  85. if (elempack == 4 && out_elempack == 4)
  86. {
  87. // src = kw-kh-inch-outch
  88. // dst = 4a-4b-kw-kh-inch/4a-outch/4b
  89. {
  90. Mat weight_data_r2 = weight_data_transposed.reshape(maxk, num_input, num_output);
  91. weight_data_pack4.create(maxk, num_input / 4, num_output / 4, (size_t)4 * 16, 16);
  92. for (int q = 0; q + 3 < num_output; q += 4)
  93. {
  94. const Mat k0 = weight_data_r2.channel(q);
  95. const Mat k1 = weight_data_r2.channel(q + 1);
  96. const Mat k2 = weight_data_r2.channel(q + 2);
  97. const Mat k3 = weight_data_r2.channel(q + 3);
  98. Mat g0 = weight_data_pack4.channel(q / 4);
  99. for (int p = 0; p + 3 < num_input; p += 4)
  100. {
  101. const float* k00 = k0.row(p);
  102. const float* k01 = k0.row(p + 1);
  103. const float* k02 = k0.row(p + 2);
  104. const float* k03 = k0.row(p + 3);
  105. const float* k10 = k1.row(p);
  106. const float* k11 = k1.row(p + 1);
  107. const float* k12 = k1.row(p + 2);
  108. const float* k13 = k1.row(p + 3);
  109. const float* k20 = k2.row(p);
  110. const float* k21 = k2.row(p + 1);
  111. const float* k22 = k2.row(p + 2);
  112. const float* k23 = k2.row(p + 3);
  113. const float* k30 = k3.row(p);
  114. const float* k31 = k3.row(p + 1);
  115. const float* k32 = k3.row(p + 2);
  116. const float* k33 = k3.row(p + 3);
  117. float* g00 = g0.row(p / 4);
  118. for (int k = 0; k < maxk; k++)
  119. {
  120. g00[0] = k00[k];
  121. g00[1] = k10[k];
  122. g00[2] = k20[k];
  123. g00[3] = k30[k];
  124. g00[4] = k01[k];
  125. g00[5] = k11[k];
  126. g00[6] = k21[k];
  127. g00[7] = k31[k];
  128. g00[8] = k02[k];
  129. g00[9] = k12[k];
  130. g00[10] = k22[k];
  131. g00[11] = k32[k];
  132. g00[12] = k03[k];
  133. g00[13] = k13[k];
  134. g00[14] = k23[k];
  135. g00[15] = k33[k];
  136. g00 += 16;
  137. }
  138. }
  139. }
  140. }
  141. }
  142. // pack1to4
  143. if (elempack == 1 && out_elempack == 4)
  144. {
  145. // src = kw-kh-inch-outch
  146. // dst = 4b-kw-kh-inch-outch/4b
  147. {
  148. Mat weight_data_r2 = weight_data_transposed.reshape(maxk, num_input, num_output);
  149. weight_data_pack1to4.create(maxk, num_input, num_output / 4, (size_t)4 * 4, 4);
  150. for (int q = 0; q + 3 < num_output; q += 4)
  151. {
  152. const Mat k0 = weight_data_r2.channel(q);
  153. const Mat k1 = weight_data_r2.channel(q + 1);
  154. const Mat k2 = weight_data_r2.channel(q + 2);
  155. const Mat k3 = weight_data_r2.channel(q + 3);
  156. Mat g0 = weight_data_pack1to4.channel(q / 4);
  157. for (int p = 0; p < num_input; p++)
  158. {
  159. const float* k00 = k0.row(p);
  160. const float* k10 = k1.row(p);
  161. const float* k20 = k2.row(p);
  162. const float* k30 = k3.row(p);
  163. float* g00 = g0.row(p);
  164. for (int k = 0; k < maxk; k++)
  165. {
  166. g00[0] = k00[k];
  167. g00[1] = k10[k];
  168. g00[2] = k20[k];
  169. g00[3] = k30[k];
  170. g00 += 4;
  171. }
  172. }
  173. }
  174. }
  175. }
  176. // pack4to1
  177. if (elempack == 4 && out_elempack == 1)
  178. {
  179. // src = kw-kh-inch-outch
  180. // dst = 4a-kw-kh-inch/4a-outch
  181. {
  182. Mat weight_data_r2 = weight_data_transposed.reshape(maxk, num_input, num_output);
  183. weight_data_pack4to1.create(maxk, num_input / 4, num_output, (size_t)4 * 4, 4);
  184. for (int q = 0; q < num_output; q++)
  185. {
  186. const Mat k0 = weight_data_r2.channel(q);
  187. Mat g0 = weight_data_pack4to1.channel(q);
  188. for (int p = 0; p + 3 < num_input; p += 4)
  189. {
  190. const float* k00 = k0.row(p);
  191. const float* k01 = k0.row(p + 1);
  192. const float* k02 = k0.row(p + 2);
  193. const float* k03 = k0.row(p + 3);
  194. float* g00 = g0.row(p / 4);
  195. for (int k = 0; k < maxk; k++)
  196. {
  197. g00[0] = k00[k];
  198. g00[1] = k01[k];
  199. g00[2] = k02[k];
  200. g00[3] = k03[k];
  201. g00 += 4;
  202. }
  203. }
  204. }
  205. }
  206. }
  207. #endif // __ARM_NEON
  208. // pack1
  209. if (elempack == 1 && out_elempack == 1)
  210. {
  211. weight_data_pack1 = weight_data_transposed;
  212. }
  213. return 0;
  214. }
  215. int Deconvolution_arm::destroy_pipeline(const Option& opt)
  216. {
  217. if (activation)
  218. {
  219. activation->destroy_pipeline(opt);
  220. delete activation;
  221. activation = 0;
  222. }
  223. return 0;
  224. }
  225. int Deconvolution_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  226. {
  227. // deconvolv with NxN kernel
  228. // value = value + bias
  229. int w = bottom_blob.w;
  230. int h = bottom_blob.h;
  231. int channels = bottom_blob.c;
  232. size_t elemsize = bottom_blob.elemsize;
  233. int elempack = bottom_blob.elempack;
  234. // 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);
  235. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  236. const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
  237. int outw = (w - 1) * stride_w + kernel_extent_w;
  238. int outh = (h - 1) * stride_h + kernel_extent_h;
  239. int out_elempack = (support_packing && opt.use_packing_layout && num_output % 4 == 0) ? 4 : 1;
  240. size_t out_elemsize = elemsize / elempack * out_elempack;
  241. Mat top_blob_bordered;
  242. 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))
  243. {
  244. top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.workspace_allocator);
  245. }
  246. else
  247. {
  248. top_blob_bordered = top_blob;
  249. top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  250. }
  251. if (top_blob_bordered.empty())
  252. return -100;
  253. const int maxk = kernel_w * kernel_h;
  254. #if __ARM_NEON
  255. if (elempack == 4 && out_elempack == 4)
  256. {
  257. // num_output
  258. #pragma omp parallel for num_threads(opt.num_threads)
  259. for (int p = 0; p < num_output / out_elempack; p++)
  260. {
  261. float* outptr = top_blob_bordered.channel(p);
  262. for (int i = 0; i < outh; i++)
  263. {
  264. for (int j = 0; j < outw; j++)
  265. {
  266. float32x4_t _sum = vdupq_n_f32(0.f);
  267. if (bias_term)
  268. {
  269. _sum = vld1q_f32(((const float*)bias_data) + p * 4);
  270. }
  271. const float* kptr = (const float*)weight_data_pack4 + maxk * channels * p * 16;
  272. // channels
  273. for (int q = 0; q < channels; q++)
  274. {
  275. const Mat m = bottom_blob.channel(q);
  276. for (int y = 0; y < kernel_h; y++)
  277. {
  278. int sys = (i + y * dilation_h - (kernel_extent_h - 1));
  279. if (sys < 0 || sys % stride_h != 0)
  280. continue;
  281. int sy = sys / stride_h;
  282. if (sy >= h)
  283. continue;
  284. for (int x = 0; x < kernel_w; x++)
  285. {
  286. int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
  287. if (sxs < 0 || sxs % stride_w != 0)
  288. continue;
  289. int sx = sxs / stride_w;
  290. if (sx >= w)
  291. continue;
  292. const float* sptr = m.row(sy) + sx * 4;
  293. float32x4_t _val = vld1q_f32(sptr);
  294. int k = y * kernel_w + x;
  295. float32x4_t _w0 = vld1q_f32(kptr + k * 16);
  296. float32x4_t _w1 = vld1q_f32(kptr + k * 16 + 4);
  297. float32x4_t _w2 = vld1q_f32(kptr + k * 16 + 8);
  298. float32x4_t _w3 = vld1q_f32(kptr + k * 16 + 12);
  299. #if __aarch64__
  300. _sum = vmlaq_laneq_f32(_sum, _w0, _val, 0);
  301. _sum = vmlaq_laneq_f32(_sum, _w1, _val, 1);
  302. _sum = vmlaq_laneq_f32(_sum, _w2, _val, 2);
  303. _sum = vmlaq_laneq_f32(_sum, _w3, _val, 3);
  304. #else
  305. _sum = vmlaq_lane_f32(_sum, _w0, vget_low_f32(_val), 0);
  306. _sum = vmlaq_lane_f32(_sum, _w1, vget_low_f32(_val), 1);
  307. _sum = vmlaq_lane_f32(_sum, _w2, vget_high_f32(_val), 0);
  308. _sum = vmlaq_lane_f32(_sum, _w3, vget_high_f32(_val), 1);
  309. #endif
  310. }
  311. }
  312. kptr += maxk * 16;
  313. }
  314. _sum = activation_ps(_sum, activation_type, activation_params);
  315. vst1q_f32(outptr + j * 4, _sum);
  316. }
  317. outptr += outw * 4;
  318. }
  319. }
  320. }
  321. if (elempack == 1 && out_elempack == 4)
  322. {
  323. // num_output
  324. #pragma omp parallel for num_threads(opt.num_threads)
  325. for (int p = 0; p < num_output / out_elempack; p++)
  326. {
  327. float* outptr = top_blob_bordered.channel(p);
  328. for (int i = 0; i < outh; i++)
  329. {
  330. for (int j = 0; j < outw; j++)
  331. {
  332. float32x4_t _sum = vdupq_n_f32(0.f);
  333. if (bias_term)
  334. {
  335. _sum = vld1q_f32(((const float*)bias_data) + p * 4);
  336. }
  337. const float* kptr = (const float*)weight_data_pack1to4 + maxk * channels * p * 4;
  338. // channels
  339. for (int q = 0; q < channels; q++)
  340. {
  341. const Mat m = bottom_blob.channel(q);
  342. for (int y = 0; y < kernel_h; y++)
  343. {
  344. int sys = (i + y * dilation_h - (kernel_extent_h - 1));
  345. if (sys < 0 || sys % stride_h != 0)
  346. continue;
  347. int sy = sys / stride_h;
  348. if (sy >= h)
  349. continue;
  350. const float* sptr = m.row(sy);
  351. for (int x = 0; x < kernel_w; x++)
  352. {
  353. int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
  354. if (sxs < 0 || sxs % stride_w != 0)
  355. continue;
  356. int sx = sxs / stride_w;
  357. if (sx >= w)
  358. continue;
  359. float32x4_t _val = vdupq_n_f32(sptr[sx]);
  360. int k = y * kernel_w + x;
  361. float32x4_t _w = vld1q_f32(kptr + k * 4);
  362. _sum = vmlaq_f32(_sum, _val, _w);
  363. }
  364. }
  365. kptr += maxk * 4;
  366. }
  367. _sum = activation_ps(_sum, activation_type, activation_params);
  368. vst1q_f32(outptr + j * 4, _sum);
  369. }
  370. outptr += outw * 4;
  371. }
  372. }
  373. }
  374. if (elempack == 4 && out_elempack == 1)
  375. {
  376. // num_output
  377. #pragma omp parallel for num_threads(opt.num_threads)
  378. for (int p = 0; p < num_output / out_elempack; p++)
  379. {
  380. float* outptr = top_blob_bordered.channel(p);
  381. for (int i = 0; i < outh; i++)
  382. {
  383. for (int j = 0; j < outw; j++)
  384. {
  385. float sum = 0.f;
  386. if (bias_term)
  387. {
  388. sum = bias_data[p];
  389. }
  390. const float* kptr = (const float*)weight_data_pack4to1 + maxk * channels * p * 4;
  391. // channels
  392. for (int q = 0; q < channels; q++)
  393. {
  394. const Mat m = bottom_blob.channel(q);
  395. for (int y = 0; y < kernel_h; y++)
  396. {
  397. int sys = (i + y * dilation_h - (kernel_extent_h - 1));
  398. if (sys < 0 || sys % stride_h != 0)
  399. continue;
  400. int sy = sys / stride_h;
  401. if (sy >= h)
  402. continue;
  403. for (int x = 0; x < kernel_w; x++)
  404. {
  405. int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
  406. if (sxs < 0 || sxs % stride_w != 0)
  407. continue;
  408. int sx = sxs / stride_w;
  409. if (sx >= w)
  410. continue;
  411. const float* sptr = m.row(sy) + sx * 4;
  412. float32x4_t _val = vld1q_f32(sptr);
  413. int k = y * kernel_w + x;
  414. float32x4_t _w = vld1q_f32(kptr + k * 4);
  415. float32x4_t _s4 = vmulq_f32(_val, _w);
  416. #if __aarch64__
  417. sum += vaddvq_f32(_s4); // dot
  418. #else
  419. float32x2_t _ss = vadd_f32(vget_low_f32(_s4), vget_high_f32(_s4));
  420. _ss = vpadd_f32(_ss, _ss);
  421. sum += vget_lane_f32(_ss, 0);
  422. #endif
  423. }
  424. }
  425. kptr += maxk * 4;
  426. }
  427. sum = activation_ss(sum, activation_type, activation_params);
  428. outptr[j] = sum;
  429. }
  430. outptr += outw;
  431. }
  432. }
  433. }
  434. #endif // __ARM_NEON
  435. if (elempack == 1 && out_elempack == 1)
  436. {
  437. if (kernel_w == 3 && kernel_h == 3 && stride_w == 1 && stride_h == 1 && dilation_w == 1 && dilation_h == 1)
  438. {
  439. deconv3x3s1_neon(bottom_blob, top_blob_bordered, weight_data, bias_data, opt);
  440. if (activation)
  441. {
  442. activation->forward_inplace(top_blob_bordered, opt);
  443. }
  444. }
  445. else if (kernel_w == 3 && kernel_h == 3 && stride_w == 2 && stride_h == 2 && dilation_w == 1 && dilation_h == 1)
  446. {
  447. deconv3x3s2_neon(bottom_blob, top_blob_bordered, weight_data, bias_data, opt);
  448. if (activation)
  449. {
  450. activation->forward_inplace(top_blob_bordered, opt);
  451. }
  452. }
  453. else if (kernel_w == 4 && kernel_h == 4 && stride_w == 1 && stride_h == 1 && dilation_w == 1 && dilation_h == 1)
  454. {
  455. deconv4x4s1_neon(bottom_blob, top_blob_bordered, weight_data, bias_data, opt);
  456. if (activation)
  457. {
  458. activation->forward_inplace(top_blob_bordered, opt);
  459. }
  460. }
  461. else if (kernel_w == 4 && kernel_h == 4 && stride_w == 2 && stride_h == 2 && dilation_w == 1 && dilation_h == 1)
  462. {
  463. deconv4x4s2_neon(bottom_blob, top_blob_bordered, weight_data, bias_data, opt);
  464. if (activation)
  465. {
  466. activation->forward_inplace(top_blob_bordered, opt);
  467. }
  468. }
  469. else
  470. {
  471. // num_output
  472. #pragma omp parallel for num_threads(opt.num_threads)
  473. for (int p = 0; p < num_output; p++)
  474. {
  475. float* outptr = top_blob_bordered.channel(p);
  476. for (int i = 0; i < outh; i++)
  477. {
  478. for (int j = 0; j < outw; j++)
  479. {
  480. float sum = 0.f;
  481. if (bias_term)
  482. {
  483. sum = bias_data[p];
  484. }
  485. const float* kptr = (const float*)weight_data_pack1 + maxk * channels * p;
  486. // channels
  487. for (int q = 0; q < channels; q++)
  488. {
  489. const Mat m = bottom_blob.channel(q);
  490. for (int y = 0; y < kernel_h; y++)
  491. {
  492. int sys = (i + y * dilation_h - (kernel_extent_h - 1));
  493. if (sys < 0 || sys % stride_h != 0)
  494. continue;
  495. int sy = sys / stride_h;
  496. if (sy >= h)
  497. continue;
  498. const float* sptr = m.row(sy);
  499. for (int x = 0; x < kernel_w; x++)
  500. {
  501. int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
  502. if (sxs < 0 || sxs % stride_w != 0)
  503. continue;
  504. int sx = sxs / stride_w;
  505. if (sx >= w)
  506. continue;
  507. float val = sptr[sx];
  508. int k = y * kernel_w + x;
  509. float w = kptr[k];
  510. sum += val * w;
  511. }
  512. }
  513. kptr += maxk;
  514. }
  515. if (activation_type == 1)
  516. {
  517. sum = std::max(sum, 0.f);
  518. }
  519. else if (activation_type == 2)
  520. {
  521. float slope = activation_params[0];
  522. sum = sum > 0.f ? sum : sum * slope;
  523. }
  524. else if (activation_type == 3)
  525. {
  526. float min = activation_params[0];
  527. float max = activation_params[1];
  528. if (sum < min)
  529. sum = min;
  530. if (sum > max)
  531. sum = max;
  532. }
  533. else if (activation_type == 4)
  534. {
  535. sum = static_cast<float>(1.f / (1.f + exp(-sum)));
  536. }
  537. outptr[j] = sum;
  538. }
  539. outptr += outw;
  540. }
  541. }
  542. }
  543. }
  544. if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0)
  545. {
  546. Mat top_blob_bordered_adj = top_blob_bordered;
  547. if (output_pad_right > 0 || output_pad_bottom > 0)
  548. {
  549. Option opt_b = opt;
  550. opt_b.blob_allocator = opt.workspace_allocator;
  551. copy_make_border(top_blob_bordered, top_blob_bordered_adj, 0, output_pad_bottom, 0, output_pad_right, BORDER_CONSTANT, 0.f, opt_b);
  552. if (top_blob_bordered_adj.empty())
  553. return -100;
  554. }
  555. copy_cut_border(top_blob_bordered_adj, top_blob, pad_top, pad_bottom, pad_left, pad_right, opt);
  556. if (top_blob.empty())
  557. return -100;
  558. outw = top_blob.w;
  559. outh = top_blob.h;
  560. }
  561. else if (output_w > 0 && output_h > 0)
  562. {
  563. Mat top_blob_bordered_adj = top_blob_bordered;
  564. if (output_pad_right > 0 || output_pad_bottom > 0)
  565. {
  566. Option opt_b = opt;
  567. opt_b.blob_allocator = opt.workspace_allocator;
  568. copy_make_border(top_blob_bordered, top_blob_bordered_adj, 0, output_pad_bottom, 0, output_pad_right, BORDER_CONSTANT, 0.f, opt_b);
  569. if (top_blob_bordered_adj.empty())
  570. return -100;
  571. }
  572. int wcut = top_blob_bordered_adj.w - output_w;
  573. int hcut = top_blob_bordered_adj.h - output_h;
  574. if (pad_left == -233 || pad_right == -233 || pad_top == -233 || pad_bottom == -233)
  575. {
  576. // onnx padding=SAME_UPPER
  577. copy_cut_border(top_blob_bordered_adj, top_blob, hcut / 2, hcut - hcut / 2, wcut / 2, wcut - wcut / 2, opt);
  578. }
  579. else if (pad_left == -234 || pad_right == -234 || pad_top == -234 || pad_bottom == -234)
  580. {
  581. // onnx padding=SAME_LOWER
  582. copy_cut_border(top_blob_bordered_adj, top_blob, hcut - hcut / 2, hcut / 2, wcut - wcut / 2, wcut / 2, opt);
  583. }
  584. if (top_blob.empty())
  585. return -100;
  586. outw = top_blob.w;
  587. outh = top_blob.h;
  588. }
  589. else
  590. {
  591. if (output_pad_right > 0 || output_pad_bottom > 0)
  592. {
  593. copy_make_border(top_blob_bordered, top_blob, 0, output_pad_bottom, 0, output_pad_right, BORDER_CONSTANT, 0.f, opt);
  594. if (top_blob.empty())
  595. return -100;
  596. }
  597. else
  598. {
  599. top_blob = top_blob_bordered;
  600. }
  601. }
  602. return 0;
  603. }
  604. } // namespace ncnn