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.

net.cpp 19 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 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 "net.h"
  15. #include <stdio.h>
  16. #include <string.h>
  17. #ifdef _OPENMP
  18. #include <omp.h>
  19. #endif // _OPENMP
  20. namespace ncnn {
  21. Net::Net()
  22. {
  23. }
  24. Net::~Net()
  25. {
  26. clear();
  27. }
  28. #if NCNN_STRING
  29. int Net::register_custom_layer(const char* type, layer_creator_func creator)
  30. {
  31. int typeindex = layer_to_index(type);
  32. if (typeindex != 0)
  33. {
  34. fprintf(stderr, "can not register build-in layer type %s\n", type);
  35. return -1;
  36. }
  37. int custom_index = custom_layer_to_index(type);
  38. if (custom_index == -1)
  39. {
  40. struct layer_registry_entry entry = { type, creator };
  41. custom_layer_registry.push_back(entry);
  42. }
  43. else
  44. {
  45. fprintf(stderr, "overwrite existing custom layer type %s\n", type);
  46. custom_layer_registry[custom_index].name = type;
  47. custom_layer_registry[custom_index].creator = creator;
  48. }
  49. return 0;
  50. }
  51. #endif // NCNN_STRING
  52. int Net::register_custom_layer(int index, layer_creator_func creator)
  53. {
  54. int custom_index = index & ~LayerType::CustomBit;
  55. if (index == custom_index)
  56. {
  57. fprintf(stderr, "can not register build-in layer index %d\n", custom_index);
  58. return -1;
  59. }
  60. if ((int)custom_layer_registry.size() <= custom_index)
  61. {
  62. #if NCNN_STRING
  63. struct layer_registry_entry dummy = { "", 0 };
  64. #else
  65. struct layer_registry_entry dummy = { 0 };
  66. #endif // NCNN_STRING
  67. custom_layer_registry.resize(custom_index + 1, dummy);
  68. }
  69. if (custom_layer_registry[custom_index].creator)
  70. {
  71. fprintf(stderr, "overwrite existing custom layer index %d\n", custom_index);
  72. }
  73. custom_layer_registry[custom_index].creator = creator;
  74. return 0;
  75. }
  76. #if NCNN_STDIO
  77. #if NCNN_STRING
  78. int Net::load_param(FILE* fp)
  79. {
  80. // parse
  81. int layer_count = 0;
  82. int blob_count = 0;
  83. fscanf(fp, "%d %d", &layer_count, &blob_count);
  84. layers.resize(layer_count);
  85. blobs.resize(blob_count);
  86. int layer_index = 0;
  87. int blob_index = 0;
  88. while (!feof(fp))
  89. {
  90. int nscan = 0;
  91. char layer_type[256];
  92. char layer_name[256];
  93. int bottom_count = 0;
  94. int top_count = 0;
  95. nscan = fscanf(fp, "%256s %256s %d %d", layer_type, layer_name, &bottom_count, &top_count);
  96. if (nscan != 4)
  97. {
  98. continue;
  99. }
  100. int typeindex = layer_to_index(layer_type);
  101. Layer* layer = create_layer(typeindex);
  102. if (!layer)
  103. {
  104. typeindex = custom_layer_to_index(layer_type);
  105. layer = create_custom_layer(typeindex);
  106. }
  107. layer->type = std::string(layer_type);
  108. layer->name = std::string(layer_name);
  109. // fprintf(stderr, "new layer %d %s\n", layer_index, layer_name);
  110. layer->bottoms.resize(bottom_count);
  111. for (int i=0; i<bottom_count; i++)
  112. {
  113. char bottom_name[256];
  114. nscan = fscanf(fp, "%256s", bottom_name);
  115. if (nscan != 1)
  116. {
  117. continue;
  118. }
  119. int bottom_blob_index = find_blob_index_by_name(bottom_name);
  120. if (bottom_blob_index == -1)
  121. {
  122. Blob& blob = blobs[blob_index];
  123. bottom_blob_index = blob_index;
  124. blob.name = std::string(bottom_name);
  125. // fprintf(stderr, "new blob %s\n", bottom_name);
  126. blob_index++;
  127. }
  128. Blob& blob = blobs[bottom_blob_index];
  129. blob.consumers.push_back(layer_index);
  130. layer->bottoms[i] = bottom_blob_index;
  131. }
  132. layer->tops.resize(top_count);
  133. for (int i=0; i<top_count; i++)
  134. {
  135. Blob& blob = blobs[blob_index];
  136. char blob_name[256];
  137. nscan = fscanf(fp, "%256s", blob_name);
  138. if (nscan != 1)
  139. {
  140. continue;
  141. }
  142. blob.name = std::string(blob_name);
  143. // fprintf(stderr, "new blob %s\n", blob_name);
  144. blob.producer = layer_index;
  145. layer->tops[i] = blob_index;
  146. blob_index++;
  147. }
  148. // layer specific params
  149. int lr = layer->load_param(fp);
  150. if (lr != 0)
  151. {
  152. fprintf(stderr, "layer load_param failed\n");
  153. continue;
  154. }
  155. layers[layer_index] = layer;
  156. layer_index++;
  157. }
  158. return 0;
  159. }
  160. int Net::load_param(const char* protopath)
  161. {
  162. FILE* fp = fopen(protopath, "rb");
  163. if (!fp)
  164. {
  165. fprintf(stderr, "fopen %s failed\n", protopath);
  166. return -1;
  167. }
  168. int ret = load_param(fp);
  169. fclose(fp);
  170. return ret;
  171. }
  172. #endif // NCNN_STRING
  173. int Net::load_param_bin(FILE* fp)
  174. {
  175. int layer_count = 0;
  176. fread(&layer_count, sizeof(int), 1, fp);
  177. int blob_count = 0;
  178. fread(&blob_count, sizeof(int), 1, fp);
  179. layers.resize(layer_count);
  180. blobs.resize(blob_count);
  181. for (int i=0; i<layer_count; i++)
  182. {
  183. int typeindex;
  184. fread(&typeindex, sizeof(int), 1, fp);
  185. int bottom_count;
  186. fread(&bottom_count, sizeof(int), 1, fp);
  187. int top_count;
  188. fread(&top_count, sizeof(int), 1, fp);
  189. Layer* layer = create_layer(typeindex);
  190. if (!layer)
  191. {
  192. int custom_index = typeindex & ~LayerType::CustomBit;
  193. layer = create_custom_layer(custom_index);
  194. }
  195. // layer->type = std::string(layer_type);
  196. // layer->name = std::string(layer_name);
  197. // fprintf(stderr, "new layer %d\n", typeindex);
  198. layer->bottoms.resize(bottom_count);
  199. for (int j=0; j<bottom_count; j++)
  200. {
  201. int bottom_blob_index;
  202. fread(&bottom_blob_index, sizeof(int), 1, fp);
  203. Blob& blob = blobs[bottom_blob_index];
  204. blob.consumers.push_back(i);
  205. layer->bottoms[j] = bottom_blob_index;
  206. }
  207. layer->tops.resize(top_count);
  208. for (int j=0; j<top_count; j++)
  209. {
  210. int top_blob_index;
  211. fread(&top_blob_index, sizeof(int), 1, fp);
  212. Blob& blob = blobs[top_blob_index];
  213. // blob.name = std::string(blob_name);
  214. // fprintf(stderr, "new blob %s\n", blob_name);
  215. blob.producer = i;
  216. layer->tops[j] = top_blob_index;
  217. }
  218. // layer specific params
  219. int lr = layer->load_param_bin(fp);
  220. if (lr != 0)
  221. {
  222. fprintf(stderr, "layer load_param failed\n");
  223. continue;
  224. }
  225. layers[i] = layer;
  226. }
  227. return 0;
  228. }
  229. int Net::load_param_bin(const char* protopath)
  230. {
  231. FILE* fp = fopen(protopath, "rb");
  232. if (!fp)
  233. {
  234. fprintf(stderr, "fopen %s failed\n", protopath);
  235. return -1;
  236. }
  237. int ret = load_param_bin(fp);
  238. fclose(fp);
  239. return ret;
  240. }
  241. int Net::load_model(FILE* fp)
  242. {
  243. // load file
  244. int ret = 0;
  245. for (size_t i=0; i<layers.size(); i++)
  246. {
  247. Layer* layer = layers[i];
  248. int lret = layer->load_model(fp);
  249. if (lret != 0)
  250. {
  251. fprintf(stderr, "layer load_model %d failed\n", (int)i);
  252. ret = -1;
  253. break;
  254. }
  255. }
  256. return ret;
  257. }
  258. int Net::load_model(const char* modelpath)
  259. {
  260. FILE* fp = fopen(modelpath, "rb");
  261. if (!fp)
  262. {
  263. fprintf(stderr, "fopen %s failed\n", modelpath);
  264. return -1;
  265. }
  266. int ret = load_model(fp);
  267. fclose(fp);
  268. return ret;
  269. }
  270. #endif // NCNN_STDIO
  271. int Net::load_param(const unsigned char* _mem)
  272. {
  273. if ((unsigned long)_mem & 0x3)
  274. {
  275. // reject unaligned memory
  276. fprintf(stderr, "memory not 32-bit aligned at %p\n", _mem);
  277. return 0;
  278. }
  279. const unsigned char* mem = _mem;
  280. int layer_count = *(int*)(mem);
  281. mem += 4;
  282. int blob_count = *(int*)(mem);
  283. mem += 4;
  284. layers.resize(layer_count);
  285. blobs.resize(blob_count);
  286. for (int i=0; i<layer_count; i++)
  287. {
  288. int typeindex = *(int*)mem;
  289. mem += 4;
  290. int bottom_count = *(int*)mem;
  291. mem += 4;
  292. int top_count = *(int*)mem;
  293. mem += 4;
  294. Layer* layer = create_layer(typeindex);
  295. if (!layer)
  296. {
  297. int custom_index = typeindex & ~LayerType::CustomBit;
  298. layer = create_custom_layer(custom_index);
  299. }
  300. // layer->type = std::string(layer_type);
  301. // layer->name = std::string(layer_name);
  302. // fprintf(stderr, "new layer %d\n", typeindex);
  303. layer->bottoms.resize(bottom_count);
  304. for (int j=0; j<bottom_count; j++)
  305. {
  306. int bottom_blob_index = *(int*)mem;
  307. mem += 4;
  308. Blob& blob = blobs[bottom_blob_index];
  309. blob.consumers.push_back(i);
  310. layer->bottoms[j] = bottom_blob_index;
  311. }
  312. layer->tops.resize(top_count);
  313. for (int j=0; j<top_count; j++)
  314. {
  315. int top_blob_index = *(int*)mem;
  316. mem += 4;
  317. Blob& blob = blobs[top_blob_index];
  318. // blob.name = std::string(blob_name);
  319. // fprintf(stderr, "new blob %s\n", blob_name);
  320. blob.producer = i;
  321. layer->tops[j] = top_blob_index;
  322. }
  323. // layer specific params
  324. int lr = layer->load_param(mem);
  325. if (lr != 0)
  326. {
  327. fprintf(stderr, "layer load_param failed\n");
  328. continue;
  329. }
  330. layers[i] = layer;
  331. }
  332. return mem - _mem;
  333. }
  334. int Net::load_model(const unsigned char* _mem)
  335. {
  336. if ((unsigned long)_mem & 0x3)
  337. {
  338. // reject unaligned memory
  339. fprintf(stderr, "memory not 32-bit aligned at %p\n", _mem);
  340. return 0;
  341. }
  342. const unsigned char* mem = _mem;
  343. for (size_t i=0; i<layers.size(); i++)
  344. {
  345. Layer* layer = layers[i];
  346. int lret = layer->load_model(mem);
  347. if (lret != 0)
  348. {
  349. fprintf(stderr, "layer load_model failed\n");
  350. return -1;
  351. }
  352. }
  353. return mem - _mem;
  354. }
  355. void Net::clear()
  356. {
  357. blobs.clear();
  358. for (size_t i=0; i<layers.size(); i++)
  359. {
  360. delete layers[i];
  361. }
  362. layers.clear();
  363. }
  364. Extractor Net::create_extractor() const
  365. {
  366. return Extractor(this, blobs.size());
  367. }
  368. #if NCNN_STRING
  369. int Net::find_blob_index_by_name(const char* name) const
  370. {
  371. for (size_t i=0; i<blobs.size(); i++)
  372. {
  373. const Blob& blob = blobs[i];
  374. if (blob.name == name)
  375. {
  376. return i;
  377. }
  378. }
  379. fprintf(stderr, "find_blob_index_by_name %s failed\n", name);
  380. return -1;
  381. }
  382. int Net::find_layer_index_by_name(const char* name) const
  383. {
  384. for (size_t i=0; i<layers.size(); i++)
  385. {
  386. const Layer* layer = layers[i];
  387. if (layer->name == name)
  388. {
  389. return i;
  390. }
  391. }
  392. fprintf(stderr, "find_layer_index_by_name %s failed\n", name);
  393. return -1;
  394. }
  395. int Net::custom_layer_to_index(const char* type)
  396. {
  397. const int custom_layer_registry_entry_count = custom_layer_registry.size();
  398. for (int i=0; i<custom_layer_registry_entry_count; i++)
  399. {
  400. if (strcmp(type, custom_layer_registry[i].name) == 0)
  401. {
  402. return i;
  403. }
  404. }
  405. fprintf(stderr, "custom layer %s not exists\n", type);
  406. return -1;
  407. }
  408. #endif // NCNN_STRING
  409. Layer* Net::create_custom_layer(int index)
  410. {
  411. const int custom_layer_registry_entry_count = custom_layer_registry.size();
  412. if (index < 0 || index >= custom_layer_registry_entry_count)
  413. {
  414. fprintf(stderr, "custom layer index %d not exists\n", index);
  415. return 0;
  416. }
  417. layer_creator_func layer_creator = custom_layer_registry[index].creator;
  418. return layer_creator();
  419. }
  420. int Net::forward_layer(int layer_index, std::vector<Mat>& blob_mats, bool lightmode) const
  421. {
  422. const Layer* layer = layers[layer_index];
  423. // fprintf(stderr, "forward_layer %d %s\n", layer_index, layer->name.c_str());
  424. if (layer->one_blob_only)
  425. {
  426. // load bottom blob
  427. int bottom_blob_index = layer->bottoms[0];
  428. int top_blob_index = layer->tops[0];
  429. if (blob_mats[bottom_blob_index].dims == 0)
  430. {
  431. int ret = forward_layer(blobs[bottom_blob_index].producer, blob_mats, lightmode);
  432. if (ret != 0)
  433. return ret;
  434. }
  435. Mat bottom_blob = blob_mats[bottom_blob_index];
  436. if (lightmode)
  437. {
  438. // delete after taken in light mode
  439. blob_mats[bottom_blob_index].release();
  440. // deep copy for inplace forward if data is shared
  441. if (layer->support_inplace && *bottom_blob.refcount != 1)
  442. {
  443. bottom_blob = bottom_blob.clone();
  444. }
  445. }
  446. // forward
  447. if (lightmode && layer->support_inplace)
  448. {
  449. Mat& bottom_top_blob = bottom_blob;
  450. int ret = layer->forward_inplace(bottom_top_blob);
  451. if (ret != 0)
  452. return ret;
  453. // store top blob
  454. blob_mats[top_blob_index] = bottom_top_blob;
  455. }
  456. else
  457. {
  458. Mat top_blob;
  459. int ret = layer->forward(bottom_blob, top_blob);
  460. if (ret != 0)
  461. return ret;
  462. // store top blob
  463. blob_mats[top_blob_index] = top_blob;
  464. }
  465. }
  466. else
  467. {
  468. // load bottom blobs
  469. std::vector<Mat> bottom_blobs;
  470. bottom_blobs.resize(layer->bottoms.size());
  471. for (size_t i=0; i<layer->bottoms.size(); i++)
  472. {
  473. int bottom_blob_index = layer->bottoms[i];
  474. if (blob_mats[bottom_blob_index].dims == 0)
  475. {
  476. int ret = forward_layer(blobs[bottom_blob_index].producer, blob_mats, lightmode);
  477. if (ret != 0)
  478. return ret;
  479. }
  480. bottom_blobs[i] = blob_mats[bottom_blob_index];
  481. if (lightmode)
  482. {
  483. // delete after taken in light mode
  484. blob_mats[bottom_blob_index].release();
  485. // deep copy for inplace forward if data is shared
  486. if (layer->support_inplace && *bottom_blobs[i].refcount != 1)
  487. {
  488. bottom_blobs[i] = bottom_blobs[i].clone();
  489. }
  490. }
  491. }
  492. // forward
  493. if (lightmode && layer->support_inplace)
  494. {
  495. std::vector<Mat>& bottom_top_blobs = bottom_blobs;
  496. int ret = layer->forward_inplace(bottom_top_blobs);
  497. if (ret != 0)
  498. return ret;
  499. // store top blobs
  500. for (size_t i=0; i<layer->tops.size(); i++)
  501. {
  502. int top_blob_index = layer->tops[i];
  503. blob_mats[top_blob_index] = bottom_top_blobs[i];
  504. }
  505. }
  506. else
  507. {
  508. std::vector<Mat> top_blobs;
  509. top_blobs.resize(layer->tops.size());
  510. int ret = layer->forward(bottom_blobs, top_blobs);
  511. if (ret != 0)
  512. return ret;
  513. // store top blobs
  514. for (size_t i=0; i<layer->tops.size(); i++)
  515. {
  516. int top_blob_index = layer->tops[i];
  517. blob_mats[top_blob_index] = top_blobs[i];
  518. }
  519. }
  520. }
  521. // fprintf(stderr, "forward_layer %d %s done\n", layer_index, layer->name.c_str());
  522. // const Mat& blob = blob_mats[layer->tops[0]];
  523. // fprintf(stderr, "[%-2d %-16s %-16s] %d blobs count = %-3d size = %-3d x %-3d\n", layer_index, layer->type.c_str(), layer->name.c_str(), layer->tops[0], blob.c, blob.h, blob.w);
  524. return 0;
  525. }
  526. Extractor::Extractor(const Net* _net, int blob_count) : net(_net)
  527. {
  528. blob_mats.resize(blob_count);
  529. lightmode = false;
  530. num_threads = 0;
  531. }
  532. void Extractor::set_light_mode(bool enable)
  533. {
  534. lightmode = enable;
  535. }
  536. void Extractor::set_num_threads(int _num_threads)
  537. {
  538. num_threads = _num_threads;
  539. }
  540. int Extractor::input(int blob_index, const Mat& in)
  541. {
  542. if (blob_index < 0 || blob_index >= (int)blob_mats.size())
  543. return -1;
  544. blob_mats[blob_index] = in;
  545. return 0;
  546. }
  547. int Extractor::extract(int blob_index, Mat& feat)
  548. {
  549. if (blob_index < 0 || blob_index >= (int)blob_mats.size())
  550. return -1;
  551. int ret = 0;
  552. if (blob_mats[blob_index].dims == 0)
  553. {
  554. int layer_index = net->blobs[blob_index].producer;
  555. #ifdef _OPENMP
  556. int dynamic_current = 0;
  557. int num_threads_current = 1;
  558. if (num_threads)
  559. {
  560. dynamic_current = omp_get_dynamic();
  561. num_threads_current = omp_get_num_threads();
  562. omp_set_dynamic(0);
  563. omp_set_num_threads(num_threads);
  564. }
  565. #endif
  566. ret = net->forward_layer(layer_index, blob_mats, lightmode);
  567. #ifdef _OPENMP
  568. if (num_threads)
  569. {
  570. omp_set_dynamic(dynamic_current);
  571. omp_set_num_threads(num_threads_current);
  572. }
  573. #endif
  574. }
  575. feat = blob_mats[blob_index];
  576. return ret;
  577. }
  578. #if NCNN_STRING
  579. int Extractor::input(const char* blob_name, const Mat& in)
  580. {
  581. int blob_index = net->find_blob_index_by_name(blob_name);
  582. if (blob_index == -1)
  583. return -1;
  584. blob_mats[blob_index] = in;
  585. return 0;
  586. }
  587. int Extractor::extract(const char* blob_name, Mat& feat)
  588. {
  589. int blob_index = net->find_blob_index_by_name(blob_name);
  590. if (blob_index == -1)
  591. return -1;
  592. int ret = 0;
  593. if (blob_mats[blob_index].dims == 0)
  594. {
  595. int layer_index = net->blobs[blob_index].producer;
  596. #ifdef _OPENMP
  597. int dynamic_current = 0;
  598. int num_threads_current = 1;
  599. if (num_threads)
  600. {
  601. dynamic_current = omp_get_dynamic();
  602. num_threads_current = omp_get_num_threads();
  603. omp_set_dynamic(0);
  604. omp_set_num_threads(num_threads);
  605. }
  606. #endif
  607. ret = net->forward_layer(layer_index, blob_mats, lightmode);
  608. #ifdef _OPENMP
  609. if (num_threads)
  610. {
  611. omp_set_dynamic(dynamic_current);
  612. omp_set_num_threads(num_threads_current);
  613. }
  614. #endif
  615. }
  616. feat = blob_mats[blob_index];
  617. return ret;
  618. }
  619. #endif // NCNN_STRING
  620. } // namespace ncnn