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 32 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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() == "Pad")
  295. {
  296. fprintf(pp, "%-16s", "Padding");
  297. }
  298. else if (node.op() == "Placeholder")
  299. {
  300. fprintf(pp, "%-16s", "Input");
  301. }
  302. else if (node.op() == "RealDiv")
  303. {
  304. fprintf(pp, "%-16s", "BinaryOp");
  305. }
  306. else if (node.op() == "Relu")
  307. {
  308. fprintf(pp, "%-16s", "ReLU");
  309. }
  310. else if (node.op() == "Reshape")
  311. {
  312. fprintf(pp, "%-16s", "Reshape");
  313. }
  314. else if (node.op() == "Rsqrt")
  315. {
  316. fprintf(pp, "%-16s", "UnaryOp");
  317. }
  318. else if (node.op() == "Softmax")
  319. {
  320. fprintf(pp, "%-16s", "Softmax");
  321. }
  322. else if (node.op() == "Sub")
  323. {
  324. fprintf(pp, "%-16s", "BinaryOp");
  325. }
  326. else if (node.op() == "Sum")
  327. {
  328. fprintf(pp, "%-16s", "Reduction");
  329. }
  330. else
  331. {
  332. fprintf(pp, "%-16s", node.op().c_str());
  333. fprintf(stderr, "%s not supported yet !\nn", node.op().c_str());
  334. }
  335. int input_size = node.input_size();
  336. for (int j=0; j<node.input_size(); j++)
  337. {
  338. const std::string& input_name = node.input(j);
  339. if (weights.find(input_name) != weights.end())
  340. {
  341. input_size--;
  342. }
  343. }
  344. fprintf(pp, " %-16s %d 1", node.name().c_str(), input_size);
  345. for (int j=0; j<node.input_size(); j++)
  346. {
  347. std::string input_name = node.input(j);
  348. if (weights.find(input_name) != weights.end())
  349. {
  350. continue;
  351. }
  352. if (node_reference.find(input_name) != node_reference.end())
  353. {
  354. int refidx = node_reference[input_name] - 1;
  355. node_reference[input_name] = refidx;
  356. char splitsuffix[256];
  357. sprintf(splitsuffix, "_splitncnn_%d", refidx);
  358. input_name = input_name + splitsuffix;
  359. }
  360. fprintf(pp, " %s", input_name.c_str());
  361. }
  362. fprintf(pp, " %s", node.name().c_str());
  363. if (node.op() == "Add" || node.op() == "BiasAdd")
  364. {
  365. int op_type = 0;
  366. fprintf(pp, " %d", op_type);
  367. }
  368. else if (node.op() == "AvgPool")
  369. {
  370. int pooling_type = 1;
  371. int kernel_size_h = 1;
  372. int kernel_size_w = 1;
  373. int stride_h = 1;
  374. int stride_w = 1;
  375. int pad = 0;
  376. int global_pooling = 0;
  377. tensorflow::AttrValue value_ksize;
  378. if (find_attr_value(node, "ksize", value_ksize))
  379. {
  380. // batch, height, width, channels
  381. kernel_size_h = value_ksize.list().i(1);
  382. kernel_size_w = value_ksize.list().i(2);
  383. }
  384. tensorflow::AttrValue value_strides;
  385. if (find_attr_value(node, "strides", value_strides))
  386. {
  387. // batch, height, width, channels
  388. stride_h = value_strides.list().i(1);
  389. stride_w = value_strides.list().i(2);
  390. }
  391. tensorflow::AttrValue value_padding;
  392. if (find_attr_value(node, "padding", value_padding))
  393. {
  394. if (value_padding.s() == "VALID")
  395. {
  396. pad = 0;
  397. }
  398. else if (value_padding.s() == "SAME")
  399. {
  400. pad = -233;
  401. }
  402. }
  403. fprintf(pp, " %d %d %d %d %d", pooling_type, kernel_size_w, stride_w, pad, global_pooling);
  404. }
  405. else if (node.op() == "Concat" || node.op() == "ConcatV2")
  406. {
  407. tensorflow::TensorProto tensor;
  408. if (find_tensor_proto(weights, node, tensor))
  409. {
  410. // TODO
  411. int axis = tensor.int_val(0);
  412. }
  413. }
  414. else if (node.op() == "Const" || node.op() == "Identity")
  415. {
  416. // check before binaryop
  417. tensorflow::TensorProto tensor;
  418. if (get_tensor_proto(binaryop_consts, node, tensor))
  419. {
  420. const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
  421. int c = 0;
  422. int h = 0;
  423. int w = 0;
  424. if (shape.dim_size() == 1)
  425. {
  426. w = shape.dim(0).size();
  427. }
  428. else if (shape.dim_size() == 2)
  429. {
  430. h = shape.dim(0).size();
  431. w = shape.dim(1).size();
  432. }
  433. else if (shape.dim_size() == 3)
  434. {
  435. c = shape.dim(2).size();
  436. h = shape.dim(0).size();
  437. w = shape.dim(1).size();
  438. }
  439. int weight_data_size = 0;
  440. if (!tensor.tensor_content().empty())
  441. {
  442. if (tensor.dtype() == 1)// float
  443. {
  444. const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
  445. weight_data_size = tensor.tensor_content().size() / sizeof(float);
  446. if (c == 0)
  447. fwrite(data, sizeof(float), weight_data_size, bp);
  448. else
  449. {
  450. float tmp;
  451. // h-w-c to c-h-w
  452. for (int p=0; p<c; p++)
  453. {
  454. for (int i=0; i<h; i++)
  455. {
  456. for (int j=0; j<w; j++)
  457. {
  458. tmp = data[i*w*c + j*c + p];
  459. fwrite(&tmp, sizeof(float), 1, bp);
  460. }
  461. }
  462. }
  463. }
  464. }
  465. else if (tensor.dtype() == 3)// int32
  466. {
  467. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  468. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  469. float tmp;
  470. if (c == 0)
  471. {
  472. for (int i=0; i<weight_data_size; i++)
  473. {
  474. tmp = data[i];
  475. fwrite(&tmp, sizeof(float), 1, bp);
  476. }
  477. }
  478. else
  479. {
  480. // h-w-c to c-h-w
  481. for (int p=0; p<c; p++)
  482. {
  483. for (int i=0; i<h; i++)
  484. {
  485. for (int j=0; j<w; j++)
  486. {
  487. tmp = data[i*w*c + j*c + p];
  488. fwrite(&tmp, sizeof(float), 1, bp);
  489. }
  490. }
  491. }
  492. }
  493. }
  494. }
  495. else
  496. {
  497. float val = tensor.float_val(0);
  498. fwrite(&val, sizeof(float), 1, bp);
  499. }
  500. fprintf(pp, " %d %d %d", c, h, w);
  501. }
  502. }
  503. else if (node.op() == "Conv2D")
  504. {
  505. // weights
  506. tensorflow::TensorProto tensor;
  507. find_tensor_proto(weights, node, tensor);
  508. const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
  509. int kernel_size_h = shape.dim(0).size();
  510. int kernel_size_w = shape.dim(1).size();
  511. int num_input = shape.dim(2).size();
  512. int num_output = shape.dim(3).size();
  513. int stride_h = 1;
  514. int stride_w = 1;
  515. int dilation = 1;
  516. int pad = 0;
  517. tensorflow::AttrValue value_strides;
  518. if (find_attr_value(node, "strides", value_strides))
  519. {
  520. // batch, height, width, channels
  521. stride_h = value_strides.list().i(1);
  522. stride_w = value_strides.list().i(2);
  523. }
  524. tensorflow::AttrValue value_padding;
  525. if (find_attr_value(node, "padding", value_padding))
  526. {
  527. if (value_padding.s() == "VALID")
  528. {
  529. pad = 0;
  530. }
  531. else if (value_padding.s() == "SAME")
  532. {
  533. pad = -233;
  534. }
  535. }
  536. int bias_term = 0;
  537. int weight_data_size = 0;
  538. // reorder h-w-i-o to o-i-h-w
  539. if (!tensor.tensor_content().empty())
  540. {
  541. int quantize_tag = 0;
  542. fwrite(&quantize_tag, sizeof(int), 1, bp);
  543. if (tensor.dtype() == 1)// float
  544. {
  545. const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
  546. weight_data_size = tensor.tensor_content().size() / sizeof(float);
  547. float tmp;
  548. for (int p=0; p<num_output; p++)
  549. {
  550. for (int q=0; q<num_input; q++)
  551. {
  552. for (int i=0; i<kernel_size_h; i++)
  553. {
  554. for (int j=0; j<kernel_size_w; j++)
  555. {
  556. tmp = data[i*kernel_size_w*num_input*num_output + j*num_input*num_output + q*num_output + p];
  557. fwrite(&tmp, sizeof(float), 1, bp);
  558. }
  559. }
  560. }
  561. }
  562. }
  563. else if (tensor.dtype() == 3)// int32
  564. {
  565. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  566. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  567. float tmp;
  568. for (int p=0; p<num_output; p++)
  569. {
  570. for (int q=0; q<num_input; q++)
  571. {
  572. for (int i=0; i<kernel_size_h; i++)
  573. {
  574. for (int j=0; j<kernel_size_w; j++)
  575. {
  576. tmp = data[i*kernel_size_w*num_input*num_output + j*num_input*num_output + q*num_output + p];
  577. fwrite(&tmp, sizeof(float), 1, bp);
  578. }
  579. }
  580. }
  581. }
  582. }
  583. }
  584. fprintf(pp, " %d %d %d %d %d %d %d", num_output, kernel_size_w, dilation, stride_w, pad, bias_term, weight_data_size);
  585. }
  586. else if (node.op() == "Exp")
  587. {
  588. int op_type = 7;
  589. fprintf(pp, " %d", op_type);
  590. }
  591. else if (node.op() == "Floor")
  592. {
  593. int op_type = 2;
  594. fprintf(pp, " %d", op_type);
  595. }
  596. else if (node.op() == "LRN")
  597. {
  598. int norm_region = 0;
  599. int local_size = 1;
  600. float alpha = 1.f;
  601. float beta = 0.5f;
  602. tensorflow::AttrValue value_depth_radius;
  603. if (find_attr_value(node, "depth_radius", value_depth_radius))
  604. {
  605. local_size = value_depth_radius.i() * 2 + 1;
  606. }
  607. tensorflow::AttrValue value_alpha;
  608. if (find_attr_value(node, "alpha", value_alpha))
  609. {
  610. alpha = value_alpha.f();
  611. }
  612. tensorflow::AttrValue value_beta;
  613. if (find_attr_value(node, "beta", value_beta))
  614. {
  615. beta = value_beta.f();
  616. }
  617. // TODO
  618. float bias = 1.f;
  619. tensorflow::AttrValue value_bias;
  620. if (find_attr_value(node, "bias", value_bias))
  621. {
  622. bias = value_bias.f();
  623. }
  624. fprintf(pp, " %d %d %f %f", norm_region, local_size, alpha, beta);
  625. }
  626. else if (node.op() == "MatMul")
  627. {
  628. // weights
  629. tensorflow::TensorProto tensor;
  630. find_tensor_proto(weights, node, tensor);
  631. const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
  632. int num_input = shape.dim(0).size();
  633. int num_output = shape.dim(1).size();
  634. int bias_term = 0;
  635. int weight_data_size = 0;
  636. // reorder i-o to o-i
  637. if (!tensor.tensor_content().empty())
  638. {
  639. int quantize_tag = 0;
  640. fwrite(&quantize_tag, sizeof(int), 1, bp);
  641. if (tensor.dtype() == 1)// float
  642. {
  643. const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
  644. weight_data_size = tensor.tensor_content().size() / sizeof(float);
  645. float tmp;
  646. for (int p=0; p<num_output; p++)
  647. {
  648. for (int q=0; q<num_input; q++)
  649. {
  650. tmp = data[q*num_output + p];
  651. fwrite(&tmp, sizeof(float), 1, bp);
  652. }
  653. }
  654. }
  655. else if (tensor.dtype() == 3)// int32
  656. {
  657. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  658. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  659. float tmp;
  660. for (int p=0; p<num_output; p++)
  661. {
  662. for (int q=0; q<num_input; q++)
  663. {
  664. tmp = data[q*num_output + p];
  665. fwrite(&tmp, sizeof(float), 1, bp);
  666. }
  667. }
  668. }
  669. }
  670. fprintf(pp, " %d %d %d", num_output, bias_term, weight_data_size);
  671. }
  672. else if (node.op() == "Max" || node.op() == "Maximum")
  673. {
  674. // check weights
  675. tensorflow::TensorProto tensor;
  676. if (find_tensor_proto(weights, node, tensor))
  677. {
  678. int operation = 4;
  679. int dim = 0;
  680. float coeff = 1.f;
  681. int axis = tensor.int_val(0);
  682. if (axis == 1)
  683. dim = 0;
  684. else if (axis == 3)
  685. dim = -2;
  686. fprintf(pp, " %d %d %f", operation, dim, coeff);
  687. }
  688. else
  689. {
  690. int op_type = 4;
  691. fprintf(pp, " %d", op_type);
  692. }
  693. }
  694. else if (node.op() == "MaxPool")
  695. {
  696. int pooling_type = 0;
  697. int kernel_size_h = 1;
  698. int kernel_size_w = 1;
  699. int stride_h = 1;
  700. int stride_w = 1;
  701. int pad = 0;
  702. int global_pooling = 0;
  703. tensorflow::AttrValue value_ksize;
  704. if (find_attr_value(node, "ksize", value_ksize))
  705. {
  706. // batch, height, width, channels
  707. kernel_size_h = value_ksize.list().i(1);
  708. kernel_size_w = value_ksize.list().i(2);
  709. }
  710. tensorflow::AttrValue value_strides;
  711. if (find_attr_value(node, "strides", value_strides))
  712. {
  713. // batch, height, width, channels
  714. stride_h = value_strides.list().i(1);
  715. stride_w = value_strides.list().i(2);
  716. }
  717. tensorflow::AttrValue value_padding;
  718. if (find_attr_value(node, "padding", value_padding))
  719. {
  720. if (value_padding.s() == "VALID")
  721. {
  722. pad = -2333;
  723. }
  724. else if (value_padding.s() == "SAME")
  725. {
  726. pad = -233;
  727. }
  728. }
  729. fprintf(pp, " %d %d %d %d %d", pooling_type, kernel_size_w, stride_w, pad, global_pooling);
  730. }
  731. else if (node.op() == "Mul")
  732. {
  733. int op_type = 2;
  734. fprintf(pp, " %d", op_type);
  735. }
  736. else if (node.op() == "Neg")
  737. {
  738. int op_type = 1;
  739. fprintf(pp, " %d", op_type);
  740. }
  741. else if (node.op() == "NoOp")
  742. {
  743. }
  744. else if (node.op() == "Pad")
  745. {
  746. int top = 0;
  747. int bottom = 0;
  748. int left = 0;
  749. int right = 0;
  750. int type = 0;
  751. float value = 0.f;
  752. // check weights
  753. tensorflow::TensorProto tensor;
  754. if (find_tensor_proto(weights, node, tensor))
  755. {
  756. if (!tensor.tensor_content().empty() && tensor.dtype() == 3)// int32
  757. {
  758. const int *data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  759. int size = tensor.tensor_content().size() / sizeof(int);
  760. if (size == 8)
  761. {
  762. // n h w c
  763. top = data[2];
  764. bottom = data[3];
  765. left = data[4];
  766. right = data[5];
  767. }
  768. }
  769. }
  770. tensorflow::AttrValue value_Tpaddings;
  771. if (find_attr_value(node, "Tpaddings", value_Tpaddings))
  772. {
  773. type = value_Tpaddings.i();
  774. }
  775. tensorflow::AttrValue value_T;
  776. if (find_attr_value(node, "T", value_T))
  777. {
  778. value = value_T.f();
  779. }
  780. fprintf(pp, " %d %d %d %d %d %f", top, bottom, left, right, type, value);
  781. }
  782. else if (node.op() == "Placeholder")
  783. {
  784. // TODO pass through
  785. fprintf(pp, " 0 0 0");
  786. }
  787. else if (node.op() == "RealDiv")
  788. {
  789. int op_type = 3;
  790. fprintf(pp, " %d", op_type);
  791. }
  792. else if (node.op() == "Relu")
  793. {
  794. float slope = 0.f;
  795. fprintf(pp, " %f", slope);
  796. }
  797. else if (node.op() == "Reshape")
  798. {
  799. tensorflow::TensorProto tensor;
  800. if (find_tensor_proto(weights, node, tensor))
  801. {
  802. if (!tensor.tensor_content().empty() && tensor.dtype() == 3)// int32
  803. {
  804. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  805. int size = tensor.tensor_content().size() / sizeof(int);
  806. // n h w c
  807. // n h w
  808. // n w
  809. if (size == 4)
  810. {
  811. fprintf(pp, " %d %d %d 0", data[2], data[1], data[3]);
  812. }
  813. if (size == 3)
  814. {
  815. fprintf(pp, " %d %d -233 1", data[2], data[1]);
  816. }
  817. if (size == 2)
  818. {
  819. fprintf(pp, " %d -233 -233 1", data[1]);
  820. }
  821. }
  822. }
  823. else
  824. {
  825. // pass through
  826. fprintf(pp, " 0 0 0");
  827. }
  828. }
  829. else if (node.op() == "Rsqrt")
  830. {
  831. int op_type = 6;
  832. fprintf(pp, " %d", op_type);
  833. }
  834. else if (node.op() == "Softmax")
  835. {
  836. }
  837. else if (node.op() == "Sub")
  838. {
  839. int op_type = 1;
  840. fprintf(pp, " %d", op_type);
  841. }
  842. else if (node.op() == "Sum")
  843. {
  844. int operation = 0;
  845. int dim = 0;
  846. float coeff = 1.f;
  847. // check weights
  848. tensorflow::TensorProto tensor;
  849. if (find_tensor_proto(weights, node, tensor))
  850. {
  851. int axis = tensor.int_val(0);
  852. if (axis == 1)
  853. dim = 0;
  854. else if (axis == 3)
  855. dim = -2;
  856. }
  857. fprintf(pp, " %d %d %f", operation, dim, coeff);
  858. }
  859. else
  860. {
  861. const google::protobuf::Map<std::string, tensorflow::AttrValue>& attr = node.attr();
  862. google::protobuf::Map<std::string, tensorflow::AttrValue>::const_iterator it = attr.begin();
  863. for (; it != attr.end(); it++)
  864. {
  865. std::cerr << it->first << std::endl;
  866. std::cerr << it->second.type() << std::endl;
  867. }
  868. }
  869. fprintf(pp, "\n");
  870. std::string output_name = node.name();
  871. if (node_reference.find(output_name) != node_reference.end())
  872. {
  873. int refcount = node_reference[output_name];
  874. if (refcount > 1)
  875. {
  876. char splitname[256];
  877. sprintf(splitname, "splitncnn_%d", internal_split);
  878. fprintf(pp, "%-16s %-16s %d %d", "Split", splitname, 1, refcount);
  879. fprintf(pp, " %s", output_name.c_str());
  880. for (int j=0; j<refcount; j++)
  881. {
  882. fprintf(pp, " %s_splitncnn_%d", output_name.c_str(), j);
  883. }
  884. fprintf(pp, "\n");
  885. internal_split++;
  886. }
  887. }
  888. }
  889. fclose(pp);
  890. fclose(bp);
  891. return 0;
  892. }