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.

mxnet2ncnn.cpp 28 kB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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 <stdio.h>
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include <map>
  18. #include <set>
  19. #include <string>
  20. #include <vector>
  21. class MXNetParam;
  22. class MXNetNode
  23. {
  24. public:
  25. bool has_attr(const char* key) const;
  26. class AttrProxy
  27. {
  28. MXNetNode const* _n;
  29. const char* const _key;
  30. public:
  31. AttrProxy( MXNetNode const* n, const char* key ) : _n(n), _key(key) {}
  32. operator int() const { return _n->attr_i(_key); }
  33. operator float() const { return _n->attr_f(_key); }
  34. operator std::string() const { return _n->attr_s(_key); }
  35. operator std::vector<int>() const { return _n->attr_ai(_key); }
  36. };
  37. AttrProxy attr(const char* key) const { return AttrProxy(this, key); }
  38. int attr_i(const char* key) const;
  39. float attr_f(const char* key) const;
  40. std::string attr_s(const char* key) const;
  41. std::vector<int> attr_ai(const char* key) const;
  42. public:
  43. bool is_weight() const;
  44. bool has_weight(int i) const;
  45. std::vector<float> weight(int i, int init_len = 0) const;
  46. std::vector<MXNetNode>* nodes;// reference
  47. std::vector<MXNetParam>* params;// reference
  48. public:
  49. std::string op;
  50. std::string name;
  51. std::map<std::string, std::string> attrs;
  52. std::vector<int> inputs;
  53. std::vector<int> weights;
  54. };
  55. class MXNetParam
  56. {
  57. public:
  58. std::string name;
  59. std::vector<float> data;
  60. std::string init;
  61. };
  62. bool MXNetNode::has_attr(const char* key) const
  63. {
  64. const std::map<std::string, std::string>::const_iterator it = attrs.find(key);
  65. return it != attrs.end();
  66. }
  67. int MXNetNode::attr_i(const char* key) const
  68. {
  69. const std::map<std::string, std::string>::const_iterator it = attrs.find(key);
  70. if (it == attrs.end())
  71. return 0;
  72. if (it->second == "False")
  73. return 0;
  74. if (it->second == "True")
  75. return 1;
  76. int i = 0;
  77. int nscan = sscanf(it->second.c_str(), "%d", &i);
  78. if (nscan != 1)
  79. return 0;
  80. return i;
  81. }
  82. float MXNetNode::attr_f(const char* key) const
  83. {
  84. const std::map<std::string, std::string>::const_iterator it = attrs.find(key);
  85. if (it == attrs.end())
  86. return 0.f;
  87. float f = 0;
  88. int nscan = sscanf(it->second.c_str(), "%f", &f);
  89. if (nscan != 1)
  90. return 0.f;
  91. return f;
  92. }
  93. std::string MXNetNode::attr_s(const char* key) const
  94. {
  95. const std::map<std::string, std::string>::const_iterator it = attrs.find(key);
  96. if (it == attrs.end())
  97. return std::string();
  98. return it->second;
  99. }
  100. std::vector<int> MXNetNode::attr_ai(const char* key) const
  101. {
  102. const std::map<std::string, std::string>::const_iterator it = attrs.find(key);
  103. if (it == attrs.end())
  104. return std::vector<int>();
  105. // (1,2,3)
  106. std::vector<int> list;
  107. int i = 0;
  108. int c = 0;
  109. int nconsumed = 0;
  110. int nscan = sscanf(it->second.c_str() + c, "%*[(,]%d%n", &i, &nconsumed);
  111. while (nscan == 1)
  112. {
  113. list.push_back(i);
  114. // fprintf(stderr, "%d\n", i);
  115. i = 0;
  116. c += nconsumed;
  117. nscan = sscanf(it->second.c_str() + c, "%*[(,]%d%n", &i, &nconsumed);
  118. }
  119. return list;
  120. }
  121. bool MXNetNode::is_weight() const
  122. {
  123. for (int i=0; i<(int)(*params).size(); i++)
  124. {
  125. const MXNetParam& p = (*params)[i];
  126. if (p.name == name)
  127. return true;
  128. }
  129. return false;
  130. }
  131. bool MXNetNode::has_weight(int i) const
  132. {
  133. if (i < 0 || i >= (int)weights.size())
  134. return false;
  135. const std::string& name = (*nodes)[ weights[i] ].name;
  136. for (int i=0; i<(int)(*params).size(); i++)
  137. {
  138. const MXNetParam& p = (*params)[i];
  139. if (p.name == name)
  140. return true;
  141. }
  142. return false;
  143. }
  144. std::vector<float> MXNetNode::weight(int i, int init_len) const
  145. {
  146. if (i < 0 || i >= (int)weights.size())
  147. return std::vector<float>();
  148. const std::string& name = (*nodes)[ weights[i] ].name;
  149. for (int i=0; i<(int)(*params).size(); i++)
  150. {
  151. const MXNetParam& p = (*params)[i];
  152. if (p.name != name)
  153. continue;
  154. if (!p.data.empty())
  155. return p.data;
  156. std::vector<float> data;
  157. if (!p.init.empty() && init_len != 0)
  158. {
  159. if (p.init == "[\\$zero\\$, {}]")
  160. {
  161. data.resize(init_len, 0.f);
  162. }
  163. else if (p.init == "[\\$one\\$, {}]")
  164. {
  165. data.resize(init_len, 1.f);
  166. }
  167. }
  168. return data;
  169. }
  170. return std::vector<float>();
  171. }
  172. static void replace_backslash_doublequote_dollar(char* s)
  173. {
  174. char* a = s;
  175. char* b = s+1;
  176. while (*a && *b)
  177. {
  178. if (*a == '\\' && *b == '\"')
  179. {
  180. *b = '$';
  181. }
  182. a++;
  183. b++;
  184. }
  185. }
  186. static std::vector<int> parse_input_list(const char* s)
  187. {
  188. std::vector<int> inputs;
  189. if (memcmp(s, "[]", 2) == 0)
  190. return inputs;
  191. int nscan = 0;
  192. int nconsumed = 0;
  193. int id;
  194. int c = 1;// skip leading [
  195. nscan = sscanf(s + c, "[%d, %*[^]]]%n", &id, &nconsumed);
  196. while (nscan == 1)
  197. {
  198. inputs.push_back(id);
  199. // fprintf(stderr, "%d\n", id);
  200. c += nconsumed;
  201. nscan = sscanf(s + c, "%*[^[][%d, %*[^]]]%n", &id, &nconsumed);
  202. }
  203. return inputs;
  204. }
  205. static bool read_mxnet_json(const char* jsonpath, std::vector<MXNetNode>& nodes)
  206. {
  207. FILE* fp = fopen(jsonpath, "rb");
  208. if (!fp)
  209. {
  210. fprintf(stderr, "fopen %s failed\n", jsonpath);
  211. return false;
  212. }
  213. int internal_unknown = 0;
  214. char line[1024];
  215. //{
  216. fgets(line, 1024, fp);
  217. MXNetNode n;
  218. bool in_nodes_list = false;
  219. bool in_node_block = false;
  220. bool in_attr_block = false;
  221. while (!feof(fp))
  222. {
  223. char* s = fgets(line, 1024, fp);
  224. if (!s)
  225. break;
  226. if (in_attr_block)
  227. {
  228. // },
  229. if (memcmp(line, " }", 7) == 0)
  230. {
  231. in_attr_block = false;
  232. continue;
  233. }
  234. // replace \" with \$
  235. replace_backslash_doublequote_dollar(line);
  236. // "kernel": "(7,7)",
  237. char key[256] = {0};
  238. char value[256] = {0};
  239. int nscan = sscanf(line, " \"%255[^\"]\": \"%255[^\"]\"", key, value);
  240. if (nscan == 2)
  241. {
  242. n.attrs[key] = value;
  243. // fprintf(stderr, "# %s = %s\n", key, value);
  244. continue;
  245. }
  246. }
  247. if (in_node_block)
  248. {
  249. // },
  250. if (memcmp(line, " }", 5) == 0)
  251. {
  252. // new node
  253. if (n.name.empty())
  254. {
  255. // assign default unknown name
  256. char unknownname[256];
  257. sprintf(unknownname, "unknownncnn_%d", internal_unknown);
  258. n.name = unknownname;
  259. internal_unknown++;
  260. }
  261. nodes.push_back(n);
  262. in_node_block = false;
  263. continue;
  264. }
  265. int nscan;
  266. // "op": "Convolution",
  267. char op[256] = {0};
  268. nscan = sscanf(line, " \"op\": \"%255[^\"]\",", op);
  269. if (nscan == 1)
  270. {
  271. n.op = op;
  272. // fprintf(stderr, "op = %s\n", op);
  273. continue;
  274. }
  275. // "name": "conv0",
  276. char name[256] = {0};
  277. nscan = sscanf(line, " \"name\": \"%255[^\"]\",", name);
  278. if (nscan == 1)
  279. {
  280. n.name = name;
  281. // fprintf(stderr, "name = %s\n", name);
  282. continue;
  283. }
  284. // "inputs": []
  285. char inputs[256] = {0};
  286. nscan = sscanf(line, " \"inputs\": %255[^\n]", inputs);
  287. if (nscan == 1)
  288. {
  289. n.inputs = parse_input_list(inputs);
  290. // fprintf(stderr, "inputs = %s\n", inputs);
  291. continue;
  292. }
  293. // "param": {},
  294. if (memcmp(line, " \"param\": {}", 17) == 0)
  295. {
  296. continue;
  297. }
  298. // replace \" with \$
  299. replace_backslash_doublequote_dollar(line);
  300. // "attr": {"__init__": "[\"zero\", {}]"},
  301. char key[256] = {0};
  302. char value[256] = {0};
  303. nscan = sscanf(line, " \"attr\": {\"%255[^\"]\": \"%255[^\"]\"}", key, value);
  304. if (nscan == 2)
  305. {
  306. n.attrs[key] = value;
  307. // fprintf(stderr, "# %s = %s\n", key, value);
  308. continue;
  309. }
  310. // "param": {"p": "0.5"},
  311. nscan = sscanf(line, " \"param\": {\"%255[^\"]\": \"%255[^\"]\"}", key, value);
  312. if (nscan == 2)
  313. {
  314. n.attrs[key] = value;
  315. // fprintf(stderr, "# %s = %s\n", key, value);
  316. continue;
  317. }
  318. // "attr": {
  319. if (memcmp(line, " \"attr\": {", 15) == 0)
  320. {
  321. in_attr_block = true;
  322. continue;
  323. }
  324. // "param": {
  325. if (memcmp(line, " \"param\": {", 16) == 0)
  326. {
  327. in_attr_block = true;
  328. continue;
  329. }
  330. }
  331. if (in_nodes_list)
  332. {
  333. // ],
  334. if (memcmp(line, " ],", 4) == 0)
  335. {
  336. in_nodes_list = false;
  337. // all nodes parsed
  338. break;
  339. }
  340. // {
  341. if (memcmp(line, " {", 5) == 0)
  342. {
  343. n = MXNetNode();
  344. in_node_block = true;
  345. continue;
  346. }
  347. }
  348. // "nodes": [
  349. if (memcmp(line, " \"nodes\": [", 12) == 0)
  350. {
  351. in_nodes_list = true;
  352. continue;
  353. }
  354. }
  355. fclose(fp);
  356. return true;
  357. }
  358. static bool read_mxnet_param(const char* parampath, std::vector<MXNetParam>& params)
  359. {
  360. FILE* fp = fopen(parampath, "rb");
  361. if (!fp)
  362. {
  363. fprintf(stderr, "fopen %s failed\n", parampath);
  364. return false;
  365. }
  366. uint64_t header;
  367. uint64_t reserved;
  368. fread(&header, 1, sizeof(uint64_t), fp);
  369. fread(&reserved, 1, sizeof(uint64_t), fp);
  370. // NDArray vec
  371. // each data
  372. uint64_t data_count;
  373. fread(&data_count, 1, sizeof(uint64_t), fp);
  374. // fprintf(stderr, "data count = %d\n", (int)data_count);
  375. for (int i = 0; i < (int)data_count; i++)
  376. {
  377. uint32_t magic;// 0xF993FAC9
  378. fread(&magic, 1, sizeof(uint32_t), fp);
  379. // shape
  380. uint32_t ndim;
  381. std::vector<int64_t> shape;
  382. if (magic == 0xF993FAC9)
  383. {
  384. int32_t stype;
  385. fread(&stype, 1, sizeof(int32_t), fp);
  386. fread(&ndim, 1, sizeof(uint32_t), fp);
  387. shape.resize(ndim);
  388. fread(&shape[0], 1, ndim * sizeof(int64_t), fp);
  389. }
  390. else if (magic == 0xF993FAC8)
  391. {
  392. fread(&ndim, 1, sizeof(uint32_t), fp);
  393. shape.resize(ndim);
  394. fread(&shape[0], 1, ndim * sizeof(int64_t), fp);
  395. }
  396. else
  397. {
  398. ndim = magic;
  399. shape.resize(ndim);
  400. std::vector<uint32_t> shape32;
  401. shape32.resize(ndim);
  402. fread(&shape32[0], 1, ndim * sizeof(uint32_t), fp);
  403. for (int j=0; j<(int)ndim; j++)
  404. {
  405. shape[j] = shape32[j];
  406. }
  407. }
  408. // context
  409. int32_t dev_type;
  410. int32_t dev_id;
  411. fread(&dev_type, 1, sizeof(int32_t), fp);
  412. fread(&dev_id, 1, sizeof(int32_t), fp);
  413. int32_t type_flag;
  414. fread(&type_flag, 1, sizeof(int32_t), fp);
  415. // data
  416. size_t len = 0;
  417. if (shape.size() == 1) len = shape[0];
  418. if (shape.size() == 2) len = shape[0] * shape[1];
  419. if (shape.size() == 3) len = shape[0] * shape[1] * shape[2];
  420. if (shape.size() == 4) len = shape[0] * shape[1] * shape[2] * shape[3];
  421. MXNetParam p;
  422. p.data.resize(len);
  423. fread(&p.data[0], 1, len * sizeof(float), fp);
  424. params.push_back(p);
  425. // fprintf(stderr, "%u read\n", len);
  426. }
  427. // each name
  428. uint64_t name_count;
  429. fread(&name_count, 1, sizeof(uint64_t), fp);
  430. // fprintf(stderr, "name count = %d\n", (int)name_count);
  431. for (int i = 0; i < (int)name_count; i++)
  432. {
  433. uint64_t len;
  434. fread(&len, 1, sizeof(uint64_t), fp);
  435. MXNetParam& p = params[i];
  436. p.name.resize(len);
  437. fread((char*)p.name.data(), 1, len, fp);
  438. // cut leading arg:
  439. if (memcmp(p.name.c_str(), "arg:", 4) == 0)
  440. {
  441. p.name = std::string(p.name.c_str() + 4);
  442. }
  443. if (memcmp(p.name.c_str(), "aux:", 4) == 0)
  444. {
  445. p.name = std::string(p.name.c_str() + 4);
  446. }
  447. // fprintf(stderr, "%s read\n", p.name.c_str());
  448. }
  449. fclose(fp);
  450. return true;
  451. }
  452. int main(int argc, char** argv)
  453. {
  454. const char* jsonpath = argv[1];
  455. const char* parampath = argv[2];
  456. const char* ncnn_prototxt = argc >= 5 ? argv[3] : "ncnn.proto";
  457. const char* ncnn_modelbin = argc >= 5 ? argv[4] : "ncnn.bin";
  458. std::vector<MXNetNode> nodes;
  459. std::vector<MXNetParam> params;
  460. read_mxnet_json(jsonpath, nodes);
  461. read_mxnet_param(parampath, params);
  462. FILE* pp = fopen(ncnn_prototxt, "wb");
  463. FILE* bp = fopen(ncnn_modelbin, "wb");
  464. // magic
  465. fprintf(pp, "7767517\n");
  466. int node_count = nodes.size();
  467. // node reference
  468. std::map<int, int> node_reference;
  469. // weight node
  470. std::vector<int> weight_nodes;
  471. // global definition line
  472. // [layer count] [blob count]
  473. std::set<std::string> blob_names;
  474. for (int i=0; i<node_count; i++)
  475. {
  476. MXNetNode& n = nodes[i];
  477. // assign global param reference
  478. n.nodes = &nodes;
  479. n.params = &params;
  480. const std::string& output_name = n.name;
  481. if (n.op == "null")
  482. {
  483. if (n.is_weight())
  484. {
  485. weight_nodes.push_back(i);
  486. }
  487. else
  488. {
  489. if (n.has_attr("__init__"))
  490. {
  491. // init weight param
  492. MXNetParam pi;
  493. pi.name = n.name;
  494. pi.init = (std::string)n.attr("__init__");
  495. params.push_back(pi);
  496. weight_nodes.push_back(i);
  497. }
  498. else
  499. {
  500. // null node without data, treat it as network input
  501. }
  502. }
  503. continue;
  504. }
  505. // distinguish weights and inputs
  506. std::vector<int> weights;
  507. std::vector<int> inputs;
  508. for (int j=0; j<(int)n.inputs.size(); j++)
  509. {
  510. int input_index = n.inputs[j];
  511. if (nodes[input_index].is_weight())
  512. {
  513. weights.push_back(input_index);
  514. continue;
  515. }
  516. inputs.push_back(input_index);
  517. }
  518. n.inputs = inputs;
  519. n.weights = weights;
  520. // input
  521. for (int j=0; j<(int)n.inputs.size(); j++)
  522. {
  523. int input_index = n.inputs[j];
  524. const std::string& input_name = nodes[input_index].name;
  525. // fprintf(stderr, "input = %s\n", input_name.c_str());
  526. blob_names.insert(input_name);
  527. if (node_reference.find(input_index) == node_reference.end())
  528. {
  529. node_reference[input_index] = 1;
  530. }
  531. else
  532. {
  533. node_reference[input_index] = node_reference[input_index] + 1;
  534. }
  535. }
  536. // output
  537. // fprintf(stderr, "output = %s\n", output_name.c_str());
  538. blob_names.insert(output_name);
  539. }
  540. // remove node_reference entry with reference equals to one
  541. int splitncnn_blob_count = 0;
  542. std::map<int, int>::iterator it = node_reference.begin();
  543. while (it != node_reference.end())
  544. {
  545. if (it->second == 1)
  546. {
  547. node_reference.erase(it++);
  548. }
  549. else
  550. {
  551. splitncnn_blob_count += it->second;
  552. // fprintf(stderr, "%s %d\n", it->first.c_str(), it->second);
  553. ++it;
  554. }
  555. }
  556. fprintf(pp, "%lu %lu\n", node_count + node_reference.size() - weight_nodes.size(), blob_names.size() + splitncnn_blob_count);
  557. int internal_split = 0;
  558. for (int i=0; i<node_count; i++)
  559. {
  560. const MXNetNode& n = nodes[i];
  561. if (n.op == "null")
  562. {
  563. if (n.is_weight())
  564. {
  565. continue;
  566. }
  567. fprintf(pp, "%-16s", "Input");
  568. }
  569. else if (n.op == "Activation")
  570. {
  571. std::string type = n.attr("act_type");
  572. if (type == "relu")
  573. {
  574. fprintf(pp, "%-16s", "ReLU");
  575. }
  576. else if (type == "sigmoid")
  577. {
  578. fprintf(pp, "%-16s", "Sigmoid");
  579. }
  580. else if (type == "tanh")
  581. {
  582. fprintf(pp, "%-16s", "TanH");
  583. }
  584. }
  585. else if (n.op == "BatchNorm")
  586. {
  587. fprintf(pp, "%-16s", "BatchNorm");
  588. }
  589. else if (n.op == "Concat")
  590. {
  591. fprintf(pp, "%-16s", "Concat");
  592. }
  593. else if (n.op == "Convolution")
  594. {
  595. int num_group = n.attr("num_group");
  596. if (num_group > 0) {
  597. fprintf(pp, "%-16s", "ConvolutionDepthWise");
  598. } else {
  599. fprintf(pp, "%-16s", "Convolution");
  600. }
  601. }
  602. else if (n.op == "Dropout")
  603. {
  604. fprintf(pp, "%-16s", "Dropout");
  605. }
  606. else if (n.op == "elemwise_add")
  607. {
  608. fprintf(pp, "%-16s", "Eltwise");
  609. }
  610. else if (n.op == "Flatten")
  611. {
  612. fprintf(pp, "%-16s", "Flatten");
  613. }
  614. else if (n.op == "FullyConnected")
  615. {
  616. fprintf(pp, "%-16s", "InnerProduct");
  617. }
  618. else if (n.op == "LeakyReLU")
  619. {
  620. std::string type = n.attr("act_type");
  621. if (type == "elu")
  622. {
  623. fprintf(pp, "%-16s", "ELU");
  624. }
  625. else if (type == "leaky")
  626. {
  627. fprintf(pp, "%-16s", "ReLU");
  628. }
  629. else if (type == "prelu")
  630. {
  631. fprintf(pp, "%-16s", "PReLU");
  632. }
  633. }
  634. else if (n.op == "Pooling")
  635. {
  636. fprintf(pp, "%-16s", "Pooling");
  637. }
  638. else if (n.op == "SoftmaxOutput")
  639. {
  640. fprintf(pp, "%-16s", "Softmax");
  641. }
  642. else
  643. {
  644. fprintf(stderr, "%s not supported yet!\n", n.op.c_str());
  645. fprintf(pp, "%-16s", n.op.c_str());
  646. }
  647. int input_size = n.inputs.size();
  648. for (int j=0; j<(int)n.inputs.size(); j++)
  649. {
  650. int input_index = n.inputs[j];
  651. if (nodes[input_index].is_weight())
  652. {
  653. input_size--;
  654. }
  655. }
  656. if (n.op == "SoftmaxOutput")
  657. {
  658. // drop label
  659. input_size--;
  660. }
  661. fprintf(pp, " %-32s %d 1", n.name.c_str(), input_size);
  662. for (int j=0; j<(int)n.inputs.size(); j++)
  663. {
  664. int input_index = n.inputs[j];
  665. if (nodes[input_index].is_weight())
  666. {
  667. continue;
  668. }
  669. if (n.op == "SoftmaxOutput")
  670. {
  671. // drop label
  672. if (nodes[input_index].op == "null")
  673. continue;
  674. }
  675. std::string input_name = nodes[input_index].name;
  676. if (node_reference.find(input_index) != node_reference.end())
  677. {
  678. int refidx = node_reference[input_index] - 1;
  679. node_reference[input_index] = refidx;
  680. char splitsuffix[256];
  681. sprintf(splitsuffix, "_splitncnn_%d", refidx);
  682. input_name = input_name + splitsuffix;
  683. }
  684. fprintf(pp, " %s", input_name.c_str());
  685. }
  686. fprintf(pp, " %s", n.name.c_str());
  687. if (n.op == "null")
  688. {
  689. // dummy input shape
  690. // fprintf(pp, " 0 0 0");
  691. }
  692. else if (n.op == "Activation")
  693. {
  694. std::string type = n.attr("act_type");
  695. if (type == "relu")
  696. {
  697. // fprintf(pp, " 0=%f", 0.f);
  698. }
  699. }
  700. else if (n.op == "BatchNorm")
  701. {
  702. float eps = 1e-3;
  703. if (n.has_attr("eps")) {
  704. eps = n.attr("eps");
  705. }
  706. std::vector<float> slope_data = n.weight(0);
  707. std::vector<float> bias_data = n.weight(1);
  708. int channels = slope_data.size();
  709. std::vector<float> mean_data = n.weight(2, channels);
  710. std::vector<float> var_data = n.weight(3, channels);
  711. for (int j=0; j<(int)var_data.size(); j++)
  712. {
  713. var_data[j] += eps;
  714. }
  715. fprintf(pp, " 0=%d", channels);
  716. fwrite(slope_data.data(), sizeof(float), slope_data.size(), bp);
  717. fwrite(mean_data.data(), sizeof(float), mean_data.size(), bp);
  718. fwrite(var_data.data(), sizeof(float), var_data.size(), bp);
  719. fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp);
  720. }
  721. else if (n.op == "Concat")
  722. {
  723. int dim = n.attr("dim");
  724. fprintf(pp, " 0=%d", dim-1);
  725. }
  726. else if (n.op == "Convolution")
  727. {
  728. int num_filter = n.attr("num_filter");
  729. std::vector<int> kernel = n.attr("kernel");
  730. std::vector<int> dilate = n.attr("dilate");
  731. std::vector<int> stride = n.attr("stride");
  732. std::vector<int> pad = n.attr("pad");
  733. int no_bias = n.attr("no_bias");
  734. int num_group = n.attr("num_group");//TODO depthwise
  735. std::vector<float> weight_data = n.weight(0);
  736. std::vector<float> bias_data = n.weight(1);
  737. fprintf(pp, " 0=%d", num_filter);
  738. if (kernel.size() == 1) {
  739. fprintf(pp, " 1=%d", kernel[0]);
  740. } else if (kernel.size() == 2) {
  741. fprintf(pp, " 1=%d", kernel[1]);
  742. fprintf(pp, " 11=%d", kernel[0]);
  743. }
  744. if (dilate.size() == 1) {
  745. fprintf(pp, " 2=%d", dilate[0]);
  746. } else if (dilate.size() == 2) {
  747. fprintf(pp, " 2=%d", dilate[1]);
  748. fprintf(pp, " 12=%d", dilate[0]);
  749. }
  750. if (stride.size() == 1) {
  751. fprintf(pp, " 3=%d", stride[0]);
  752. } else if (stride.size() == 2) {
  753. fprintf(pp, " 3=%d", stride[1]);
  754. fprintf(pp, " 13=%d", stride[0]);
  755. }
  756. if (pad.size() == 1) {
  757. fprintf(pp, " 4=%d", pad[0]);
  758. } else if (pad.size() == 2) {
  759. fprintf(pp, " 4=%d", pad[1]);
  760. fprintf(pp, " 14=%d", pad[0]);
  761. }
  762. fprintf(pp, " 5=%d", no_bias == 1 ? 0 : 1);
  763. fprintf(pp, " 6=%d", (int)weight_data.size());
  764. if (num_group > 0) {
  765. fprintf(pp, " 7=%d", num_group);
  766. }
  767. int quantize_tag = 0;
  768. fwrite(&quantize_tag, sizeof(int), 1, bp);
  769. fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp);
  770. fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp);
  771. }
  772. else if (n.op == "Dropout")
  773. {
  774. // float p = n.attr("p");
  775. // fprintf(pp, " 0=%d", p);
  776. }
  777. else if (n.op == "elemwise_add")
  778. {
  779. int op_type = 1;
  780. fprintf(pp, " 0=%d", op_type);
  781. }
  782. else if (n.op == "Flatten")
  783. {
  784. }
  785. else if (n.op == "FullyConnected")
  786. {
  787. int num_hidden = n.attr("num_hidden");
  788. int no_bias = n.attr("no_bias");
  789. // int flatten = n.attr("flatten");
  790. // TODO flatten
  791. std::vector<float> weight_data = n.weight(0);
  792. std::vector<float> bias_data = n.weight(1);
  793. fprintf(pp, " 0=%d", num_hidden);
  794. fprintf(pp, " 1=%d", no_bias == 1 ? 0 : 1);
  795. fprintf(pp, " 2=%d", (int)weight_data.size());
  796. int quantize_tag = 0;
  797. fwrite(&quantize_tag, sizeof(int), 1, bp);
  798. fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp);
  799. fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp);
  800. }
  801. else if (n.op == "LeakyReLU")
  802. {
  803. std::string type = n.attr("act_type");
  804. if (type == "elu")
  805. {
  806. }
  807. else if (type == "leaky")
  808. {
  809. }
  810. else if (type == "prelu")
  811. {
  812. std::vector<float> weight_data = n.weight(0);
  813. fprintf(pp, " 0=%d", (int)weight_data.size());
  814. fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp);
  815. }
  816. }
  817. else if (n.op == "Pooling")
  818. {
  819. std::string pool_type = n.attr("pool_type");
  820. std::vector<int> kernel = n.attr("kernel");
  821. std::vector<int> stride = n.attr("stride");
  822. std::vector<int> pad = n.attr("pad");
  823. std::string pooling_convention = n.attr("pooling_convention");
  824. int global_pool = n.attr("global_pool");
  825. int pool = 0;
  826. if (pool_type == "max")
  827. {
  828. pool = 0;
  829. }
  830. else if (pool_type == "avg")
  831. {
  832. pool = 1;
  833. }
  834. if (pooling_convention == "valid")
  835. {
  836. // TODO valid and full mode
  837. }
  838. fprintf(pp, " 0=%d", pool);
  839. if (!kernel.empty())
  840. fprintf(pp, " 1=%d", kernel[0]);
  841. if (!stride.empty())
  842. fprintf(pp, " 2=%d", stride[0]);
  843. if (!pad.empty())
  844. fprintf(pp, " 3=%d", pad[0]);
  845. fprintf(pp, " 4=%d", global_pool);
  846. }
  847. else if (n.op == "SoftmaxOutput")
  848. {
  849. }
  850. else
  851. {
  852. // TODO op specific params
  853. std::map<std::string, std::string>::const_iterator it = n.attrs.begin();
  854. for (; it != n.attrs.end(); it++)
  855. {
  856. fprintf(stderr, "# %s=%s\n", it->first.c_str(), it->second.c_str());
  857. // fprintf(pp, " %s=%s", it->first.c_str(), it->second.c_str());
  858. }
  859. }
  860. fprintf(pp, "\n");
  861. if (node_reference.find(i) != node_reference.end())
  862. {
  863. int refcount = node_reference[i];
  864. if (refcount > 1)
  865. {
  866. std::string output_name = n.name;
  867. char splitname[256];
  868. sprintf(splitname, "splitncnn_%d", internal_split);
  869. fprintf(pp, "%-16s %-32s %d %d", "Split", splitname, 1, refcount);
  870. fprintf(pp, " %s", output_name.c_str());
  871. for (int j=0; j<refcount; j++)
  872. {
  873. fprintf(pp, " %s_splitncnn_%d", output_name.c_str(), j);
  874. }
  875. fprintf(pp, "\n");
  876. internal_split++;
  877. }
  878. }
  879. }
  880. fclose(pp);
  881. fclose(bp);
  882. return 0;
  883. }