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 31 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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. int main(int argc, char** argv)
  80. {
  81. const char* tensorflowpb = argv[1];
  82. const char* ncnn_prototxt = argc >= 4 ? argv[2] : "ncnn.proto";
  83. const char* ncnn_modelbin = argc >= 4 ? argv[3] : "ncnn.bin";
  84. tensorflow::GraphDef graph;
  85. // load
  86. bool s1 = read_proto_from_binary(tensorflowpb, &graph);
  87. if (!s1)
  88. {
  89. fprintf(stderr, "read_proto_from_binary failed\n");
  90. return -1;
  91. }
  92. FILE* pp = fopen(ncnn_prototxt, "wb");
  93. FILE* bp = fopen(ncnn_modelbin, "wb");
  94. int node_count = graph.node_size();
  95. // fprintf(stderr, "node_count = %d\n\n", node_count);
  96. // node reference
  97. std::map<std::string, int> node_reference;
  98. // mapping for Const and Const-Identity
  99. std::map<std::string, tensorflow::TensorProto> weights;
  100. // Dropout like Identity
  101. std::set<std::string> dropouts;
  102. // Const before BinaryOp
  103. std::map<std::string, tensorflow::TensorProto> binaryop_consts;
  104. // global definition line
  105. // [layer count] [blob count]
  106. std::set<std::string> blob_names;
  107. for (int i=0; i<node_count; i++)
  108. {
  109. const tensorflow::NodeDef& node = graph.node(i);
  110. const std::string& output_name = node.name();
  111. if (node.op() == "Const")
  112. {
  113. tensorflow::AttrValue value;
  114. if (find_attr_value(node, "value", value))
  115. {
  116. const tensorflow::TensorProto& tensor = value.tensor();
  117. weights[output_name] = tensor;
  118. }
  119. continue;
  120. }
  121. else if (node.op() == "Identity")
  122. {
  123. const std::string& input_name = node.input(0);
  124. if (weights.find(input_name) != weights.end())
  125. {
  126. weights[output_name] = weights[input_name];
  127. continue;
  128. }
  129. else
  130. {
  131. dropouts.insert(output_name);
  132. }
  133. }
  134. else if (node.op() == "NoOp")
  135. {
  136. weights[output_name] = tensorflow::TensorProto();
  137. continue;
  138. }
  139. else if (node.op() == "Add" || node.op() == "BiasAdd"
  140. || node.op() == "Max" || node.op() == "Maximum" || node.op() == "Mul"
  141. || node.op() == "RealDiv" || node.op() == "Sub")
  142. {
  143. // check weights
  144. for (int j=0; j<node.input_size(); j++)
  145. {
  146. const std::string& input_name = node.input(j);
  147. std::map<std::string, tensorflow::TensorProto>::iterator it = weights.find(input_name);
  148. if (it != weights.end())
  149. {
  150. // binary op with const, insert MemoryData layer and const blob
  151. binaryop_consts[input_name] = it->second;
  152. weights.erase(it);
  153. }
  154. }
  155. }
  156. // input
  157. for (int j=0; j<node.input_size(); j++)
  158. {
  159. const std::string& input_name = node.input(j);
  160. // fprintf(stderr, "input = %s\n", input_name.c_str());
  161. if (weights.find(input_name) != weights.end())
  162. {
  163. continue;
  164. }
  165. blob_names.insert(input_name);
  166. if (node_reference.find(input_name) == node_reference.end())
  167. {
  168. node_reference[input_name] = 1;
  169. }
  170. else
  171. {
  172. node_reference[input_name] = node_reference[input_name] + 1;
  173. }
  174. }
  175. // output
  176. // fprintf(stderr, "output = %s\n", output_name.c_str());
  177. blob_names.insert(output_name);
  178. }
  179. // remove node_reference entry with reference equals to one
  180. int splitncnn_blob_count = 0;
  181. std::map<std::string, int>::iterator it = node_reference.begin();
  182. while (it != node_reference.end())
  183. {
  184. if (it->second == 1)
  185. {
  186. node_reference.erase(it++);
  187. }
  188. else
  189. {
  190. splitncnn_blob_count += it->second;
  191. // fprintf(stderr, "%s %d\n", it->first.c_str(), it->second);
  192. ++it;
  193. }
  194. }
  195. fprintf(pp, "%lu %lu\n", node_count + node_reference.size() - weights.size(), blob_names.size() + splitncnn_blob_count);
  196. int internal_split = 0;
  197. for (int i=0; i<node_count; i++)
  198. {
  199. const tensorflow::NodeDef& node = graph.node(i);
  200. // layer definition line, repeated
  201. // [type] [name] [bottom blob count] [top blob count] [bottom blobs] [top blobs] [layer specific params]
  202. // fprintf(pp, "%-16s %-16s %d %d", layer.type().c_str(), layer.name().c_str(), node.input_size(), layer.top_size());
  203. if (node.op() == "Add" || node.op() == "BiasAdd")
  204. {
  205. fprintf(pp, "%-16s", "BinaryOp");
  206. }
  207. else if (node.op() == "AvgPool")
  208. {
  209. fprintf(pp, "%-16s", "Pooling");
  210. }
  211. else if (node.op() == "Concat" || node.op() == "ConcatV2")
  212. {
  213. fprintf(pp, "%-16s", "Concat");
  214. }
  215. else if (node.op() == "Const")
  216. {
  217. // check before binaryop
  218. tensorflow::TensorProto tensor;
  219. if (get_tensor_proto(binaryop_consts, node, tensor))
  220. {
  221. fprintf(pp, "%-16s", "MemoryData");
  222. }
  223. else
  224. {
  225. continue;
  226. }
  227. }
  228. else if (node.op() == "Conv2D")
  229. {
  230. fprintf(pp, "%-16s", "Convolution");
  231. }
  232. else if (node.op() == "Exp")
  233. {
  234. fprintf(pp, "%-16s", "UnaryOp");
  235. }
  236. else if (node.op() == "Floor")
  237. {
  238. fprintf(pp, "%-16s", "UnaryOp");
  239. }
  240. else if (node.op() == "Identity")
  241. {
  242. // check before binaryop
  243. tensorflow::TensorProto tensor;
  244. if (get_tensor_proto(binaryop_consts, node, tensor))
  245. {
  246. fprintf(pp, "%-16s", "MemoryData");
  247. }
  248. else if (dropouts.find(node.name()) != dropouts.end())
  249. {
  250. fprintf(pp, "%-16s", "Dropout");
  251. }
  252. else
  253. {
  254. continue;
  255. }
  256. }
  257. else if (node.op() == "LRN")
  258. {
  259. fprintf(pp, "%-16s", "LRN");
  260. }
  261. else if (node.op() == "MatMul")
  262. {
  263. fprintf(pp, "%-16s", "InnerProduct");
  264. }
  265. else if (node.op() == "Max" || node.op() == "Maximum")
  266. {
  267. // check weights
  268. tensorflow::TensorProto tensor;
  269. if (find_tensor_proto(weights, node, tensor))
  270. {
  271. fprintf(pp, "%-16s", "Reduction");
  272. }
  273. else
  274. {
  275. fprintf(pp, "%-16s", "BinaryOp");
  276. }
  277. }
  278. else if (node.op() == "MaxPool")
  279. {
  280. fprintf(pp, "%-16s", "Pooling");
  281. }
  282. else if (node.op() == "Mul")
  283. {
  284. fprintf(pp, "%-16s", "BinaryOp");
  285. }
  286. else if (node.op() == "Neg")
  287. {
  288. fprintf(pp, "%-16s", "UnaryOp");
  289. }
  290. else if (node.op() == "NoOp")
  291. {
  292. continue;
  293. }
  294. else if (node.op() == "Placeholder")
  295. {
  296. fprintf(pp, "%-16s", "Input");
  297. }
  298. else if (node.op() == "RealDiv")
  299. {
  300. fprintf(pp, "%-16s", "BinaryOp");
  301. }
  302. else if (node.op() == "Relu")
  303. {
  304. fprintf(pp, "%-16s", "ReLU");
  305. }
  306. else if (node.op() == "Reshape")
  307. {
  308. fprintf(pp, "%-16s", "Reshape");
  309. }
  310. else if (node.op() == "Rsqrt")
  311. {
  312. fprintf(pp, "%-16s", "UnaryOp");
  313. }
  314. else if (node.op() == "Softmax")
  315. {
  316. fprintf(pp, "%-16s", "Softmax");
  317. }
  318. else if (node.op() == "Sub")
  319. {
  320. fprintf(pp, "%-16s", "BinaryOp");
  321. }
  322. else if (node.op() == "Sum")
  323. {
  324. fprintf(pp, "%-16s", "Reduction");
  325. }
  326. else
  327. {
  328. fprintf(pp, "%-16s", node.op().c_str());
  329. fprintf(stderr, "%s not supported yet !\nn", node.op().c_str());
  330. }
  331. int input_size = node.input_size();
  332. for (int j=0; j<node.input_size(); j++)
  333. {
  334. const std::string& input_name = node.input(j);
  335. if (weights.find(input_name) != weights.end())
  336. {
  337. input_size--;
  338. }
  339. }
  340. fprintf(pp, " %-16s %d 1", node.name().c_str(), input_size);
  341. for (int j=0; j<node.input_size(); j++)
  342. {
  343. std::string input_name = node.input(j);
  344. if (weights.find(input_name) != weights.end())
  345. {
  346. continue;
  347. }
  348. if (node_reference.find(input_name) != node_reference.end())
  349. {
  350. int refidx = node_reference[input_name] - 1;
  351. node_reference[input_name] = refidx;
  352. char splitsuffix[256];
  353. sprintf(splitsuffix, "_splitncnn_%d", refidx);
  354. input_name = input_name + splitsuffix;
  355. }
  356. fprintf(pp, " %s", input_name.c_str());
  357. }
  358. fprintf(pp, " %s", node.name().c_str());
  359. if (node.op() == "Add" || node.op() == "BiasAdd")
  360. {
  361. int op_type = 0;
  362. fprintf(pp, " %d", op_type);
  363. }
  364. else if (node.op() == "AvgPool")
  365. {
  366. int pooling_type = 1;
  367. int kernel_size_h = 1;
  368. int kernel_size_w = 1;
  369. int stride_h = 1;
  370. int stride_w = 1;
  371. int pad = 0;
  372. int global_pooling = 0;
  373. tensorflow::AttrValue value_ksize;
  374. if (find_attr_value(node, "ksize", value_ksize))
  375. {
  376. // batch, height, width, channels
  377. kernel_size_h = value_ksize.list().i(1);
  378. kernel_size_w = value_ksize.list().i(2);
  379. }
  380. tensorflow::AttrValue value_strides;
  381. if (find_attr_value(node, "strides", value_strides))
  382. {
  383. // batch, height, width, channels
  384. stride_h = value_strides.list().i(1);
  385. stride_w = value_strides.list().i(2);
  386. }
  387. tensorflow::AttrValue value_padding;
  388. if (find_attr_value(node, "padding", value_padding))
  389. {
  390. if (value_padding.s() == "VALID")
  391. {
  392. pad = 0;
  393. }
  394. else if (value_padding.s() == "SAME")
  395. {
  396. pad = -233;
  397. }
  398. }
  399. fprintf(pp, " %d %d %d %d %d", pooling_type, kernel_size_w, stride_w, pad, global_pooling);
  400. }
  401. else if (node.op() == "Concat" || node.op() == "ConcatV2")
  402. {
  403. tensorflow::TensorProto tensor;
  404. if (find_tensor_proto(weights, node, tensor))
  405. {
  406. // TODO
  407. int axis = tensor.int_val(0);
  408. }
  409. }
  410. else if (node.op() == "Const" || node.op() == "Identity")
  411. {
  412. // check before binaryop
  413. tensorflow::TensorProto tensor;
  414. if (get_tensor_proto(binaryop_consts, node, tensor))
  415. {
  416. const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
  417. int c = 0;
  418. int h = 0;
  419. int w = 0;
  420. if (shape.dim_size() == 1)
  421. {
  422. w = shape.dim(0).size();
  423. }
  424. else if (shape.dim_size() == 2)
  425. {
  426. h = shape.dim(0).size();
  427. w = shape.dim(1).size();
  428. }
  429. else if (shape.dim_size() == 3)
  430. {
  431. c = shape.dim(2).size();
  432. h = shape.dim(0).size();
  433. w = shape.dim(1).size();
  434. }
  435. int weight_data_size = 0;
  436. if (!tensor.tensor_content().empty())
  437. {
  438. if (tensor.dtype() == 1)// float
  439. {
  440. const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
  441. weight_data_size = tensor.tensor_content().size() / sizeof(float);
  442. if (c == 0)
  443. fwrite(data, sizeof(float), weight_data_size, bp);
  444. else
  445. {
  446. float tmp;
  447. // h-w-c to c-h-w
  448. for (int p=0; p<c; p++)
  449. {
  450. for (int i=0; i<h; i++)
  451. {
  452. for (int j=0; j<w; j++)
  453. {
  454. tmp = data[i*w*c + j*c + p];
  455. fwrite(&tmp, sizeof(float), 1, bp);
  456. }
  457. }
  458. }
  459. }
  460. }
  461. else if (tensor.dtype() == 3)// int32
  462. {
  463. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  464. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  465. float tmp;
  466. if (c == 0)
  467. {
  468. for (int i=0; i<weight_data_size; i++)
  469. {
  470. tmp = data[i];
  471. fwrite(&tmp, sizeof(float), 1, bp);
  472. }
  473. }
  474. else
  475. {
  476. // h-w-c to c-h-w
  477. for (int p=0; p<c; p++)
  478. {
  479. for (int i=0; i<h; i++)
  480. {
  481. for (int j=0; j<w; j++)
  482. {
  483. tmp = data[i*w*c + j*c + p];
  484. fwrite(&tmp, sizeof(float), 1, bp);
  485. }
  486. }
  487. }
  488. }
  489. }
  490. }
  491. else
  492. {
  493. float val = tensor.float_val(0);
  494. fwrite(&val, sizeof(float), 1, bp);
  495. }
  496. fprintf(pp, " %d %d %d", c, h, w);
  497. }
  498. }
  499. else if (node.op() == "Conv2D")
  500. {
  501. // weights
  502. tensorflow::TensorProto tensor;
  503. find_tensor_proto(weights, node, tensor);
  504. const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
  505. int kernel_size_h = shape.dim(0).size();
  506. int kernel_size_w = shape.dim(1).size();
  507. int num_input = shape.dim(2).size();
  508. int num_output = shape.dim(3).size();
  509. int stride_h = 1;
  510. int stride_w = 1;
  511. int dilation = 1;
  512. int pad = 0;
  513. tensorflow::AttrValue value_strides;
  514. if (find_attr_value(node, "strides", value_strides))
  515. {
  516. // batch, height, width, channels
  517. stride_h = value_strides.list().i(1);
  518. stride_w = value_strides.list().i(2);
  519. }
  520. tensorflow::AttrValue value_padding;
  521. if (find_attr_value(node, "padding", value_padding))
  522. {
  523. if (value_padding.s() == "VALID")
  524. {
  525. pad = 0;
  526. }
  527. else if (value_padding.s() == "SAME")
  528. {
  529. pad = -233;
  530. }
  531. }
  532. int bias_term = 0;
  533. int weight_data_size = 0;
  534. // reorder h-w-i-o to o-i-h-w
  535. if (!tensor.tensor_content().empty())
  536. {
  537. int quantize_tag = 0;
  538. fwrite(&quantize_tag, sizeof(int), 1, bp);
  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. float tmp;
  544. for (int p=0; p<num_output; p++)
  545. {
  546. for (int q=0; q<num_input; q++)
  547. {
  548. for (int i=0; i<kernel_size_h; i++)
  549. {
  550. for (int j=0; j<kernel_size_w; j++)
  551. {
  552. tmp = data[i*kernel_size_w*num_input*num_output + j*num_input*num_output + q*num_output + p];
  553. fwrite(&tmp, sizeof(float), 1, bp);
  554. }
  555. }
  556. }
  557. }
  558. }
  559. else if (tensor.dtype() == 3)// int32
  560. {
  561. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  562. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  563. float tmp;
  564. for (int p=0; p<num_output; p++)
  565. {
  566. for (int q=0; q<num_input; q++)
  567. {
  568. for (int i=0; i<kernel_size_h; i++)
  569. {
  570. for (int j=0; j<kernel_size_w; j++)
  571. {
  572. tmp = data[i*kernel_size_w*num_input*num_output + j*num_input*num_output + q*num_output + p];
  573. fwrite(&tmp, sizeof(float), 1, bp);
  574. }
  575. }
  576. }
  577. }
  578. }
  579. }
  580. fprintf(pp, " %d %d %d %d %d %d %d", num_output, kernel_size_w, dilation, stride_w, pad, bias_term, weight_data_size);
  581. }
  582. else if (node.op() == "Exp")
  583. {
  584. int op_type = 7;
  585. fprintf(pp, " %d", op_type);
  586. }
  587. else if (node.op() == "Floor")
  588. {
  589. int op_type = 2;
  590. fprintf(pp, " %d", op_type);
  591. }
  592. else if (node.op() == "LRN")
  593. {
  594. int norm_region = 0;
  595. int local_size = 1;
  596. float alpha = 1.f;
  597. float beta = 0.5f;
  598. tensorflow::AttrValue value_depth_radius;
  599. if (find_attr_value(node, "depth_radius", value_depth_radius))
  600. {
  601. local_size = value_depth_radius.i() * 2 + 1;
  602. }
  603. tensorflow::AttrValue value_alpha;
  604. if (find_attr_value(node, "alpha", value_alpha))
  605. {
  606. alpha = value_alpha.f();
  607. }
  608. tensorflow::AttrValue value_beta;
  609. if (find_attr_value(node, "beta", value_beta))
  610. {
  611. beta = value_beta.f();
  612. }
  613. // TODO
  614. float bias = 1.f;
  615. tensorflow::AttrValue value_bias;
  616. if (find_attr_value(node, "bias", value_bias))
  617. {
  618. bias = value_bias.f();
  619. }
  620. fprintf(pp, " %d %d %f %f", norm_region, local_size, alpha, beta);
  621. }
  622. else if (node.op() == "MatMul")
  623. {
  624. // weights
  625. tensorflow::TensorProto tensor;
  626. find_tensor_proto(weights, node, tensor);
  627. const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
  628. int num_input = shape.dim(0).size();
  629. int num_output = shape.dim(1).size();
  630. int bias_term = 0;
  631. int weight_data_size = 0;
  632. // reorder i-o to o-i
  633. if (!tensor.tensor_content().empty())
  634. {
  635. int quantize_tag = 0;
  636. fwrite(&quantize_tag, sizeof(int), 1, bp);
  637. if (tensor.dtype() == 1)// float
  638. {
  639. const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
  640. weight_data_size = tensor.tensor_content().size() / sizeof(float);
  641. float tmp;
  642. for (int p=0; p<num_output; p++)
  643. {
  644. for (int q=0; q<num_input; q++)
  645. {
  646. tmp = data[q*num_output + p];
  647. fwrite(&tmp, sizeof(float), 1, bp);
  648. }
  649. }
  650. }
  651. else if (tensor.dtype() == 3)// int32
  652. {
  653. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  654. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  655. float tmp;
  656. for (int p=0; p<num_output; p++)
  657. {
  658. for (int q=0; q<num_input; q++)
  659. {
  660. tmp = data[q*num_output + p];
  661. fwrite(&tmp, sizeof(float), 1, bp);
  662. }
  663. }
  664. }
  665. }
  666. fprintf(pp, " %d %d %d", num_output, bias_term, weight_data_size);
  667. }
  668. else if (node.op() == "Max" || node.op() == "Maximum")
  669. {
  670. // check weights
  671. tensorflow::TensorProto tensor;
  672. if (find_tensor_proto(weights, node, tensor))
  673. {
  674. int operation = 4;
  675. int dim = 0;
  676. float coeff = 1.f;
  677. int axis = tensor.int_val(0);
  678. if (axis == 1)
  679. dim = 0;
  680. else if (axis == 3)
  681. dim = -2;
  682. fprintf(pp, " %d %d %f", operation, dim, coeff);
  683. }
  684. else
  685. {
  686. int op_type = 4;
  687. fprintf(pp, " %d", op_type);
  688. }
  689. }
  690. else if (node.op() == "MaxPool")
  691. {
  692. int pooling_type = 0;
  693. int kernel_size_h = 1;
  694. int kernel_size_w = 1;
  695. int stride_h = 1;
  696. int stride_w = 1;
  697. int pad = 0;
  698. int global_pooling = 0;
  699. tensorflow::AttrValue value_ksize;
  700. if (find_attr_value(node, "ksize", value_ksize))
  701. {
  702. // batch, height, width, channels
  703. kernel_size_h = value_ksize.list().i(1);
  704. kernel_size_w = value_ksize.list().i(2);
  705. }
  706. tensorflow::AttrValue value_strides;
  707. if (find_attr_value(node, "strides", value_strides))
  708. {
  709. // batch, height, width, channels
  710. stride_h = value_strides.list().i(1);
  711. stride_w = value_strides.list().i(2);
  712. }
  713. tensorflow::AttrValue value_padding;
  714. if (find_attr_value(node, "padding", value_padding))
  715. {
  716. if (value_padding.s() == "VALID")
  717. {
  718. pad = 0;
  719. }
  720. else if (value_padding.s() == "SAME")
  721. {
  722. pad = -233;
  723. }
  724. }
  725. fprintf(pp, " %d %d %d %d %d", pooling_type, kernel_size_w, stride_w, pad, global_pooling);
  726. }
  727. else if (node.op() == "Mul")
  728. {
  729. int op_type = 2;
  730. fprintf(pp, " %d", op_type);
  731. }
  732. else if (node.op() == "Neg")
  733. {
  734. int op_type = 1;
  735. fprintf(pp, " %d", op_type);
  736. }
  737. else if (node.op() == "NoOp")
  738. {
  739. }
  740. else if (node.op() == "Placeholder")
  741. {
  742. // TODO pass through
  743. fprintf(pp, " 0 0 0");
  744. }
  745. else if (node.op() == "RealDiv")
  746. {
  747. int op_type = 3;
  748. fprintf(pp, " %d", op_type);
  749. }
  750. else if (node.op() == "Relu")
  751. {
  752. float slope = 0.f;
  753. fprintf(pp, " %f", slope);
  754. }
  755. else if (node.op() == "Reshape")
  756. {
  757. tensorflow::TensorProto tensor;
  758. if (find_tensor_proto(weights, node, tensor))
  759. {
  760. if (!tensor.tensor_content().empty() && tensor.dtype() == 3)// int32
  761. {
  762. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  763. int size = tensor.tensor_content().size() / sizeof(int);
  764. // n h w c
  765. // n h w
  766. // n w
  767. if (size == 4)
  768. {
  769. fprintf(pp, " %d %d %d 0", data[2], data[1], data[3]);
  770. }
  771. if (size == 3)
  772. {
  773. fprintf(pp, " %d %d -233 1", data[2], data[1]);
  774. }
  775. if (size == 2)
  776. {
  777. fprintf(pp, " %d -233 -233 1", data[1]);
  778. }
  779. }
  780. }
  781. else
  782. {
  783. // pass through
  784. fprintf(pp, " 0 0 0");
  785. }
  786. }
  787. else if (node.op() == "Rsqrt")
  788. {
  789. int op_type = 6;
  790. fprintf(pp, " %d", op_type);
  791. }
  792. else if (node.op() == "Softmax")
  793. {
  794. }
  795. else if (node.op() == "Sub")
  796. {
  797. int op_type = 1;
  798. fprintf(pp, " %d", op_type);
  799. }
  800. else if (node.op() == "Sum")
  801. {
  802. int operation = 0;
  803. int dim = 0;
  804. float coeff = 1.f;
  805. // check weights
  806. tensorflow::TensorProto tensor;
  807. if (find_tensor_proto(weights, node, tensor))
  808. {
  809. int axis = tensor.int_val(0);
  810. if (axis == 1)
  811. dim = 0;
  812. else if (axis == 3)
  813. dim = -2;
  814. }
  815. fprintf(pp, " %d %d %f", operation, dim, coeff);
  816. }
  817. else
  818. {
  819. const google::protobuf::Map<std::string, tensorflow::AttrValue>& attr = node.attr();
  820. google::protobuf::Map<std::string, tensorflow::AttrValue>::const_iterator it = attr.begin();
  821. for (; it != attr.end(); it++)
  822. {
  823. std::cerr << it->first << std::endl;
  824. std::cerr << it->second.type() << std::endl;
  825. }
  826. }
  827. fprintf(pp, "\n");
  828. std::string output_name = node.name();
  829. if (node_reference.find(output_name) != node_reference.end())
  830. {
  831. int refcount = node_reference[output_name];
  832. if (refcount > 1)
  833. {
  834. char splitname[256];
  835. sprintf(splitname, "splitncnn_%d", internal_split);
  836. fprintf(pp, "%-16s %-16s %d %d", "Split", splitname, 1, refcount);
  837. fprintf(pp, " %s", output_name.c_str());
  838. for (int j=0; j<refcount; j++)
  839. {
  840. fprintf(pp, " %s_splitncnn_%d", output_name.c_str(), j);
  841. }
  842. fprintf(pp, "\n");
  843. internal_split++;
  844. }
  845. }
  846. }
  847. fclose(pp);
  848. fclose(bp);
  849. return 0;
  850. }