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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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. }
  585. else if (bs.dim_size() == 2)
  586. {
  587. fprintf(pp, " 0=%zd", size_t(bs.dim(1)));
  588. }
  589. }
  590. else if (layer.type() == "Interp")
  591. {
  592. const caffe::InterpParameter& interp_param = layer.interp_param();
  593. fprintf(pp, " 0=%d", 2);
  594. fprintf(pp, " 1=%e", (float)interp_param.zoom_factor());
  595. fprintf(pp, " 2=%e", (float)interp_param.zoom_factor());
  596. fprintf(pp, " 3=%d", interp_param.height());
  597. fprintf(pp, " 4=%d", interp_param.width());
  598. }
  599. else if (layer.type() == "LRN")
  600. {
  601. const caffe::LRNParameter& lrn_param = layer.lrn_param();
  602. fprintf(pp, " 0=%d", lrn_param.norm_region());
  603. fprintf(pp, " 1=%d", lrn_param.local_size());
  604. fprintf(pp, " 2=%e", lrn_param.alpha());
  605. fprintf(pp, " 3=%e", lrn_param.beta());
  606. }
  607. else if (layer.type() == "LSTM")
  608. {
  609. const caffe::LayerParameter& binlayer = net.layer(netidx);
  610. const caffe::BlobProto& weight_blob = binlayer.blobs(0);
  611. const caffe::RecurrentParameter& recurrent_param = layer.recurrent_param();
  612. fprintf(pp, " 0=%d", recurrent_param.num_output());
  613. fprintf(pp, " 1=%d", weight_blob.data_size());
  614. for (int j = 0; j < binlayer.blobs_size(); j++)
  615. {
  616. int quantize_tag = 0;
  617. const caffe::BlobProto& blob = binlayer.blobs(j);
  618. // write quantize tag first
  619. fwrite(&quantize_tag, sizeof(int), 1, bp);
  620. // write original data
  621. fwrite(blob.data().data(), sizeof(float), blob.data_size(), bp);
  622. }
  623. }
  624. else if (layer.type() == "MemoryData")
  625. {
  626. const caffe::MemoryDataParameter& memory_data_param = layer.memory_data_param();
  627. fprintf(pp, " 0=%d", memory_data_param.width());
  628. fprintf(pp, " 1=%d", memory_data_param.height());
  629. fprintf(pp, " 2=%d", memory_data_param.channels());
  630. }
  631. else if (layer.type() == "MVN")
  632. {
  633. const caffe::MVNParameter& mvn_param = layer.mvn_param();
  634. fprintf(pp, " 0=%d", mvn_param.normalize_variance());
  635. fprintf(pp, " 1=%d", mvn_param.across_channels());
  636. fprintf(pp, " 2=%e", mvn_param.eps());
  637. }
  638. else if (layer.type() == "Normalize")
  639. {
  640. const caffe::LayerParameter& binlayer = net.layer(netidx);
  641. const caffe::BlobProto& scale_blob = binlayer.blobs(0);
  642. const caffe::NormalizeParameter& norm_param = layer.norm_param();
  643. fprintf(pp, " 0=%d", norm_param.across_spatial());
  644. fprintf(pp, " 1=%d", norm_param.channel_shared());
  645. fprintf(pp, " 2=%e", norm_param.eps());
  646. fprintf(pp, " 3=%d", scale_blob.data_size());
  647. fwrite(scale_blob.data().data(), sizeof(float), scale_blob.data_size(), bp);
  648. }
  649. else if (layer.type() == "Permute")
  650. {
  651. const caffe::PermuteParameter& permute_param = layer.permute_param();
  652. int order_size = permute_param.order_size();
  653. int order_type = 0;
  654. if (order_size == 0)
  655. order_type = 0;
  656. if (order_size == 1)
  657. {
  658. int order0 = permute_param.order(0);
  659. if (order0 == 0)
  660. order_type = 0;
  661. // permute with N not supported
  662. }
  663. if (order_size == 2)
  664. {
  665. int order0 = permute_param.order(0);
  666. int order1 = permute_param.order(1);
  667. if (order0 == 0)
  668. {
  669. if (order1 == 1) // 0 1 2 3
  670. order_type = 0;
  671. else if (order1 == 2) // 0 2 1 3
  672. order_type = 2;
  673. else if (order1 == 3) // 0 3 1 2
  674. order_type = 4;
  675. }
  676. // permute with N not supported
  677. }
  678. if (order_size == 3 || order_size == 4)
  679. {
  680. int order0 = permute_param.order(0);
  681. int order1 = permute_param.order(1);
  682. int order2 = permute_param.order(2);
  683. if (order0 == 0)
  684. {
  685. if (order1 == 1)
  686. {
  687. if (order2 == 2) // 0 1 2 3
  688. order_type = 0;
  689. if (order2 == 3) // 0 1 3 2
  690. order_type = 1;
  691. }
  692. else if (order1 == 2)
  693. {
  694. if (order2 == 1) // 0 2 1 3
  695. order_type = 2;
  696. if (order2 == 3) // 0 2 3 1
  697. order_type = 3;
  698. }
  699. else if (order1 == 3)
  700. {
  701. if (order2 == 1) // 0 3 1 2
  702. order_type = 4;
  703. if (order2 == 2) // 0 3 2 1
  704. order_type = 5;
  705. }
  706. }
  707. // permute with N not supported
  708. }
  709. fprintf(pp, " 0=%d", order_type);
  710. }
  711. else if (layer.type() == "Pooling")
  712. {
  713. const caffe::PoolingParameter& pooling_param = layer.pooling_param();
  714. fprintf(pp, " 0=%d", pooling_param.pool());
  715. if (pooling_param.has_kernel_w() && pooling_param.has_kernel_h())
  716. {
  717. fprintf(pp, " 1=%d", pooling_param.kernel_w());
  718. fprintf(pp, " 11=%d", pooling_param.kernel_h());
  719. }
  720. else
  721. {
  722. fprintf(pp, " 1=%d", pooling_param.kernel_size());
  723. }
  724. if (pooling_param.has_stride_w() && pooling_param.has_stride_h())
  725. {
  726. fprintf(pp, " 2=%d", pooling_param.stride_w());
  727. fprintf(pp, " 12=%d", pooling_param.stride_h());
  728. }
  729. else
  730. {
  731. fprintf(pp, " 2=%d", pooling_param.stride());
  732. }
  733. if (pooling_param.has_pad_w() && pooling_param.has_pad_h())
  734. {
  735. fprintf(pp, " 3=%d", pooling_param.pad_w());
  736. fprintf(pp, " 13=%d", pooling_param.pad_h());
  737. }
  738. else
  739. {
  740. fprintf(pp, " 3=%d", pooling_param.pad());
  741. }
  742. fprintf(pp, " 4=%d", pooling_param.has_global_pooling() ? pooling_param.global_pooling() : 0);
  743. }
  744. else if (layer.type() == "Power")
  745. {
  746. const caffe::PowerParameter& power_param = layer.power_param();
  747. fprintf(pp, " 0=%e", power_param.power());
  748. fprintf(pp, " 1=%e", power_param.scale());
  749. fprintf(pp, " 2=%e", power_param.shift());
  750. }
  751. else if (layer.type() == "PReLU")
  752. {
  753. const caffe::LayerParameter& binlayer = net.layer(netidx);
  754. const caffe::BlobProto& slope_blob = binlayer.blobs(0);
  755. fprintf(pp, " 0=%d", slope_blob.data_size());
  756. fwrite(slope_blob.data().data(), sizeof(float), slope_blob.data_size(), bp);
  757. }
  758. else if (layer.type() == "PriorBox")
  759. {
  760. const caffe::PriorBoxParameter& prior_box_param = layer.prior_box_param();
  761. int num_aspect_ratio = prior_box_param.aspect_ratio_size();
  762. for (int j = 0; j < prior_box_param.aspect_ratio_size(); j++)
  763. {
  764. float ar = prior_box_param.aspect_ratio(j);
  765. if (fabs(ar - 1.) < 1e-6)
  766. {
  767. num_aspect_ratio--;
  768. }
  769. }
  770. float variances[4] = {0.1f, 0.1f, 0.1f, 0.1f};
  771. if (prior_box_param.variance_size() == 4)
  772. {
  773. variances[0] = prior_box_param.variance(0);
  774. variances[1] = prior_box_param.variance(1);
  775. variances[2] = prior_box_param.variance(2);
  776. variances[3] = prior_box_param.variance(3);
  777. }
  778. else if (prior_box_param.variance_size() == 1)
  779. {
  780. variances[0] = prior_box_param.variance(0);
  781. variances[1] = prior_box_param.variance(0);
  782. variances[2] = prior_box_param.variance(0);
  783. variances[3] = prior_box_param.variance(0);
  784. }
  785. int flip = prior_box_param.has_flip() ? prior_box_param.flip() : 1;
  786. int clip = prior_box_param.has_clip() ? prior_box_param.clip() : 0;
  787. int image_width = -233;
  788. int image_height = -233;
  789. if (prior_box_param.has_img_size())
  790. {
  791. image_width = prior_box_param.img_size();
  792. image_height = prior_box_param.img_size();
  793. }
  794. else if (prior_box_param.has_img_w() && prior_box_param.has_img_h())
  795. {
  796. image_width = prior_box_param.img_w();
  797. image_height = prior_box_param.img_h();
  798. }
  799. float step_width = -233;
  800. float step_height = -233;
  801. if (prior_box_param.has_step())
  802. {
  803. step_width = prior_box_param.step();
  804. step_height = prior_box_param.step();
  805. }
  806. else if (prior_box_param.has_step_w() && prior_box_param.has_step_h())
  807. {
  808. step_width = prior_box_param.step_w();
  809. step_height = prior_box_param.step_h();
  810. }
  811. fprintf(pp, " -23300=%d", prior_box_param.min_size_size());
  812. for (int j = 0; j < prior_box_param.min_size_size(); j++)
  813. {
  814. fprintf(pp, ",%e", prior_box_param.min_size(j));
  815. }
  816. fprintf(pp, " -23301=%d", prior_box_param.max_size_size());
  817. for (int j = 0; j < prior_box_param.max_size_size(); j++)
  818. {
  819. fprintf(pp, ",%e", prior_box_param.max_size(j));
  820. }
  821. fprintf(pp, " -23302=%d", num_aspect_ratio);
  822. for (int j = 0; j < prior_box_param.aspect_ratio_size(); j++)
  823. {
  824. float ar = prior_box_param.aspect_ratio(j);
  825. if (fabs(ar - 1.) < 1e-6)
  826. {
  827. continue;
  828. }
  829. fprintf(pp, ",%e", ar);
  830. }
  831. fprintf(pp, " 3=%e", variances[0]);
  832. fprintf(pp, " 4=%e", variances[1]);
  833. fprintf(pp, " 5=%e", variances[2]);
  834. fprintf(pp, " 6=%e", variances[3]);
  835. fprintf(pp, " 7=%d", flip);
  836. fprintf(pp, " 8=%d", clip);
  837. fprintf(pp, " 9=%d", image_width);
  838. fprintf(pp, " 10=%d", image_height);
  839. fprintf(pp, " 11=%e", step_width);
  840. fprintf(pp, " 12=%e", step_height);
  841. fprintf(pp, " 13=%e", prior_box_param.offset());
  842. }
  843. else if (layer.type() == "PSROIPooling")
  844. {
  845. const caffe::PSROIPoolingParameter& psroi_pooling_param = layer.psroi_pooling_param();
  846. fprintf(pp, " 0=%d", psroi_pooling_param.group_size());
  847. fprintf(pp, " 1=%d", psroi_pooling_param.group_size());
  848. fprintf(pp, " 2=%e", psroi_pooling_param.spatial_scale());
  849. fprintf(pp, " 3=%d", psroi_pooling_param.output_dim());
  850. }
  851. else if (layer.type() == "Python")
  852. {
  853. const caffe::PythonParameter& python_param = layer.python_param();
  854. std::string python_layer_name = python_param.layer();
  855. if (python_layer_name == "ProposalLayer")
  856. {
  857. int feat_stride = 16;
  858. sscanf(python_param.param_str().c_str(), "'feat_stride': %d", &feat_stride);
  859. int base_size = 16;
  860. // float ratio;
  861. // float scale;
  862. int pre_nms_topN = 6000;
  863. int after_nms_topN = 300;
  864. float nms_thresh = 0.7f;
  865. int min_size = 16;
  866. fprintf(pp, " 0=%d", feat_stride);
  867. fprintf(pp, " 1=%d", base_size);
  868. fprintf(pp, " 2=%d", pre_nms_topN);
  869. fprintf(pp, " 3=%d", after_nms_topN);
  870. fprintf(pp, " 4=%e", nms_thresh);
  871. fprintf(pp, " 5=%d", min_size);
  872. }
  873. }
  874. else if (layer.type() == "ReLU")
  875. {
  876. const caffe::ReLUParameter& relu_param = layer.relu_param();
  877. if (relu_param.has_negative_slope())
  878. {
  879. fprintf(pp, " 0=%e", relu_param.negative_slope());
  880. }
  881. }
  882. else if (layer.type() == "ReLU6")
  883. {
  884. float min = 0.f;
  885. float max = 6.f;
  886. fprintf(pp, " 0=%e", min);
  887. fprintf(pp, " 1=%e", max);
  888. }
  889. else if (layer.type() == "Reorg")
  890. {
  891. const caffe::ReorgParameter& reorg_param = layer.reorg_param();
  892. fprintf(pp, " 0=%d", reorg_param.stride());
  893. }
  894. else if (layer.type() == "Reshape")
  895. {
  896. const caffe::ReshapeParameter& reshape_param = layer.reshape_param();
  897. const caffe::BlobShape& bs = reshape_param.shape();
  898. if (bs.dim_size() == 1)
  899. {
  900. fprintf(pp, " 0=%zd 1=-233 2=-233", size_t(bs.dim(0)));
  901. }
  902. else if (bs.dim_size() == 2)
  903. {
  904. fprintf(pp, " 0=%zd 1=-233 2=-233", size_t(bs.dim(1)));
  905. }
  906. else if (bs.dim_size() == 3)
  907. {
  908. fprintf(pp, " 0=%zd 1=%zd 2=-233", size_t(bs.dim(2)), size_t(bs.dim(1)));
  909. }
  910. else // bs.dim_size() == 4
  911. {
  912. fprintf(pp, " 0=%zd 1=%zd 2=%zd", size_t(bs.dim(3)), size_t(bs.dim(2)), size_t(bs.dim(1)));
  913. }
  914. fprintf(pp, " 3=0"); // permute
  915. }
  916. else if (layer.type() == "ROIAlign")
  917. {
  918. const caffe::ROIAlignParameter& roi_align_param = layer.roi_align_param();
  919. fprintf(pp, " 0=%d", roi_align_param.pooled_w());
  920. fprintf(pp, " 1=%d", roi_align_param.pooled_h());
  921. fprintf(pp, " 2=%e", roi_align_param.spatial_scale());
  922. fprintf(pp, " 3=%d", 0);
  923. fprintf(pp, " 4=%d", false);
  924. fprintf(pp, " 5=%d", 0);
  925. }
  926. else if (layer.type() == "ROIPooling")
  927. {
  928. const caffe::ROIPoolingParameter& roi_pooling_param = layer.roi_pooling_param();
  929. fprintf(pp, " 0=%d", roi_pooling_param.pooled_w());
  930. fprintf(pp, " 1=%d", roi_pooling_param.pooled_h());
  931. fprintf(pp, " 2=%e", roi_pooling_param.spatial_scale());
  932. }
  933. else if (layer.type() == "Scale")
  934. {
  935. const caffe::LayerParameter& binlayer = net.layer(netidx);
  936. const caffe::ScaleParameter& scale_param = layer.scale_param();
  937. bool scale_weight = scale_param.bias_term() ? (binlayer.blobs_size() == 2) : (binlayer.blobs_size() == 1);
  938. if (scale_weight)
  939. {
  940. const caffe::BlobProto& weight_blob = binlayer.blobs(0);
  941. fprintf(pp, " 0=%d", int(weight_blob.data_size()));
  942. }
  943. else
  944. {
  945. fprintf(pp, " 0=-233");
  946. }
  947. fprintf(pp, " 1=%d", scale_param.bias_term());
  948. for (int j = 0; j < binlayer.blobs_size(); j++)
  949. {
  950. const caffe::BlobProto& blob = binlayer.blobs(j);
  951. fwrite(blob.data().data(), sizeof(float), blob.data_size(), bp);
  952. }
  953. }
  954. else if (layer.type() == "ShuffleChannel")
  955. {
  956. const caffe::ShuffleChannelParameter& shuffle_channel_param = layer.shuffle_channel_param();
  957. fprintf(pp, " 0=%d", shuffle_channel_param.group());
  958. }
  959. else if (layer.type() == "Slice")
  960. {
  961. const caffe::SliceParameter& slice_param = layer.slice_param();
  962. if (slice_param.slice_point_size() == 0)
  963. {
  964. int num_slice = layer.top_size();
  965. fprintf(pp, " -23300=%d", num_slice);
  966. for (int j = 0; j < num_slice; j++)
  967. {
  968. fprintf(pp, ",-233");
  969. }
  970. }
  971. else
  972. {
  973. int num_slice = slice_param.slice_point_size() + 1;
  974. fprintf(pp, " -23300=%d", num_slice);
  975. int prev_offset = 0;
  976. for (int j = 0; j < slice_param.slice_point_size(); j++)
  977. {
  978. int offset = slice_param.slice_point(j);
  979. fprintf(pp, ",%d", offset - prev_offset);
  980. prev_offset = offset;
  981. }
  982. fprintf(pp, ",-233");
  983. }
  984. int axis = 0;
  985. if (slice_param.has_axis())
  986. {
  987. axis = slice_param.axis() - 1;
  988. }
  989. else if (slice_param.has_slice_dim())
  990. {
  991. axis = slice_param.slice_dim() - 1;
  992. }
  993. fprintf(pp, " 1=%d", axis);
  994. }
  995. else if (layer.type() == "Softmax")
  996. {
  997. const caffe::SoftmaxParameter& softmax_param = layer.softmax_param();
  998. int dim = softmax_param.axis() - 1;
  999. fprintf(pp, " 0=%d", dim);
  1000. fprintf(pp, " 1=1");
  1001. }
  1002. else if (layer.type() == "Threshold")
  1003. {
  1004. const caffe::ThresholdParameter& threshold_param = layer.threshold_param();
  1005. fprintf(pp, " 0=%e", threshold_param.threshold());
  1006. }
  1007. else if (layer.type() == "YoloDetectionOutput")
  1008. {
  1009. const caffe::YoloDetectionOutputParameter& yolo_detection_output_param = layer.yolo_detection_output_param();
  1010. fprintf(pp, " 0=%d", yolo_detection_output_param.num_classes());
  1011. fprintf(pp, " 1=%d", yolo_detection_output_param.num_box());
  1012. fprintf(pp, " 2=%e", yolo_detection_output_param.confidence_threshold());
  1013. fprintf(pp, " 3=%e", yolo_detection_output_param.nms_threshold());
  1014. int num_bias = yolo_detection_output_param.biases_size();
  1015. fprintf(pp, " -23304=%d", num_bias);
  1016. for (int j = 0; j < num_bias; j++)
  1017. {
  1018. fprintf(pp, ",%e", yolo_detection_output_param.biases(j));
  1019. }
  1020. }
  1021. else if (layer.type() == "Yolov3DetectionOutput")
  1022. {
  1023. const caffe::Yolov3DetectionOutputParameter& yolov3_detection_output_param = layer.yolov3_detection_output_param();
  1024. fprintf(pp, " 0=%d", yolov3_detection_output_param.num_classes());
  1025. fprintf(pp, " 1=%d", yolov3_detection_output_param.num_box());
  1026. fprintf(pp, " 2=%e", yolov3_detection_output_param.confidence_threshold());
  1027. fprintf(pp, " 3=%e", yolov3_detection_output_param.nms_threshold());
  1028. int num_bias = yolov3_detection_output_param.biases_size();
  1029. fprintf(pp, " -23304=%d", num_bias);
  1030. for (int j = 0; j < num_bias; j++)
  1031. {
  1032. fprintf(pp, ",%e", yolov3_detection_output_param.biases(j));
  1033. }
  1034. int num_mask = yolov3_detection_output_param.mask_size();
  1035. fprintf(pp, " -23305=%d", num_mask);
  1036. for (int j = 0; j < num_mask; j++)
  1037. {
  1038. fprintf(pp, ",%e", (float)yolov3_detection_output_param.mask(j));
  1039. }
  1040. int num_anchors = yolov3_detection_output_param.anchors_scale_size();
  1041. fprintf(pp, " -23306=%d", num_anchors);
  1042. for (int j = 0; j < num_anchors; j++)
  1043. {
  1044. fprintf(pp, ",%e", (float)yolov3_detection_output_param.anchors_scale(j));
  1045. }
  1046. fprintf(pp, " 7=%d", yolov3_detection_output_param.mask_group_num());
  1047. }
  1048. fprintf(pp, "\n");
  1049. // add split layer if top reference larger than one
  1050. if (layer.bottom_size() == 1 && layer.top_size() == 1 && layer.bottom(0) == layer.top(0))
  1051. {
  1052. std::string blob_name = blob_name_decorated[layer.top(0)];
  1053. if (bottom_reference.find(blob_name) != bottom_reference.end())
  1054. {
  1055. int refcount = bottom_reference[blob_name];
  1056. if (refcount > 1)
  1057. {
  1058. char splitname[256];
  1059. sprintf(splitname, "splitncnn_%d", internal_split);
  1060. fprintf(pp, "%-16s %-16s %d %d", "Split", splitname, 1, refcount);
  1061. fprintf(pp, " %s", blob_name.c_str());
  1062. for (int j = 0; j < refcount; j++)
  1063. {
  1064. fprintf(pp, " %s_splitncnn_%d", blob_name.c_str(), j);
  1065. }
  1066. fprintf(pp, "\n");
  1067. internal_split++;
  1068. }
  1069. }
  1070. }
  1071. else
  1072. {
  1073. for (int j = 0; j < layer.top_size(); j++)
  1074. {
  1075. std::string blob_name = layer.top(j);
  1076. if (bottom_reference.find(blob_name) != bottom_reference.end())
  1077. {
  1078. int refcount = bottom_reference[blob_name];
  1079. if (refcount > 1)
  1080. {
  1081. char splitname[256];
  1082. sprintf(splitname, "splitncnn_%d", internal_split);
  1083. fprintf(pp, "%-16s %-16s %d %d", "Split", splitname, 1, refcount);
  1084. fprintf(pp, " %s", blob_name.c_str());
  1085. for (int j = 0; j < refcount; j++)
  1086. {
  1087. fprintf(pp, " %s_splitncnn_%d", blob_name.c_str(), j);
  1088. }
  1089. fprintf(pp, "\n");
  1090. internal_split++;
  1091. }
  1092. }
  1093. }
  1094. }
  1095. }
  1096. fclose(pp);
  1097. fclose(bp);
  1098. return 0;
  1099. }