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

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