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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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);
  19. ncnn_mat_t b = ncnn_mat_create_1d(2);
  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. ncnn_layer_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. ncnn_layer_load_model(op, mb);
  41. ncnn_modelbin_destroy(mb);
  42. }
  43. ncnn_layer_create_pipeline(op, opt);
  44. const ncnn_mat_t bottom_blobs[2] = {a, b};
  45. ncnn_mat_t* top_blobs[1] = {&c};
  46. ncnn_layer_forward_n(op, bottom_blobs, 2, top_blobs, 1, opt);
  47. ncnn_layer_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. return success ? 0 : -1;
  64. }
  65. static int test_c_api_1()
  66. {
  67. const float data[] =
  68. {
  69. 0,1,2,3,4,5,6,7,
  70. 10,11,12,13,14,15,16,17,
  71. 20,21,22,23,24,25,26,27
  72. };
  73. ncnn_mat_t a = ncnn_mat_create_external_1d(24, (void*)data);
  74. ncnn_mat_t b = ncnn_mat_reshape_3d(a, 4, 2, 3);
  75. ncnn_mat_t c = 0;
  76. ncnn_option_t opt = ncnn_option_create();
  77. // c = reorg(b, 2)
  78. {
  79. ncnn_layer_t op = ncnn_layer_create_by_type("Reorg");
  80. // load param
  81. {
  82. ncnn_paramdict_t pd = ncnn_paramdict_create();
  83. ncnn_paramdict_set_int(pd, 0, 2); // stride
  84. ncnn_layer_load_param(op, pd);
  85. ncnn_paramdict_destroy(pd);
  86. }
  87. // load model
  88. {
  89. ncnn_modelbin_t mb = ncnn_modelbin_create_from_mat_array(0, 0);
  90. ncnn_layer_load_model(op, mb);
  91. ncnn_modelbin_destroy(mb);
  92. }
  93. ncnn_layer_create_pipeline(op, opt);
  94. ncnn_layer_forward_1(op, b, &c, opt);
  95. ncnn_layer_destroy_pipeline(op, opt);
  96. ncnn_layer_destroy(op);
  97. }
  98. // check c
  99. bool success = false;
  100. if (c)
  101. {
  102. int dims = ncnn_mat_get_dims(c);
  103. int w = ncnn_mat_get_w(c);
  104. int h = ncnn_mat_get_h(c);
  105. int ch = ncnn_mat_get_c(c);
  106. success = dims == 3 && w == 2 && h == 1 && ch == 12;
  107. const float expected[] =
  108. {
  109. 0,2,
  110. 1,3,
  111. 4,6,
  112. 5,7,
  113. 10,12,
  114. 11,13,
  115. 14,16,
  116. 15,17,
  117. 20,22,
  118. 21,23,
  119. 24,26,
  120. 25,27
  121. };
  122. ncnn_mat_t c2 = 0;
  123. ncnn_flatten(c, &c2, opt);
  124. const float* c2_data = (const float*)ncnn_mat_get_data(c2);
  125. if (memcmp(c2_data, expected, 24) != 0)
  126. {
  127. success = false;
  128. }
  129. ncnn_mat_destroy(c2);
  130. }
  131. ncnn_option_destroy(opt);
  132. ncnn_mat_destroy(a);
  133. ncnn_mat_destroy(b);
  134. ncnn_mat_destroy(c);
  135. return success ? 0 : -1;
  136. }
  137. int main()
  138. {
  139. return test_c_api_0() || test_c_api_1();
  140. }