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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // Copyright 2020 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "platform.h"
  4. #include "net.h"
  5. #include "testutil.h"
  6. #include <stdio.h>
  7. #ifdef __EMSCRIPTEN__
  8. #include <emscripten.h>
  9. #endif
  10. static ncnn::Mat generate_ncnn_logo(int pixel_type_to, int w, int h)
  11. {
  12. // clang-format off
  13. // *INDENT-OFF*
  14. static const unsigned char ncnn_logo_data[16][16] =
  15. {
  16. {245, 245, 33, 245, 245, 245, 245, 245, 245, 245, 245, 245, 245, 33, 245, 245},
  17. {245, 33, 33, 33, 245, 245, 245, 245, 245, 245, 245, 245, 33, 33, 33, 245},
  18. {245, 33, 158, 158, 33, 245, 245, 245, 245, 245, 245, 33, 158, 158, 33, 245},
  19. { 33, 117, 158, 224, 158, 33, 245, 245, 245, 245, 33, 158, 224, 158, 117, 33},
  20. { 33, 117, 224, 224, 224, 66, 33, 33, 33, 33, 66, 224, 224, 224, 117, 33},
  21. { 33, 189, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 189, 33},
  22. { 33, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 33},
  23. { 33, 224, 224, 97, 97, 97, 97, 224, 224, 97, 97, 97, 97, 224, 224, 33},
  24. { 33, 224, 224, 97, 33, 0, 189, 224, 224, 97, 0, 33, 97, 224, 224, 33},
  25. { 33, 224, 224, 97, 33, 0, 189, 224, 224, 97, 0, 33, 97, 224, 224, 33},
  26. { 33, 224, 224, 97, 97, 97, 97, 224, 224, 97, 189, 189, 97, 224, 224, 33},
  27. { 33, 66, 66, 66, 224, 224, 224, 224, 224, 224, 224, 224, 66, 66, 66, 33},
  28. { 66, 158, 158, 66, 66, 224, 224, 224, 224, 224, 224, 66, 158, 158, 66, 66},
  29. { 66, 158, 158, 208, 66, 224, 224, 224, 224, 224, 224, 66, 158, 158, 208, 66},
  30. { 66, 224, 202, 158, 66, 224, 224, 224, 224, 224, 224, 66, 224, 202, 158, 66},
  31. { 66, 158, 224, 158, 66, 224, 224, 224, 224, 224, 224, 66, 158, 224, 158, 66}
  32. };
  33. // *INDENT-ON*
  34. // clang-format on
  35. const unsigned char* p_ncnn_logo_data = (const unsigned char*)ncnn_logo_data;
  36. 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);
  37. ncnn::Mat m;
  38. ncnn::Option opt;
  39. opt.num_threads = 1;
  40. ncnn::resize_nearest(logo, m, w, h, opt);
  41. return m;
  42. }
  43. struct compare_score_index
  44. {
  45. inline bool operator()(const std::pair<float, int>& a, const std::pair<float, int>& b)
  46. {
  47. return a.first > b.first;
  48. }
  49. };
  50. static int check_top2(const std::vector<float>& cls_scores, float epsilon = 0.001)
  51. {
  52. // partial sort topk with index
  53. int size = cls_scores.size();
  54. std::vector<std::pair<float, int> > vec;
  55. vec.resize(size);
  56. for (int i = 0; i < size; i++)
  57. {
  58. vec[i] = std::make_pair(cls_scores[i], i);
  59. }
  60. std::partial_sort(vec.begin(), vec.begin() + 2, vec.end(), compare_score_index());
  61. int expect_indexes[2] = {532, 920};
  62. float expect_scores[2] = {0.189459f, 0.082801f};
  63. for (int i = 0; i < 2; i++)
  64. {
  65. int index = vec[i].second;
  66. float score = vec[i].first;
  67. if (index != expect_indexes[i])
  68. {
  69. fprintf(stderr, "top %d index not match expect %d but got %d\n", i, expect_indexes[i], index);
  70. return -1;
  71. }
  72. if (!NearlyEqual(score, expect_scores[i], epsilon))
  73. {
  74. fprintf(stderr, "top %d score not match expect %f but got %f\n", i, expect_scores[i], score);
  75. return -1;
  76. }
  77. }
  78. return 0;
  79. }
  80. static void fread_or_error(void* buffer, size_t size, size_t count, FILE* fp, const char* s)
  81. {
  82. if (count != fread(buffer, size, count, fp))
  83. {
  84. fprintf(stderr, "Couldn't read from file: %s\n", s);
  85. fclose(fp);
  86. exit(EXIT_FAILURE);
  87. }
  88. }
  89. static std::string read_file_string(const char* filepath)
  90. {
  91. FILE* fp = fopen(filepath, "rb");
  92. if (!fp)
  93. {
  94. fprintf(stderr, "fopen %s failed\n", filepath);
  95. return std::string();
  96. }
  97. fseek(fp, 0, SEEK_END);
  98. int len = ftell(fp);
  99. rewind(fp);
  100. std::string s;
  101. s.resize(len + 1); // +1 for '\0'
  102. fread_or_error((char*)s.c_str(), 1, len, fp, filepath);
  103. fclose(fp);
  104. s[len] = '\0';
  105. return s;
  106. }
  107. static ncnn::Mat read_file_content(const char* filepath)
  108. {
  109. FILE* fp = fopen(filepath, "rb");
  110. if (!fp)
  111. {
  112. fprintf(stderr, "fopen %s failed\n", filepath);
  113. return ncnn::Mat();
  114. }
  115. fseek(fp, 0, SEEK_END);
  116. int len = ftell(fp);
  117. rewind(fp);
  118. ncnn::Mat m(len, (size_t)1u, 1);
  119. fread_or_error(m, 1, len, fp, filepath);
  120. fclose(fp);
  121. return m;
  122. }
  123. static int test_squeezenet(const ncnn::Option& opt, int load_model_type, float epsilon = 0.001)
  124. {
  125. ncnn::Net squeezenet;
  126. squeezenet.opt = opt;
  127. #ifdef __EMSCRIPTEN__
  128. #define MODEL_DIR "/working"
  129. #else
  130. #define MODEL_DIR "../../examples"
  131. #endif
  132. std::string param_str;
  133. ncnn::Mat param_data;
  134. ncnn::Mat model_data;
  135. if (load_model_type == 0)
  136. {
  137. // load from plain model file
  138. squeezenet.load_param(MODEL_DIR "/squeezenet_v1.1.param");
  139. // test random feature disabled bits
  140. {
  141. std::vector<ncnn::Layer*>& layers = squeezenet.mutable_layers();
  142. for (size_t i = 0; i < layers.size(); i++)
  143. {
  144. layers[i]->featmask = i * 11 % 128;
  145. }
  146. }
  147. squeezenet.load_model(MODEL_DIR "/squeezenet_v1.1.bin");
  148. }
  149. if (load_model_type == 1)
  150. {
  151. // load from plain model memory
  152. param_str = read_file_string(MODEL_DIR "/squeezenet_v1.1.param");
  153. model_data = read_file_content(MODEL_DIR "/squeezenet_v1.1.bin");
  154. squeezenet.load_param_mem((const char*)param_str.c_str());
  155. squeezenet.load_model((const unsigned char*)model_data);
  156. }
  157. if (load_model_type == 2)
  158. {
  159. // load from binary model file
  160. squeezenet.load_param_bin(MODEL_DIR "/squeezenet_v1.1.param.bin");
  161. squeezenet.load_model(MODEL_DIR "/squeezenet_v1.1.bin");
  162. }
  163. if (load_model_type == 3)
  164. {
  165. // load from binary model memory
  166. param_data = read_file_content(MODEL_DIR "/squeezenet_v1.1.param.bin");
  167. model_data = read_file_content(MODEL_DIR "/squeezenet_v1.1.bin");
  168. squeezenet.load_param((const unsigned char*)param_data);
  169. squeezenet.load_model((const unsigned char*)model_data);
  170. }
  171. ncnn::Mat in = generate_ncnn_logo(ncnn::Mat::PIXEL_BGR, 227, 227);
  172. const float mean_vals[3] = {104.f, 117.f, 123.f};
  173. in.substract_mean_normalize(mean_vals, 0);
  174. ncnn::Extractor ex = squeezenet.create_extractor();
  175. ncnn::Mat out;
  176. if (load_model_type == 0 || load_model_type == 1)
  177. {
  178. ex.input("data", in);
  179. ex.extract("prob", out);
  180. }
  181. if (load_model_type == 2 || load_model_type == 3)
  182. {
  183. ex.input(0, in);
  184. ex.extract(82, out);
  185. }
  186. std::vector<float> cls_scores;
  187. cls_scores.resize(out.w);
  188. for (int j = 0; j < out.w; j++)
  189. {
  190. cls_scores[j] = out[j];
  191. }
  192. return check_top2(cls_scores, epsilon);
  193. }
  194. class MyConvolution : public ncnn::Layer
  195. {
  196. public:
  197. MyConvolution()
  198. {
  199. impl = ncnn::create_layer("Convolution");
  200. one_blob_only = impl->one_blob_only;
  201. support_inplace = impl->support_inplace;
  202. support_packing = impl->support_packing;
  203. support_vulkan = impl->support_vulkan;
  204. support_bf16_storage = impl->support_bf16_storage;
  205. support_fp16_storage = impl->support_fp16_storage;
  206. support_int8_storage = impl->support_int8_storage;
  207. }
  208. ~MyConvolution()
  209. {
  210. delete impl;
  211. }
  212. virtual int load_param(const ncnn::ParamDict& pd)
  213. {
  214. #if NCNN_VULKAN
  215. impl->vkdev = vkdev;
  216. #endif // NCNN_VULKAN
  217. return impl->load_param(pd);
  218. }
  219. virtual int load_model(const ncnn::ModelBin& mb)
  220. {
  221. return impl->load_model(mb);
  222. }
  223. virtual int create_pipeline(const ncnn::Option& opt)
  224. {
  225. int ret = impl->create_pipeline(opt);
  226. one_blob_only = impl->one_blob_only;
  227. support_inplace = impl->support_inplace;
  228. support_packing = impl->support_packing;
  229. support_vulkan = impl->support_vulkan;
  230. support_bf16_storage = impl->support_bf16_storage;
  231. support_fp16_storage = impl->support_fp16_storage;
  232. support_int8_storage = impl->support_int8_storage;
  233. return ret;
  234. }
  235. virtual int destroy_pipeline(const ncnn::Option& opt)
  236. {
  237. return impl->destroy_pipeline(opt);
  238. }
  239. virtual int forward(const ncnn::Mat& bottom_blob, ncnn::Mat& top_blob, const ncnn::Option& opt) const
  240. {
  241. return impl->forward(bottom_blob, top_blob, opt);
  242. }
  243. #if NCNN_VULKAN
  244. virtual int upload_model(ncnn::VkTransfer& cmd, const ncnn::Option& opt)
  245. {
  246. return impl->upload_model(cmd, opt);
  247. }
  248. virtual int forward(const ncnn::VkMat& bottom_blob, ncnn::VkMat& top_blob, ncnn::VkCompute& cmd, const ncnn::Option& opt) const
  249. {
  250. return impl->forward(bottom_blob, top_blob, cmd, opt);
  251. }
  252. #endif // NCNN_VULKAN
  253. private:
  254. ncnn::Layer* impl;
  255. };
  256. DEFINE_LAYER_CREATOR(MyConvolution)
  257. DEFINE_LAYER_DESTROYER(MyConvolution)
  258. static int test_squeezenet_overwrite_softmax(const ncnn::Option& opt, int load_model_type, float epsilon = 0.001)
  259. {
  260. ncnn::Net squeezenet;
  261. squeezenet.opt = opt;
  262. #ifdef __EMSCRIPTEN__
  263. #define MODEL_DIR "/working"
  264. #else
  265. #define MODEL_DIR "../../examples"
  266. #endif
  267. std::string param_str;
  268. ncnn::Mat param_data;
  269. ncnn::Mat model_data;
  270. if (load_model_type == 0)
  271. {
  272. // load from plain model file
  273. squeezenet.register_custom_layer("Convolution", MyConvolution_layer_creator, MyConvolution_layer_destroyer);
  274. squeezenet.load_param(MODEL_DIR "/squeezenet_v1.1.param");
  275. // test random feature disabled bits
  276. {
  277. std::vector<ncnn::Layer*>& layers = squeezenet.mutable_layers();
  278. for (size_t i = 0; i < layers.size(); i++)
  279. {
  280. layers[i]->featmask = i * 11 % 128;
  281. }
  282. }
  283. squeezenet.load_model(MODEL_DIR "/squeezenet_v1.1.bin");
  284. }
  285. if (load_model_type == 1)
  286. {
  287. // load from plain model memory
  288. squeezenet.register_custom_layer("Convolution", MyConvolution_layer_creator, MyConvolution_layer_destroyer);
  289. param_str = read_file_string(MODEL_DIR "/squeezenet_v1.1.param");
  290. model_data = read_file_content(MODEL_DIR "/squeezenet_v1.1.bin");
  291. squeezenet.load_param_mem((const char*)param_str.c_str());
  292. squeezenet.load_model((const unsigned char*)model_data);
  293. }
  294. if (load_model_type == 2)
  295. {
  296. // load from binary model file
  297. squeezenet.register_custom_layer(ncnn::layer_to_index("Convolution"), MyConvolution_layer_creator, MyConvolution_layer_destroyer);
  298. squeezenet.load_param_bin(MODEL_DIR "/squeezenet_v1.1.param.bin");
  299. squeezenet.load_model(MODEL_DIR "/squeezenet_v1.1.bin");
  300. }
  301. if (load_model_type == 3)
  302. {
  303. // load from binary model memory
  304. squeezenet.register_custom_layer(ncnn::layer_to_index("Convolution"), MyConvolution_layer_creator, MyConvolution_layer_destroyer);
  305. param_data = read_file_content(MODEL_DIR "/squeezenet_v1.1.param.bin");
  306. model_data = read_file_content(MODEL_DIR "/squeezenet_v1.1.bin");
  307. squeezenet.load_param((const unsigned char*)param_data);
  308. squeezenet.load_model((const unsigned char*)model_data);
  309. }
  310. ncnn::Mat in = generate_ncnn_logo(ncnn::Mat::PIXEL_BGR, 227, 227);
  311. const float mean_vals[3] = {104.f, 117.f, 123.f};
  312. in.substract_mean_normalize(mean_vals, 0);
  313. ncnn::Extractor ex = squeezenet.create_extractor();
  314. ncnn::Mat out;
  315. if (load_model_type == 0 || load_model_type == 1)
  316. {
  317. ex.input("data", in);
  318. ex.extract("prob", out);
  319. }
  320. if (load_model_type == 2 || load_model_type == 3)
  321. {
  322. ex.input(0, in);
  323. ex.extract(82, out);
  324. }
  325. std::vector<float> cls_scores;
  326. cls_scores.resize(out.w);
  327. for (int j = 0; j < out.w; j++)
  328. {
  329. cls_scores[j] = out[j];
  330. }
  331. return check_top2(cls_scores, epsilon);
  332. }
  333. int main()
  334. {
  335. SRAND(7767517);
  336. #ifdef __EMSCRIPTEN__
  337. EM_ASM(
  338. FS.mkdir('/working');
  339. FS.mount(NODEFS, {root: '../../examples'}, '/working'););
  340. #endif // __EMSCRIPTEN__
  341. ncnn::UnlockedPoolAllocator g_blob_pool_allocator;
  342. ncnn::PoolAllocator g_workspace_pool_allocator;
  343. ncnn::Option opts[4];
  344. opts[0].use_packing_layout = false;
  345. opts[0].use_fp16_packed = false;
  346. opts[0].use_fp16_storage = false;
  347. opts[0].use_fp16_arithmetic = false;
  348. opts[0].use_shader_pack8 = false;
  349. opts[1].use_packing_layout = true;
  350. opts[1].use_fp16_packed = true;
  351. opts[1].use_fp16_storage = false;
  352. opts[1].use_fp16_arithmetic = false;
  353. opts[1].use_shader_pack8 = true;
  354. opts[2].use_packing_layout = true;
  355. opts[2].use_fp16_packed = true;
  356. opts[2].use_fp16_storage = true;
  357. opts[2].use_fp16_arithmetic = false;
  358. opts[2].use_bf16_storage = false; // FIXME enable me
  359. opts[2].use_shader_pack8 = true;
  360. opts[2].blob_allocator = &g_blob_pool_allocator;
  361. opts[2].workspace_allocator = &g_workspace_pool_allocator;
  362. opts[3].use_packing_layout = true;
  363. opts[3].use_fp16_packed = true;
  364. opts[3].use_fp16_storage = true;
  365. opts[3].use_fp16_arithmetic = false; // FIXME enable me
  366. opts[3].use_bf16_storage = false;
  367. opts[3].use_shader_pack8 = true;
  368. opts[3].blob_allocator = &g_blob_pool_allocator;
  369. opts[3].workspace_allocator = &g_workspace_pool_allocator;
  370. int load_model_types[4] = {0, 1, 2, 3};
  371. for (int i = 0; i < 4; i++)
  372. {
  373. opts[i].num_threads = 1;
  374. }
  375. for (int i = 0; i < 4; i++)
  376. {
  377. const ncnn::Option& opt = opts[i];
  378. float epsilon;
  379. if (opt.use_bf16_storage || opt.use_fp16_packed || opt.use_fp16_storage)
  380. {
  381. epsilon = 0.1;
  382. }
  383. else
  384. {
  385. epsilon = 0.01;
  386. }
  387. int ret;
  388. ncnn::Option opt_cpu = opt;
  389. opt_cpu.use_vulkan_compute = false;
  390. ret = test_squeezenet(opt_cpu, load_model_types[i], epsilon);
  391. if (ret != 0)
  392. {
  393. 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\n", opt.use_packing_layout, opt.use_fp16_packed, opt.use_fp16_storage, opt.use_shader_pack8, opt.use_bf16_storage);
  394. return ret;
  395. }
  396. #if NCNN_VULKAN
  397. ncnn::Option opt_gpu = opt;
  398. opt_gpu.use_vulkan_compute = true;
  399. ret = test_squeezenet(opt_gpu, load_model_types[i], epsilon);
  400. if (ret != 0)
  401. {
  402. 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\n", opt.use_packing_layout, opt.use_fp16_packed, opt.use_fp16_storage, opt.use_shader_pack8, opt.use_bf16_storage);
  403. return ret;
  404. }
  405. #endif // NCNN_VULKAN
  406. ret = test_squeezenet_overwrite_softmax(opt_cpu, load_model_types[i], epsilon);
  407. if (ret != 0)
  408. {
  409. fprintf(stderr, "test_squeezenet_overwrite_softmax cpu failed use_packing_layout=%d use_fp16_packed=%d use_fp16_storage=%d use_shader_pack8=%d use_bf16_storage=%d\n", opt.use_packing_layout, opt.use_fp16_packed, opt.use_fp16_storage, opt.use_shader_pack8, opt.use_bf16_storage);
  410. return ret;
  411. }
  412. #if NCNN_VULKAN
  413. ret = test_squeezenet_overwrite_softmax(opt_gpu, load_model_types[i], epsilon);
  414. if (ret != 0)
  415. {
  416. fprintf(stderr, "test_squeezenet_overwrite_softmax gpu failed use_packing_layout=%d use_fp16_packed=%d use_fp16_storage=%d use_shader_pack8=%d use_bf16_storage=%d\n", opt.use_packing_layout, opt.use_fp16_packed, opt.use_fp16_storage, opt.use_shader_pack8, opt.use_bf16_storage);
  417. return ret;
  418. }
  419. #endif // NCNN_VULKAN
  420. }
  421. return 0;
  422. }