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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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<(*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 >= weights.size())
  134. return false;
  135. const std::string& name = (*nodes)[ weights[i] ].name;
  136. for (int i=0; i<(*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 >= weights.size())
  147. return std::vector<float>();
  148. const std::string& name = (*nodes)[ weights[i] ].name;
  149. for (int i=0; i<(*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<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<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<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<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> mean_data = n.weight(0);
  695. std::vector<float> var_data = n.weight(1);
  696. int channels = mean_data.size();
  697. std::vector<float> slope_data = n.weight(2, channels);
  698. std::vector<float> bias_data = n.weight(3, channels);
  699. fprintf(pp, " 0=%d", channels);
  700. fwrite(slope_data.data(), sizeof(float), slope_data.size(), bp);
  701. fwrite(mean_data.data(), sizeof(float), mean_data.size(), bp);
  702. fwrite(var_data.data(), sizeof(float), var_data.size(), bp);
  703. fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp);
  704. }
  705. else if (n.op == "Concat")
  706. {
  707. int dim = n.attr("dim");
  708. fprintf(pp, " 0=%d", dim-1);
  709. }
  710. else if (n.op == "Convolution")
  711. {
  712. int num_filter = n.attr("num_filter");
  713. std::vector<int> kernel = n.attr("kernel");
  714. std::vector<int> dilate = n.attr("dilate");
  715. std::vector<int> stride = n.attr("stride");
  716. std::vector<int> pad = n.attr("pad");
  717. int no_bias = n.attr("no_bias");
  718. int num_group = n.attr("num_group");//TODO depthwise
  719. std::vector<float> weight_data = n.weight(0);
  720. std::vector<float> bias_data = n.weight(1);
  721. fprintf(pp, " 0=%d", num_filter);
  722. if (!kernel.empty())
  723. fprintf(pp, " 1=%d", kernel[0]);
  724. if (!dilate.empty())
  725. fprintf(pp, " 2=%d", dilate[0]);
  726. if (!stride.empty())
  727. fprintf(pp, " 3=%d", stride[0]);
  728. if (!pad.empty())
  729. fprintf(pp, " 4=%d", pad[0]);
  730. fprintf(pp, " 5=%d", no_bias == 1 ? 0 : 1);
  731. fprintf(pp, " 6=%d", weight_data.size());
  732. int quantize_tag = 0;
  733. fwrite(&quantize_tag, sizeof(int), 1, bp);
  734. fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp);
  735. fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp);
  736. }
  737. else if (n.op == "Dropout")
  738. {
  739. // float p = n.attr("p");
  740. // fprintf(pp, " 0=%d", p);
  741. }
  742. else if (n.op == "elemwise_add")
  743. {
  744. int op_type = 1;
  745. fprintf(pp, " 0=%d", op_type);
  746. }
  747. else if (n.op == "Flatten")
  748. {
  749. }
  750. else if (n.op == "FullyConnected")
  751. {
  752. int num_hidden = n.attr("num_hidden");
  753. int no_bias = n.attr("no_bias");
  754. int flatten = n.attr("flatten");
  755. // TODO flatten
  756. std::vector<float> weight_data = n.weight(0);
  757. std::vector<float> bias_data = n.weight(1);
  758. fprintf(pp, " 0=%d", num_hidden);
  759. fprintf(pp, " 1=%d", no_bias == 1 ? 0 : 1);
  760. fprintf(pp, " 2=%d", weight_data.size());
  761. int quantize_tag = 0;
  762. fwrite(&quantize_tag, sizeof(int), 1, bp);
  763. fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp);
  764. fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp);
  765. }
  766. else if (n.op == "LeakyReLU")
  767. {
  768. std::string type = n.attr("act_type");
  769. if (type == "elu")
  770. {
  771. }
  772. else if (type == "leaky")
  773. {
  774. }
  775. else if (type == "prelu")
  776. {
  777. std::vector<float> weight_data = n.weight(0);
  778. fprintf(pp, " 0=%d", weight_data.size());
  779. fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp);
  780. }
  781. }
  782. else if (n.op == "Pooling")
  783. {
  784. std::string pool_type = n.attr("pool_type");
  785. std::vector<int> kernel = n.attr("kernel");
  786. std::vector<int> stride = n.attr("stride");
  787. std::vector<int> pad = n.attr("pad");
  788. std::string pooling_convention = n.attr("pooling_convention");
  789. int global_pool = n.attr("global_pool");
  790. int pool = 0;
  791. if (pool_type == "max")
  792. {
  793. pool = 0;
  794. }
  795. else if (pool_type == "avg")
  796. {
  797. pool = 1;
  798. }
  799. if (pooling_convention == "valid")
  800. {
  801. // TODO valid and full mode
  802. }
  803. fprintf(pp, " 0=%d", pool);
  804. if (!kernel.empty())
  805. fprintf(pp, " 1=%d", kernel[0]);
  806. if (!stride.empty())
  807. fprintf(pp, " 2=%d", stride[0]);
  808. if (!pad.empty())
  809. fprintf(pp, " 3=%d", pad[0]);
  810. fprintf(pp, " 4=%d", global_pool);
  811. }
  812. else if (n.op == "SoftmaxOutput")
  813. {
  814. }
  815. else
  816. {
  817. // TODO op specific params
  818. std::map<std::string, std::string>::const_iterator it = n.attrs.begin();
  819. for (; it != n.attrs.end(); it++)
  820. {
  821. fprintf(stderr, "# %s=%s\n", it->first.c_str(), it->second.c_str());
  822. // fprintf(pp, " %s=%s", it->first.c_str(), it->second.c_str());
  823. }
  824. }
  825. fprintf(pp, "\n");
  826. if (node_reference.find(i) != node_reference.end())
  827. {
  828. int refcount = node_reference[i];
  829. if (refcount > 1)
  830. {
  831. std::string output_name = n.name;
  832. char splitname[256];
  833. sprintf(splitname, "splitncnn_%d", internal_split);
  834. fprintf(pp, "%-16s %-32s %d %d", "Split", splitname, 1, refcount);
  835. fprintf(pp, " %s", output_name.c_str());
  836. for (int j=0; j<refcount; j++)
  837. {
  838. fprintf(pp, " %s_splitncnn_%d", output_name.c_str(), j);
  839. }
  840. fprintf(pp, "\n");
  841. internal_split++;
  842. }
  843. }
  844. }
  845. fclose(pp);
  846. fclose(bp);
  847. return 0;
  848. }