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.

caffe2ncnn.cpp 46 kB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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 <math.h>
  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 "caffe.pb.h"
  26. static inline size_t alignSize(size_t sz, int n)
  27. {
  28. return (sz + n-1) & -n;
  29. }
  30. // convert float to half precision floating point
  31. static unsigned short float2half(float value)
  32. {
  33. // 1 : 8 : 23
  34. union
  35. {
  36. unsigned int u;
  37. float f;
  38. } tmp;
  39. tmp.f = value;
  40. // 1 : 8 : 23
  41. unsigned short sign = (tmp.u & 0x80000000) >> 31;
  42. unsigned short exponent = (tmp.u & 0x7F800000) >> 23;
  43. unsigned int significand = tmp.u & 0x7FFFFF;
  44. // fprintf(stderr, "%d %d %d\n", sign, exponent, significand);
  45. // 1 : 5 : 10
  46. unsigned short fp16;
  47. if (exponent == 0)
  48. {
  49. // zero or denormal, always underflow
  50. fp16 = (sign << 15) | (0x00 << 10) | 0x00;
  51. }
  52. else if (exponent == 0xFF)
  53. {
  54. // infinity or NaN
  55. fp16 = (sign << 15) | (0x1F << 10) | (significand ? 0x200 : 0x00);
  56. }
  57. else
  58. {
  59. // normalized
  60. short newexp = exponent + (- 127 + 15);
  61. if (newexp >= 31)
  62. {
  63. // overflow, return infinity
  64. fp16 = (sign << 15) | (0x1F << 10) | 0x00;
  65. }
  66. else if (newexp <= 0)
  67. {
  68. // underflow
  69. if (newexp >= -10)
  70. {
  71. // denormal half-precision
  72. unsigned short sig = (significand | 0x800000) >> (14 - newexp);
  73. fp16 = (sign << 15) | (0x00 << 10) | sig;
  74. }
  75. else
  76. {
  77. // underflow
  78. fp16 = (sign << 15) | (0x00 << 10) | 0x00;
  79. }
  80. }
  81. else
  82. {
  83. fp16 = (sign << 15) | (newexp << 10) | (significand >> 13);
  84. }
  85. }
  86. return fp16;
  87. }
  88. static int quantize_weight(float *data, size_t data_length, std::vector<unsigned short>& float16_weights)
  89. {
  90. float16_weights.resize(data_length);
  91. for (size_t i = 0; i < data_length; i++)
  92. {
  93. float f = data[i];
  94. unsigned short fp16 = float2half(f);
  95. float16_weights[i] = fp16;
  96. }
  97. // magic tag for half-precision floating point
  98. return 0x01306B47;
  99. }
  100. static bool quantize_weight(float *data, size_t data_length, int quantize_level, std::vector<float> &quantize_table, std::vector<unsigned char> &quantize_index) {
  101. assert(quantize_level != 0);
  102. assert(data != NULL);
  103. assert(data_length > 0);
  104. if (data_length < static_cast<size_t>(quantize_level)) {
  105. fprintf(stderr, "No need quantize,because: data_length < quantize_level");
  106. return false;
  107. }
  108. quantize_table.reserve(quantize_level);
  109. quantize_index.reserve(data_length);
  110. // 1. Find min and max value
  111. float max_value = std::numeric_limits<float>::min();
  112. float min_value = std::numeric_limits<float>::max();
  113. for (size_t i = 0; i < data_length; ++i)
  114. {
  115. if (max_value < data[i]) max_value = data[i];
  116. if (min_value > data[i]) min_value = data[i];
  117. }
  118. float strides = (max_value - min_value) / quantize_level;
  119. // 2. Generate quantize table
  120. for (int i = 0; i < quantize_level; ++i)
  121. {
  122. quantize_table.push_back(min_value + i * strides);
  123. }
  124. // 3. Align data to the quantized value
  125. for (size_t i = 0; i < data_length; ++i)
  126. {
  127. size_t table_index = int((data[i] - min_value) / strides);
  128. table_index = std::min<float>(table_index, quantize_level - 1);
  129. float low_value = quantize_table[table_index];
  130. float high_value = low_value + strides;
  131. // find a nearest value between low and high value.
  132. float targetValue = data[i] - low_value < high_value - data[i] ? low_value : high_value;
  133. table_index = int((targetValue - min_value) / strides);
  134. table_index = std::min<float>(table_index, quantize_level - 1);
  135. quantize_index.push_back(table_index);
  136. }
  137. return true;
  138. }
  139. static bool read_proto_from_text(const char* filepath, google::protobuf::Message* message)
  140. {
  141. std::ifstream fs(filepath, std::ifstream::in);
  142. if (!fs.is_open())
  143. {
  144. fprintf(stderr, "open failed %s\n", filepath);
  145. return false;
  146. }
  147. google::protobuf::io::IstreamInputStream input(&fs);
  148. bool success = google::protobuf::TextFormat::Parse(&input, message);
  149. fs.close();
  150. return success;
  151. }
  152. static bool read_proto_from_binary(const char* filepath, google::protobuf::Message* message)
  153. {
  154. std::ifstream fs(filepath, std::ifstream::in | std::ifstream::binary);
  155. if (!fs.is_open())
  156. {
  157. fprintf(stderr, "open failed %s\n", filepath);
  158. return false;
  159. }
  160. google::protobuf::io::IstreamInputStream input(&fs);
  161. google::protobuf::io::CodedInputStream codedstr(&input);
  162. codedstr.SetTotalBytesLimit(INT_MAX, INT_MAX / 2);
  163. bool success = message->ParseFromCodedStream(&codedstr);
  164. fs.close();
  165. return success;
  166. }
  167. int main(int argc, char** argv)
  168. {
  169. if (!(argc == 3 || argc == 5 || argc == 6))
  170. {
  171. fprintf(stderr, "Usage: %s [caffeproto] [caffemodel] [ncnnproto] [ncnnbin] [quantizelevel]\n", argv[0]);
  172. return -1;
  173. }
  174. const char* caffeproto = argv[1];
  175. const char* caffemodel = argv[2];
  176. const char* ncnn_prototxt = argc >= 5 ? argv[3] : "ncnn.proto";
  177. const char* ncnn_modelbin = argc >= 5 ? argv[4] : "ncnn.bin";
  178. const char* quantize_param = argc == 6 ? argv[5] : "0";
  179. int quantize_level = atoi(quantize_param);
  180. if (quantize_level != 0 && quantize_level != 256 && quantize_level != 65536) {
  181. fprintf(stderr, "%s: only support quantize level = 0, 256, or 65536", argv[0]);
  182. return -1;
  183. }
  184. caffe::NetParameter proto;
  185. caffe::NetParameter net;
  186. // load
  187. bool s0 = read_proto_from_text(caffeproto, &proto);
  188. if (!s0)
  189. {
  190. fprintf(stderr, "read_proto_from_text failed\n");
  191. return -1;
  192. }
  193. bool s1 = read_proto_from_binary(caffemodel, &net);
  194. if (!s1)
  195. {
  196. fprintf(stderr, "read_proto_from_binary failed\n");
  197. return -1;
  198. }
  199. FILE* pp = fopen(ncnn_prototxt, "wb");
  200. FILE* bp = fopen(ncnn_modelbin, "wb");
  201. // magic
  202. fprintf(pp, "7767517\n");
  203. // rename mapping for identical bottom top style
  204. std::map<std::string, std::string> blob_name_decorated;
  205. // bottom blob reference
  206. std::map<std::string, int> bottom_reference;
  207. // global definition line
  208. // [layer count] [blob count]
  209. int layer_count = proto.layer_size();
  210. std::set<std::string> blob_names;
  211. for (int i=0; i<layer_count; i++)
  212. {
  213. const caffe::LayerParameter& layer = proto.layer(i);
  214. for (int j=0; j<layer.bottom_size(); j++)
  215. {
  216. std::string blob_name = layer.bottom(j);
  217. if (blob_name_decorated.find(blob_name) != blob_name_decorated.end())
  218. {
  219. blob_name = blob_name_decorated[blob_name];
  220. }
  221. blob_names.insert(blob_name);
  222. if (bottom_reference.find(blob_name) == bottom_reference.end())
  223. {
  224. bottom_reference[blob_name] = 1;
  225. }
  226. else
  227. {
  228. bottom_reference[blob_name] = bottom_reference[blob_name] + 1;
  229. }
  230. }
  231. if (layer.bottom_size() == 1 && layer.top_size() == 1 && layer.bottom(0) == layer.top(0))
  232. {
  233. std::string blob_name = layer.top(0) + "_" + layer.name();
  234. blob_name_decorated[layer.top(0)] = blob_name;
  235. blob_names.insert(blob_name);
  236. }
  237. else
  238. {
  239. for (int j=0; j<layer.top_size(); j++)
  240. {
  241. std::string blob_name = layer.top(j);
  242. blob_names.insert(blob_name);
  243. }
  244. }
  245. }
  246. // remove bottom_reference entry with reference equals to one
  247. int splitncnn_blob_count = 0;
  248. std::map<std::string, int>::iterator it = bottom_reference.begin();
  249. while (it != bottom_reference.end())
  250. {
  251. if (it->second == 1)
  252. {
  253. bottom_reference.erase(it++);
  254. }
  255. else
  256. {
  257. splitncnn_blob_count += it->second;
  258. // fprintf(stderr, "%s %d\n", it->first.c_str(), it->second);
  259. ++it;
  260. }
  261. }
  262. fprintf(pp, "%lu %lu\n", layer_count + bottom_reference.size(), blob_names.size() + splitncnn_blob_count);
  263. // populate
  264. blob_name_decorated.clear();
  265. int internal_split = 0;
  266. for (int i=0; i<layer_count; i++)
  267. {
  268. const caffe::LayerParameter& layer = proto.layer(i);
  269. // layer definition line, repeated
  270. // [type] [name] [bottom blob count] [top blob count] [bottom blobs] [top blobs] [layer specific params]
  271. if (layer.type() == "Convolution")
  272. {
  273. const caffe::ConvolutionParameter& convolution_param = layer.convolution_param();
  274. if (convolution_param.group() != 1)
  275. fprintf(pp, "%-16s", "ConvolutionDepthWise");
  276. else
  277. fprintf(pp, "%-16s", "Convolution");
  278. }
  279. else if (layer.type() == "ConvolutionDepthwise" || layer.type() == "DepthwiseConvolution")
  280. {
  281. fprintf(pp, "%-16s", "ConvolutionDepthWise");
  282. }
  283. else if (layer.type() == "Deconvolution")
  284. {
  285. const caffe::ConvolutionParameter& convolution_param = layer.convolution_param();
  286. if (convolution_param.group() != 1)
  287. fprintf(pp, "%-16s", "DeconvolutionDepthWise");
  288. else
  289. fprintf(pp, "%-16s", "Deconvolution");
  290. }
  291. else if (layer.type() == "MemoryData")
  292. {
  293. fprintf(pp, "%-16s", "Input");
  294. }
  295. else if (layer.type() == "Python")
  296. {
  297. const caffe::PythonParameter& python_param = layer.python_param();
  298. std::string python_layer_name = python_param.layer();
  299. if (python_layer_name == "ProposalLayer")
  300. fprintf(pp, "%-16s", "Proposal");
  301. else
  302. fprintf(pp, "%-16s", python_layer_name.c_str());
  303. }
  304. else
  305. {
  306. fprintf(pp, "%-16s", layer.type().c_str());
  307. }
  308. fprintf(pp, " %-16s %d %d", layer.name().c_str(), layer.bottom_size(), layer.top_size());
  309. for (int j=0; j<layer.bottom_size(); j++)
  310. {
  311. std::string blob_name = layer.bottom(j);
  312. if (blob_name_decorated.find(layer.bottom(j)) != blob_name_decorated.end())
  313. {
  314. blob_name = blob_name_decorated[layer.bottom(j)];
  315. }
  316. if (bottom_reference.find(blob_name) != bottom_reference.end())
  317. {
  318. int refidx = bottom_reference[blob_name] - 1;
  319. bottom_reference[blob_name] = refidx;
  320. char splitsuffix[256];
  321. sprintf(splitsuffix, "_splitncnn_%d", refidx);
  322. blob_name = blob_name + splitsuffix;
  323. }
  324. fprintf(pp, " %s", blob_name.c_str());
  325. }
  326. // decorated
  327. if (layer.bottom_size() == 1 && layer.top_size() == 1 && layer.bottom(0) == layer.top(0))
  328. {
  329. std::string blob_name = layer.top(0) + "_" + layer.name();
  330. blob_name_decorated[layer.top(0)] = blob_name;
  331. fprintf(pp, " %s", blob_name.c_str());
  332. }
  333. else
  334. {
  335. for (int j=0; j<layer.top_size(); j++)
  336. {
  337. std::string blob_name = layer.top(j);
  338. fprintf(pp, " %s", blob_name.c_str());
  339. }
  340. }
  341. // find blob binary by layer name
  342. int netidx;
  343. for (netidx=0; netidx<net.layer_size(); netidx++)
  344. {
  345. if (net.layer(netidx).name() == layer.name())
  346. {
  347. break;
  348. }
  349. }
  350. // layer specific params
  351. if (layer.type() == "BatchNorm")
  352. {
  353. const caffe::LayerParameter& binlayer = net.layer(netidx);
  354. const caffe::BlobProto& mean_blob = binlayer.blobs(0);
  355. const caffe::BlobProto& var_blob = binlayer.blobs(1);
  356. fprintf(pp, " 0=%d", (int)mean_blob.data_size());
  357. const caffe::BatchNormParameter& batch_norm_param = layer.batch_norm_param();
  358. float eps = batch_norm_param.eps();
  359. std::vector<float> ones(mean_blob.data_size(), 1.f);
  360. fwrite(ones.data(), sizeof(float), ones.size(), bp);// slope
  361. if (binlayer.blobs_size() < 3)
  362. {
  363. fwrite(mean_blob.data().data(), sizeof(float), mean_blob.data_size(), bp);
  364. float tmp;
  365. for (int j=0; j<var_blob.data_size(); j++)
  366. {
  367. tmp = var_blob.data().data()[j] + eps;
  368. fwrite(&tmp, sizeof(float), 1, bp);
  369. }
  370. }
  371. else
  372. {
  373. float scale_factor = binlayer.blobs(2).data().data()[0] == 0 ? 0 : 1 / binlayer.blobs(2).data().data()[0];
  374. // premultiply scale_factor to mean and variance
  375. float tmp;
  376. for (int j=0; j<mean_blob.data_size(); j++)
  377. {
  378. tmp = mean_blob.data().data()[j] * scale_factor;
  379. fwrite(&tmp, sizeof(float), 1, bp);
  380. }
  381. for (int j=0; j<var_blob.data_size(); j++)
  382. {
  383. tmp = var_blob.data().data()[j] * scale_factor + eps;
  384. fwrite(&tmp, sizeof(float), 1, bp);
  385. }
  386. }
  387. std::vector<float> zeros(mean_blob.data_size(), 0.f);
  388. fwrite(zeros.data(), sizeof(float), zeros.size(), bp);// bias
  389. }
  390. else if (layer.type() == "Concat")
  391. {
  392. const caffe::ConcatParameter& concat_param = layer.concat_param();
  393. int dim = concat_param.axis() - 1;
  394. fprintf(pp, " 0=%d", dim);
  395. }
  396. else if (layer.type() == "Convolution" || layer.type() == "ConvolutionDepthwise" || layer.type() == "DepthwiseConvolution")
  397. {
  398. const caffe::LayerParameter& binlayer = net.layer(netidx);
  399. const caffe::BlobProto& weight_blob = binlayer.blobs(0);
  400. const caffe::ConvolutionParameter& convolution_param = layer.convolution_param();
  401. fprintf(pp, " 0=%d", convolution_param.num_output());
  402. if (convolution_param.has_kernel_w() && convolution_param.has_kernel_h())
  403. {
  404. fprintf(pp, " 1=%d", convolution_param.kernel_w());
  405. fprintf(pp, " 11=%d", convolution_param.kernel_h());
  406. }
  407. else
  408. {
  409. fprintf(pp, " 1=%d", convolution_param.kernel_size(0));
  410. }
  411. fprintf(pp, " 2=%d", convolution_param.dilation_size() != 0 ? convolution_param.dilation(0) : 1);
  412. if (convolution_param.has_stride_w() && convolution_param.has_stride_h())
  413. {
  414. fprintf(pp, " 3=%d", convolution_param.stride_w());
  415. fprintf(pp, " 13=%d", convolution_param.stride_h());
  416. }
  417. else
  418. {
  419. fprintf(pp, " 3=%d", convolution_param.stride_size() != 0 ? convolution_param.stride(0) : 1);
  420. }
  421. if (convolution_param.has_pad_w() && convolution_param.has_pad_h())
  422. {
  423. fprintf(pp, " 4=%d", convolution_param.pad_w());
  424. fprintf(pp, " 14=%d", convolution_param.pad_h());
  425. }
  426. else
  427. {
  428. fprintf(pp, " 4=%d", convolution_param.pad_size() != 0 ? convolution_param.pad(0) : 0);
  429. }
  430. fprintf(pp, " 5=%d", convolution_param.bias_term());
  431. fprintf(pp, " 6=%d", weight_blob.data_size());
  432. if (layer.type() == "ConvolutionDepthwise")
  433. {
  434. fprintf(pp, " 7=%d", convolution_param.num_output());
  435. }
  436. else if (convolution_param.group() != 1)
  437. {
  438. fprintf(pp, " 7=%d", convolution_param.group());
  439. }
  440. for (int j = 0; j < binlayer.blobs_size(); j++)
  441. {
  442. int quantize_tag = 0;
  443. const caffe::BlobProto& blob = binlayer.blobs(j);
  444. std::vector<float> quantize_table;
  445. std::vector<unsigned char> quantize_index;
  446. std::vector<unsigned short> float16_weights;
  447. // we will not quantize the bias values
  448. if (j == 0 && quantize_level != 0)
  449. {
  450. if (quantize_level == 256)
  451. {
  452. quantize_tag = quantize_weight((float *)blob.data().data(), blob.data_size(), quantize_level, quantize_table, quantize_index);
  453. }
  454. else if (quantize_level == 65536)
  455. {
  456. quantize_tag = quantize_weight((float *)blob.data().data(), blob.data_size(), float16_weights);
  457. }
  458. }
  459. // write quantize tag first
  460. if (j == 0)
  461. fwrite(&quantize_tag, sizeof(int), 1, bp);
  462. if (quantize_tag)
  463. {
  464. int p0 = ftell(bp);
  465. if (quantize_level == 256)
  466. {
  467. // write quantize table and index
  468. fwrite(quantize_table.data(), sizeof(float), quantize_table.size(), bp);
  469. fwrite(quantize_index.data(), sizeof(unsigned char), quantize_index.size(), bp);
  470. }
  471. else if (quantize_level == 65536)
  472. {
  473. fwrite(float16_weights.data(), sizeof(unsigned short), float16_weights.size(), bp);
  474. }
  475. // padding to 32bit align
  476. int nwrite = ftell(bp) - p0;
  477. int nalign = alignSize(nwrite, 4);
  478. unsigned char padding[4] = {0x00, 0x00, 0x00, 0x00};
  479. fwrite(padding, sizeof(unsigned char), nalign - nwrite, bp);
  480. }
  481. else
  482. {
  483. // write original data
  484. fwrite(blob.data().data(), sizeof(float), blob.data_size(), bp);
  485. }
  486. }
  487. }
  488. else if (layer.type() == "Crop")
  489. {
  490. const caffe::CropParameter& crop_param = layer.crop_param();
  491. int num_offset = crop_param.offset_size();
  492. if (num_offset == 2)
  493. {
  494. int woffset = crop_param.offset(1);
  495. int hoffset = crop_param.offset(0);
  496. fprintf(pp, " 0=%d", woffset);
  497. fprintf(pp, " 1=%d", hoffset);
  498. }
  499. else if (num_offset == 3)
  500. {
  501. int woffset = crop_param.offset(2);
  502. int hoffset = crop_param.offset(1);
  503. int coffset = crop_param.offset(0);
  504. fprintf(pp, " 0=%d", woffset);
  505. fprintf(pp, " 1=%d", hoffset);
  506. fprintf(pp, " 2=%d", coffset);
  507. }
  508. }
  509. else if (layer.type() == "Deconvolution")
  510. {
  511. const caffe::LayerParameter& binlayer = net.layer(netidx);
  512. const caffe::BlobProto& weight_blob = binlayer.blobs(0);
  513. const caffe::ConvolutionParameter& convolution_param = layer.convolution_param();
  514. fprintf(pp, " 0=%d", convolution_param.num_output());
  515. if (convolution_param.has_kernel_w() && convolution_param.has_kernel_h())
  516. {
  517. fprintf(pp, " 1=%d", convolution_param.kernel_w());
  518. fprintf(pp, " 11=%d", convolution_param.kernel_h());
  519. }
  520. else
  521. {
  522. fprintf(pp, " 1=%d", convolution_param.kernel_size(0));
  523. }
  524. fprintf(pp, " 2=%d", convolution_param.dilation_size() != 0 ? convolution_param.dilation(0) : 1);
  525. if (convolution_param.has_stride_w() && convolution_param.has_stride_h())
  526. {
  527. fprintf(pp, " 3=%d", convolution_param.stride_w());
  528. fprintf(pp, " 13=%d", convolution_param.stride_h());
  529. }
  530. else
  531. {
  532. fprintf(pp, " 3=%d", convolution_param.stride_size() != 0 ? convolution_param.stride(0) : 1);
  533. }
  534. if (convolution_param.has_pad_w() && convolution_param.has_pad_h())
  535. {
  536. fprintf(pp, " 4=%d", convolution_param.pad_w());
  537. fprintf(pp, " 14=%d", convolution_param.pad_h());
  538. }
  539. else
  540. {
  541. fprintf(pp, " 4=%d", convolution_param.pad_size() != 0 ? convolution_param.pad(0) : 0);
  542. }
  543. fprintf(pp, " 5=%d", convolution_param.bias_term());
  544. fprintf(pp, " 6=%d", weight_blob.data_size());
  545. int group = convolution_param.group();
  546. if (group != 1)
  547. {
  548. fprintf(pp, " 7=%d", group);
  549. }
  550. int quantized_weight = 0;
  551. fwrite(&quantized_weight, sizeof(int), 1, bp);
  552. int maxk = 0;
  553. if (convolution_param.has_kernel_w() && convolution_param.has_kernel_h())
  554. {
  555. maxk = convolution_param.kernel_w() * convolution_param.kernel_h();
  556. }
  557. else
  558. {
  559. maxk = convolution_param.kernel_size(0) * convolution_param.kernel_size(0);
  560. }
  561. for (int g=0; g<group; g++)
  562. {
  563. // reorder weight from inch-outch to outch-inch
  564. int num_output = convolution_param.num_output() / group;
  565. int num_input = weight_blob.data_size() / maxk / num_output / group;
  566. const float* weight_data_ptr = weight_blob.data().data() + g * maxk * num_output * num_input;
  567. for (int k=0; k<num_output; k++)
  568. {
  569. for (int j=0; j<num_input; j++)
  570. {
  571. fwrite(weight_data_ptr + (j*num_output + k) * maxk, sizeof(float), maxk, bp);
  572. }
  573. }
  574. }
  575. for (int j=1; j<binlayer.blobs_size(); j++)
  576. {
  577. const caffe::BlobProto& blob = binlayer.blobs(j);
  578. fwrite(blob.data().data(), sizeof(float), blob.data_size(), bp);
  579. }
  580. }
  581. else if (layer.type() == "DetectionOutput")
  582. {
  583. const caffe::DetectionOutputParameter& detection_output_param = layer.detection_output_param();
  584. const caffe::NonMaximumSuppressionParameter& nms_param = detection_output_param.nms_param();
  585. fprintf(pp, " 0=%d", detection_output_param.num_classes());
  586. fprintf(pp, " 1=%f", nms_param.nms_threshold());
  587. fprintf(pp, " 2=%d", nms_param.top_k());
  588. fprintf(pp, " 3=%d", detection_output_param.keep_top_k());
  589. fprintf(pp, " 4=%f", detection_output_param.confidence_threshold());
  590. }
  591. else if (layer.type() == "Dropout")
  592. {
  593. const caffe::DropoutParameter& dropout_param = layer.dropout_param();
  594. if (dropout_param.has_scale_train() && !dropout_param.scale_train())
  595. {
  596. float scale = 1.f - dropout_param.dropout_ratio();
  597. fprintf(pp, " 0=%f", scale);
  598. }
  599. }
  600. else if (layer.type() == "Eltwise")
  601. {
  602. const caffe::EltwiseParameter& eltwise_param = layer.eltwise_param();
  603. int coeff_size = eltwise_param.coeff_size();
  604. fprintf(pp, " 0=%d", (int)eltwise_param.operation());
  605. fprintf(pp, " -23301=%d", coeff_size);
  606. for (int j=0; j<coeff_size; j++)
  607. {
  608. fprintf(pp, ",%f", eltwise_param.coeff(j));
  609. }
  610. }
  611. else if (layer.type() == "ELU")
  612. {
  613. const caffe::ELUParameter& elu_param = layer.elu_param();
  614. fprintf(pp, " 0=%f", elu_param.alpha());
  615. }
  616. else if (layer.type() == "InnerProduct")
  617. {
  618. const caffe::LayerParameter& binlayer = net.layer(netidx);
  619. const caffe::BlobProto& weight_blob = binlayer.blobs(0);
  620. const caffe::InnerProductParameter& inner_product_param = layer.inner_product_param();
  621. fprintf(pp, " 0=%d", inner_product_param.num_output());
  622. fprintf(pp, " 1=%d", inner_product_param.bias_term());
  623. fprintf(pp, " 2=%d", weight_blob.data_size());
  624. for (int j=0; j<binlayer.blobs_size(); j++)
  625. {
  626. int quantize_tag = 0;
  627. const caffe::BlobProto& blob = binlayer.blobs(j);
  628. std::vector<float> quantize_table;
  629. std::vector<unsigned char> quantize_index;
  630. std::vector<unsigned short> float16_weights;
  631. // we will not quantize the bias values
  632. if (j == 0 && quantize_level != 0)
  633. {
  634. if (quantize_level == 256)
  635. {
  636. quantize_tag = quantize_weight((float *)blob.data().data(), blob.data_size(), quantize_level, quantize_table, quantize_index);
  637. }
  638. else if (quantize_level == 65536)
  639. {
  640. quantize_tag = quantize_weight((float *)blob.data().data(), blob.data_size(), float16_weights);
  641. }
  642. }
  643. // write quantize tag first
  644. if (j == 0)
  645. fwrite(&quantize_tag, sizeof(int), 1, bp);
  646. if (quantize_tag)
  647. {
  648. int p0 = ftell(bp);
  649. if (quantize_level == 256)
  650. {
  651. // write quantize table and index
  652. fwrite(quantize_table.data(), sizeof(float), quantize_table.size(), bp);
  653. fwrite(quantize_index.data(), sizeof(unsigned char), quantize_index.size(), bp);
  654. }
  655. else if (quantize_level == 65536)
  656. {
  657. fwrite(float16_weights.data(), sizeof(unsigned short), float16_weights.size(), bp);
  658. }
  659. // padding to 32bit align
  660. int nwrite = ftell(bp) - p0;
  661. int nalign = alignSize(nwrite, 4);
  662. unsigned char padding[4] = {0x00, 0x00, 0x00, 0x00};
  663. fwrite(padding, sizeof(unsigned char), nalign - nwrite, bp);
  664. }
  665. else
  666. {
  667. // write original data
  668. fwrite(blob.data().data(), sizeof(float), blob.data_size(), bp);
  669. }
  670. }
  671. }
  672. else if (layer.type() == "Input")
  673. {
  674. const caffe::InputParameter& input_param = layer.input_param();
  675. const caffe::BlobShape& bs = input_param.shape(0);
  676. if (bs.dim_size() == 4)
  677. {
  678. fprintf(pp, " 0=%ld", bs.dim(3));
  679. fprintf(pp, " 1=%ld", bs.dim(2));
  680. fprintf(pp, " 2=%ld", bs.dim(1));
  681. }
  682. else if (bs.dim_size() == 3)
  683. {
  684. fprintf(pp, " 0=%ld", bs.dim(2));
  685. fprintf(pp, " 1=%ld", bs.dim(1));
  686. fprintf(pp, " 2=-233");
  687. }
  688. else if (bs.dim_size() == 2)
  689. {
  690. fprintf(pp, " 0=%ld", bs.dim(1));
  691. fprintf(pp, " 1=-233");
  692. fprintf(pp, " 2=-233");
  693. }
  694. }
  695. else if (layer.type() == "Interp")
  696. {
  697. const caffe::InterpParameter& interp_param = layer.interp_param();
  698. fprintf(pp, " 0=%d", 2);
  699. fprintf(pp, " 1=%f", (float)interp_param.zoom_factor());
  700. fprintf(pp, " 2=%f", (float)interp_param.zoom_factor());
  701. fprintf(pp, " 3=%d", interp_param.height());
  702. fprintf(pp, " 4=%d", interp_param.width());
  703. }
  704. else if (layer.type() == "LRN")
  705. {
  706. const caffe::LRNParameter& lrn_param = layer.lrn_param();
  707. fprintf(pp, " 0=%d", lrn_param.norm_region());
  708. fprintf(pp, " 1=%d", lrn_param.local_size());
  709. fprintf(pp, " 2=%f", lrn_param.alpha());
  710. fprintf(pp, " 3=%f", lrn_param.beta());
  711. }
  712. else if (layer.type() == "MemoryData")
  713. {
  714. const caffe::MemoryDataParameter& memory_data_param = layer.memory_data_param();
  715. fprintf(pp, " 0=%d", memory_data_param.width());
  716. fprintf(pp, " 1=%d", memory_data_param.height());
  717. fprintf(pp, " 2=%d", memory_data_param.channels());
  718. }
  719. else if (layer.type() == "MVN")
  720. {
  721. const caffe::MVNParameter& mvn_param = layer.mvn_param();
  722. fprintf(pp, " 0=%d", mvn_param.normalize_variance());
  723. fprintf(pp, " 1=%d", mvn_param.across_channels());
  724. fprintf(pp, " 2=%f", mvn_param.eps());
  725. }
  726. else if (layer.type() == "Normalize")
  727. {
  728. const caffe::LayerParameter& binlayer = net.layer(netidx);
  729. const caffe::BlobProto& scale_blob = binlayer.blobs(0);
  730. const caffe::NormalizeParameter& norm_param = layer.norm_param();
  731. fprintf(pp, " 0=%d", norm_param.across_spatial());
  732. fprintf(pp, " 1=%d", norm_param.channel_shared());
  733. fprintf(pp, " 2=%f", norm_param.eps());
  734. fprintf(pp, " 3=%d", scale_blob.data_size());
  735. fwrite(scale_blob.data().data(), sizeof(float), scale_blob.data_size(), bp);
  736. }
  737. else if (layer.type() == "Permute")
  738. {
  739. const caffe::PermuteParameter& permute_param = layer.permute_param();
  740. int order_size = permute_param.order_size();
  741. int order_type = 0;
  742. if (order_size == 0)
  743. order_type = 0;
  744. if (order_size == 1)
  745. {
  746. int order0 = permute_param.order(0);
  747. if (order0 == 0)
  748. order_type = 0;
  749. // permute with N not supported
  750. }
  751. if (order_size == 2)
  752. {
  753. int order0 = permute_param.order(0);
  754. int order1 = permute_param.order(1);
  755. if (order0 == 0)
  756. {
  757. if (order1 == 1) // 0 1 2 3
  758. order_type = 0;
  759. else if (order1 == 2) // 0 2 1 3
  760. order_type = 2;
  761. else if (order1 == 3) // 0 3 1 2
  762. order_type = 4;
  763. }
  764. // permute with N not supported
  765. }
  766. if (order_size == 3 || order_size == 4)
  767. {
  768. int order0 = permute_param.order(0);
  769. int order1 = permute_param.order(1);
  770. int order2 = permute_param.order(2);
  771. if (order0 == 0)
  772. {
  773. if (order1 == 1)
  774. {
  775. if (order2 == 2) // 0 1 2 3
  776. order_type = 0;
  777. if (order2 == 3) // 0 1 3 2
  778. order_type = 1;
  779. }
  780. else if (order1 == 2)
  781. {
  782. if (order2 == 1) // 0 2 1 3
  783. order_type = 2;
  784. if (order2 == 3) // 0 2 3 1
  785. order_type = 3;
  786. }
  787. else if (order1 == 3)
  788. {
  789. if (order2 == 1) // 0 3 1 2
  790. order_type = 4;
  791. if (order2 == 2) // 0 3 2 1
  792. order_type = 5;
  793. }
  794. }
  795. // permute with N not supported
  796. }
  797. fprintf(pp, " 0=%d", order_type);
  798. }
  799. else if (layer.type() == "Pooling")
  800. {
  801. const caffe::PoolingParameter& pooling_param = layer.pooling_param();
  802. fprintf(pp, " 0=%d", pooling_param.pool());
  803. if (pooling_param.has_kernel_w() && pooling_param.has_kernel_h())
  804. {
  805. fprintf(pp, " 1=%d", pooling_param.kernel_w());
  806. fprintf(pp, " 11=%d", pooling_param.kernel_h());
  807. }
  808. else
  809. {
  810. fprintf(pp, " 1=%d", pooling_param.kernel_size());
  811. }
  812. if (pooling_param.has_stride_w() && pooling_param.has_stride_h())
  813. {
  814. fprintf(pp, " 2=%d", pooling_param.stride_w());
  815. fprintf(pp, " 12=%d", pooling_param.stride_h());
  816. }
  817. else
  818. {
  819. fprintf(pp, " 2=%d", pooling_param.stride());
  820. }
  821. if (pooling_param.has_pad_w() && pooling_param.has_pad_h())
  822. {
  823. fprintf(pp, " 3=%d", pooling_param.pad_w());
  824. fprintf(pp, " 13=%d", pooling_param.pad_h());
  825. }
  826. else
  827. {
  828. fprintf(pp, " 3=%d", pooling_param.pad());
  829. }
  830. fprintf(pp, " 4=%d", pooling_param.has_global_pooling() ? pooling_param.global_pooling() : 0);
  831. }
  832. else if (layer.type() == "Power")
  833. {
  834. const caffe::PowerParameter& power_param = layer.power_param();
  835. fprintf(pp, " 0=%f", power_param.power());
  836. fprintf(pp, " 1=%f", power_param.scale());
  837. fprintf(pp, " 2=%f", power_param.shift());
  838. }
  839. else if (layer.type() == "PReLU")
  840. {
  841. const caffe::LayerParameter& binlayer = net.layer(netidx);
  842. const caffe::BlobProto& slope_blob = binlayer.blobs(0);
  843. fprintf(pp, " 0=%d", slope_blob.data_size());
  844. fwrite(slope_blob.data().data(), sizeof(float), slope_blob.data_size(), bp);
  845. }
  846. else if (layer.type() == "PriorBox")
  847. {
  848. const caffe::PriorBoxParameter& prior_box_param = layer.prior_box_param();
  849. int num_aspect_ratio = prior_box_param.aspect_ratio_size();
  850. for (int j=0; j<prior_box_param.aspect_ratio_size(); j++)
  851. {
  852. float ar = prior_box_param.aspect_ratio(j);
  853. if (fabs(ar - 1.) < 1e-6) {
  854. num_aspect_ratio--;
  855. }
  856. }
  857. float variances[4] = {0.1f, 0.1f, 0.1f, 0.1f};
  858. if (prior_box_param.variance_size() == 4)
  859. {
  860. variances[0] = prior_box_param.variance(0);
  861. variances[1] = prior_box_param.variance(1);
  862. variances[2] = prior_box_param.variance(2);
  863. variances[3] = prior_box_param.variance(3);
  864. }
  865. else if (prior_box_param.variance_size() == 1)
  866. {
  867. variances[0] = prior_box_param.variance(0);
  868. variances[1] = prior_box_param.variance(0);
  869. variances[2] = prior_box_param.variance(0);
  870. variances[3] = prior_box_param.variance(0);
  871. }
  872. int flip = prior_box_param.has_flip() ? prior_box_param.flip() : 1;
  873. int clip = prior_box_param.has_clip() ? prior_box_param.clip() : 0;
  874. int image_width = -233;
  875. int image_height = -233;
  876. if (prior_box_param.has_img_size())
  877. {
  878. image_width = prior_box_param.img_size();
  879. image_height = prior_box_param.img_size();
  880. }
  881. else if (prior_box_param.has_img_w() && prior_box_param.has_img_h())
  882. {
  883. image_width = prior_box_param.img_w();
  884. image_height = prior_box_param.img_h();
  885. }
  886. float step_width = -233;
  887. float step_height = -233;
  888. if (prior_box_param.has_step())
  889. {
  890. step_width = prior_box_param.step();
  891. step_height = prior_box_param.step();
  892. }
  893. else if (prior_box_param.has_step_w() && prior_box_param.has_step_h())
  894. {
  895. step_width = prior_box_param.step_w();
  896. step_height = prior_box_param.step_h();
  897. }
  898. fprintf(pp, " -23300=%d", prior_box_param.min_size_size());
  899. for (int j=0; j<prior_box_param.min_size_size(); j++)
  900. {
  901. fprintf(pp, ",%f", prior_box_param.min_size(j));
  902. }
  903. fprintf(pp, " -23301=%d", prior_box_param.max_size_size());
  904. for (int j=0; j<prior_box_param.max_size_size(); j++)
  905. {
  906. fprintf(pp, ",%f", prior_box_param.max_size(j));
  907. }
  908. fprintf(pp, " -23302=%d", num_aspect_ratio);
  909. for (int j=0; j<prior_box_param.aspect_ratio_size(); j++)
  910. {
  911. float ar = prior_box_param.aspect_ratio(j);
  912. if (fabs(ar - 1.) < 1e-6) {
  913. continue;
  914. }
  915. fprintf(pp, ",%f", ar);
  916. }
  917. fprintf(pp, " 3=%f", variances[0]);
  918. fprintf(pp, " 4=%f", variances[1]);
  919. fprintf(pp, " 5=%f", variances[2]);
  920. fprintf(pp, " 6=%f", variances[3]);
  921. fprintf(pp, " 7=%d", flip);
  922. fprintf(pp, " 8=%d", clip);
  923. fprintf(pp, " 9=%d", image_width);
  924. fprintf(pp, " 10=%d", image_height);
  925. fprintf(pp, " 11=%f", step_width);
  926. fprintf(pp, " 12=%f", step_height);
  927. fprintf(pp, " 13=%f", prior_box_param.offset());
  928. }
  929. else if (layer.type() == "Python")
  930. {
  931. const caffe::PythonParameter& python_param = layer.python_param();
  932. std::string python_layer_name = python_param.layer();
  933. if (python_layer_name == "ProposalLayer")
  934. {
  935. int feat_stride = 16;
  936. sscanf(python_param.param_str().c_str(), "'feat_stride': %d", &feat_stride);
  937. int base_size = 16;
  938. // float ratio;
  939. // float scale;
  940. int pre_nms_topN = 6000;
  941. int after_nms_topN = 300;
  942. float nms_thresh = 0.7;
  943. int min_size = 16;
  944. fprintf(pp, " 0=%d", feat_stride);
  945. fprintf(pp, " 1=%d", base_size);
  946. fprintf(pp, " 2=%d", pre_nms_topN);
  947. fprintf(pp, " 3=%d", after_nms_topN);
  948. fprintf(pp, " 4=%f", nms_thresh);
  949. fprintf(pp, " 5=%d", min_size);
  950. }
  951. }
  952. else if (layer.type() == "ReLU")
  953. {
  954. const caffe::ReLUParameter& relu_param = layer.relu_param();
  955. if (relu_param.has_negative_slope())
  956. {
  957. fprintf(pp, " 0=%f", relu_param.negative_slope());
  958. }
  959. }
  960. else if (layer.type() == "Reshape")
  961. {
  962. const caffe::ReshapeParameter& reshape_param = layer.reshape_param();
  963. const caffe::BlobShape& bs = reshape_param.shape();
  964. if (bs.dim_size() == 1)
  965. {
  966. fprintf(pp, " 0=%ld 1=-233 2=-233", bs.dim(0));
  967. }
  968. else if (bs.dim_size() == 2)
  969. {
  970. fprintf(pp, " 0=%ld 1=%ld 2=-233", bs.dim(1), bs.dim(0));
  971. }
  972. else if (bs.dim_size() == 3)
  973. {
  974. fprintf(pp, " 0=%ld 1=%ld 2=%ld", bs.dim(2), bs.dim(1), bs.dim(0));
  975. }
  976. else // bs.dim_size() == 4
  977. {
  978. fprintf(pp, " 0=%ld 1=%ld 2=%ld", bs.dim(3), bs.dim(2), bs.dim(1));
  979. }
  980. fprintf(pp, " 3=0");// permute
  981. }
  982. else if (layer.type() == "ROIPooling")
  983. {
  984. const caffe::ROIPoolingParameter& roi_pooling_param = layer.roi_pooling_param();
  985. fprintf(pp, " 0=%d", roi_pooling_param.pooled_w());
  986. fprintf(pp, " 1=%d", roi_pooling_param.pooled_h());
  987. fprintf(pp, " 2=%f", roi_pooling_param.spatial_scale());
  988. }
  989. else if (layer.type() == "Scale")
  990. {
  991. const caffe::LayerParameter& binlayer = net.layer(netidx);
  992. const caffe::ScaleParameter& scale_param = layer.scale_param();
  993. bool scale_weight = scale_param.bias_term() ? (binlayer.blobs_size() == 2) : (binlayer.blobs_size() == 1);
  994. if (scale_weight)
  995. {
  996. const caffe::BlobProto& weight_blob = binlayer.blobs(0);
  997. fprintf(pp, " 0=%d", (int)weight_blob.data_size());
  998. }
  999. else
  1000. {
  1001. fprintf(pp, " 0=-233");
  1002. }
  1003. fprintf(pp, " 1=%d", scale_param.bias_term());
  1004. for (int j=0; j<binlayer.blobs_size(); j++)
  1005. {
  1006. const caffe::BlobProto& blob = binlayer.blobs(j);
  1007. fwrite(blob.data().data(), sizeof(float), blob.data_size(), bp);
  1008. }
  1009. }
  1010. else if (layer.type() == "ShuffleChannel")
  1011. {
  1012. const caffe::ShuffleChannelParameter&
  1013. shuffle_channel_param = layer.shuffle_channel_param();
  1014. fprintf(pp, " 0=%d", shuffle_channel_param.group());
  1015. }
  1016. else if (layer.type() == "Slice")
  1017. {
  1018. const caffe::SliceParameter& slice_param = layer.slice_param();
  1019. if (slice_param.slice_point_size() == 0)
  1020. {
  1021. int num_slice = layer.top_size();
  1022. fprintf(pp, " -23300=%d", num_slice);
  1023. for (int j=0; j<num_slice; j++)
  1024. {
  1025. fprintf(pp, ",-233");
  1026. }
  1027. }
  1028. else
  1029. {
  1030. int num_slice = slice_param.slice_point_size() + 1;
  1031. fprintf(pp, " -23300=%d", num_slice);
  1032. int prev_offset = 0;
  1033. for (int j=0; j<slice_param.slice_point_size(); j++)
  1034. {
  1035. int offset = slice_param.slice_point(j);
  1036. fprintf(pp, ",%d", offset - prev_offset);
  1037. prev_offset = offset;
  1038. }
  1039. fprintf(pp, ",-233");
  1040. }
  1041. int dim = slice_param.axis() - 1;
  1042. fprintf(pp, " 1=%d", dim);
  1043. }
  1044. else if (layer.type() == "Softmax")
  1045. {
  1046. const caffe::SoftmaxParameter& softmax_param = layer.softmax_param();
  1047. int dim = softmax_param.axis() - 1;
  1048. fprintf(pp, " 0=%d", dim);
  1049. }
  1050. else if (layer.type() == "Threshold")
  1051. {
  1052. const caffe::ThresholdParameter& threshold_param = layer.threshold_param();
  1053. fprintf(pp, " 0=%f", threshold_param.threshold());
  1054. }
  1055. fprintf(pp, "\n");
  1056. // add split layer if top reference larger than one
  1057. if (layer.bottom_size() == 1 && layer.top_size() == 1 && layer.bottom(0) == layer.top(0))
  1058. {
  1059. std::string blob_name = blob_name_decorated[layer.top(0)];
  1060. if (bottom_reference.find(blob_name) != bottom_reference.end())
  1061. {
  1062. int refcount = bottom_reference[blob_name];
  1063. if (refcount > 1)
  1064. {
  1065. char splitname[256];
  1066. sprintf(splitname, "splitncnn_%d", internal_split);
  1067. fprintf(pp, "%-16s %-16s %d %d", "Split", splitname, 1, refcount);
  1068. fprintf(pp, " %s", blob_name.c_str());
  1069. for (int j=0; j<refcount; j++)
  1070. {
  1071. fprintf(pp, " %s_splitncnn_%d", blob_name.c_str(), j);
  1072. }
  1073. fprintf(pp, "\n");
  1074. internal_split++;
  1075. }
  1076. }
  1077. }
  1078. else
  1079. {
  1080. for (int j=0; j<layer.top_size(); j++)
  1081. {
  1082. std::string blob_name = layer.top(j);
  1083. if (bottom_reference.find(blob_name) != bottom_reference.end())
  1084. {
  1085. int refcount = bottom_reference[blob_name];
  1086. if (refcount > 1)
  1087. {
  1088. char splitname[256];
  1089. sprintf(splitname, "splitncnn_%d", internal_split);
  1090. fprintf(pp, "%-16s %-16s %d %d", "Split", splitname, 1, refcount);
  1091. fprintf(pp, " %s", blob_name.c_str());
  1092. for (int j=0; j<refcount; j++)
  1093. {
  1094. fprintf(pp, " %s_splitncnn_%d", blob_name.c_str(), j);
  1095. }
  1096. fprintf(pp, "\n");
  1097. internal_split++;
  1098. }
  1099. }
  1100. }
  1101. }
  1102. }
  1103. fclose(pp);
  1104. fclose(bp);
  1105. return 0;
  1106. }