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

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