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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  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. int pad_mode = 1;
  471. tensorflow::AttrValue value_ksize;
  472. if (find_attr_value(node, "ksize", value_ksize))
  473. {
  474. // batch, height, width, channels
  475. kernel_size_h = value_ksize.list().i(1);
  476. kernel_size_w = value_ksize.list().i(2);
  477. }
  478. tensorflow::AttrValue value_strides;
  479. if (find_attr_value(node, "strides", value_strides))
  480. {
  481. // batch, height, width, channels
  482. stride_h = value_strides.list().i(1);
  483. stride_w = value_strides.list().i(2);
  484. }
  485. tensorflow::AttrValue value_padding;
  486. if (find_attr_value(node, "padding", value_padding))
  487. {
  488. if (value_padding.s() == "VALID")
  489. {
  490. pad_mode = 1;
  491. }
  492. else if (value_padding.s() == "SAME")
  493. {
  494. pad_mode = 2;
  495. }
  496. }
  497. fprintf(pp, " 0=%d", pooling_type);
  498. fprintf(pp, " 1=%d", kernel_size_w);
  499. fprintf(pp, " 11=%d", kernel_size_h);
  500. fprintf(pp, " 2=%d", stride_w);
  501. fprintf(pp, " 12=%d", stride_h);
  502. fprintf(pp, " 3=%d", pad);
  503. fprintf(pp, " 4=%d", global_pooling);
  504. fprintf(pp, " 5=%d", pad_mode);
  505. }
  506. else if (node.op() == "Concat" || node.op() == "ConcatV2")
  507. {
  508. tensorflow::TensorProto tensor;
  509. if (find_tensor_proto(weights, node, tensor))
  510. {
  511. // TODO
  512. // int axis = tensor.int_val(0);
  513. }
  514. }
  515. else if (node.op() == "Const" || node.op() == "Identity")
  516. {
  517. // check before binaryop
  518. tensorflow::TensorProto tensor;
  519. if (get_tensor_proto(binaryop_consts, node, tensor))
  520. {
  521. const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
  522. int w = 0;
  523. int h = 0;
  524. int c = 0;
  525. if (shape.dim_size() == 1)
  526. {
  527. w = shape.dim(0).size();
  528. }
  529. else if (shape.dim_size() == 2)
  530. {
  531. h = shape.dim(0).size();
  532. w = shape.dim(1).size();
  533. }
  534. else if (shape.dim_size() == 3)
  535. {
  536. c = shape.dim(2).size();
  537. h = shape.dim(0).size();
  538. w = shape.dim(1).size();
  539. }
  540. int weight_data_size = 0;
  541. if (!tensor.tensor_content().empty())
  542. {
  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. if (c == 0)
  548. fwrite(data, sizeof(float), weight_data_size, bp);
  549. else
  550. {
  551. float tmp;
  552. // h-w-c to c-h-w
  553. for (int p=0; p<c; p++)
  554. {
  555. for (int i=0; i<h; i++)
  556. {
  557. for (int j=0; j<w; j++)
  558. {
  559. tmp = data[i*w*c + j*c + p];
  560. fwrite(&tmp, sizeof(float), 1, bp);
  561. }
  562. }
  563. }
  564. }
  565. }
  566. else if (tensor.dtype() == 3)// int32
  567. {
  568. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  569. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  570. float tmp;
  571. if (c == 0)
  572. {
  573. for (int i=0; i<weight_data_size; i++)
  574. {
  575. tmp = data[i];
  576. fwrite(&tmp, sizeof(float), 1, bp);
  577. }
  578. }
  579. else
  580. {
  581. // h-w-c to c-h-w
  582. for (int p=0; p<c; p++)
  583. {
  584. for (int i=0; i<h; i++)
  585. {
  586. for (int j=0; j<w; j++)
  587. {
  588. tmp = data[i*w*c + j*c + p];
  589. fwrite(&tmp, sizeof(float), 1, bp);
  590. }
  591. }
  592. }
  593. }
  594. }
  595. }
  596. else
  597. {
  598. if (tensor.dtype() == 1)// float
  599. {
  600. float val = tensor.float_val(0);
  601. fwrite(&val, sizeof(float), 1, bp);
  602. }
  603. else if (tensor.dtype() == 3)// int32
  604. {
  605. float val = tensor.int_val(0);
  606. fwrite(&val, sizeof(float), 1, bp);
  607. }
  608. }
  609. fprintf(pp, " 0=%d", w);
  610. fprintf(pp, " 1=%d", h);
  611. fprintf(pp, " 2=%d", c);
  612. }
  613. }
  614. else if (node.op() == "Conv2D")
  615. {
  616. // weights
  617. tensorflow::TensorProto tensor;
  618. find_tensor_proto(weights, node, tensor);
  619. const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
  620. int kernel_size_h = shape.dim(0).size();
  621. int kernel_size_w = shape.dim(1).size();
  622. int num_input = shape.dim(2).size();
  623. int num_output = shape.dim(3).size();
  624. int stride_h = 1;
  625. int stride_w = 1;
  626. int dilation_h = 1;
  627. int dilation_w = 1;
  628. int pad = 0;
  629. tensorflow::AttrValue value_strides;
  630. if (find_attr_value(node, "strides", value_strides))
  631. {
  632. // batch, height, width, channels
  633. stride_h = value_strides.list().i(1);
  634. stride_w = value_strides.list().i(2);
  635. }
  636. tensorflow::AttrValue value_padding;
  637. if (find_attr_value(node, "padding", value_padding))
  638. {
  639. if (value_padding.s() == "VALID")
  640. {
  641. pad = 0;
  642. }
  643. else if (value_padding.s() == "SAME")
  644. {
  645. pad = -233;
  646. }
  647. }
  648. tensorflow::AttrValue value_rate;
  649. if (find_attr_value(node, "rate", value_rate))
  650. {
  651. // height, width
  652. dilation_h = value_rate.list().i(0);
  653. dilation_w = value_rate.list().i(1);
  654. }
  655. int bias_term = 0;
  656. int weight_data_size = 0;
  657. // reorder h-w-i-o to o-i-h-w
  658. if (!tensor.tensor_content().empty())
  659. {
  660. int quantize_tag = 0;
  661. fwrite(&quantize_tag, sizeof(int), 1, bp);
  662. if (tensor.dtype() == 1)// float
  663. {
  664. const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
  665. weight_data_size = tensor.tensor_content().size() / sizeof(float);
  666. float tmp;
  667. for (int p=0; p<num_output; p++)
  668. {
  669. for (int q=0; q<num_input; q++)
  670. {
  671. for (int i=0; i<kernel_size_h; i++)
  672. {
  673. for (int j=0; j<kernel_size_w; j++)
  674. {
  675. tmp = data[i*kernel_size_w*num_input*num_output + j*num_input*num_output + q*num_output + p];
  676. fwrite(&tmp, sizeof(float), 1, bp);
  677. }
  678. }
  679. }
  680. }
  681. }
  682. else if (tensor.dtype() == 3)// int32
  683. {
  684. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  685. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  686. float tmp;
  687. for (int p=0; p<num_output; p++)
  688. {
  689. for (int q=0; q<num_input; q++)
  690. {
  691. for (int i=0; i<kernel_size_h; i++)
  692. {
  693. for (int j=0; j<kernel_size_w; j++)
  694. {
  695. tmp = data[i*kernel_size_w*num_input*num_output + j*num_input*num_output + q*num_output + p];
  696. fwrite(&tmp, sizeof(float), 1, bp);
  697. }
  698. }
  699. }
  700. }
  701. }
  702. }
  703. fprintf(pp, " 0=%d", num_output);
  704. fprintf(pp, " 1=%d", kernel_size_w);
  705. fprintf(pp, " 11=%d", kernel_size_h);
  706. fprintf(pp, " 2=%d", dilation_w);
  707. fprintf(pp, " 12=%d", dilation_h);
  708. fprintf(pp, " 3=%d", stride_w);
  709. fprintf(pp, " 13=%d", stride_h);
  710. fprintf(pp, " 4=%d", pad);
  711. fprintf(pp, " 5=%d", bias_term);
  712. fprintf(pp, " 6=%d", weight_data_size);
  713. }
  714. else if (node.op() == "DepthwiseConv2dNative")
  715. {
  716. // weights
  717. tensorflow::TensorProto tensor;
  718. find_tensor_proto(weights, node, tensor);
  719. const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
  720. int kernel_size_h = shape.dim(0).size();
  721. int kernel_size_w = shape.dim(1).size();
  722. int num_input = shape.dim(2).size();
  723. int channel_multiplier = shape.dim(3).size();
  724. int num_output = num_input * channel_multiplier;
  725. int group = num_input;
  726. int stride_h = 1;
  727. int stride_w = 1;
  728. int dilation_h = 1;
  729. int dilation_w = 1;
  730. int pad = 0;
  731. tensorflow::AttrValue value_strides;
  732. if (find_attr_value(node, "strides", value_strides))
  733. {
  734. // batch, height, width, channels
  735. stride_h = value_strides.list().i(1);
  736. stride_w = value_strides.list().i(2);
  737. }
  738. tensorflow::AttrValue value_padding;
  739. if (find_attr_value(node, "padding", value_padding))
  740. {
  741. if (value_padding.s() == "VALID")
  742. {
  743. pad = 0;
  744. }
  745. else if (value_padding.s() == "SAME")
  746. {
  747. pad = -233;
  748. }
  749. }
  750. tensorflow::AttrValue value_rate;
  751. if (find_attr_value(node, "rate", value_rate))
  752. {
  753. // height, width
  754. dilation_h = value_rate.list().i(0);
  755. dilation_w = value_rate.list().i(1);
  756. }
  757. int bias_term = 0;
  758. int weight_data_size = 0;
  759. // reorder h-w-i-cm to i-cm-h-w
  760. if (!tensor.tensor_content().empty())
  761. {
  762. int quantize_tag = 0;
  763. fwrite(&quantize_tag, sizeof(int), 1, bp);
  764. if (tensor.dtype() == 1)// float
  765. {
  766. const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
  767. weight_data_size = tensor.tensor_content().size() / sizeof(float);
  768. float tmp;
  769. for (int p=0; p<num_input; p++)
  770. {
  771. for (int q=0; q<channel_multiplier; q++)
  772. {
  773. for (int i=0; i<kernel_size_h; i++)
  774. {
  775. for (int j=0; j<kernel_size_w; j++)
  776. {
  777. tmp = data[i*kernel_size_w*channel_multiplier*num_input + j*channel_multiplier*num_input + p*channel_multiplier + q];
  778. fwrite(&tmp, sizeof(float), 1, bp);
  779. }
  780. }
  781. }
  782. }
  783. }
  784. else if (tensor.dtype() == 3)// int32
  785. {
  786. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  787. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  788. float tmp;
  789. for (int p=0; p<num_input; p++)
  790. {
  791. for (int q=0; q<channel_multiplier; q++)
  792. {
  793. for (int i=0; i<kernel_size_h; i++)
  794. {
  795. for (int j=0; j<kernel_size_w; j++)
  796. {
  797. tmp = data[i*kernel_size_w*channel_multiplier*num_input + j*channel_multiplier*num_input + p*channel_multiplier + q];
  798. fwrite(&tmp, sizeof(float), 1, bp);
  799. }
  800. }
  801. }
  802. }
  803. }
  804. }
  805. fprintf(pp, " 0=%d", num_output);
  806. fprintf(pp, " 1=%d", kernel_size_w);
  807. fprintf(pp, " 11=%d", kernel_size_h);
  808. fprintf(pp, " 2=%d", dilation_w);
  809. fprintf(pp, " 12=%d", dilation_h);
  810. fprintf(pp, " 3=%d", stride_w);
  811. fprintf(pp, " 13=%d", stride_h);
  812. fprintf(pp, " 4=%d", pad);
  813. fprintf(pp, " 5=%d", bias_term);
  814. fprintf(pp, " 6=%d", weight_data_size);
  815. fprintf(pp, " 7=%d", group);
  816. }
  817. else if (node.op() == "Div" || node.op() == "RealDiv")
  818. {
  819. int op_type = 3;
  820. fprintf(pp, " 0=%d", op_type);
  821. }
  822. else if (node.op() == "Exp")
  823. {
  824. int op_type = 7;
  825. fprintf(pp, " 0=%d", op_type);
  826. }
  827. else if (node.op() == "ExpandDims")
  828. {
  829. int expand_w = 0;
  830. int expand_h = 0;
  831. int expand_c = 0;
  832. tensorflow::AttrValue value_dim;
  833. if (find_attr_value(node, "Tdim", value_dim))
  834. {
  835. int dim = value_dim.i();
  836. if (dim == 0)
  837. expand_w = 1;
  838. if (dim == 1)
  839. expand_h = 1;
  840. if (dim == 2)
  841. expand_c = 1;
  842. }
  843. fprintf(pp, " 0=%d", expand_w);
  844. fprintf(pp, " 1=%d", expand_h);
  845. fprintf(pp, " 2=%d", expand_c);
  846. }
  847. else if (node.op() == "Floor")
  848. {
  849. int op_type = 2;
  850. fprintf(pp, " 0=%d", op_type);
  851. }
  852. else if (node.op() == "LRN")
  853. {
  854. int norm_region = 0;
  855. int local_size = 1;
  856. float alpha = 1.f;
  857. float beta = 0.5f;
  858. tensorflow::AttrValue value_depth_radius;
  859. if (find_attr_value(node, "depth_radius", value_depth_radius))
  860. {
  861. local_size = value_depth_radius.i() * 2 + 1;
  862. }
  863. tensorflow::AttrValue value_alpha;
  864. if (find_attr_value(node, "alpha", value_alpha))
  865. {
  866. alpha = value_alpha.f();
  867. }
  868. tensorflow::AttrValue value_beta;
  869. if (find_attr_value(node, "beta", value_beta))
  870. {
  871. beta = value_beta.f();
  872. }
  873. // TODO
  874. float bias = 1.f;
  875. tensorflow::AttrValue value_bias;
  876. if (find_attr_value(node, "bias", value_bias))
  877. {
  878. bias = value_bias.f();
  879. }
  880. fprintf(pp, " 0=%d", norm_region);
  881. fprintf(pp, " 1=%d", local_size);
  882. fprintf(pp, " 2=%f", alpha);
  883. fprintf(pp, " 3=%f", beta);
  884. }
  885. else if (node.op() == "MatMul")
  886. {
  887. // weights
  888. tensorflow::TensorProto tensor;
  889. find_tensor_proto(weights, node, tensor);
  890. const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
  891. int num_input = shape.dim(0).size();
  892. int num_output = shape.dim(1).size();
  893. int bias_term = 0;
  894. int weight_data_size = 0;
  895. // reorder i-o to o-i
  896. if (!tensor.tensor_content().empty())
  897. {
  898. int quantize_tag = 0;
  899. fwrite(&quantize_tag, sizeof(int), 1, bp);
  900. if (tensor.dtype() == 1)// float
  901. {
  902. const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
  903. weight_data_size = tensor.tensor_content().size() / sizeof(float);
  904. float tmp;
  905. for (int p=0; p<num_output; p++)
  906. {
  907. for (int q=0; q<num_input; q++)
  908. {
  909. tmp = data[q*num_output + p];
  910. fwrite(&tmp, sizeof(float), 1, bp);
  911. }
  912. }
  913. }
  914. else if (tensor.dtype() == 3)// int32
  915. {
  916. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  917. weight_data_size = tensor.tensor_content().size() / sizeof(int);
  918. float tmp;
  919. for (int p=0; p<num_output; p++)
  920. {
  921. for (int q=0; q<num_input; q++)
  922. {
  923. tmp = data[q*num_output + p];
  924. fwrite(&tmp, sizeof(float), 1, bp);
  925. }
  926. }
  927. }
  928. }
  929. fprintf(pp, " 0=%d", num_output);
  930. fprintf(pp, " 1=%d", bias_term);
  931. fprintf(pp, " 2=%d", weight_data_size);
  932. }
  933. else if (node.op() == "Max" || node.op() == "Maximum")
  934. {
  935. // check weights
  936. tensorflow::TensorProto tensor;
  937. if (find_tensor_proto(weights, node, tensor))
  938. {
  939. int operation = 4;
  940. int dim = 0;
  941. float coeff = 1.f;
  942. dim = parse_tensor_reduction_dim(tensor);
  943. fprintf(pp, " 0=%d", operation);
  944. fprintf(pp, " 1=%d", dim);
  945. fprintf(pp, " 2=%f", coeff);
  946. }
  947. else
  948. {
  949. int op_type = 4;
  950. fprintf(pp, " 0=%d", op_type);
  951. }
  952. }
  953. else if (node.op() == "MaxPool")
  954. {
  955. int pooling_type = 0;
  956. int kernel_size_h = 1;
  957. int kernel_size_w = 1;
  958. int stride_h = 1;
  959. int stride_w = 1;
  960. int pad = 0;
  961. int global_pooling = 0;
  962. int pad_mode = 1;
  963. tensorflow::AttrValue value_ksize;
  964. if (find_attr_value(node, "ksize", value_ksize))
  965. {
  966. // batch, height, width, channels
  967. kernel_size_h = value_ksize.list().i(1);
  968. kernel_size_w = value_ksize.list().i(2);
  969. }
  970. tensorflow::AttrValue value_strides;
  971. if (find_attr_value(node, "strides", value_strides))
  972. {
  973. // batch, height, width, channels
  974. stride_h = value_strides.list().i(1);
  975. stride_w = value_strides.list().i(2);
  976. }
  977. tensorflow::AttrValue value_padding;
  978. if (find_attr_value(node, "padding", value_padding))
  979. {
  980. if (value_padding.s() == "VALID")
  981. {
  982. pad_mode = 1;
  983. }
  984. else if (value_padding.s() == "SAME")
  985. {
  986. pad_mode = 2;
  987. }
  988. }
  989. fprintf(pp, " 0=%d", pooling_type);
  990. fprintf(pp, " 1=%d", kernel_size_w);
  991. fprintf(pp, " 11=%d", kernel_size_h);
  992. fprintf(pp, " 2=%d", stride_w);
  993. fprintf(pp, " 12=%d", stride_h);
  994. fprintf(pp, " 3=%d", pad);
  995. fprintf(pp, " 4=%d", global_pooling);
  996. fprintf(pp, " 5=%d", pad_mode);
  997. }
  998. else if (node.op() == "Min" || node.op() == "Minimum")
  999. {
  1000. // check weights
  1001. tensorflow::TensorProto tensor;
  1002. if (find_tensor_proto(weights, node, tensor))
  1003. {
  1004. int operation = 5;
  1005. int dim = 0;
  1006. float coeff = 1.f;
  1007. dim = parse_tensor_reduction_dim(tensor);
  1008. fprintf(pp, " 0=%d", operation);
  1009. fprintf(pp, " 1=%d", dim);
  1010. fprintf(pp, " 2=%f", coeff);
  1011. }
  1012. else
  1013. {
  1014. int op_type = 5;
  1015. fprintf(pp, " 0=%d", op_type);
  1016. }
  1017. }
  1018. else if (node.op() == "Mul")
  1019. {
  1020. int op_type = 2;
  1021. fprintf(pp, " 0=%d", op_type);
  1022. }
  1023. else if (node.op() == "Neg")
  1024. {
  1025. int op_type = 1;
  1026. fprintf(pp, " 0=%d", op_type);
  1027. }
  1028. else if (node.op() == "NoOp")
  1029. {
  1030. }
  1031. else if (node.op() == "Pad")
  1032. {
  1033. int top = 0;
  1034. int bottom = 0;
  1035. int left = 0;
  1036. int right = 0;
  1037. int type = 0;
  1038. float value = 0.f;
  1039. // check weights
  1040. tensorflow::TensorProto tensor;
  1041. if (find_tensor_proto(weights, node, tensor))
  1042. {
  1043. if (!tensor.tensor_content().empty() && tensor.dtype() == 3)// int32
  1044. {
  1045. const int *data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  1046. int size = tensor.tensor_content().size() / sizeof(int);
  1047. if (size == 8)
  1048. {
  1049. // n h w c
  1050. top = data[2];
  1051. bottom = data[3];
  1052. left = data[4];
  1053. right = data[5];
  1054. }
  1055. }
  1056. }
  1057. tensorflow::AttrValue value_Tpaddings;
  1058. if (find_attr_value(node, "Tpaddings", value_Tpaddings))
  1059. {
  1060. type = value_Tpaddings.i();
  1061. }
  1062. tensorflow::AttrValue value_T;
  1063. if (find_attr_value(node, "T", value_T))
  1064. {
  1065. value = value_T.f();
  1066. }
  1067. fprintf(pp, " 0=%d", top);
  1068. fprintf(pp, " 1=%d", bottom);
  1069. fprintf(pp, " 2=%d", left);
  1070. fprintf(pp, " 3=%d", right);
  1071. fprintf(pp, " 4=%d", type);
  1072. fprintf(pp, " 5=%f", value);
  1073. }
  1074. else if (node.op() == "Placeholder")
  1075. {
  1076. // TODO pass through
  1077. fprintf(pp, " 0=0 1=0 2=0");
  1078. }
  1079. else if (node.op() == "Prod")
  1080. {
  1081. int operation = 6;
  1082. int dim = 0;
  1083. float coeff = 1.f;
  1084. // check weights
  1085. tensorflow::TensorProto tensor;
  1086. if (find_tensor_proto(weights, node, tensor))
  1087. {
  1088. dim = parse_tensor_reduction_dim(tensor);
  1089. }
  1090. fprintf(pp, " 0=%d", operation);
  1091. fprintf(pp, " 1=%d", dim);
  1092. fprintf(pp, " 2=%f", coeff);
  1093. }
  1094. else if (node.op() == "Reciprocal")
  1095. {
  1096. int op_type = 15;
  1097. fprintf(pp, " 0=%d", op_type);
  1098. }
  1099. else if (node.op() == "Relu")
  1100. {
  1101. float slope = 0.f;
  1102. fprintf(pp, " 0=%f", slope);
  1103. }
  1104. else if (node.op() == "Reshape")
  1105. {
  1106. tensorflow::TensorProto tensor;
  1107. if (find_tensor_proto(weights, node, tensor))
  1108. {
  1109. if (!tensor.tensor_content().empty() && tensor.dtype() == 3)// int32
  1110. {
  1111. const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
  1112. int size = tensor.tensor_content().size() / sizeof(int);
  1113. // n h w c
  1114. // n h w
  1115. // n w
  1116. if (size == 4)
  1117. {
  1118. fprintf(pp, " 0=%d 1=%d 2=%d 3=0", data[2], data[1], data[3]);
  1119. }
  1120. if (size == 3)
  1121. {
  1122. fprintf(pp, " 0=%d 1=%d 2=-233 3=1", data[2], data[1]);
  1123. }
  1124. if (size == 2)
  1125. {
  1126. fprintf(pp, " 0=%d 1=-233 2=-233 3=1", data[1]);
  1127. }
  1128. }
  1129. }
  1130. else
  1131. {
  1132. // pass through
  1133. fprintf(pp, " 0=0 1=0 2=0 3=0");
  1134. }
  1135. }
  1136. else if (node.op() == "Rsqrt")
  1137. {
  1138. int op_type = 6;
  1139. fprintf(pp, " 0=%d", op_type);
  1140. }
  1141. else if (node.op() == "Sigmoid")
  1142. {
  1143. }
  1144. else if (node.op() == "Softmax")
  1145. {
  1146. }
  1147. else if (node.op() == "Square")
  1148. {
  1149. int op_type = 4;
  1150. fprintf(pp, " 0=%d", op_type);
  1151. }
  1152. else if (node.op() == "Squeeze")
  1153. {
  1154. int squeeze_w = 0;
  1155. int squeeze_h = 0;
  1156. int squeeze_c = 0;
  1157. tensorflow::AttrValue value_squeeze_dims;
  1158. if (find_attr_value(node, "squeeze_dims", value_squeeze_dims))
  1159. {
  1160. for (int i = 0; i<value_squeeze_dims.list().i_size(); i++)
  1161. {
  1162. int dim = value_squeeze_dims.list().i(i);
  1163. if (dim == 0)
  1164. squeeze_w = 1;
  1165. if (dim == 1)
  1166. squeeze_h = 1;
  1167. if (dim == 2)
  1168. squeeze_c = 1;
  1169. }
  1170. }
  1171. fprintf(pp, " 0=%d", squeeze_w);
  1172. fprintf(pp, " 1=%d", squeeze_h);
  1173. fprintf(pp, " 2=%d", squeeze_c);
  1174. }
  1175. else if (node.op() == "Sub")
  1176. {
  1177. int op_type = 1;
  1178. fprintf(pp, " 0=%d", op_type);
  1179. }
  1180. else if (node.op() == "Sum")
  1181. {
  1182. int operation = 0;
  1183. int dim = 0;
  1184. float coeff = 1.f;
  1185. // check weights
  1186. tensorflow::TensorProto tensor;
  1187. if (find_tensor_proto(weights, node, tensor))
  1188. {
  1189. dim = parse_tensor_reduction_dim(tensor);
  1190. }
  1191. fprintf(pp, " 0=%d", operation);
  1192. fprintf(pp, " 1=%d", dim);
  1193. fprintf(pp, " 2=%f", coeff);
  1194. }
  1195. else
  1196. {
  1197. const google::protobuf::Map<std::string, tensorflow::AttrValue>& attr = node.attr();
  1198. google::protobuf::Map<std::string, tensorflow::AttrValue>::const_iterator it = attr.begin();
  1199. for (; it != attr.end(); it++)
  1200. {
  1201. std::cerr << it->first << " #" << it->second.type() << std::endl;
  1202. }
  1203. }
  1204. fprintf(pp, "\n");
  1205. std::string output_name = node.name();
  1206. if (node_reference.find(output_name) != node_reference.end())
  1207. {
  1208. int refcount = node_reference[output_name];
  1209. if (refcount > 1)
  1210. {
  1211. char splitname[256];
  1212. sprintf(splitname, "splitncnn_%d", internal_split);
  1213. fprintf(pp, "%-16s %-32s %d %d", "Split", splitname, 1, refcount);
  1214. fprintf(pp, " %s", output_name.c_str());
  1215. for (int j=0; j<refcount; j++)
  1216. {
  1217. fprintf(pp, " %s_splitncnn_%d", output_name.c_str(), j);
  1218. }
  1219. fprintf(pp, "\n");
  1220. internal_split++;
  1221. }
  1222. }
  1223. }
  1224. fclose(pp);
  1225. fclose(bp);
  1226. return 0;
  1227. }