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 25 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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 find_attr_value(const tensorflow::NodeDef& node, const char* key, tensorflow::AttrValue& value)
  57. {
  58. const google::protobuf::Map<std::string, tensorflow::AttrValue>& attr = node.attr();
  59. const google::protobuf::Map<std::string, tensorflow::AttrValue>::const_iterator it = attr.find(key);
  60. if (it != attr.end())
  61. {
  62. value = it->second;
  63. return true;
  64. }
  65. return false;
  66. }
  67. int main(int argc, char** argv)
  68. {
  69. const char* tensorflowpb = argv[1];
  70. const char* ncnn_prototxt = argc >= 4 ? argv[2] : "ncnn.proto";
  71. const char* ncnn_modelbin = argc >= 4 ? argv[3] : "ncnn.bin";
  72. tensorflow::GraphDef graph;
  73. // load
  74. bool s1 = read_proto_from_binary(tensorflowpb, &graph);
  75. if (!s1)
  76. {
  77. fprintf(stderr, "read_proto_from_binary failed\n");
  78. return -1;
  79. }
  80. FILE* pp = fopen(ncnn_prototxt, "wb");
  81. FILE* bp = fopen(ncnn_modelbin, "wb");
  82. int node_count = graph.node_size();
  83. // fprintf(stderr, "node_count = %d\n\n", node_count);
  84. // node reference
  85. std::map<std::string, int> node_reference;
  86. // mapping for Const and Const-Identity
  87. std::map<std::string, tensorflow::TensorProto> weights;
  88. // Dropout like Identity
  89. std::set<std::string> dropouts;
  90. // global definition line
  91. // [layer count] [blob count]
  92. std::set<std::string> blob_names;
  93. for (int i=0; i<node_count; i++)
  94. {
  95. const tensorflow::NodeDef& node = graph.node(i);
  96. const std::string& output_name = node.name();
  97. if (node.op() == "Const")
  98. {
  99. tensorflow::AttrValue value;
  100. if (find_attr_value(node, "value", value))
  101. {
  102. const tensorflow::TensorProto& tensor = value.tensor();
  103. weights[output_name] = tensor;
  104. }
  105. continue;
  106. }
  107. else if (node.op() == "Identity")
  108. {
  109. const std::string& input_name = node.input(0);
  110. if (weights.find(input_name) != weights.end())
  111. {
  112. weights[output_name] = weights[input_name];
  113. continue;
  114. }
  115. else
  116. {
  117. dropouts.insert(output_name);
  118. }
  119. }
  120. else if (node.op() == "NoOp")
  121. {
  122. weights[output_name] = tensorflow::TensorProto();
  123. continue;
  124. }
  125. // input
  126. for (int j=0; j<node.input_size(); j++)
  127. {
  128. const std::string& input_name = node.input(j);
  129. // fprintf(stderr, "input = %s\n", input_name.c_str());
  130. if (weights.find(input_name) != weights.end())
  131. {
  132. continue;
  133. }
  134. blob_names.insert(input_name);
  135. if (node_reference.find(input_name) == node_reference.end())
  136. {
  137. node_reference[input_name] = 1;
  138. }
  139. else
  140. {
  141. node_reference[input_name] = node_reference[input_name] + 1;
  142. }
  143. }
  144. // output
  145. // fprintf(stderr, "output = %s\n", output_name.c_str());
  146. blob_names.insert(output_name);
  147. }
  148. // remove node_reference entry with reference equals to one
  149. int splitncnn_blob_count = 0;
  150. std::map<std::string, int>::iterator it = node_reference.begin();
  151. while (it != node_reference.end())
  152. {
  153. if (it->second == 1)
  154. {
  155. node_reference.erase(it++);
  156. }
  157. else
  158. {
  159. splitncnn_blob_count += it->second;
  160. // fprintf(stderr, "%s %d\n", it->first.c_str(), it->second);
  161. ++it;
  162. }
  163. }
  164. fprintf(pp, "%lu %lu\n", node_count + node_reference.size() - weights.size(), blob_names.size() + splitncnn_blob_count);
  165. int internal_split = 0;
  166. for (int i=0; i<node_count; i++)
  167. {
  168. const tensorflow::NodeDef& node = graph.node(i);
  169. // layer definition line, repeated
  170. // [type] [name] [bottom blob count] [top blob count] [bottom blobs] [top blobs] [layer specific params]
  171. // fprintf(pp, "%-16s %-16s %d %d", layer.type().c_str(), layer.name().c_str(), node.input_size(), layer.top_size());
  172. if (node.op() == "Add" || node.op() == "BiasAdd")
  173. {
  174. // check weights
  175. tensorflow::TensorProto tensor;
  176. if (find_tensor_proto(weights, node, tensor))
  177. {
  178. fprintf(pp, "%-16s", "Bias");
  179. }
  180. else
  181. {
  182. fprintf(pp, "%-16s", "BinaryOp");
  183. }
  184. }
  185. else if (node.op() == "AvgPool")
  186. {
  187. fprintf(pp, "%-16s", "Pooling");
  188. }
  189. else if (node.op() == "Const")
  190. {
  191. continue;
  192. }
  193. else if (node.op() == "Conv2D")
  194. {
  195. fprintf(pp, "%-16s", "Convolution");
  196. }
  197. else if (node.op() == "Exp")
  198. {
  199. fprintf(pp, "%-16s", "UnaryOp");
  200. }
  201. else if (node.op() == "Identity")
  202. {
  203. if (dropouts.find(node.name()) != dropouts.end())
  204. {
  205. fprintf(pp, "%-16s", "Dropout");
  206. }
  207. else
  208. {
  209. continue;
  210. }
  211. }
  212. else if (node.op() == "MatMul")
  213. {
  214. fprintf(pp, "%-16s", "InnerProduct");
  215. }
  216. else if (node.op() == "Max")
  217. {
  218. fprintf(pp, "%-16s", "BinaryOp");
  219. }
  220. else if (node.op() == "MaxPool")
  221. {
  222. fprintf(pp, "%-16s", "Pooling");
  223. }
  224. else if (node.op() == "Mul")
  225. {
  226. // check weights
  227. tensorflow::TensorProto tensor;
  228. if (find_tensor_proto(weights, node, tensor))
  229. {
  230. fprintf(pp, "%-16s", "Scale");
  231. }
  232. else
  233. {
  234. fprintf(pp, "%-16s", "BinaryOp");
  235. }
  236. }
  237. else if (node.op() == "Neg")
  238. {
  239. fprintf(pp, "%-16s", "UnaryOp");
  240. }
  241. else if (node.op() == "NoOp")
  242. {
  243. continue;
  244. }
  245. else if (node.op() == "Placeholder")
  246. {
  247. fprintf(pp, "%-16s", "Input");
  248. }
  249. else if (node.op() == "RealDiv")
  250. {
  251. fprintf(pp, "%-16s", "BinaryOp");
  252. }
  253. else if (node.op() == "Relu")
  254. {
  255. fprintf(pp, "%-16s", "ReLU");
  256. }
  257. else if (node.op() == "Reshape")
  258. {
  259. fprintf(pp, "%-16s", "Reshape");
  260. }
  261. else if (node.op() == "Rsqrt")
  262. {
  263. fprintf(pp, "%-16s", "UnaryOp");
  264. }
  265. else if (node.op() == "Softmax")
  266. {
  267. fprintf(pp, "%-16s", "Softmax");
  268. }
  269. else if (node.op() == "Sub")
  270. {
  271. fprintf(pp, "%-16s", "BinaryOp");
  272. }
  273. else
  274. {
  275. fprintf(pp, "%-16s", node.op().c_str());
  276. }
  277. int input_size = node.input_size();
  278. for (int j=0; j<node.input_size(); j++)
  279. {
  280. const std::string& input_name = node.input(j);
  281. if (weights.find(input_name) != weights.end())
  282. {
  283. input_size--;
  284. }
  285. }
  286. fprintf(pp, " %-16s %d 1", node.name().c_str(), input_size);
  287. for (int j=0; j<node.input_size(); j++)
  288. {
  289. std::string input_name = node.input(j);
  290. if (weights.find(input_name) != weights.end())
  291. {
  292. continue;
  293. }
  294. if (node_reference.find(input_name) != node_reference.end())
  295. {
  296. int refidx = node_reference[input_name] - 1;
  297. node_reference[input_name] = refidx;
  298. char splitsuffix[256];
  299. sprintf(splitsuffix, "_splitncnn_%d", refidx);
  300. input_name = input_name + splitsuffix;
  301. }
  302. fprintf(pp, " %s", input_name.c_str());
  303. }
  304. fprintf(pp, " %s", node.name().c_str());
  305. if (node.op() == "Add" || node.op() == "BiasAdd")
  306. {
  307. // check weights
  308. tensorflow::TensorProto tensor;
  309. if (find_tensor_proto(weights, node, tensor))
  310. {
  311. int weight_data_size = 0;
  312. if (!tensor.tensor_content().empty())
  313. {
  314. if (tensor.dtype() == 1)// float
  315. {
  316. const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
  317. weight_data_size = tensor.tensor_content().size() / sizeof(float);
  318. fwrite(data, sizeof(float), weight_data_size, bp);
  319. }
  320. else if (tensor.dtype() == 3)// int32
  321. {
  322. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  323. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  324. float tmp;
  325. for (int i=0; i<weight_data_size; i++)
  326. {
  327. tmp = data[i];
  328. fwrite(&tmp, sizeof(float), 1, bp);
  329. }
  330. }
  331. }
  332. fprintf(pp, " %d", weight_data_size);
  333. }
  334. else
  335. {
  336. int op_type = 1;
  337. fprintf(pp, " %d", op_type);
  338. }
  339. }
  340. else if (node.op() == "AvgPool")
  341. {
  342. int pooling_type = 1;
  343. int kernel_size_h = 1;
  344. int kernel_size_w = 1;
  345. int stride_h = 1;
  346. int stride_w = 1;
  347. int pad = 0;
  348. int global_pooling = 0;
  349. tensorflow::AttrValue value_ksize;
  350. if (find_attr_value(node, "ksize", value_ksize))
  351. {
  352. // batch, height, width, channels
  353. kernel_size_h = value_ksize.list().i(1);
  354. kernel_size_w = value_ksize.list().i(2);
  355. }
  356. tensorflow::AttrValue value_strides;
  357. if (find_attr_value(node, "strides", value_strides))
  358. {
  359. // batch, height, width, channels
  360. stride_h = value_strides.list().i(1);
  361. stride_w = value_strides.list().i(2);
  362. }
  363. tensorflow::AttrValue value_padding;
  364. if (find_attr_value(node, "padding", value_padding))
  365. {
  366. if (value_padding.s() == "VALID")
  367. {
  368. pad = 0;
  369. }
  370. else if (value_padding.s() == "SAME")
  371. {
  372. pad = -233;
  373. }
  374. }
  375. fprintf(pp, " %d %d %d %d %d", pooling_type, kernel_size_w, stride_w, pad, global_pooling);
  376. }
  377. else if (node.op() == "Const")
  378. {
  379. }
  380. else if (node.op() == "Conv2D")
  381. {
  382. // weights
  383. tensorflow::TensorProto tensor;
  384. find_tensor_proto(weights, node, tensor);
  385. const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
  386. int kernel_size_h = shape.dim(0).size();
  387. int kernel_size_w = shape.dim(1).size();
  388. int num_input = shape.dim(2).size();
  389. int num_output = shape.dim(3).size();
  390. int stride_h = 1;
  391. int stride_w = 1;
  392. int dilation = 1;
  393. int pad = 0;
  394. tensorflow::AttrValue value_strides;
  395. if (find_attr_value(node, "strides", value_strides))
  396. {
  397. // batch, height, width, channels
  398. stride_h = value_strides.list().i(1);
  399. stride_w = value_strides.list().i(2);
  400. }
  401. tensorflow::AttrValue value_padding;
  402. if (find_attr_value(node, "padding", value_padding))
  403. {
  404. if (value_padding.s() == "VALID")
  405. {
  406. pad = 0;
  407. }
  408. else if (value_padding.s() == "SAME")
  409. {
  410. pad = -233;
  411. }
  412. }
  413. int bias_term = 0;
  414. int weight_data_size = 0;
  415. // reorder h-w-i-o to o-i-h-w
  416. if (!tensor.tensor_content().empty())
  417. {
  418. int quantize_tag = 0;
  419. fwrite(&quantize_tag, sizeof(int), 1, bp);
  420. if (tensor.dtype() == 1)// float
  421. {
  422. const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
  423. weight_data_size = tensor.tensor_content().size() / sizeof(float);
  424. float tmp;
  425. for (int p=0; p<num_output; p++)
  426. {
  427. for (int q=0; q<num_input; q++)
  428. {
  429. for (int i=0; i<kernel_size_h; i++)
  430. {
  431. for (int j=0; j<kernel_size_w; j++)
  432. {
  433. tmp = data[i*kernel_size_w*num_input*num_output + j*num_input*num_output + q*num_output + p];
  434. fwrite(&tmp, sizeof(float), 1, bp);
  435. }
  436. }
  437. }
  438. }
  439. }
  440. else if (tensor.dtype() == 3)// int32
  441. {
  442. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  443. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  444. float tmp;
  445. for (int p=0; p<num_output; p++)
  446. {
  447. for (int q=0; q<num_input; q++)
  448. {
  449. for (int i=0; i<kernel_size_h; i++)
  450. {
  451. for (int j=0; j<kernel_size_w; j++)
  452. {
  453. tmp = data[i*kernel_size_w*num_input*num_output + j*num_input*num_output + q*num_output + p];
  454. fwrite(&tmp, sizeof(float), 1, bp);
  455. }
  456. }
  457. }
  458. }
  459. }
  460. }
  461. fprintf(pp, " %d %d %d %d %d %d %d", num_output, kernel_size_w, dilation, stride_w, pad, bias_term, weight_data_size);
  462. }
  463. else if (node.op() == "Exp")
  464. {
  465. int op_type = 7;
  466. fprintf(pp, " %d", op_type);
  467. }
  468. else if (node.op() == "Identity")
  469. {
  470. }
  471. else if (node.op() == "MatMul")
  472. {
  473. // weights
  474. tensorflow::TensorProto tensor;
  475. find_tensor_proto(weights, node, tensor);
  476. const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
  477. int num_input = shape.dim(0).size();
  478. int num_output = shape.dim(1).size();
  479. int bias_term = 0;
  480. int weight_data_size = 0;
  481. // reorder i-o to o-i
  482. if (!tensor.tensor_content().empty())
  483. {
  484. int quantize_tag = 0;
  485. fwrite(&quantize_tag, sizeof(int), 1, bp);
  486. if (tensor.dtype() == 1)// float
  487. {
  488. const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
  489. weight_data_size = tensor.tensor_content().size() / sizeof(float);
  490. float tmp;
  491. for (int p=0; p<num_output; p++)
  492. {
  493. for (int q=0; q<num_input; q++)
  494. {
  495. tmp = data[q*num_output + p];
  496. fwrite(&tmp, sizeof(float), 1, bp);
  497. }
  498. }
  499. }
  500. else if (tensor.dtype() == 3)// int32
  501. {
  502. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  503. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  504. float tmp;
  505. for (int p=0; p<num_output; p++)
  506. {
  507. for (int q=0; q<num_input; q++)
  508. {
  509. tmp = data[q*num_output + p];
  510. fwrite(&tmp, sizeof(float), 1, bp);
  511. }
  512. }
  513. }
  514. }
  515. fprintf(pp, " %d %d %d", num_output, bias_term, weight_data_size);
  516. }
  517. else if (node.op() == "Max")
  518. {
  519. int op_type = 4;
  520. fprintf(pp, " %d", op_type);
  521. }
  522. else if (node.op() == "MaxPool")
  523. {
  524. int pooling_type = 0;
  525. int kernel_size_h = 1;
  526. int kernel_size_w = 1;
  527. int stride_h = 1;
  528. int stride_w = 1;
  529. int pad = 0;
  530. int global_pooling = 0;
  531. tensorflow::AttrValue value_ksize;
  532. if (find_attr_value(node, "ksize", value_ksize))
  533. {
  534. // batch, height, width, channels
  535. kernel_size_h = value_ksize.list().i(1);
  536. kernel_size_w = value_ksize.list().i(2);
  537. }
  538. tensorflow::AttrValue value_strides;
  539. if (find_attr_value(node, "strides", value_strides))
  540. {
  541. // batch, height, width, channels
  542. stride_h = value_strides.list().i(1);
  543. stride_w = value_strides.list().i(2);
  544. }
  545. tensorflow::AttrValue value_padding;
  546. if (find_attr_value(node, "padding", value_padding))
  547. {
  548. if (value_padding.s() == "VALID")
  549. {
  550. pad = 0;
  551. }
  552. else if (value_padding.s() == "SAME")
  553. {
  554. pad = -233;
  555. }
  556. }
  557. fprintf(pp, " %d %d %d %d %d", pooling_type, kernel_size_w, stride_w, pad, global_pooling);
  558. }
  559. else if (node.op() == "Mul")
  560. {
  561. // check weights
  562. tensorflow::TensorProto tensor;
  563. if (find_tensor_proto(weights, node, tensor))
  564. {
  565. int scale_data_size = 0;
  566. int bias_term = 0;
  567. if (!tensor.tensor_content().empty())
  568. {
  569. if (tensor.dtype() == 1)// float
  570. {
  571. const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
  572. scale_data_size = tensor.tensor_content().size() / sizeof(float);
  573. fwrite(data, sizeof(float), scale_data_size, bp);
  574. }
  575. else if (tensor.dtype() == 3)// int32
  576. {
  577. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  578. scale_data_size = tensor.tensor_content().size() / sizeof(int);
  579. float tmp;
  580. for (int i=0; i<scale_data_size; i++)
  581. {
  582. tmp = data[i];
  583. fwrite(&tmp, sizeof(float), 1, bp);
  584. }
  585. }
  586. }
  587. fprintf(pp, " %d %d", scale_data_size, bias_term);
  588. }
  589. else
  590. {
  591. int op_type = 2;
  592. fprintf(pp, " %d", op_type);
  593. }
  594. }
  595. else if (node.op() == "Neg")
  596. {
  597. int op_type = 1;
  598. fprintf(pp, " %d", op_type);
  599. }
  600. else if (node.op() == "NoOp")
  601. {
  602. }
  603. else if (node.op() == "Placeholder")
  604. {
  605. // TODO pass through
  606. fprintf(pp, " 0 0 0");
  607. }
  608. else if (node.op() == "RealDiv")
  609. {
  610. int op_type = 3;
  611. fprintf(pp, " %d", op_type);
  612. }
  613. else if (node.op() == "Relu")
  614. {
  615. float slope = 0.f;
  616. fprintf(pp, " %f", slope);
  617. }
  618. else if (node.op() == "Reshape")
  619. {
  620. tensorflow::TensorProto tensor;
  621. if (find_tensor_proto(weights, node, tensor))
  622. {
  623. if (!tensor.tensor_content().empty() && tensor.dtype() == 3)// int32
  624. {
  625. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  626. int size = tensor.tensor_content().size() / sizeof(int);
  627. // n h w c
  628. // n h w
  629. // n w
  630. if (size == 4)
  631. {
  632. fprintf(pp, " %d %d %d 0", data[2], data[1], data[3]);
  633. }
  634. if (size == 3)
  635. {
  636. fprintf(pp, " %d %d -233 1", data[2], data[1]);
  637. }
  638. if (size == 2)
  639. {
  640. fprintf(pp, " %d -233 -233 1", data[1]);
  641. }
  642. }
  643. }
  644. else
  645. {
  646. // pass through
  647. fprintf(pp, " 0 0 0");
  648. }
  649. }
  650. else if (node.op() == "Rsqrt")
  651. {
  652. int op_type = 6;
  653. fprintf(pp, " %d", op_type);
  654. }
  655. else if (node.op() == "Softmax")
  656. {
  657. }
  658. else if (node.op() == "Sub")
  659. {
  660. int op_type = 1;
  661. fprintf(pp, " %d", op_type);
  662. }
  663. else
  664. {
  665. const google::protobuf::Map<std::string, tensorflow::AttrValue>& attr = node.attr();
  666. google::protobuf::Map<std::string, tensorflow::AttrValue>::const_iterator it = attr.begin();
  667. for (; it != attr.end(); it++)
  668. {
  669. std::cerr << it->first << std::endl;
  670. std::cerr << it->second.type() << std::endl;
  671. }
  672. }
  673. fprintf(pp, "\n");
  674. std::string output_name = node.name();
  675. if (node_reference.find(output_name) != node_reference.end())
  676. {
  677. int refcount = node_reference[output_name];
  678. if (refcount > 1)
  679. {
  680. char splitname[256];
  681. sprintf(splitname, "splitncnn_%d", internal_split);
  682. fprintf(pp, "%-16s %-16s %d %d", "Split", splitname, 1, refcount);
  683. fprintf(pp, " %s", output_name.c_str());
  684. for (int j=0; j<refcount; j++)
  685. {
  686. fprintf(pp, " %s_splitncnn_%d", output_name.c_str(), j);
  687. }
  688. fprintf(pp, "\n");
  689. internal_split++;
  690. }
  691. }
  692. }
  693. fclose(pp);
  694. fclose(bp);
  695. return 0;
  696. }