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

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