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.

reshape.cpp 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "reshape.h"
  15. namespace ncnn {
  16. Reshape::Reshape()
  17. {
  18. one_blob_only = true;
  19. support_inplace = false;
  20. }
  21. int Reshape::load_param(const ParamDict& pd)
  22. {
  23. w = pd.get(0, -233);
  24. h = pd.get(1, -233);
  25. c = pd.get(2, -233);
  26. permute = pd.get(3, 0);
  27. ndim = 3;
  28. if (c == -233)
  29. ndim = 2;
  30. if (h == -233)
  31. ndim = 1;
  32. if (w == -233)
  33. ndim = 0;
  34. return 0;
  35. }
  36. int Reshape::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  37. {
  38. size_t elemsize = bottom_blob.elemsize;
  39. int total = bottom_blob.w * bottom_blob.h * bottom_blob.c;
  40. int dims = bottom_blob.dims;
  41. // resolve out shape
  42. int outw = w;
  43. int outh = h;
  44. int outc = c;
  45. if (ndim == 1)
  46. {
  47. if (outw == 0)
  48. outw = bottom_blob.w;
  49. if (outw == -1)
  50. outw = total;
  51. if (dims == 1 && bottom_blob.w == outw)
  52. {
  53. top_blob = bottom_blob;
  54. return 0;
  55. }
  56. }
  57. if (ndim == 2)
  58. {
  59. if (outw == 0)
  60. outw = bottom_blob.w;
  61. if (outh == 0)
  62. outh = bottom_blob.h;
  63. if (outw == -1)
  64. outw = total / outh;
  65. if (outh == -1)
  66. outh = total / outw;
  67. if (dims == 2 && bottom_blob.h == outh)
  68. {
  69. top_blob = bottom_blob;
  70. return 0;
  71. }
  72. }
  73. if (ndim == 3)
  74. {
  75. if (outw == 0)
  76. outw = bottom_blob.w;
  77. if (outh == 0)
  78. outh = bottom_blob.h;
  79. if (outc == 0)
  80. outc = bottom_blob.c;
  81. if (outw == -1)
  82. outw = total / outc / outh;
  83. if (outh == -1)
  84. outh = total / outc / outw;
  85. if (outc == -1)
  86. outc = total / outh / outw;
  87. if (dims == 3 && bottom_blob.c == outc)
  88. {
  89. top_blob = bottom_blob;
  90. top_blob.w = outw;
  91. top_blob.h = outh;
  92. return 0;
  93. }
  94. }
  95. bool need_permute = permute == 1;
  96. if (dims == 2 && ndim == 2 && bottom_blob.h == outh)
  97. need_permute = false;
  98. if (dims == 3 && ndim == 3 && bottom_blob.c == outc)
  99. need_permute = false;
  100. if (need_permute)
  101. {
  102. Mat bottom_blob_permuted = bottom_blob;
  103. if (dims == 2)
  104. {
  105. // hw -> wh
  106. int _w = bottom_blob.w;
  107. int _h = bottom_blob.h;
  108. bottom_blob_permuted.create(_h, _w, elemsize, opt.workspace_allocator);
  109. if (bottom_blob_permuted.empty())
  110. return -100;
  111. const float* ptr = bottom_blob;
  112. float* outptr = bottom_blob_permuted;
  113. for (int i = 0; i < _w; i++)
  114. {
  115. for (int j = 0; j < _h; j++)
  116. {
  117. outptr[i * _h + j] = ptr[j * _w + i];
  118. }
  119. }
  120. }
  121. if (dims == 3)
  122. {
  123. // chw -> hwc
  124. int _w = bottom_blob.w;
  125. int _h = bottom_blob.h;
  126. int channels = bottom_blob.c;
  127. bottom_blob_permuted.create(channels, _w, _h, elemsize, opt.workspace_allocator);
  128. if (bottom_blob_permuted.empty())
  129. return -100;
  130. #pragma omp parallel for num_threads(opt.num_threads)
  131. for (int q = 0; q < _h; q++)
  132. {
  133. float* outptr = bottom_blob_permuted.channel(q);
  134. for (int i = 0; i < _w; i++)
  135. {
  136. for (int j = 0; j < channels; j++)
  137. {
  138. const float* ptr = bottom_blob.channel(j).row(q);
  139. outptr[i * channels + j] = ptr[i];
  140. }
  141. }
  142. }
  143. }
  144. if (ndim == 1)
  145. {
  146. top_blob = bottom_blob_permuted.reshape(outw, opt.blob_allocator);
  147. if (top_blob.empty())
  148. return -100;
  149. return 0;
  150. }
  151. // permute on nhwc/nhc
  152. Mat top_blob_permuted;
  153. if (ndim == 2)
  154. {
  155. top_blob_permuted = bottom_blob_permuted.reshape(outh, outw, opt.workspace_allocator);
  156. }
  157. if (ndim == 3)
  158. {
  159. top_blob_permuted = bottom_blob_permuted.reshape(outc, outw, outh, opt.workspace_allocator);
  160. }
  161. if (top_blob_permuted.empty())
  162. return -100;
  163. if (ndim == 2)
  164. {
  165. // wh -> hw
  166. top_blob.create(outw, outh, elemsize, opt.blob_allocator);
  167. if (top_blob.empty())
  168. return -100;
  169. const float* ptr = top_blob_permuted;
  170. float* outptr = top_blob;
  171. for (int i = 0; i < outh; i++)
  172. {
  173. for (int j = 0; j < outw; j++)
  174. {
  175. outptr[i * outw + j] = ptr[j * outh + i];
  176. }
  177. }
  178. }
  179. if (ndim == 3)
  180. {
  181. // chw -> hwc
  182. top_blob.create(outw, outh, outc, elemsize, opt.blob_allocator);
  183. if (top_blob.empty())
  184. return -100;
  185. #pragma omp parallel for num_threads(opt.num_threads)
  186. for (int q = 0; q < outc; q++)
  187. {
  188. float* outptr = top_blob.channel(q);
  189. for (int i = 0; i < outh; i++)
  190. {
  191. const float* ptr = top_blob_permuted.channel(i);
  192. for (int j = 0; j < outw; j++)
  193. {
  194. outptr[i * outw + j] = ptr[j * outc + q];
  195. }
  196. }
  197. }
  198. }
  199. return 0;
  200. }
  201. if (ndim == 1)
  202. {
  203. top_blob = bottom_blob.reshape(outw, opt.blob_allocator);
  204. }
  205. if (ndim == 2)
  206. {
  207. top_blob = bottom_blob.reshape(outw, outh, opt.blob_allocator);
  208. }
  209. if (ndim == 3)
  210. {
  211. top_blob = bottom_blob.reshape(outw, outh, outc, opt.blob_allocator);
  212. }
  213. if (top_blob.empty())
  214. return -100;
  215. return 0;
  216. }
  217. } // namespace ncnn