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.

test_c_api.cpp 8.1 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2020 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 <string.h>
  15. #include "c_api.h"
  16. static int test_c_api_0()
  17. {
  18. ncnn_mat_t a = ncnn_mat_create_1d(2, NULL);
  19. ncnn_mat_t b = ncnn_mat_create_1d(2, NULL);
  20. ncnn_mat_t c = 0;
  21. ncnn_option_t opt = ncnn_option_create();
  22. // set a and b
  23. {
  24. ncnn_mat_fill_float(a, 2.f);
  25. ncnn_mat_fill_float(b, 3.f);
  26. }
  27. // c = a + b
  28. {
  29. ncnn_layer_t op = ncnn_layer_create_by_type("BinaryOp");
  30. // load param
  31. {
  32. ncnn_paramdict_t pd = ncnn_paramdict_create();
  33. ncnn_paramdict_set_int(pd, 0, 0); // op_type = ADD
  34. op->load_param(op, pd);
  35. ncnn_paramdict_destroy(pd);
  36. }
  37. // load model
  38. {
  39. ncnn_modelbin_t mb = ncnn_modelbin_create_from_mat_array(0, 0);
  40. op->load_model(op, mb);
  41. ncnn_modelbin_destroy(mb);
  42. }
  43. op->create_pipeline(op, opt);
  44. const ncnn_mat_t bottom_blobs[2] = {a, b};
  45. ncnn_mat_t* top_blobs[1] = {&c};
  46. op->forward_n(op, bottom_blobs, 2, top_blobs, 1, opt);
  47. op->destroy_pipeline(op, opt);
  48. ncnn_layer_destroy(op);
  49. }
  50. // check c == a + b
  51. bool success = false;
  52. if (c)
  53. {
  54. int dims = ncnn_mat_get_dims(c);
  55. int w = ncnn_mat_get_w(c);
  56. const float* c_data = (const float*)ncnn_mat_get_data(c);
  57. success = dims == 1 && w == 2 && c_data[0] == 5.f && c_data[1] == 5.f;
  58. }
  59. ncnn_option_destroy(opt);
  60. ncnn_mat_destroy(a);
  61. ncnn_mat_destroy(b);
  62. ncnn_mat_destroy(c);
  63. if (!success)
  64. {
  65. fprintf(stderr, "test_c_api_0 failed\n");
  66. }
  67. return success ? 0 : -1;
  68. }
  69. static int test_c_api_1()
  70. {
  71. ncnn_mat_t a = ncnn_mat_create_1d(24, NULL);
  72. // set a
  73. {
  74. const float data[] =
  75. {
  76. 0,1,2,3,4,5,6,7,
  77. 10,11,12,13,14,15,16,17,
  78. 20,21,22,23,24,25,26,27
  79. };
  80. float* a_data = (float*)ncnn_mat_get_data(a);
  81. memcpy(a_data, data, 24 * sizeof(float));
  82. }
  83. ncnn_mat_t b = ncnn_mat_reshape_3d(a, 4, 2, 3, NULL);
  84. ncnn_mat_t c = 0;
  85. ncnn_option_t opt = ncnn_option_create();
  86. // c = reorg(b, 2)
  87. {
  88. ncnn_layer_t op = ncnn_layer_create_by_type("Reorg");
  89. // load param
  90. {
  91. ncnn_paramdict_t pd = ncnn_paramdict_create();
  92. ncnn_paramdict_set_int(pd, 0, 2); // stride
  93. op->load_param(op, pd);
  94. ncnn_paramdict_destroy(pd);
  95. }
  96. // load model
  97. {
  98. ncnn_modelbin_t mb = ncnn_modelbin_create_from_mat_array(0, 0);
  99. op->load_model(op, mb);
  100. ncnn_modelbin_destroy(mb);
  101. }
  102. op->create_pipeline(op, opt);
  103. op->forward_1(op, b, &c, opt);
  104. op->destroy_pipeline(op, opt);
  105. ncnn_layer_destroy(op);
  106. }
  107. // check c
  108. bool success = false;
  109. if (c)
  110. {
  111. int dims = ncnn_mat_get_dims(c);
  112. int w = ncnn_mat_get_w(c);
  113. int h = ncnn_mat_get_h(c);
  114. int ch = ncnn_mat_get_c(c);
  115. success = dims == 3 && w == 2 && h == 1 && ch == 12;
  116. const float expected[] =
  117. {
  118. 0,2,
  119. 1,3,
  120. 4,6,
  121. 5,7,
  122. 10,12,
  123. 11,13,
  124. 14,16,
  125. 15,17,
  126. 20,22,
  127. 21,23,
  128. 24,26,
  129. 25,27
  130. };
  131. ncnn_mat_t c2 = 0;
  132. ncnn_flatten(c, &c2, opt);
  133. const float* c2_data = (const float*)ncnn_mat_get_data(c2);
  134. if (memcmp(c2_data, expected, 24) != 0)
  135. {
  136. success = false;
  137. }
  138. ncnn_mat_destroy(c2);
  139. }
  140. ncnn_option_destroy(opt);
  141. ncnn_mat_destroy(a);
  142. ncnn_mat_destroy(b);
  143. ncnn_mat_destroy(c);
  144. if (!success)
  145. {
  146. fprintf(stderr, "test_c_api_1 failed\n");
  147. }
  148. return success ? 0 : -1;
  149. }
  150. static int mylayer_forward_inplace_1(const ncnn_layer_t layer, ncnn_mat_t bottom_top_blob, const ncnn_option_t opt)
  151. {
  152. int w = ncnn_mat_get_w(bottom_top_blob);
  153. int h = ncnn_mat_get_h(bottom_top_blob);
  154. int channels = ncnn_mat_get_c(bottom_top_blob);
  155. int size = w * h;
  156. #pragma omp parallel for num_threads(ncnn_option_get_num_threads(opt))
  157. for (int q = 0; q < channels; q++)
  158. {
  159. float* ptr = (float*)ncnn_mat_get_channel_data(bottom_top_blob, q);
  160. for (int i = 0; i < size; i++)
  161. {
  162. *ptr = *ptr + 100.f;
  163. ptr++;
  164. }
  165. }
  166. return 0;
  167. }
  168. static ncnn_layer_t mylayer_creator(void* /*userdata*/)
  169. {
  170. ncnn_layer_t layer = ncnn_layer_create();
  171. ncnn_layer_set_one_blob_only(layer, 1);
  172. ncnn_layer_set_support_inplace(layer, 1);
  173. layer->forward_inplace_1 = mylayer_forward_inplace_1;
  174. return layer;
  175. }
  176. static void mylayer_destroyer(ncnn_layer_t layer, void* /*userdata*/)
  177. {
  178. ncnn_layer_destroy(layer);
  179. }
  180. static size_t emptydr_read(ncnn_datareader_t /*dr*/, void* buf, size_t size)
  181. {
  182. memset(buf, 0, size);
  183. return size;
  184. }
  185. static int test_c_api_2()
  186. {
  187. // datareader from empty
  188. ncnn_datareader_t emptydr = ncnn_datareader_create();
  189. {
  190. emptydr->read = emptydr_read;
  191. }
  192. ncnn_option_t opt = ncnn_option_create();
  193. {
  194. ncnn_option_set_num_threads(opt, 1);
  195. }
  196. ncnn_net_t net = ncnn_net_create();
  197. {
  198. ncnn_net_set_option(net, opt);
  199. ncnn_net_register_custom_layer_by_type(net, "MyLayer", mylayer_creator, mylayer_destroyer, 0);
  200. const char param_txt[] = "7767517\n2 2\nInput input 0 1 data\nMyLayer mylayer 1 1 data output\n";
  201. ncnn_net_load_param_memory(net, param_txt);
  202. ncnn_net_load_model_datareader(net, emptydr);
  203. }
  204. ncnn_mat_t a = ncnn_mat_create_1d(24, NULL);
  205. // set a
  206. {
  207. const float data[] =
  208. {
  209. 0,1,2,3,4,5,6,7,
  210. 10,11,12,13,14,15,16,17,
  211. 20,21,22,23,24,25,26,27
  212. };
  213. float* a_data = (float*)ncnn_mat_get_data(a);
  214. memcpy(a_data, data, 24 * sizeof(float));
  215. }
  216. ncnn_mat_t b = ncnn_mat_reshape_3d(a, 4, 2, 3, NULL);
  217. ncnn_mat_t c = 0;
  218. {
  219. ncnn_extractor_t ex = ncnn_extractor_create(net);
  220. ncnn_extractor_input(ex, "data", b);
  221. ncnn_extractor_extract(ex, "output", &c);
  222. ncnn_extractor_destroy(ex);
  223. }
  224. ncnn_net_destroy(net);
  225. // check c
  226. bool success = false;
  227. if (c)
  228. {
  229. int dims = ncnn_mat_get_dims(c);
  230. int w = ncnn_mat_get_w(c);
  231. int h = ncnn_mat_get_h(c);
  232. int ch = ncnn_mat_get_c(c);
  233. success = dims == 3 && w == 4 && h == 2 && ch == 3;
  234. const float expected[] =
  235. {
  236. 100,101,102,103,104,105,106,107,
  237. 110,111,112,113,114,115,116,117,
  238. 120,121,122,123,124,125,126,127
  239. };
  240. ncnn_mat_t c2 = 0;
  241. ncnn_flatten(c, &c2, opt);
  242. const float* c2_data = (const float*)ncnn_mat_get_data(c2);
  243. if (memcmp(c2_data, expected, 24) != 0)
  244. {
  245. success = false;
  246. }
  247. ncnn_mat_destroy(c2);
  248. }
  249. ncnn_option_destroy(opt);
  250. ncnn_mat_destroy(a);
  251. ncnn_mat_destroy(b);
  252. ncnn_mat_destroy(c);
  253. ncnn_datareader_destroy(emptydr);
  254. if (!success)
  255. {
  256. fprintf(stderr, "test_c_api_2 failed\n");
  257. }
  258. return success ? 0 : -1;
  259. }
  260. int main()
  261. {
  262. return test_c_api_0() || test_c_api_1() || test_c_api_2();
  263. }