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

8 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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<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. // fprintf(stderr, "%s read\n", p.name.c_str());
  444. }
  445. fclose(fp);
  446. return true;
  447. }
  448. int main(int argc, char** argv)
  449. {
  450. const char* jsonpath = argv[1];
  451. const char* parampath = argv[2];
  452. const char* ncnn_prototxt = argc >= 5 ? argv[3] : "ncnn.proto";
  453. const char* ncnn_modelbin = argc >= 5 ? argv[4] : "ncnn.bin";
  454. std::vector<MXNetNode> nodes;
  455. std::vector<MXNetParam> params;
  456. read_mxnet_json(jsonpath, nodes);
  457. read_mxnet_param(parampath, params);
  458. FILE* pp = fopen(ncnn_prototxt, "wb");
  459. FILE* bp = fopen(ncnn_modelbin, "wb");
  460. // magic
  461. fprintf(pp, "7767517\n");
  462. int node_count = nodes.size();
  463. // node reference
  464. std::map<int, int> node_reference;
  465. // weight node
  466. std::vector<int> weight_nodes;
  467. // global definition line
  468. // [layer count] [blob count]
  469. std::set<std::string> blob_names;
  470. for (int i=0; i<node_count; i++)
  471. {
  472. MXNetNode& n = nodes[i];
  473. // assign global param reference
  474. n.nodes = &nodes;
  475. n.params = &params;
  476. const std::string& output_name = n.name;
  477. if (n.op == "null")
  478. {
  479. if (n.is_weight())
  480. {
  481. weight_nodes.push_back(i);
  482. }
  483. else
  484. {
  485. if (n.has_attr("__init__"))
  486. {
  487. // init weight param
  488. MXNetParam pi;
  489. pi.name = n.name;
  490. pi.init = (std::string)n.attr("__init__");
  491. params.push_back(pi);
  492. weight_nodes.push_back(i);
  493. }
  494. else
  495. {
  496. // null node without data, treat it as network input
  497. }
  498. }
  499. continue;
  500. }
  501. // distinguish weights and inputs
  502. std::vector<int> weights;
  503. std::vector<int> inputs;
  504. for (int j=0; j<(int)n.inputs.size(); j++)
  505. {
  506. int input_index = n.inputs[j];
  507. if (nodes[input_index].is_weight())
  508. {
  509. weights.push_back(input_index);
  510. continue;
  511. }
  512. inputs.push_back(input_index);
  513. }
  514. n.inputs = inputs;
  515. n.weights = weights;
  516. // input
  517. for (int j=0; j<(int)n.inputs.size(); j++)
  518. {
  519. int input_index = n.inputs[j];
  520. const std::string& input_name = nodes[input_index].name;
  521. // fprintf(stderr, "input = %s\n", input_name.c_str());
  522. blob_names.insert(input_name);
  523. if (node_reference.find(input_index) == node_reference.end())
  524. {
  525. node_reference[input_index] = 1;
  526. }
  527. else
  528. {
  529. node_reference[input_index] = node_reference[input_index] + 1;
  530. }
  531. }
  532. // output
  533. // fprintf(stderr, "output = %s\n", output_name.c_str());
  534. blob_names.insert(output_name);
  535. }
  536. // remove node_reference entry with reference equals to one
  537. int splitncnn_blob_count = 0;
  538. std::map<int, int>::iterator it = node_reference.begin();
  539. while (it != node_reference.end())
  540. {
  541. if (it->second == 1)
  542. {
  543. node_reference.erase(it++);
  544. }
  545. else
  546. {
  547. splitncnn_blob_count += it->second;
  548. // fprintf(stderr, "%s %d\n", it->first.c_str(), it->second);
  549. ++it;
  550. }
  551. }
  552. fprintf(pp, "%lu %lu\n", node_count + node_reference.size() - weight_nodes.size(), blob_names.size() + splitncnn_blob_count);
  553. int internal_split = 0;
  554. for (int i=0; i<node_count; i++)
  555. {
  556. const MXNetNode& n = nodes[i];
  557. if (n.op == "null")
  558. {
  559. if (n.is_weight())
  560. {
  561. continue;
  562. }
  563. fprintf(pp, "%-16s", "Input");
  564. }
  565. else if (n.op == "Activation")
  566. {
  567. std::string type = n.attr("act_type");
  568. if (type == "relu")
  569. {
  570. fprintf(pp, "%-16s", "ReLU");
  571. }
  572. else if (type == "sigmoid")
  573. {
  574. fprintf(pp, "%-16s", "Sigmoid");
  575. }
  576. else if (type == "tanh")
  577. {
  578. fprintf(pp, "%-16s", "TanH");
  579. }
  580. }
  581. else if (n.op == "BatchNorm")
  582. {
  583. fprintf(pp, "%-16s", "BatchNorm");
  584. }
  585. else if (n.op == "Concat")
  586. {
  587. fprintf(pp, "%-16s", "Concat");
  588. }
  589. else if (n.op == "Convolution")
  590. {
  591. fprintf(pp, "%-16s", "Convolution");
  592. }
  593. else if (n.op == "Dropout")
  594. {
  595. fprintf(pp, "%-16s", "Dropout");
  596. }
  597. else if (n.op == "elemwise_add")
  598. {
  599. fprintf(pp, "%-16s", "Eltwise");
  600. }
  601. else if (n.op == "Flatten")
  602. {
  603. fprintf(pp, "%-16s", "Flatten");
  604. }
  605. else if (n.op == "FullyConnected")
  606. {
  607. fprintf(pp, "%-16s", "InnerProduct");
  608. }
  609. else if (n.op == "LeakyReLU")
  610. {
  611. std::string type = n.attr("act_type");
  612. if (type == "elu")
  613. {
  614. fprintf(pp, "%-16s", "ELU");
  615. }
  616. else if (type == "leaky")
  617. {
  618. fprintf(pp, "%-16s", "ReLU");
  619. }
  620. else if (type == "prelu")
  621. {
  622. fprintf(pp, "%-16s", "PReLU");
  623. }
  624. }
  625. else if (n.op == "Pooling")
  626. {
  627. fprintf(pp, "%-16s", "Pooling");
  628. }
  629. else if (n.op == "SoftmaxOutput")
  630. {
  631. fprintf(pp, "%-16s", "Softmax");
  632. }
  633. else
  634. {
  635. fprintf(stderr, "%s not supported yet!\n", n.op.c_str());
  636. fprintf(pp, "%-16s", n.op.c_str());
  637. }
  638. int input_size = n.inputs.size();
  639. for (int j=0; j<(int)n.inputs.size(); j++)
  640. {
  641. int input_index = n.inputs[j];
  642. if (nodes[input_index].is_weight())
  643. {
  644. input_size--;
  645. }
  646. }
  647. if (n.op == "SoftmaxOutput")
  648. {
  649. // drop label
  650. input_size--;
  651. }
  652. fprintf(pp, " %-32s %d 1", n.name.c_str(), input_size);
  653. for (int j=0; j<(int)n.inputs.size(); j++)
  654. {
  655. int input_index = n.inputs[j];
  656. if (nodes[input_index].is_weight())
  657. {
  658. continue;
  659. }
  660. if (n.op == "SoftmaxOutput")
  661. {
  662. // drop label
  663. if (nodes[input_index].op == "null")
  664. continue;
  665. }
  666. std::string input_name = nodes[input_index].name;
  667. if (node_reference.find(input_index) != node_reference.end())
  668. {
  669. int refidx = node_reference[input_index] - 1;
  670. node_reference[input_index] = refidx;
  671. char splitsuffix[256];
  672. sprintf(splitsuffix, "_splitncnn_%d", refidx);
  673. input_name = input_name + splitsuffix;
  674. }
  675. fprintf(pp, " %s", input_name.c_str());
  676. }
  677. fprintf(pp, " %s", n.name.c_str());
  678. if (n.op == "null")
  679. {
  680. // dummy input shape
  681. // fprintf(pp, " 0 0 0");
  682. }
  683. else if (n.op == "Activation")
  684. {
  685. std::string type = n.attr("act_type");
  686. if (type == "relu")
  687. {
  688. // fprintf(pp, " 0=%f", 0.f);
  689. }
  690. }
  691. else if (n.op == "BatchNorm")
  692. {
  693. float eps = n.attr("eps");
  694. std::vector<float> slope_data = n.weight(0);
  695. std::vector<float> bias_data = n.weight(1);
  696. int channels = slope_data.size();
  697. std::vector<float> mean_data = n.weight(2, channels);
  698. std::vector<float> var_data = n.weight(3, channels);
  699. for (int j=0; j<(int)var_data.size(); j++)
  700. {
  701. var_data[j] += eps;
  702. }
  703. fprintf(pp, " 0=%d", channels);
  704. fwrite(slope_data.data(), sizeof(float), slope_data.size(), bp);
  705. fwrite(mean_data.data(), sizeof(float), mean_data.size(), bp);
  706. fwrite(var_data.data(), sizeof(float), var_data.size(), bp);
  707. fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp);
  708. }
  709. else if (n.op == "Concat")
  710. {
  711. int dim = n.attr("dim");
  712. fprintf(pp, " 0=%d", dim-1);
  713. }
  714. else if (n.op == "Convolution")
  715. {
  716. int num_filter = n.attr("num_filter");
  717. std::vector<int> kernel = n.attr("kernel");
  718. std::vector<int> dilate = n.attr("dilate");
  719. std::vector<int> stride = n.attr("stride");
  720. std::vector<int> pad = n.attr("pad");
  721. int no_bias = n.attr("no_bias");
  722. int num_group = n.attr("num_group");//TODO depthwise
  723. std::vector<float> weight_data = n.weight(0);
  724. std::vector<float> bias_data = n.weight(1);
  725. fprintf(pp, " 0=%d", num_filter);
  726. if (!kernel.empty())
  727. fprintf(pp, " 1=%d", kernel[0]);
  728. if (!dilate.empty())
  729. fprintf(pp, " 2=%d", dilate[0]);
  730. if (!stride.empty())
  731. fprintf(pp, " 3=%d", stride[0]);
  732. if (!pad.empty())
  733. fprintf(pp, " 4=%d", pad[0]);
  734. fprintf(pp, " 5=%d", no_bias == 1 ? 0 : 1);
  735. fprintf(pp, " 6=%d", weight_data.size());
  736. int quantize_tag = 0;
  737. fwrite(&quantize_tag, sizeof(int), 1, bp);
  738. fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp);
  739. fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp);
  740. }
  741. else if (n.op == "Dropout")
  742. {
  743. // float p = n.attr("p");
  744. // fprintf(pp, " 0=%d", p);
  745. }
  746. else if (n.op == "elemwise_add")
  747. {
  748. int op_type = 1;
  749. fprintf(pp, " 0=%d", op_type);
  750. }
  751. else if (n.op == "Flatten")
  752. {
  753. }
  754. else if (n.op == "FullyConnected")
  755. {
  756. int num_hidden = n.attr("num_hidden");
  757. int no_bias = n.attr("no_bias");
  758. int flatten = n.attr("flatten");
  759. // TODO flatten
  760. std::vector<float> weight_data = n.weight(0);
  761. std::vector<float> bias_data = n.weight(1);
  762. fprintf(pp, " 0=%d", num_hidden);
  763. fprintf(pp, " 1=%d", no_bias == 1 ? 0 : 1);
  764. fprintf(pp, " 2=%d", weight_data.size());
  765. int quantize_tag = 0;
  766. fwrite(&quantize_tag, sizeof(int), 1, bp);
  767. fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp);
  768. fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp);
  769. }
  770. else if (n.op == "LeakyReLU")
  771. {
  772. std::string type = n.attr("act_type");
  773. if (type == "elu")
  774. {
  775. }
  776. else if (type == "leaky")
  777. {
  778. }
  779. else if (type == "prelu")
  780. {
  781. std::vector<float> weight_data = n.weight(0);
  782. fprintf(pp, " 0=%d", weight_data.size());
  783. fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp);
  784. }
  785. }
  786. else if (n.op == "Pooling")
  787. {
  788. std::string pool_type = n.attr("pool_type");
  789. std::vector<int> kernel = n.attr("kernel");
  790. std::vector<int> stride = n.attr("stride");
  791. std::vector<int> pad = n.attr("pad");
  792. std::string pooling_convention = n.attr("pooling_convention");
  793. int global_pool = n.attr("global_pool");
  794. int pool = 0;
  795. if (pool_type == "max")
  796. {
  797. pool = 0;
  798. }
  799. else if (pool_type == "avg")
  800. {
  801. pool = 1;
  802. }
  803. if (pooling_convention == "valid")
  804. {
  805. // TODO valid and full mode
  806. }
  807. fprintf(pp, " 0=%d", pool);
  808. if (!kernel.empty())
  809. fprintf(pp, " 1=%d", kernel[0]);
  810. if (!stride.empty())
  811. fprintf(pp, " 2=%d", stride[0]);
  812. if (!pad.empty())
  813. fprintf(pp, " 3=%d", pad[0]);
  814. fprintf(pp, " 4=%d", global_pool);
  815. }
  816. else if (n.op == "SoftmaxOutput")
  817. {
  818. }
  819. else
  820. {
  821. // TODO op specific params
  822. std::map<std::string, std::string>::const_iterator it = n.attrs.begin();
  823. for (; it != n.attrs.end(); it++)
  824. {
  825. fprintf(stderr, "# %s=%s\n", it->first.c_str(), it->second.c_str());
  826. // fprintf(pp, " %s=%s", it->first.c_str(), it->second.c_str());
  827. }
  828. }
  829. fprintf(pp, "\n");
  830. if (node_reference.find(i) != node_reference.end())
  831. {
  832. int refcount = node_reference[i];
  833. if (refcount > 1)
  834. {
  835. std::string output_name = n.name;
  836. char splitname[256];
  837. sprintf(splitname, "splitncnn_%d", internal_split);
  838. fprintf(pp, "%-16s %-32s %d %d", "Split", splitname, 1, refcount);
  839. fprintf(pp, " %s", output_name.c_str());
  840. for (int j=0; j<refcount; j++)
  841. {
  842. fprintf(pp, " %s_splitncnn_%d", output_name.c_str(), j);
  843. }
  844. fprintf(pp, "\n");
  845. internal_split++;
  846. }
  847. }
  848. }
  849. fclose(pp);
  850. fclose(bp);
  851. return 0;
  852. }