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.

tensorflow2ncnn.cpp 44 kB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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 <limits.h>
  16. #include <iostream>
  17. #include <fstream>
  18. #include <set>
  19. #include <limits>
  20. #include <algorithm>
  21. #include <google/protobuf/io/coded_stream.h>
  22. #include <google/protobuf/io/zero_copy_stream_impl.h>
  23. #include <google/protobuf/text_format.h>
  24. #include <google/protobuf/message.h>
  25. #include "graph.pb.h"
  26. static bool read_proto_from_binary(const char* filepath, google::protobuf::Message* message)
  27. {
  28. std::ifstream fs(filepath, std::ifstream::in | std::ifstream::binary);
  29. if (!fs.is_open())
  30. {
  31. fprintf(stderr, "open failed %s\n", filepath);
  32. return false;
  33. }
  34. google::protobuf::io::IstreamInputStream input(&fs);
  35. google::protobuf::io::CodedInputStream codedstr(&input);
  36. codedstr.SetTotalBytesLimit(INT_MAX, INT_MAX / 2);
  37. bool success = message->ParseFromCodedStream(&codedstr);
  38. fs.close();
  39. return success;
  40. }
  41. static bool find_tensor_proto(const std::map<std::string, tensorflow::TensorProto>& weights,
  42. const tensorflow::NodeDef& node, tensorflow::TensorProto& tensor)
  43. {
  44. for (int j=0; j<node.input_size(); j++)
  45. {
  46. const std::string& input_name = node.input(j);
  47. const std::map<std::string, tensorflow::TensorProto>::const_iterator it = weights.find(input_name);
  48. if (it != weights.end())
  49. {
  50. tensor = it->second;
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56. static bool get_tensor_proto(const std::map<std::string, tensorflow::TensorProto>& consts,
  57. const tensorflow::NodeDef& node, tensorflow::TensorProto& tensor)
  58. {
  59. const std::string& output_name = node.name();
  60. const std::map<std::string, tensorflow::TensorProto>::const_iterator it = consts.find(output_name);
  61. if (it != consts.end())
  62. {
  63. tensor = it->second;
  64. return true;
  65. }
  66. return false;
  67. }
  68. static bool find_attr_value(const tensorflow::NodeDef& node, const char* key, tensorflow::AttrValue& value)
  69. {
  70. const google::protobuf::Map<std::string, tensorflow::AttrValue>& attr = node.attr();
  71. const google::protobuf::Map<std::string, tensorflow::AttrValue>::const_iterator it = attr.find(key);
  72. if (it != attr.end())
  73. {
  74. value = it->second;
  75. return true;
  76. }
  77. return false;
  78. }
  79. static int parse_tensor_reduction_dim(const tensorflow::TensorProto& tensor)
  80. {
  81. int dim = 0;
  82. // dim == 0 // w h c -> X X X
  83. // dim == 1 // w h c -> X X c
  84. // dim == 2 // w h c -> X h c
  85. // dim == -1 // w h c -> w X X
  86. // dim == -2 // w h c -> w h X
  87. if (!tensor.tensor_content().empty() && tensor.dtype() == 3)// int32
  88. {
  89. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  90. int size = tensor.tensor_content().size() / sizeof(int);
  91. // n h w c
  92. // n h w
  93. // n w
  94. // TODO investigate two stage / three stage reduction
  95. if (size == 2)
  96. {
  97. if (data[0] == 1 && data[1] == 2)
  98. {
  99. dim = 1;
  100. }
  101. }
  102. }
  103. else
  104. {
  105. int axis = tensor.int_val(0);
  106. if (axis == 1)
  107. dim = 0;
  108. else if (axis == 3)
  109. dim = -2;
  110. }
  111. return dim;
  112. }
  113. int main(int argc, char** argv)
  114. {
  115. const char* tensorflowpb = argv[1];
  116. const char* ncnn_prototxt = argc >= 4 ? argv[2] : "ncnn.proto";
  117. const char* ncnn_modelbin = argc >= 4 ? argv[3] : "ncnn.bin";
  118. tensorflow::GraphDef graph;
  119. // load
  120. bool s1 = read_proto_from_binary(tensorflowpb, &graph);
  121. if (!s1)
  122. {
  123. fprintf(stderr, "read_proto_from_binary failed\n");
  124. return -1;
  125. }
  126. FILE* pp = fopen(ncnn_prototxt, "wb");
  127. FILE* bp = fopen(ncnn_modelbin, "wb");
  128. // magic
  129. fprintf(pp, "7767517\n");
  130. int node_count = graph.node_size();
  131. // fprintf(stderr, "node_count = %d\n\n", node_count);
  132. // node reference
  133. std::map<std::string, int> node_reference;
  134. // mapping for Const and Const-Identity
  135. std::map<std::string, tensorflow::TensorProto> weights;
  136. // Dropout like Identity
  137. std::set<std::string> dropouts;
  138. // Const before BinaryOp
  139. std::map<std::string, tensorflow::TensorProto> binaryop_consts;
  140. // global definition line
  141. // [layer count] [blob count]
  142. std::set<std::string> blob_names;
  143. for (int i=0; i<node_count; i++)
  144. {
  145. const tensorflow::NodeDef& node = graph.node(i);
  146. const std::string& output_name = node.name();
  147. if (node.op() == "Const")
  148. {
  149. tensorflow::AttrValue value;
  150. if (find_attr_value(node, "value", value))
  151. {
  152. const tensorflow::TensorProto& tensor = value.tensor();
  153. weights[output_name] = tensor;
  154. }
  155. continue;
  156. }
  157. else if (node.op() == "Identity")
  158. {
  159. const std::string& input_name = node.input(0);
  160. if (weights.find(input_name) != weights.end())
  161. {
  162. weights[output_name] = weights[input_name];
  163. continue;
  164. }
  165. else
  166. {
  167. dropouts.insert(output_name);
  168. }
  169. }
  170. else if (node.op() == "NoOp")
  171. {
  172. weights[output_name] = tensorflow::TensorProto();
  173. continue;
  174. }
  175. else
  176. {
  177. bool isBinaryOp = false;
  178. if (node.op() == "Add" || node.op() == "BiasAdd" || node.op() == "Div"
  179. || node.op() == "Mul" || node.op() == "RealDiv" || node.op() == "Sub")
  180. {
  181. isBinaryOp = true;
  182. }
  183. if (node.op() == "Max" || node.op() == "Maximum" || node.op() == "Min" || node.op() == "Minimum")
  184. {
  185. // check weights
  186. tensorflow::TensorProto tensor;
  187. if (!find_tensor_proto(weights, node, tensor))
  188. {
  189. isBinaryOp = true;
  190. }
  191. }
  192. if (isBinaryOp)
  193. {
  194. // check weights
  195. for (int j=0; j<node.input_size(); j++)
  196. {
  197. const std::string& input_name = node.input(j);
  198. std::map<std::string, tensorflow::TensorProto>::iterator it = weights.find(input_name);
  199. if (it != weights.end())
  200. {
  201. // binary op with const, insert MemoryData layer and const blob
  202. binaryop_consts[input_name] = it->second;
  203. weights.erase(it);
  204. }
  205. }
  206. }
  207. }
  208. // input
  209. for (int j=0; j<node.input_size(); j++)
  210. {
  211. const std::string& input_name = node.input(j);
  212. // fprintf(stderr, "input = %s\n", input_name.c_str());
  213. if (weights.find(input_name) != weights.end())
  214. {
  215. continue;
  216. }
  217. blob_names.insert(input_name);
  218. if (node_reference.find(input_name) == node_reference.end())
  219. {
  220. node_reference[input_name] = 1;
  221. }
  222. else
  223. {
  224. node_reference[input_name] = node_reference[input_name] + 1;
  225. }
  226. }
  227. // output
  228. // fprintf(stderr, "output = %s\n", output_name.c_str());
  229. blob_names.insert(output_name);
  230. }
  231. // remove node_reference entry with reference equals to one
  232. int splitncnn_blob_count = 0;
  233. std::map<std::string, int>::iterator it = node_reference.begin();
  234. while (it != node_reference.end())
  235. {
  236. if (it->second == 1)
  237. {
  238. node_reference.erase(it++);
  239. }
  240. else
  241. {
  242. splitncnn_blob_count += it->second;
  243. // fprintf(stderr, "%s %d\n", it->first.c_str(), it->second);
  244. ++it;
  245. }
  246. }
  247. fprintf(pp, "%lu %lu\n", node_count + node_reference.size() - weights.size(), blob_names.size() + splitncnn_blob_count);
  248. int internal_split = 0;
  249. for (int i=0; i<node_count; i++)
  250. {
  251. const tensorflow::NodeDef& node = graph.node(i);
  252. // layer definition line, repeated
  253. // [type] [name] [bottom blob count] [top blob count] [bottom blobs] [top blobs] [layer specific params]
  254. // fprintf(pp, "%-16s %-16s %d %d", layer.type().c_str(), layer.name().c_str(), node.input_size(), layer.top_size());
  255. if (node.op() == "Add" || node.op() == "BiasAdd")
  256. {
  257. fprintf(pp, "%-16s", "BinaryOp");
  258. }
  259. else if (node.op() == "AvgPool")
  260. {
  261. fprintf(pp, "%-16s", "Pooling");
  262. }
  263. else if (node.op() == "Concat" || node.op() == "ConcatV2")
  264. {
  265. fprintf(pp, "%-16s", "Concat");
  266. }
  267. else if (node.op() == "Const")
  268. {
  269. // check before binaryop
  270. tensorflow::TensorProto tensor;
  271. if (get_tensor_proto(binaryop_consts, node, tensor))
  272. {
  273. fprintf(pp, "%-16s", "MemoryData");
  274. }
  275. else
  276. {
  277. continue;
  278. }
  279. }
  280. else if (node.op() == "Conv2D")
  281. {
  282. fprintf(pp, "%-16s", "Convolution");
  283. }
  284. else if (node.op() == "DepthwiseConv2dNative")
  285. {
  286. fprintf(pp, "%-16s", "ConvolutionDepthWise");
  287. }
  288. else if (node.op() == "Div" || node.op() == "RealDiv")
  289. {
  290. fprintf(pp, "%-16s", "BinaryOp");
  291. }
  292. else if (node.op() == "Exp")
  293. {
  294. fprintf(pp, "%-16s", "UnaryOp");
  295. }
  296. else if (node.op() == "ExpandDims")
  297. {
  298. fprintf(pp, "%-16s", "ExpandDims");
  299. }
  300. else if (node.op() == "Floor")
  301. {
  302. fprintf(pp, "%-16s", "UnaryOp");
  303. }
  304. else if (node.op() == "Identity")
  305. {
  306. // check before binaryop
  307. tensorflow::TensorProto tensor;
  308. if (get_tensor_proto(binaryop_consts, node, tensor))
  309. {
  310. fprintf(pp, "%-16s", "MemoryData");
  311. }
  312. else if (dropouts.find(node.name()) != dropouts.end())
  313. {
  314. fprintf(pp, "%-16s", "Dropout");
  315. }
  316. else
  317. {
  318. continue;
  319. }
  320. }
  321. else if (node.op() == "LRN")
  322. {
  323. fprintf(pp, "%-16s", "LRN");
  324. }
  325. else if (node.op() == "MatMul")
  326. {
  327. fprintf(pp, "%-16s", "InnerProduct");
  328. }
  329. else if (node.op() == "Max" || node.op() == "Maximum")
  330. {
  331. // check weights
  332. tensorflow::TensorProto tensor;
  333. if (find_tensor_proto(weights, node, tensor))
  334. {
  335. fprintf(pp, "%-16s", "Reduction");
  336. }
  337. else
  338. {
  339. fprintf(pp, "%-16s", "BinaryOp");
  340. }
  341. }
  342. else if (node.op() == "MaxPool")
  343. {
  344. fprintf(pp, "%-16s", "Pooling");
  345. }
  346. else if (node.op() == "Min" || node.op() == "Minimum")
  347. {
  348. // check weights
  349. tensorflow::TensorProto tensor;
  350. if (find_tensor_proto(weights, node, tensor))
  351. {
  352. fprintf(pp, "%-16s", "Reduction");
  353. }
  354. else
  355. {
  356. fprintf(pp, "%-16s", "BinaryOp");
  357. }
  358. }
  359. else if (node.op() == "Mul")
  360. {
  361. fprintf(pp, "%-16s", "BinaryOp");
  362. }
  363. else if (node.op() == "Neg")
  364. {
  365. fprintf(pp, "%-16s", "UnaryOp");
  366. }
  367. else if (node.op() == "NoOp")
  368. {
  369. continue;
  370. }
  371. else if (node.op() == "Pad")
  372. {
  373. fprintf(pp, "%-16s", "Padding");
  374. }
  375. else if (node.op() == "Placeholder")
  376. {
  377. fprintf(pp, "%-16s", "Input");
  378. }
  379. else if (node.op() == "Prod")
  380. {
  381. fprintf(pp, "%-16s", "Reduction");
  382. }
  383. else if (node.op() == "Reciprocal")
  384. {
  385. fprintf(pp, "%-16s", "UnaryOp");
  386. }
  387. else if (node.op() == "Relu")
  388. {
  389. fprintf(pp, "%-16s", "ReLU");
  390. }
  391. else if (node.op() == "Reshape")
  392. {
  393. fprintf(pp, "%-16s", "Reshape");
  394. }
  395. else if (node.op() == "Rsqrt")
  396. {
  397. fprintf(pp, "%-16s", "UnaryOp");
  398. }
  399. else if (node.op() == "Sigmoid")
  400. {
  401. fprintf(pp, "%-16s", "Sigmoid");
  402. }
  403. else if (node.op() == "Softmax")
  404. {
  405. fprintf(pp, "%-16s", "Softmax");
  406. }
  407. else if (node.op() == "Square")
  408. {
  409. fprintf(pp, "%-16s", "UnaryOp");
  410. }
  411. else if (node.op() == "Squeeze")
  412. {
  413. fprintf(pp, "%-16s", "Squeeze");
  414. }
  415. else if (node.op() == "Sub")
  416. {
  417. fprintf(pp, "%-16s", "BinaryOp");
  418. }
  419. else if (node.op() == "Sum")
  420. {
  421. fprintf(pp, "%-16s", "Reduction");
  422. }
  423. else
  424. {
  425. fprintf(pp, "%-16s", node.op().c_str());
  426. fprintf(stderr, "%s not supported yet !\nn", node.op().c_str());
  427. }
  428. int input_size = node.input_size();
  429. for (int j=0; j<node.input_size(); j++)
  430. {
  431. const std::string& input_name = node.input(j);
  432. if (weights.find(input_name) != weights.end())
  433. {
  434. input_size--;
  435. }
  436. }
  437. fprintf(pp, " %-32s %d 1", node.name().c_str(), input_size);
  438. for (int j=0; j<node.input_size(); j++)
  439. {
  440. std::string input_name = node.input(j);
  441. if (weights.find(input_name) != weights.end())
  442. {
  443. continue;
  444. }
  445. if (node_reference.find(input_name) != node_reference.end())
  446. {
  447. int refidx = node_reference[input_name] - 1;
  448. node_reference[input_name] = refidx;
  449. char splitsuffix[256];
  450. sprintf(splitsuffix, "_splitncnn_%d", refidx);
  451. input_name = input_name + splitsuffix;
  452. }
  453. fprintf(pp, " %s", input_name.c_str());
  454. }
  455. fprintf(pp, " %s", node.name().c_str());
  456. if (node.op() == "Add" || node.op() == "BiasAdd")
  457. {
  458. int op_type = 0;
  459. fprintf(pp, " 0=%d", op_type);
  460. }
  461. else if (node.op() == "AvgPool")
  462. {
  463. int pooling_type = 1;
  464. int kernel_size_h = 1;
  465. int kernel_size_w = 1;
  466. int stride_h = 1;
  467. int stride_w = 1;
  468. int pad = 0;
  469. int global_pooling = 0;
  470. tensorflow::AttrValue value_ksize;
  471. if (find_attr_value(node, "ksize", value_ksize))
  472. {
  473. // batch, height, width, channels
  474. kernel_size_h = value_ksize.list().i(1);
  475. kernel_size_w = value_ksize.list().i(2);
  476. }
  477. tensorflow::AttrValue value_strides;
  478. if (find_attr_value(node, "strides", value_strides))
  479. {
  480. // batch, height, width, channels
  481. stride_h = value_strides.list().i(1);
  482. stride_w = value_strides.list().i(2);
  483. }
  484. tensorflow::AttrValue value_padding;
  485. if (find_attr_value(node, "padding", value_padding))
  486. {
  487. if (value_padding.s() == "VALID")
  488. {
  489. pad = 0;
  490. }
  491. else if (value_padding.s() == "SAME")
  492. {
  493. pad = -233;
  494. }
  495. }
  496. fprintf(pp, " 0=%d", pooling_type);
  497. fprintf(pp, " 1=%d", kernel_size_w);
  498. fprintf(pp, " 2=%d", stride_w);
  499. fprintf(pp, " 3=%d", pad);
  500. fprintf(pp, " 4=%d", global_pooling);
  501. }
  502. else if (node.op() == "Concat" || node.op() == "ConcatV2")
  503. {
  504. tensorflow::TensorProto tensor;
  505. if (find_tensor_proto(weights, node, tensor))
  506. {
  507. // TODO
  508. // int axis = tensor.int_val(0);
  509. }
  510. }
  511. else if (node.op() == "Const" || node.op() == "Identity")
  512. {
  513. // check before binaryop
  514. tensorflow::TensorProto tensor;
  515. if (get_tensor_proto(binaryop_consts, node, tensor))
  516. {
  517. const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
  518. int w = 0;
  519. int h = 0;
  520. int c = 0;
  521. if (shape.dim_size() == 1)
  522. {
  523. w = shape.dim(0).size();
  524. }
  525. else if (shape.dim_size() == 2)
  526. {
  527. h = shape.dim(0).size();
  528. w = shape.dim(1).size();
  529. }
  530. else if (shape.dim_size() == 3)
  531. {
  532. c = shape.dim(2).size();
  533. h = shape.dim(0).size();
  534. w = shape.dim(1).size();
  535. }
  536. int weight_data_size = 0;
  537. if (!tensor.tensor_content().empty())
  538. {
  539. if (tensor.dtype() == 1)// float
  540. {
  541. const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
  542. weight_data_size = tensor.tensor_content().size() / sizeof(float);
  543. if (c == 0)
  544. fwrite(data, sizeof(float), weight_data_size, bp);
  545. else
  546. {
  547. float tmp;
  548. // h-w-c to c-h-w
  549. for (int p=0; p<c; p++)
  550. {
  551. for (int i=0; i<h; i++)
  552. {
  553. for (int j=0; j<w; j++)
  554. {
  555. tmp = data[i*w*c + j*c + p];
  556. fwrite(&tmp, sizeof(float), 1, bp);
  557. }
  558. }
  559. }
  560. }
  561. }
  562. else if (tensor.dtype() == 3)// int32
  563. {
  564. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  565. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  566. float tmp;
  567. if (c == 0)
  568. {
  569. for (int i=0; i<weight_data_size; i++)
  570. {
  571. tmp = data[i];
  572. fwrite(&tmp, sizeof(float), 1, bp);
  573. }
  574. }
  575. else
  576. {
  577. // h-w-c to c-h-w
  578. for (int p=0; p<c; p++)
  579. {
  580. for (int i=0; i<h; i++)
  581. {
  582. for (int j=0; j<w; j++)
  583. {
  584. tmp = data[i*w*c + j*c + p];
  585. fwrite(&tmp, sizeof(float), 1, bp);
  586. }
  587. }
  588. }
  589. }
  590. }
  591. }
  592. else
  593. {
  594. if (tensor.dtype() == 1)// float
  595. {
  596. float val = tensor.float_val(0);
  597. fwrite(&val, sizeof(float), 1, bp);
  598. }
  599. else if (tensor.dtype() == 3)// int32
  600. {
  601. float val = tensor.int_val(0);
  602. fwrite(&val, sizeof(float), 1, bp);
  603. }
  604. }
  605. fprintf(pp, " 0=%d", w);
  606. fprintf(pp, " 1=%d", h);
  607. fprintf(pp, " 2=%d", c);
  608. }
  609. }
  610. else if (node.op() == "Conv2D")
  611. {
  612. // weights
  613. tensorflow::TensorProto tensor;
  614. find_tensor_proto(weights, node, tensor);
  615. const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
  616. int kernel_size_h = shape.dim(0).size();
  617. int kernel_size_w = shape.dim(1).size();
  618. int num_input = shape.dim(2).size();
  619. int num_output = shape.dim(3).size();
  620. int stride_h = 1;
  621. int stride_w = 1;
  622. int dilation_h = 1;
  623. int dilation_w = 1;
  624. int pad = 0;
  625. tensorflow::AttrValue value_strides;
  626. if (find_attr_value(node, "strides", value_strides))
  627. {
  628. // batch, height, width, channels
  629. stride_h = value_strides.list().i(1);
  630. stride_w = value_strides.list().i(2);
  631. }
  632. tensorflow::AttrValue value_padding;
  633. if (find_attr_value(node, "padding", value_padding))
  634. {
  635. if (value_padding.s() == "VALID")
  636. {
  637. pad = 0;
  638. }
  639. else if (value_padding.s() == "SAME")
  640. {
  641. pad = -233;
  642. }
  643. }
  644. tensorflow::AttrValue value_rate;
  645. if (find_attr_value(node, "rate", value_rate))
  646. {
  647. // height, width
  648. dilation_h = value_rate.list().i(0);
  649. dilation_w = value_rate.list().i(1);
  650. }
  651. int bias_term = 0;
  652. int weight_data_size = 0;
  653. // reorder h-w-i-o to o-i-h-w
  654. if (!tensor.tensor_content().empty())
  655. {
  656. int quantize_tag = 0;
  657. fwrite(&quantize_tag, sizeof(int), 1, bp);
  658. if (tensor.dtype() == 1)// float
  659. {
  660. const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
  661. weight_data_size = tensor.tensor_content().size() / sizeof(float);
  662. float tmp;
  663. for (int p=0; p<num_output; p++)
  664. {
  665. for (int q=0; q<num_input; q++)
  666. {
  667. for (int i=0; i<kernel_size_h; i++)
  668. {
  669. for (int j=0; j<kernel_size_w; j++)
  670. {
  671. tmp = data[i*kernel_size_w*num_input*num_output + j*num_input*num_output + q*num_output + p];
  672. fwrite(&tmp, sizeof(float), 1, bp);
  673. }
  674. }
  675. }
  676. }
  677. }
  678. else if (tensor.dtype() == 3)// int32
  679. {
  680. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  681. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  682. float tmp;
  683. for (int p=0; p<num_output; p++)
  684. {
  685. for (int q=0; q<num_input; q++)
  686. {
  687. for (int i=0; i<kernel_size_h; i++)
  688. {
  689. for (int j=0; j<kernel_size_w; j++)
  690. {
  691. tmp = data[i*kernel_size_w*num_input*num_output + j*num_input*num_output + q*num_output + p];
  692. fwrite(&tmp, sizeof(float), 1, bp);
  693. }
  694. }
  695. }
  696. }
  697. }
  698. }
  699. fprintf(pp, " 0=%d", num_output);
  700. fprintf(pp, " 1=%d", kernel_size_w);
  701. fprintf(pp, " 2=%d", dilation_w);
  702. fprintf(pp, " 3=%d", stride_w);
  703. fprintf(pp, " 4=%d", pad);
  704. fprintf(pp, " 5=%d", bias_term);
  705. fprintf(pp, " 6=%d", weight_data_size);
  706. }
  707. else if (node.op() == "DepthwiseConv2dNative")
  708. {
  709. // weights
  710. tensorflow::TensorProto tensor;
  711. find_tensor_proto(weights, node, tensor);
  712. const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
  713. int kernel_size_h = shape.dim(0).size();
  714. int kernel_size_w = shape.dim(1).size();
  715. int num_input = shape.dim(2).size();
  716. int channel_multiplier = shape.dim(3).size();
  717. int num_output = num_input * channel_multiplier;
  718. int group = num_input;
  719. int stride_h = 1;
  720. int stride_w = 1;
  721. int dilation_h = 1;
  722. int dilation_w = 1;
  723. int pad = 0;
  724. tensorflow::AttrValue value_strides;
  725. if (find_attr_value(node, "strides", value_strides))
  726. {
  727. // batch, height, width, channels
  728. stride_h = value_strides.list().i(1);
  729. stride_w = value_strides.list().i(2);
  730. }
  731. tensorflow::AttrValue value_padding;
  732. if (find_attr_value(node, "padding", value_padding))
  733. {
  734. if (value_padding.s() == "VALID")
  735. {
  736. pad = 0;
  737. }
  738. else if (value_padding.s() == "SAME")
  739. {
  740. pad = -233;
  741. }
  742. }
  743. tensorflow::AttrValue value_rate;
  744. if (find_attr_value(node, "rate", value_rate))
  745. {
  746. // height, width
  747. dilation_h = value_rate.list().i(0);
  748. dilation_w = value_rate.list().i(1);
  749. }
  750. int bias_term = 0;
  751. int weight_data_size = 0;
  752. // reorder h-w-i-cm to i-cm-h-w
  753. if (!tensor.tensor_content().empty())
  754. {
  755. int quantize_tag = 0;
  756. fwrite(&quantize_tag, sizeof(int), 1, bp);
  757. if (tensor.dtype() == 1)// float
  758. {
  759. const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
  760. weight_data_size = tensor.tensor_content().size() / sizeof(float);
  761. float tmp;
  762. for (int p=0; p<num_input; p++)
  763. {
  764. for (int q=0; q<channel_multiplier; q++)
  765. {
  766. for (int i=0; i<kernel_size_h; i++)
  767. {
  768. for (int j=0; j<kernel_size_w; j++)
  769. {
  770. tmp = data[i*kernel_size_w*channel_multiplier*num_input + j*channel_multiplier*num_input + p*channel_multiplier + q];
  771. fwrite(&tmp, sizeof(float), 1, bp);
  772. }
  773. }
  774. }
  775. }
  776. }
  777. else if (tensor.dtype() == 3)// int32
  778. {
  779. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  780. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  781. float tmp;
  782. for (int p=0; p<num_input; p++)
  783. {
  784. for (int q=0; q<channel_multiplier; q++)
  785. {
  786. for (int i=0; i<kernel_size_h; i++)
  787. {
  788. for (int j=0; j<kernel_size_w; j++)
  789. {
  790. tmp = data[i*kernel_size_w*channel_multiplier*num_input + j*channel_multiplier*num_input + p*channel_multiplier + q];
  791. fwrite(&tmp, sizeof(float), 1, bp);
  792. }
  793. }
  794. }
  795. }
  796. }
  797. }
  798. fprintf(pp, " 0=%d", num_output);
  799. fprintf(pp, " 1=%d", kernel_size_w);
  800. fprintf(pp, " 2=%d", dilation_w);
  801. fprintf(pp, " 3=%d", stride_w);
  802. fprintf(pp, " 4=%d", pad);
  803. fprintf(pp, " 5=%d", bias_term);
  804. fprintf(pp, " 6=%d", weight_data_size);
  805. fprintf(pp, " 7=%d", group);
  806. }
  807. else if (node.op() == "Div" || node.op() == "RealDiv")
  808. {
  809. int op_type = 3;
  810. fprintf(pp, " 0=%d", op_type);
  811. }
  812. else if (node.op() == "Exp")
  813. {
  814. int op_type = 7;
  815. fprintf(pp, " 0=%d", op_type);
  816. }
  817. else if (node.op() == "ExpandDims")
  818. {
  819. int expand_w = 0;
  820. int expand_h = 0;
  821. int expand_c = 0;
  822. tensorflow::AttrValue value_dim;
  823. if (find_attr_value(node, "Tdim", value_dim))
  824. {
  825. int dim = value_dim.i();
  826. if (dim == 0)
  827. expand_w = 1;
  828. if (dim == 1)
  829. expand_h = 1;
  830. if (dim == 2)
  831. expand_c = 1;
  832. }
  833. fprintf(pp, " 0=%d", expand_w);
  834. fprintf(pp, " 1=%d", expand_h);
  835. fprintf(pp, " 2=%d", expand_c);
  836. }
  837. else if (node.op() == "Floor")
  838. {
  839. int op_type = 2;
  840. fprintf(pp, " 0=%d", op_type);
  841. }
  842. else if (node.op() == "LRN")
  843. {
  844. int norm_region = 0;
  845. int local_size = 1;
  846. float alpha = 1.f;
  847. float beta = 0.5f;
  848. tensorflow::AttrValue value_depth_radius;
  849. if (find_attr_value(node, "depth_radius", value_depth_radius))
  850. {
  851. local_size = value_depth_radius.i() * 2 + 1;
  852. }
  853. tensorflow::AttrValue value_alpha;
  854. if (find_attr_value(node, "alpha", value_alpha))
  855. {
  856. alpha = value_alpha.f();
  857. }
  858. tensorflow::AttrValue value_beta;
  859. if (find_attr_value(node, "beta", value_beta))
  860. {
  861. beta = value_beta.f();
  862. }
  863. // TODO
  864. float bias = 1.f;
  865. tensorflow::AttrValue value_bias;
  866. if (find_attr_value(node, "bias", value_bias))
  867. {
  868. bias = value_bias.f();
  869. }
  870. fprintf(pp, " 0=%d", norm_region);
  871. fprintf(pp, " 1=%d", local_size);
  872. fprintf(pp, " 2=%f", alpha);
  873. fprintf(pp, " 3=%f", beta);
  874. }
  875. else if (node.op() == "MatMul")
  876. {
  877. // weights
  878. tensorflow::TensorProto tensor;
  879. find_tensor_proto(weights, node, tensor);
  880. const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
  881. int num_input = shape.dim(0).size();
  882. int num_output = shape.dim(1).size();
  883. int bias_term = 0;
  884. int weight_data_size = 0;
  885. // reorder i-o to o-i
  886. if (!tensor.tensor_content().empty())
  887. {
  888. int quantize_tag = 0;
  889. fwrite(&quantize_tag, sizeof(int), 1, bp);
  890. if (tensor.dtype() == 1)// float
  891. {
  892. const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
  893. weight_data_size = tensor.tensor_content().size() / sizeof(float);
  894. float tmp;
  895. for (int p=0; p<num_output; p++)
  896. {
  897. for (int q=0; q<num_input; q++)
  898. {
  899. tmp = data[q*num_output + p];
  900. fwrite(&tmp, sizeof(float), 1, bp);
  901. }
  902. }
  903. }
  904. else if (tensor.dtype() == 3)// int32
  905. {
  906. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  907. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  908. float tmp;
  909. for (int p=0; p<num_output; p++)
  910. {
  911. for (int q=0; q<num_input; q++)
  912. {
  913. tmp = data[q*num_output + p];
  914. fwrite(&tmp, sizeof(float), 1, bp);
  915. }
  916. }
  917. }
  918. }
  919. fprintf(pp, " 0=%d", num_output);
  920. fprintf(pp, " 1=%d", bias_term);
  921. fprintf(pp, " 2=%d", weight_data_size);
  922. }
  923. else if (node.op() == "Max" || node.op() == "Maximum")
  924. {
  925. // check weights
  926. tensorflow::TensorProto tensor;
  927. if (find_tensor_proto(weights, node, tensor))
  928. {
  929. int operation = 4;
  930. int dim = 0;
  931. float coeff = 1.f;
  932. dim = parse_tensor_reduction_dim(tensor);
  933. fprintf(pp, " 0=%d", operation);
  934. fprintf(pp, " 1=%d", dim);
  935. fprintf(pp, " 2=%f", coeff);
  936. }
  937. else
  938. {
  939. int op_type = 4;
  940. fprintf(pp, " 0=%d", op_type);
  941. }
  942. }
  943. else if (node.op() == "MaxPool")
  944. {
  945. int pooling_type = 0;
  946. int kernel_size_h = 1;
  947. int kernel_size_w = 1;
  948. int stride_h = 1;
  949. int stride_w = 1;
  950. int pad = 0;
  951. int global_pooling = 0;
  952. tensorflow::AttrValue value_ksize;
  953. if (find_attr_value(node, "ksize", value_ksize))
  954. {
  955. // batch, height, width, channels
  956. kernel_size_h = value_ksize.list().i(1);
  957. kernel_size_w = value_ksize.list().i(2);
  958. }
  959. tensorflow::AttrValue value_strides;
  960. if (find_attr_value(node, "strides", value_strides))
  961. {
  962. // batch, height, width, channels
  963. stride_h = value_strides.list().i(1);
  964. stride_w = value_strides.list().i(2);
  965. }
  966. tensorflow::AttrValue value_padding;
  967. if (find_attr_value(node, "padding", value_padding))
  968. {
  969. if (value_padding.s() == "VALID")
  970. {
  971. pad = -2333;
  972. }
  973. else if (value_padding.s() == "SAME")
  974. {
  975. pad = -233;
  976. }
  977. }
  978. fprintf(pp, " 0=%d", pooling_type);
  979. fprintf(pp, " 1=%d", kernel_size_w);
  980. fprintf(pp, " 2=%d", stride_w);
  981. fprintf(pp, " 3=%d", pad);
  982. fprintf(pp, " 4=%d", global_pooling);
  983. }
  984. else if (node.op() == "Min" || node.op() == "Minimum")
  985. {
  986. // check weights
  987. tensorflow::TensorProto tensor;
  988. if (find_tensor_proto(weights, node, tensor))
  989. {
  990. int operation = 5;
  991. int dim = 0;
  992. float coeff = 1.f;
  993. dim = parse_tensor_reduction_dim(tensor);
  994. fprintf(pp, " 0=%d", operation);
  995. fprintf(pp, " 1=%d", dim);
  996. fprintf(pp, " 2=%f", coeff);
  997. }
  998. else
  999. {
  1000. int op_type = 5;
  1001. fprintf(pp, " 0=%d", op_type);
  1002. }
  1003. }
  1004. else if (node.op() == "Mul")
  1005. {
  1006. int op_type = 2;
  1007. fprintf(pp, " 0=%d", op_type);
  1008. }
  1009. else if (node.op() == "Neg")
  1010. {
  1011. int op_type = 1;
  1012. fprintf(pp, " 0=%d", op_type);
  1013. }
  1014. else if (node.op() == "NoOp")
  1015. {
  1016. }
  1017. else if (node.op() == "Pad")
  1018. {
  1019. int top = 0;
  1020. int bottom = 0;
  1021. int left = 0;
  1022. int right = 0;
  1023. int type = 0;
  1024. float value = 0.f;
  1025. // check weights
  1026. tensorflow::TensorProto tensor;
  1027. if (find_tensor_proto(weights, node, tensor))
  1028. {
  1029. if (!tensor.tensor_content().empty() && tensor.dtype() == 3)// int32
  1030. {
  1031. const int *data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  1032. int size = tensor.tensor_content().size() / sizeof(int);
  1033. if (size == 8)
  1034. {
  1035. // n h w c
  1036. top = data[2];
  1037. bottom = data[3];
  1038. left = data[4];
  1039. right = data[5];
  1040. }
  1041. }
  1042. }
  1043. tensorflow::AttrValue value_Tpaddings;
  1044. if (find_attr_value(node, "Tpaddings", value_Tpaddings))
  1045. {
  1046. type = value_Tpaddings.i();
  1047. }
  1048. tensorflow::AttrValue value_T;
  1049. if (find_attr_value(node, "T", value_T))
  1050. {
  1051. value = value_T.f();
  1052. }
  1053. fprintf(pp, " 0=%d", top);
  1054. fprintf(pp, " 1=%d", bottom);
  1055. fprintf(pp, " 2=%d", left);
  1056. fprintf(pp, " 3=%d", right);
  1057. fprintf(pp, " 4=%d", type);
  1058. fprintf(pp, " 5=%f", value);
  1059. }
  1060. else if (node.op() == "Placeholder")
  1061. {
  1062. // TODO pass through
  1063. fprintf(pp, " 0=0 1=0 2=0");
  1064. }
  1065. else if (node.op() == "Prod")
  1066. {
  1067. int operation = 6;
  1068. int dim = 0;
  1069. float coeff = 1.f;
  1070. // check weights
  1071. tensorflow::TensorProto tensor;
  1072. if (find_tensor_proto(weights, node, tensor))
  1073. {
  1074. dim = parse_tensor_reduction_dim(tensor);
  1075. }
  1076. fprintf(pp, " 0=%d", operation);
  1077. fprintf(pp, " 1=%d", dim);
  1078. fprintf(pp, " 2=%f", coeff);
  1079. }
  1080. else if (node.op() == "Reciprocal")
  1081. {
  1082. int op_type = 15;
  1083. fprintf(pp, " 0=%d", op_type);
  1084. }
  1085. else if (node.op() == "Relu")
  1086. {
  1087. float slope = 0.f;
  1088. fprintf(pp, " 0=%f", slope);
  1089. }
  1090. else if (node.op() == "Reshape")
  1091. {
  1092. tensorflow::TensorProto tensor;
  1093. if (find_tensor_proto(weights, node, tensor))
  1094. {
  1095. if (!tensor.tensor_content().empty() && tensor.dtype() == 3)// int32
  1096. {
  1097. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  1098. int size = tensor.tensor_content().size() / sizeof(int);
  1099. // n h w c
  1100. // n h w
  1101. // n w
  1102. if (size == 4)
  1103. {
  1104. fprintf(pp, " 0=%d 1=%d 2=%d 3=0", data[2], data[1], data[3]);
  1105. }
  1106. if (size == 3)
  1107. {
  1108. fprintf(pp, " 0=%d 1=%d 2=-233 3=1", data[2], data[1]);
  1109. }
  1110. if (size == 2)
  1111. {
  1112. fprintf(pp, " 0=%d 1=-233 2=-233 3=1", data[1]);
  1113. }
  1114. }
  1115. }
  1116. else
  1117. {
  1118. // pass through
  1119. fprintf(pp, " 0=0 1=0 2=0 3=0");
  1120. }
  1121. }
  1122. else if (node.op() == "Rsqrt")
  1123. {
  1124. int op_type = 6;
  1125. fprintf(pp, " 0=%d", op_type);
  1126. }
  1127. else if (node.op() == "Sigmoid")
  1128. {
  1129. }
  1130. else if (node.op() == "Softmax")
  1131. {
  1132. }
  1133. else if (node.op() == "Square")
  1134. {
  1135. int op_type = 4;
  1136. fprintf(pp, " 0=%d", op_type);
  1137. }
  1138. else if (node.op() == "Squeeze")
  1139. {
  1140. int squeeze_w = 0;
  1141. int squeeze_h = 0;
  1142. int squeeze_c = 0;
  1143. tensorflow::AttrValue value_squeeze_dims;
  1144. if (find_attr_value(node, "squeeze_dims", value_squeeze_dims))
  1145. {
  1146. for (int i = 0; i<value_squeeze_dims.list().i_size(); i++)
  1147. {
  1148. int dim = value_squeeze_dims.list().i(i);
  1149. if (dim == 0)
  1150. squeeze_w = 1;
  1151. if (dim == 1)
  1152. squeeze_h = 1;
  1153. if (dim == 2)
  1154. squeeze_c = 1;
  1155. }
  1156. }
  1157. fprintf(pp, " 0=%d", squeeze_w);
  1158. fprintf(pp, " 1=%d", squeeze_h);
  1159. fprintf(pp, " 2=%d", squeeze_c);
  1160. }
  1161. else if (node.op() == "Sub")
  1162. {
  1163. int op_type = 1;
  1164. fprintf(pp, " 0=%d", op_type);
  1165. }
  1166. else if (node.op() == "Sum")
  1167. {
  1168. int operation = 0;
  1169. int dim = 0;
  1170. float coeff = 1.f;
  1171. // check weights
  1172. tensorflow::TensorProto tensor;
  1173. if (find_tensor_proto(weights, node, tensor))
  1174. {
  1175. dim = parse_tensor_reduction_dim(tensor);
  1176. }
  1177. fprintf(pp, " 0=%d", operation);
  1178. fprintf(pp, " 1=%d", dim);
  1179. fprintf(pp, " 2=%f", coeff);
  1180. }
  1181. else
  1182. {
  1183. const google::protobuf::Map<std::string, tensorflow::AttrValue>& attr = node.attr();
  1184. google::protobuf::Map<std::string, tensorflow::AttrValue>::const_iterator it = attr.begin();
  1185. for (; it != attr.end(); it++)
  1186. {
  1187. std::cerr << it->first << " #" << it->second.type() << std::endl;
  1188. }
  1189. }
  1190. fprintf(pp, "\n");
  1191. std::string output_name = node.name();
  1192. if (node_reference.find(output_name) != node_reference.end())
  1193. {
  1194. int refcount = node_reference[output_name];
  1195. if (refcount > 1)
  1196. {
  1197. char splitname[256];
  1198. sprintf(splitname, "splitncnn_%d", internal_split);
  1199. fprintf(pp, "%-16s %-32s %d %d", "Split", splitname, 1, refcount);
  1200. fprintf(pp, " %s", output_name.c_str());
  1201. for (int j=0; j<refcount; j++)
  1202. {
  1203. fprintf(pp, " %s_splitncnn_%d", output_name.c_str(), j);
  1204. }
  1205. fprintf(pp, "\n");
  1206. internal_split++;
  1207. }
  1208. }
  1209. }
  1210. fclose(pp);
  1211. fclose(bp);
  1212. return 0;
  1213. }