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

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