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