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.

concat.cpp 7.2 kB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 "concat.h"
  15. namespace ncnn {
  16. Concat::Concat()
  17. {
  18. one_blob_only = false;
  19. support_inplace = false;
  20. }
  21. int Concat::load_param(const ParamDict& pd)
  22. {
  23. axis = pd.get(0, 0);
  24. return 0;
  25. }
  26. int Concat::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
  27. {
  28. int dims = bottom_blobs[0].dims;
  29. size_t elemsize = bottom_blobs[0].elemsize;
  30. if (dims == 1) // axis == 0
  31. {
  32. // concat vector
  33. // total length
  34. int top_w = 0;
  35. for (size_t b = 0; b < bottom_blobs.size(); b++)
  36. {
  37. const Mat& bottom_blob = bottom_blobs[b];
  38. top_w += bottom_blob.w;
  39. }
  40. Mat& top_blob = top_blobs[0];
  41. top_blob.create(top_w, elemsize, opt.blob_allocator);
  42. if (top_blob.empty())
  43. return -100;
  44. unsigned char* outptr = top_blob;
  45. for (size_t b = 0; b < bottom_blobs.size(); b++)
  46. {
  47. const Mat& bottom_blob = bottom_blobs[b];
  48. int w = bottom_blob.w;
  49. const unsigned char* ptr = bottom_blob;
  50. memcpy(outptr, ptr, w * elemsize);
  51. outptr += w * elemsize;
  52. }
  53. return 0;
  54. }
  55. if (dims == 2 && axis == 0)
  56. {
  57. // concat image
  58. int w = bottom_blobs[0].w;
  59. // total height
  60. int top_h = 0;
  61. for (size_t b = 0; b < bottom_blobs.size(); b++)
  62. {
  63. const Mat& bottom_blob = bottom_blobs[b];
  64. top_h += bottom_blob.h;
  65. }
  66. Mat& top_blob = top_blobs[0];
  67. top_blob.create(w, top_h, elemsize, opt.blob_allocator);
  68. if (top_blob.empty())
  69. return -100;
  70. unsigned char* outptr = top_blob;
  71. for (size_t b = 0; b < bottom_blobs.size(); b++)
  72. {
  73. const Mat& bottom_blob = bottom_blobs[b];
  74. int size = w * bottom_blob.h;
  75. const unsigned char* ptr = bottom_blob;
  76. memcpy(outptr, ptr, size * elemsize);
  77. outptr += size * elemsize;
  78. }
  79. return 0;
  80. }
  81. if (dims == 2 && axis == 1)
  82. {
  83. // interleave image row
  84. int h = bottom_blobs[0].h;
  85. // total width
  86. int top_w = 0;
  87. for (size_t b = 0; b < bottom_blobs.size(); b++)
  88. {
  89. const Mat& bottom_blob = bottom_blobs[b];
  90. top_w += bottom_blob.w;
  91. }
  92. Mat& top_blob = top_blobs[0];
  93. top_blob.create(top_w, h, elemsize, opt.blob_allocator);
  94. if (top_blob.empty())
  95. return -100;
  96. #pragma omp parallel for num_threads(opt.num_threads)
  97. for (int i = 0; i < h; i++)
  98. {
  99. unsigned char* outptr = top_blob.row<unsigned char>(i);
  100. for (size_t b = 0; b < bottom_blobs.size(); b++)
  101. {
  102. const Mat& bottom_blob = bottom_blobs[b];
  103. const unsigned char* ptr = bottom_blob.row<const unsigned char>(i);
  104. memcpy(outptr, ptr, bottom_blob.w * elemsize);
  105. outptr += bottom_blob.w * elemsize;
  106. }
  107. }
  108. return 0;
  109. }
  110. if (dims == 3 && axis == 0)
  111. {
  112. // concat dim
  113. int w = bottom_blobs[0].w;
  114. int h = bottom_blobs[0].h;
  115. // total channels
  116. int top_channels = 0;
  117. for (size_t b = 0; b < bottom_blobs.size(); b++)
  118. {
  119. const Mat& bottom_blob = bottom_blobs[b];
  120. top_channels += bottom_blob.c;
  121. }
  122. Mat& top_blob = top_blobs[0];
  123. top_blob.create(w, h, top_channels, elemsize, opt.blob_allocator);
  124. if (top_blob.empty())
  125. return -100;
  126. int q = 0;
  127. for (size_t b = 0; b < bottom_blobs.size(); b++)
  128. {
  129. const Mat& bottom_blob = bottom_blobs[b];
  130. int channels = bottom_blob.c;
  131. size_t size = bottom_blob.cstep * channels;
  132. const unsigned char* ptr = bottom_blob;
  133. unsigned char* outptr = top_blob.channel(q);
  134. memcpy(outptr, ptr, size * elemsize);
  135. q += channels;
  136. }
  137. return 0;
  138. }
  139. if (dims == 3 && axis == 1)
  140. {
  141. // interleave dim height
  142. int w = bottom_blobs[0].w;
  143. int channels = bottom_blobs[0].c;
  144. // total height
  145. int top_h = 0;
  146. for (size_t b = 0; b < bottom_blobs.size(); b++)
  147. {
  148. const Mat& bottom_blob = bottom_blobs[b];
  149. top_h += bottom_blob.h;
  150. }
  151. Mat& top_blob = top_blobs[0];
  152. top_blob.create(w, top_h, channels, elemsize, opt.blob_allocator);
  153. if (top_blob.empty())
  154. return -100;
  155. #pragma omp parallel for num_threads(opt.num_threads)
  156. for (int q = 0; q < channels; q++)
  157. {
  158. unsigned char* outptr = top_blob.channel(q);
  159. for (size_t b = 0; b < bottom_blobs.size(); b++)
  160. {
  161. const Mat& bottom_blob = bottom_blobs[b];
  162. int size = bottom_blob.w * bottom_blob.h;
  163. const unsigned char* ptr = bottom_blob.channel(q);
  164. memcpy(outptr, ptr, size * elemsize);
  165. outptr += size * elemsize;
  166. }
  167. }
  168. return 0;
  169. }
  170. if (dims == 3 && axis == 2)
  171. {
  172. // interleave dim width
  173. int h = bottom_blobs[0].h;
  174. int channels = bottom_blobs[0].c;
  175. // total height
  176. int top_w = 0;
  177. for (size_t b = 0; b < bottom_blobs.size(); b++)
  178. {
  179. const Mat& bottom_blob = bottom_blobs[b];
  180. top_w += bottom_blob.w;
  181. }
  182. Mat& top_blob = top_blobs[0];
  183. top_blob.create(top_w, h, channels, elemsize, opt.blob_allocator);
  184. if (top_blob.empty())
  185. return -100;
  186. #pragma omp parallel for num_threads(opt.num_threads)
  187. for (int q = 0; q < channels; q++)
  188. {
  189. unsigned char* outptr = top_blob.channel(q);
  190. for (int i = 0; i < h; i++)
  191. {
  192. for (size_t b = 0; b < bottom_blobs.size(); b++)
  193. {
  194. const Mat& bottom_blob = bottom_blobs[b];
  195. const unsigned char* ptr = bottom_blob.channel(q).row<const unsigned char>(i);
  196. memcpy(outptr, ptr, bottom_blob.w * elemsize);
  197. outptr += bottom_blob.w * elemsize;
  198. }
  199. }
  200. }
  201. return 0;
  202. }
  203. return 0;
  204. }
  205. } // namespace ncnn