You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tensorflow2ncnn.cpp 44 kB

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