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