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_squeezenet.cpp 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 "platform.h"
  15. #include "net.h"
  16. #include "testutil.h"
  17. #include <stdio.h>
  18. static ncnn::Mat generate_ncnn_logo(int pixel_type_to, int w, int h)
  19. {
  20. // clang-format off
  21. // *INDENT-OFF*
  22. static const unsigned char ncnn_logo_data[16][16] =
  23. {
  24. {245, 245, 33, 245, 245, 245, 245, 245, 245, 245, 245, 245, 245, 33, 245, 245},
  25. {245, 33, 33, 33, 245, 245, 245, 245, 245, 245, 245, 245, 33, 33, 33, 245},
  26. {245, 33, 158, 158, 33, 245, 245, 245, 245, 245, 245, 33, 158, 158, 33, 245},
  27. { 33, 117, 158, 224, 158, 33, 245, 245, 245, 245, 33, 158, 224, 158, 117, 33},
  28. { 33, 117, 224, 224, 224, 66, 33, 33, 33, 33, 66, 224, 224, 224, 117, 33},
  29. { 33, 189, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 189, 33},
  30. { 33, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 33},
  31. { 33, 224, 224, 97, 97, 97, 97, 224, 224, 97, 97, 97, 97, 224, 224, 33},
  32. { 33, 224, 224, 97, 33, 0, 189, 224, 224, 97, 0, 33, 97, 224, 224, 33},
  33. { 33, 224, 224, 97, 33, 0, 189, 224, 224, 97, 0, 33, 97, 224, 224, 33},
  34. { 33, 224, 224, 97, 97, 97, 97, 224, 224, 97, 189, 189, 97, 224, 224, 33},
  35. { 33, 66, 66, 66, 224, 224, 224, 224, 224, 224, 224, 224, 66, 66, 66, 33},
  36. { 66, 158, 158, 66, 66, 224, 224, 224, 224, 224, 224, 66, 158, 158, 66, 66},
  37. { 66, 158, 158, 208, 66, 224, 224, 224, 224, 224, 224, 66, 158, 158, 208, 66},
  38. { 66, 224, 202, 158, 66, 224, 224, 224, 224, 224, 224, 66, 224, 202, 158, 66},
  39. { 66, 158, 224, 158, 66, 224, 224, 224, 224, 224, 224, 66, 158, 224, 158, 66}
  40. };
  41. // *INDENT-ON*
  42. // clang-format on
  43. const unsigned char* p_ncnn_logo_data = (const unsigned char*)ncnn_logo_data;
  44. ncnn::Mat logo = ncnn::Mat::from_pixels(p_ncnn_logo_data, ncnn::Mat::PIXEL_GRAY | (pixel_type_to << ncnn::Mat::PIXEL_CONVERT_SHIFT), 16, 16);
  45. ncnn::Mat m;
  46. ncnn::resize_nearest(logo, m, w, h);
  47. return m;
  48. }
  49. struct compare_score_index
  50. {
  51. inline bool operator()(const std::pair<float, int>& a, const std::pair<float, int>& b)
  52. {
  53. return a.first > b.first;
  54. }
  55. };
  56. static int check_top3(const std::vector<float>& cls_scores, float epsilon = 0.001)
  57. {
  58. // partial sort topk with index
  59. int size = cls_scores.size();
  60. std::vector<std::pair<float, int> > vec;
  61. vec.resize(size);
  62. for (int i = 0; i < size; i++)
  63. {
  64. vec[i] = std::make_pair(cls_scores[i], i);
  65. }
  66. std::partial_sort(vec.begin(), vec.begin() + 3, vec.end(), compare_score_index());
  67. int expect_indexes[3] = {532, 920, 716};
  68. float expect_scores[3] = {0.189459f, 0.082801f, 0.034684f};
  69. for (int i = 0; i < 3; i++)
  70. {
  71. int index = vec[i].second;
  72. float score = vec[i].first;
  73. if (index != expect_indexes[i])
  74. {
  75. fprintf(stderr, "top %d index not match expect %d but got %d\n", i, expect_indexes[i], index);
  76. return -1;
  77. }
  78. if (!NearlyEqual(score, expect_scores[i], epsilon))
  79. {
  80. fprintf(stderr, "top %d score not match expect %f but got %f\n", i, expect_scores[i], score);
  81. return -1;
  82. }
  83. }
  84. return 0;
  85. }
  86. static void fread_or_error(void* buffer, size_t size, size_t count, FILE* fp, const char* s)
  87. {
  88. if (count != fread(buffer, size, count, fp))
  89. {
  90. fprintf(stderr, "Couldn't read from file: %s\n", s);
  91. fclose(fp);
  92. exit(EXIT_FAILURE);
  93. }
  94. }
  95. static std::string read_file_string(const char* filepath)
  96. {
  97. FILE* fp = fopen(filepath, "rb");
  98. if (!fp)
  99. {
  100. fprintf(stderr, "fopen %s failed\n", filepath);
  101. return std::string();
  102. }
  103. fseek(fp, 0, SEEK_END);
  104. int len = ftell(fp);
  105. rewind(fp);
  106. std::string s;
  107. s.resize(len + 1); // +1 for '\0'
  108. fread_or_error((char*)s.c_str(), 1, len, fp, filepath);
  109. fclose(fp);
  110. s[len] = '\0';
  111. return s;
  112. }
  113. static ncnn::Mat read_file_content(const char* filepath)
  114. {
  115. FILE* fp = fopen(filepath, "rb");
  116. if (!fp)
  117. {
  118. fprintf(stderr, "fopen %s failed\n", filepath);
  119. return ncnn::Mat();
  120. }
  121. fseek(fp, 0, SEEK_END);
  122. int len = ftell(fp);
  123. rewind(fp);
  124. ncnn::Mat m(len, (size_t)1u, 1);
  125. fread_or_error(m, 1, len, fp, filepath);
  126. fclose(fp);
  127. return m;
  128. }
  129. static int test_squeezenet(const ncnn::Option& opt, int load_model_type, float epsilon = 0.001)
  130. {
  131. ncnn::Net squeezenet;
  132. squeezenet.opt = opt;
  133. std::string param_str;
  134. ncnn::Mat param_data;
  135. ncnn::Mat model_data;
  136. if (load_model_type == 0)
  137. {
  138. // load from plain model file
  139. squeezenet.load_param("../../examples/squeezenet_v1.1.param");
  140. squeezenet.load_model("../../examples/squeezenet_v1.1.bin");
  141. }
  142. if (load_model_type == 1)
  143. {
  144. // load from plain model memory
  145. param_str = read_file_string("../../examples/squeezenet_v1.1.param");
  146. model_data = read_file_content("../../examples/squeezenet_v1.1.bin");
  147. squeezenet.load_param_mem((const char*)param_str.c_str());
  148. squeezenet.load_model((const unsigned char*)model_data);
  149. }
  150. if (load_model_type == 2)
  151. {
  152. // load from binary model file
  153. squeezenet.load_param_bin("../../examples/squeezenet_v1.1.param.bin");
  154. squeezenet.load_model("../../examples/squeezenet_v1.1.bin");
  155. }
  156. if (load_model_type == 3)
  157. {
  158. // load from binary model memory
  159. param_data = read_file_content("../../examples/squeezenet_v1.1.param.bin");
  160. model_data = read_file_content("../../examples/squeezenet_v1.1.bin");
  161. squeezenet.load_param((const unsigned char*)param_data);
  162. squeezenet.load_model((const unsigned char*)model_data);
  163. }
  164. ncnn::Mat in = generate_ncnn_logo(ncnn::Mat::PIXEL_BGR, 227, 227);
  165. const float mean_vals[3] = {104.f, 117.f, 123.f};
  166. in.substract_mean_normalize(mean_vals, 0);
  167. ncnn::Extractor ex = squeezenet.create_extractor();
  168. ncnn::Mat out;
  169. if (load_model_type == 0 || load_model_type == 1)
  170. {
  171. ex.input("data", in);
  172. ex.extract("prob", out);
  173. }
  174. if (load_model_type == 2 || load_model_type == 3)
  175. {
  176. ex.input(0, in);
  177. ex.extract(82, out);
  178. }
  179. std::vector<float> cls_scores;
  180. cls_scores.resize(out.w);
  181. for (int j = 0; j < out.w; j++)
  182. {
  183. cls_scores[j] = out[j];
  184. }
  185. return check_top3(cls_scores, epsilon);
  186. }
  187. int main()
  188. {
  189. ncnn::UnlockedPoolAllocator g_blob_pool_allocator;
  190. ncnn::PoolAllocator g_workspace_pool_allocator;
  191. ncnn::Option opts[4];
  192. opts[0].use_packing_layout = false;
  193. opts[0].use_fp16_packed = false;
  194. opts[0].use_fp16_storage = false;
  195. opts[0].use_fp16_arithmetic = false;
  196. opts[0].use_shader_pack8 = false;
  197. opts[0].use_image_storage = false;
  198. opts[1].use_packing_layout = true;
  199. opts[1].use_fp16_packed = true;
  200. opts[1].use_fp16_storage = false;
  201. opts[1].use_fp16_arithmetic = false;
  202. opts[1].use_shader_pack8 = true;
  203. opts[1].use_image_storage = false;
  204. opts[2].use_packing_layout = true;
  205. opts[2].use_fp16_packed = true;
  206. opts[2].use_fp16_storage = true;
  207. opts[2].use_fp16_arithmetic = false;
  208. opts[2].use_bf16_storage = true;
  209. opts[2].use_shader_pack8 = true;
  210. opts[2].use_image_storage = true;
  211. opts[2].blob_allocator = &g_blob_pool_allocator;
  212. opts[2].workspace_allocator = &g_workspace_pool_allocator;
  213. opts[3].use_packing_layout = true;
  214. opts[3].use_fp16_packed = true;
  215. opts[3].use_fp16_storage = true;
  216. opts[3].use_fp16_arithmetic = false; // FIXME enable me
  217. opts[3].use_bf16_storage = false;
  218. opts[3].use_shader_pack8 = true;
  219. opts[3].use_image_storage = true;
  220. opts[3].blob_allocator = &g_blob_pool_allocator;
  221. opts[3].workspace_allocator = &g_workspace_pool_allocator;
  222. int load_model_types[4] = {0, 1, 2, 3};
  223. for (int i = 0; i < 4; i++)
  224. {
  225. const ncnn::Option& opt = opts[i];
  226. float epsilon;
  227. if (opt.use_bf16_storage || opt.use_fp16_packed || opt.use_fp16_storage)
  228. {
  229. epsilon = 0.1;
  230. }
  231. else
  232. {
  233. epsilon = 0.01;
  234. }
  235. int ret;
  236. ncnn::Option opt_cpu = opt;
  237. opt_cpu.use_vulkan_compute = false;
  238. ret = test_squeezenet(opt_cpu, load_model_types[i], epsilon);
  239. if (ret != 0)
  240. {
  241. fprintf(stderr, "test_squeezenet cpu failed use_packing_layout=%d use_fp16_packed=%d use_fp16_storage=%d use_shader_pack8=%d use_bf16_storage=%d use_image_storage=%d\n", opt.use_packing_layout, opt.use_fp16_packed, opt.use_fp16_storage, opt.use_shader_pack8, opt.use_bf16_storage, opt.use_image_storage);
  242. return ret;
  243. }
  244. #if NCNN_VULKAN
  245. ncnn::Option opt_gpu = opt;
  246. opt_gpu.use_vulkan_compute = true;
  247. ret = test_squeezenet(opt_gpu, load_model_types[i], epsilon);
  248. if (ret != 0)
  249. {
  250. fprintf(stderr, "test_squeezenet gpu failed use_packing_layout=%d use_fp16_packed=%d use_fp16_storage=%d use_shader_pack8=%d use_bf16_storage=%d use_image_storage=%d\n", opt.use_packing_layout, opt.use_fp16_packed, opt.use_fp16_storage, opt.use_shader_pack8, opt.use_bf16_storage, opt.use_image_storage);
  251. return ret;
  252. }
  253. #endif // NCNN_VULKAN
  254. }
  255. return 0;
  256. }