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.

ncnn2int8.cpp 44 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. // BUG1989 is pleased to support the open source community by supporting ncnn available.
  2. //
  3. // Copyright (C) 2019 BUG1989. 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 <cstdio>
  18. #include <cstring>
  19. #include <vector>
  20. #include <set>
  21. #include <map>
  22. // ncnn public header
  23. #include "net.h"
  24. #include "layer.h"
  25. #include "layer_type.h"
  26. // ncnn private header
  27. #include "layer/batchnorm.h"
  28. #include "layer/bias.h"
  29. #include "layer/binaryop.h"
  30. #include "layer/clip.h"
  31. #include "layer/concat.h"
  32. #include "layer/convolution.h"
  33. #include "layer/convolutiondepthwise.h"
  34. #include "layer/crop.h"
  35. #include "layer/deconvolution.h"
  36. #include "layer/deconvolutiondepthwise.h"
  37. #include "layer/detectionoutput.h"
  38. #include "layer/dropout.h"
  39. #include "layer/eltwise.h"
  40. #include "layer/elu.h"
  41. #include "layer/exp.h"
  42. #include "layer/flatten.h"
  43. #include "layer/innerproduct.h"
  44. #include "layer/input.h"
  45. #include "layer/instancenorm.h"
  46. #include "layer/interp.h"
  47. #include "layer/log.h"
  48. #include "layer/lrn.h"
  49. #include "layer/lstm.h"
  50. #include "layer/memorydata.h"
  51. #include "layer/mvn.h"
  52. #include "layer/normalize.h"
  53. #include "layer/padding.h"
  54. #include "layer/permute.h"
  55. #include "layer/pixelshuffle.h"
  56. #include "layer/pooling.h"
  57. #include "layer/power.h"
  58. #include "layer/prelu.h"
  59. #include "layer/priorbox.h"
  60. #include "layer/proposal.h"
  61. #include "layer/psroipooling.h"
  62. #include "layer/quantize.h"
  63. #include "layer/reduction.h"
  64. #include "layer/relu.h"
  65. #include "layer/reorg.h"
  66. #include "layer/requantize.h"
  67. #include "layer/reshape.h"
  68. #include "layer/roialign.h"
  69. #include "layer/roipooling.h"
  70. #include "layer/scale.h"
  71. #include "layer/slice.h"
  72. #include "layer/shufflechannel.h"
  73. #include "layer/softmax.h"
  74. #include "layer/threshold.h"
  75. #include "layer/unaryop.h"
  76. #include "layer/yolodetectionoutput.h"
  77. #include "layer/yolov3detectionoutput.h"
  78. static bool read_int8scale_table(const char* filepath, std::map<std::string, std::vector<float> >& blob_int8scale_table, std::map<std::string, std::vector<float> >& weight_int8scale_table)
  79. {
  80. blob_int8scale_table.clear();
  81. weight_int8scale_table.clear();
  82. FILE* fp = fopen(filepath, "rb");
  83. if (!fp)
  84. {
  85. fprintf(stderr, "Open %s failed.\n", filepath);
  86. return false;
  87. }
  88. std::string key_str;
  89. std::vector<float> scales;
  90. std::vector<char>line(102400);
  91. char *pch = NULL;
  92. size_t len = 0;
  93. while (NULL != std::fgets(line.data(), static_cast<int>(line.size()), fp))
  94. {
  95. float scale = 1.f;
  96. char key[256];
  97. line[strcspn(line.data(), "\r\n")] = 0;
  98. pch = strtok(line.data(), " ");
  99. if (pch == NULL) break;
  100. bool is_key = true;
  101. while (pch != NULL)
  102. {
  103. if (is_key)
  104. {
  105. sscanf(pch, "%255s", key);
  106. key_str = key;
  107. is_key = false;
  108. }
  109. else
  110. {
  111. sscanf(pch, "%f", &scale);
  112. scales.push_back(scale);
  113. }
  114. pch = strtok(NULL, " ");
  115. }
  116. // XYZ_param_N pattern
  117. if (strstr(key_str.c_str(), "_param_"))
  118. {
  119. weight_int8scale_table[key_str] = scales;
  120. }
  121. else
  122. {
  123. blob_int8scale_table[key_str] = scales;
  124. }
  125. key_str.clear();
  126. scales.clear();
  127. }
  128. fclose(fp);
  129. return true;
  130. }
  131. class NetQuantize : public ncnn::Net
  132. {
  133. public:
  134. // 0=fp32 1=fp16 2=int8
  135. int storage_type;
  136. std::map<std::string, std::vector<float> > blob_int8scale_table;
  137. std::map<std::string, std::vector<float> > weight_int8scale_table;
  138. public:
  139. int quantize_convolution();
  140. int quantize_convolutiondepthwise();
  141. int quantize_innerproduct();
  142. public:
  143. int fprintf_param_int_array(int id, const ncnn::Mat& m, FILE* pp);
  144. int fprintf_param_float_array(int id, const ncnn::Mat& m, FILE* pp);
  145. int fwrite_weight_tag_data(int tag, const ncnn::Mat& data, FILE* bp);
  146. int fwrite_weight_data(const ncnn::Mat& data, FILE* bp);
  147. int save(const char* parampath, const char* binpath);
  148. };
  149. int NetQuantize::quantize_convolution()
  150. {
  151. const int layer_count = static_cast<int>(layers.size());
  152. for (int i = 0; i < layer_count; i++)
  153. {
  154. // find convoultion layer
  155. if (layers[i]->type != "Convolution")
  156. continue;
  157. // find convolution layer
  158. std::map<std::string, std::vector<float> >::iterator iter_data = blob_int8scale_table.find(layers[i]->name);
  159. if (iter_data == blob_int8scale_table.end())
  160. continue;
  161. char key[256];
  162. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  163. std::map<std::string, std::vector<float> >::iterator iter = weight_int8scale_table.find(key);
  164. if (iter == weight_int8scale_table.end())
  165. {
  166. fprintf(stderr, "this layer need to be quantized, but no scale param!\n");
  167. return -1;
  168. }
  169. // Convolution - quantize weight from fp32 to int8
  170. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[i];
  171. std::vector<float> weight_data_int8_scales = iter->second;
  172. fprintf(stderr, "quantize_convolution %s\n", convolution->name.c_str());
  173. {
  174. ncnn::Mat int8_weight_data(convolution->weight_data_size, (size_t)1u);
  175. if (int8_weight_data.empty())
  176. return -100;
  177. const int weight_data_size_output = convolution->weight_data_size / convolution->num_output;
  178. // quantize weight to int8
  179. for (int n = 0; n < convolution->num_output; n++)
  180. {
  181. ncnn::Layer* op = ncnn::create_layer(ncnn::LayerType::Quantize);
  182. ncnn::ParamDict pd;
  183. pd.set(0, weight_data_int8_scales[n]);// scale
  184. op->load_param(pd);
  185. ncnn::Option opt;
  186. opt.blob_allocator = int8_weight_data.allocator;
  187. const ncnn::Mat weight_data_n = convolution->weight_data.range(weight_data_size_output * n, weight_data_size_output);
  188. ncnn::Mat int8_weight_data_n = int8_weight_data.range(weight_data_size_output * n, weight_data_size_output);
  189. op->forward(weight_data_n, int8_weight_data_n, opt);
  190. delete op;
  191. }
  192. convolution->weight_data = int8_weight_data;
  193. }
  194. convolution->int8_scale_term = 2;
  195. }
  196. return 0;
  197. }
  198. int NetQuantize::quantize_convolutiondepthwise()
  199. {
  200. const int layer_count = static_cast<int>(layers.size());
  201. for (int i = 0; i < layer_count; i++)
  202. {
  203. // find convoultion layer
  204. if (layers[i]->type != "ConvolutionDepthWise")
  205. continue;
  206. // find convolutiondepthwise layer
  207. std::map<std::string, std::vector<float> >::iterator iter_data = blob_int8scale_table.find(layers[i]->name);
  208. if (iter_data == blob_int8scale_table.end())
  209. continue;
  210. char key[256];
  211. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  212. std::map<std::string, std::vector<float> >::iterator iter = weight_int8scale_table.find(key);
  213. if (iter == weight_int8scale_table.end())
  214. {
  215. fprintf(stderr, "this layer need to be quantized, but no scale param!\n");
  216. return -1;
  217. }
  218. // Convolution - quantize weight from fp32 to int8
  219. ncnn::ConvolutionDepthWise* convdw = (ncnn::ConvolutionDepthWise*)layers[i];
  220. std::vector<float> weight_data_int8_scales = iter->second;
  221. fprintf(stderr, "quantize_convolution %s\n", convdw->name.c_str());
  222. {
  223. ncnn::Mat int8_weight_data(convdw->weight_data_size, (size_t)1u);
  224. if (int8_weight_data.empty())
  225. return -100;
  226. const int weight_data_size_output = convdw->weight_data_size / convdw->group;
  227. // quantize weight to int8
  228. for (int n = 0; n < convdw->group; n++)
  229. {
  230. ncnn::Layer* op = ncnn::create_layer(ncnn::LayerType::Quantize);
  231. ncnn::ParamDict pd;
  232. pd.set(0, weight_data_int8_scales[n]);// scale
  233. op->load_param(pd);
  234. ncnn::Option opt;
  235. opt.blob_allocator = int8_weight_data.allocator;
  236. const ncnn::Mat weight_data_n = convdw->weight_data.range(weight_data_size_output * n, weight_data_size_output);
  237. ncnn::Mat int8_weight_data_n = int8_weight_data.range(weight_data_size_output * n, weight_data_size_output);
  238. op->forward(weight_data_n, int8_weight_data_n, opt);
  239. delete op;
  240. }
  241. convdw->weight_data = int8_weight_data;
  242. }
  243. convdw->int8_scale_term = 1;
  244. }
  245. return 0;
  246. }
  247. int NetQuantize::quantize_innerproduct()
  248. {
  249. const int layer_count = static_cast<int>(layers.size());
  250. for (int i = 0; i < layer_count; i++)
  251. {
  252. // find convoultion layer
  253. if (layers[i]->type != "InnerProduct")
  254. continue;
  255. // find InnerProduct layer
  256. std::map<std::string, std::vector<float> >::iterator iter_data = blob_int8scale_table.find(layers[i]->name);
  257. if (iter_data == blob_int8scale_table.end())
  258. continue;
  259. char key[256];
  260. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  261. std::map<std::string, std::vector<float> >::iterator iter = weight_int8scale_table.find(key);
  262. if (iter == weight_int8scale_table.end())
  263. {
  264. fprintf(stderr, "this layer need to be quantized, but no scale param!\n");
  265. return -1;
  266. }
  267. // InnerProduct - quantize weight from fp32 to int8
  268. ncnn::InnerProduct* fc = (ncnn::InnerProduct*)layers[i];
  269. std::vector<float> weight_data_int8_scales = iter->second;
  270. fprintf(stderr, "quantize_convolution %s\n", fc->name.c_str());
  271. {
  272. ncnn::Mat int8_weight_data(fc->weight_data_size, (size_t)1u);
  273. if (int8_weight_data.empty())
  274. return -100;
  275. const int weight_data_size_output = fc->weight_data_size / fc->num_output;
  276. // quantize weight to int8
  277. for (int n = 0; n < fc->num_output; n++)
  278. {
  279. ncnn::Layer* op = ncnn::create_layer(ncnn::LayerType::Quantize);
  280. ncnn::ParamDict pd;
  281. pd.set(0, weight_data_int8_scales[n]);// scale
  282. op->load_param(pd);
  283. ncnn::Option opt;
  284. opt.blob_allocator = int8_weight_data.allocator;
  285. const ncnn::Mat weight_data_n = fc->weight_data.range(weight_data_size_output * n, weight_data_size_output);
  286. ncnn::Mat int8_weight_data_n = int8_weight_data.range(weight_data_size_output * n, weight_data_size_output);
  287. op->forward(weight_data_n, int8_weight_data_n, opt);
  288. delete op;
  289. }
  290. fc->weight_data = int8_weight_data;
  291. }
  292. fc->int8_scale_term = 2;
  293. }
  294. return 0;
  295. }
  296. int NetQuantize::fprintf_param_int_array(int id, const ncnn::Mat& m, FILE* pp)
  297. {
  298. const int count = m.w;
  299. const int* ptr = m;
  300. fprintf(pp, " -%d=%d", 23300 + id, count);
  301. for (int i = 0; i < count; i++)
  302. {
  303. fprintf(pp, ",%d", ptr[i]);
  304. }
  305. return 0;
  306. }
  307. int NetQuantize::fprintf_param_float_array(int id, const ncnn::Mat& m, FILE* pp)
  308. {
  309. const int count = m.w;
  310. const float* ptr = m;
  311. fprintf(pp, " -%d=%d", 23300 + id, count);
  312. for (int i = 0; i < count; i++)
  313. {
  314. fprintf(pp, ",%f", ptr[i]);
  315. }
  316. return 0;
  317. }
  318. static inline size_t alignSize(size_t sz, int n)
  319. {
  320. return (sz + n - 1) & -n;
  321. }
  322. int NetQuantize::fwrite_weight_tag_data(int tag, const ncnn::Mat& data, FILE* bp)
  323. {
  324. int p0 = ftell(bp);
  325. ncnn::Mat data_flattened = data.reshape(data.w * data.h * data.c);
  326. if (data.elemsize == 1)
  327. tag = 0x000D4B38; // int8 magic
  328. fwrite(&tag, sizeof(int), 1, bp);
  329. fwrite(data_flattened.data, data_flattened.elemsize, data_flattened.w, bp);
  330. // padding to 32bit align
  331. int nwrite = ftell(bp) - p0;
  332. int nalign = static_cast<int>(alignSize(nwrite, 4));
  333. unsigned char padding[4] = { 0x00, 0x00, 0x00, 0x00 };
  334. fwrite(padding, sizeof(unsigned char), nalign - nwrite, bp);
  335. return 0;
  336. }
  337. int NetQuantize::fwrite_weight_data(const ncnn::Mat& data, FILE* bp)
  338. {
  339. int p0 = ftell(bp);
  340. ncnn::Mat data_flattened = data.reshape(data.w * data.h * data.c);
  341. fwrite(data_flattened.data, data_flattened.elemsize, data_flattened.w, bp);
  342. // padding to 32bit align
  343. int nwrite = ftell(bp) - p0;
  344. int nalign = static_cast<int>(alignSize(nwrite, 4));
  345. unsigned char padding[4] = { 0x00, 0x00, 0x00, 0x00 };
  346. fwrite(padding, sizeof(unsigned char), nalign - nwrite, bp);
  347. return 0;
  348. }
  349. int NetQuantize::save(const char* parampath, const char* binpath)
  350. {
  351. FILE* pp = fopen(parampath, "wb");
  352. FILE* bp = fopen(binpath, "wb");
  353. fprintf(pp, "7767517\n");
  354. const int layer_count = static_cast<int>(layers.size());
  355. int layer_count_fused = 0;
  356. std::set<std::string> blob_names;
  357. for (int i = 0; i < layer_count; i++)
  358. {
  359. const ncnn::Layer* layer = layers[i];
  360. if (layer->type == "ncnnfused")
  361. continue;
  362. layer_count_fused++;
  363. int bottom_count = static_cast<int>(layer->bottoms.size());
  364. for (int j = 0; j < bottom_count; j++)
  365. {
  366. int bottom_blob_index = layer->bottoms[j];
  367. blob_names.insert(blobs[bottom_blob_index].name);
  368. }
  369. int top_count = static_cast<int>(layer->tops.size());
  370. for (int j = 0; j < top_count; j++)
  371. {
  372. int top_blob_index = layer->tops[j];
  373. blob_names.insert(blobs[top_blob_index].name);
  374. }
  375. }
  376. int blob_count_fused = static_cast<int>(blob_names.size());
  377. fprintf(pp, "%d %d\n", layer_count_fused, blob_count_fused);
  378. for (int i = 0; i < layer_count; i++)
  379. {
  380. const ncnn::Layer* layer = layers[i];
  381. if (layer->type == "ncnnfused")
  382. continue;
  383. int bottom_count = static_cast<int>(layer->bottoms.size());
  384. int top_count = static_cast<int>(layer->tops.size());
  385. fprintf(pp, "%-24s %-24s %d %d", layer->type.c_str(), layer->name.c_str(), bottom_count, top_count);
  386. for (int j = 0; j < bottom_count; j++)
  387. {
  388. int bottom_blob_index = layer->bottoms[j];
  389. fprintf(pp, " %s", blobs[bottom_blob_index].name.c_str());
  390. }
  391. for (int j = 0; j < top_count; j++)
  392. {
  393. int top_blob_index = layer->tops[j];
  394. fprintf(pp, " %s", blobs[top_blob_index].name.c_str());
  395. }
  396. ncnn::Layer* layer_default = ncnn::create_layer(layer->typeindex);
  397. ncnn::ParamDict pd;
  398. layer_default->load_param(pd);
  399. #define fprintf_param_value(format, phase) \
  400. { if (op->phase != op_default->phase) fprintf(pp, format, op->phase); }
  401. if (layer->type == "BatchNorm")
  402. {
  403. ncnn::BatchNorm* op = (ncnn::BatchNorm*)layer;
  404. ncnn::BatchNorm* op_default = (ncnn::BatchNorm*)layer_default;
  405. fprintf_param_value(" 0=%d", channels)
  406. fprintf_param_value(" 1=%f", eps)
  407. fwrite_weight_data(op->slope_data, bp);
  408. fwrite_weight_data(op->mean_data, bp);
  409. fwrite_weight_data(op->var_data, bp);
  410. fwrite_weight_data(op->bias_data, bp);
  411. }
  412. else if (layer->type == "Bias")
  413. {
  414. ncnn::Bias* op = (ncnn::Bias*)layer;
  415. ncnn::Bias* op_default = (ncnn::Bias*)layer_default;
  416. fprintf_param_value(" 0=%d", bias_data_size)
  417. fwrite_weight_data(op->bias_data, bp);
  418. }
  419. else if (layer->type == "BinaryOp")
  420. {
  421. ncnn::BinaryOp* op = (ncnn::BinaryOp*)layer;
  422. ncnn::BinaryOp* op_default = (ncnn::BinaryOp*)layer_default;
  423. fprintf_param_value(" 0=%d", op_type)
  424. fprintf_param_value(" 1=%d", with_scalar)
  425. fprintf_param_value(" 2=%f", b)
  426. }
  427. else if (layer->type == "Clip")
  428. {
  429. ncnn::Clip* op = (ncnn::Clip*)layer;
  430. ncnn::Clip* op_default = (ncnn::Clip*)layer_default;
  431. fprintf_param_value(" 0=%f", min)
  432. fprintf_param_value(" 1=%f", max)
  433. }
  434. else if (layer->type == "Concat")
  435. {
  436. ncnn::Concat* op = (ncnn::Concat*)layer;
  437. ncnn::Concat* op_default = (ncnn::Concat*)layer_default;
  438. fprintf_param_value(" 0=%d", axis)
  439. }
  440. else if (layer->type == "Convolution")
  441. {
  442. ncnn::Convolution* op = (ncnn::Convolution*)layer;
  443. ncnn::Convolution* op_default = (ncnn::Convolution*)layer_default;
  444. fprintf_param_value(" 0=%d", num_output)
  445. fprintf_param_value(" 1=%d", kernel_w)
  446. { if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); }
  447. fprintf_param_value(" 2=%d", dilation_w)
  448. { if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); }
  449. fprintf_param_value(" 3=%d", stride_w)
  450. { if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); }
  451. fprintf_param_value(" 4=%d", pad_left)
  452. { if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); }
  453. { if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); }
  454. { if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); }
  455. fprintf_param_value(" 5=%d", bias_term)
  456. fprintf_param_value(" 6=%d", weight_data_size)
  457. fprintf_param_value(" 8=%d", int8_scale_term)
  458. fprintf_param_value(" 9=%d", activation_type)
  459. { if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); }
  460. fwrite_weight_tag_data(0, op->weight_data, bp);
  461. fwrite_weight_data(op->bias_data, bp);
  462. // write int8_scale data
  463. if (op->int8_scale_term)
  464. {
  465. std::vector<float> weight_int8scale;
  466. std::vector<float> blob_int8scale;
  467. char key[256];
  468. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  469. if (weight_int8scale_table.find(std::string(key)) != weight_int8scale_table.end())
  470. {
  471. weight_int8scale = weight_int8scale_table[std::string(key)];
  472. }
  473. if (blob_int8scale_table.find(layer->name) != blob_int8scale_table.end())
  474. {
  475. blob_int8scale = blob_int8scale_table[layer->name];
  476. }
  477. // write int8_scale data
  478. fwrite(weight_int8scale.data(), sizeof(float), weight_int8scale.size(), bp);
  479. fwrite(blob_int8scale.data(), sizeof(float), blob_int8scale.size(), bp);
  480. }
  481. }
  482. else if (layer->type == "ConvolutionDepthWise")
  483. {
  484. ncnn::ConvolutionDepthWise* op = (ncnn::ConvolutionDepthWise*)layer;
  485. ncnn::ConvolutionDepthWise* op_default = (ncnn::ConvolutionDepthWise*)layer_default;
  486. fprintf_param_value(" 0=%d", num_output)
  487. fprintf_param_value(" 1=%d", kernel_w)
  488. { if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); }
  489. fprintf_param_value(" 2=%d", dilation_w)
  490. { if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); }
  491. fprintf_param_value(" 3=%d", stride_w)
  492. { if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); }
  493. fprintf_param_value(" 4=%d", pad_left)
  494. { if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); }
  495. { if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); }
  496. { if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); }
  497. fprintf_param_value(" 5=%d", bias_term)
  498. fprintf_param_value(" 6=%d", weight_data_size)
  499. fprintf_param_value(" 7=%d", group)
  500. fprintf_param_value(" 8=%d", int8_scale_term)
  501. fprintf_param_value(" 9=%d", activation_type)
  502. { if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); }
  503. fwrite_weight_tag_data(0, op->weight_data, bp);
  504. fwrite_weight_data(op->bias_data, bp);
  505. // write int8_scale data
  506. if (op->int8_scale_term)
  507. {
  508. std::vector<float> weight_int8scale;
  509. std::vector<float> blob_int8scale;
  510. char key[256];
  511. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  512. if (weight_int8scale_table.find(std::string(key)) != weight_int8scale_table.end())
  513. {
  514. weight_int8scale = weight_int8scale_table[std::string(key)];
  515. }
  516. if (blob_int8scale_table.find(layer->name) != blob_int8scale_table.end())
  517. {
  518. blob_int8scale = blob_int8scale_table[layer->name];
  519. }
  520. // write int8_scale data
  521. fwrite(weight_int8scale.data(), sizeof(float), weight_int8scale.size(), bp);
  522. fwrite(blob_int8scale.data(), sizeof(float), blob_int8scale.size(), bp);
  523. }
  524. }
  525. else if (layer->type == "Crop")
  526. {
  527. ncnn::Crop* op = (ncnn::Crop*)layer;
  528. ncnn::Crop* op_default = (ncnn::Crop*)layer_default;
  529. fprintf_param_value(" 0=%d", woffset)
  530. fprintf_param_value(" 1=%d", hoffset)
  531. fprintf_param_value(" 2=%d", coffset)
  532. fprintf_param_value(" 3=%d", outw)
  533. fprintf_param_value(" 4=%d", outh)
  534. fprintf_param_value(" 5=%d", outc)
  535. fprintf_param_value(" 6=%d", woffset2)
  536. fprintf_param_value(" 7=%d", hoffset2)
  537. fprintf_param_value(" 8=%d", coffset2)
  538. { if (!op->starts.empty()) fprintf_param_int_array(9, op->starts, pp); }
  539. { if (!op->ends.empty()) fprintf_param_int_array(10, op->ends, pp); }
  540. { if (!op->axes.empty()) fprintf_param_int_array(11, op->axes, pp); }
  541. }
  542. else if (layer->type == "Deconvolution")
  543. {
  544. ncnn::Deconvolution* op = (ncnn::Deconvolution*)layer;
  545. ncnn::Deconvolution* op_default = (ncnn::Deconvolution*)layer_default;
  546. fprintf_param_value(" 0=%d", num_output)
  547. fprintf_param_value(" 1=%d", kernel_w)
  548. { if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); }
  549. fprintf_param_value(" 2=%d", dilation_w)
  550. { if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); }
  551. fprintf_param_value(" 3=%d", stride_w)
  552. { if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); }
  553. fprintf_param_value(" 4=%d", pad_left)
  554. { if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); }
  555. { if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); }
  556. { if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); }
  557. fprintf_param_value(" 5=%d", bias_term)
  558. fprintf_param_value(" 6=%d", weight_data_size)
  559. fprintf_param_value(" 9=%d", activation_type)
  560. { if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); }
  561. fwrite_weight_tag_data(0, op->weight_data, bp);
  562. fwrite_weight_data(op->bias_data, bp);
  563. }
  564. else if (layer->type == "DeconvolutionDepthWise")
  565. {
  566. ncnn::DeconvolutionDepthWise* op = (ncnn::DeconvolutionDepthWise*)layer;
  567. ncnn::DeconvolutionDepthWise* op_default = (ncnn::DeconvolutionDepthWise*)layer_default;
  568. fprintf_param_value(" 0=%d", num_output)
  569. fprintf_param_value(" 1=%d", kernel_w)
  570. { if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); }
  571. fprintf_param_value(" 2=%d", dilation_w)
  572. { if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); }
  573. fprintf_param_value(" 3=%d", stride_w)
  574. { if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); }
  575. fprintf_param_value(" 4=%d", pad_left)
  576. { if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); }
  577. { if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); }
  578. { if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); }
  579. fprintf_param_value(" 5=%d", bias_term)
  580. fprintf_param_value(" 6=%d", weight_data_size)
  581. fprintf_param_value(" 7=%d", group)
  582. fprintf_param_value(" 9=%d", activation_type)
  583. { if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); }
  584. fwrite_weight_tag_data(0, op->weight_data, bp);
  585. fwrite_weight_data(op->bias_data, bp);
  586. }
  587. else if (layer->type == "DetectionOutput")
  588. {
  589. ncnn::DetectionOutput* op = (ncnn::DetectionOutput*)layer;
  590. ncnn::DetectionOutput* op_default = (ncnn::DetectionOutput*)layer_default;
  591. fprintf_param_value(" 0=%d", num_class)
  592. fprintf_param_value(" 1=%f", nms_threshold)
  593. fprintf_param_value(" 2=%d", nms_top_k)
  594. fprintf_param_value(" 3=%d", keep_top_k)
  595. fprintf_param_value(" 4=%f", confidence_threshold)
  596. fprintf_param_value(" 5=%f", variances[0])
  597. fprintf_param_value(" 6=%f", variances[1])
  598. fprintf_param_value(" 7=%f", variances[2])
  599. fprintf_param_value(" 8=%f", variances[3])
  600. }
  601. else if (layer->type == "Dropout")
  602. {
  603. ncnn::Dropout* op = (ncnn::Dropout*)layer;
  604. ncnn::Dropout* op_default = (ncnn::Dropout*)layer_default;
  605. fprintf_param_value(" 0=%f", scale)
  606. }
  607. else if (layer->type == "Eltwise")
  608. {
  609. ncnn::Eltwise* op = (ncnn::Eltwise*)layer;
  610. ncnn::Eltwise* op_default = (ncnn::Eltwise*)layer_default;
  611. fprintf_param_value(" 0=%d", op_type)
  612. { if (!op->coeffs.empty()) fprintf_param_float_array(1, op->coeffs, pp); }
  613. }
  614. else if (layer->type == "ELU")
  615. {
  616. ncnn::ELU* op = (ncnn::ELU*)layer;
  617. ncnn::ELU* op_default = (ncnn::ELU*)layer_default;
  618. fprintf_param_value(" 0=%f", alpha)
  619. }
  620. else if (layer->type == "Exp")
  621. {
  622. ncnn::Exp* op = (ncnn::Exp*)layer;
  623. ncnn::Exp* op_default = (ncnn::Exp*)layer_default;
  624. fprintf_param_value(" 0=%f", base)
  625. fprintf_param_value(" 1=%f", scale)
  626. fprintf_param_value(" 2=%f", shift)
  627. }
  628. else if (layer->type == "InnerProduct")
  629. {
  630. ncnn::InnerProduct* op = (ncnn::InnerProduct*)layer;
  631. ncnn::InnerProduct* op_default = (ncnn::InnerProduct*)layer_default;
  632. fprintf_param_value(" 0=%d", num_output)
  633. fprintf_param_value(" 1=%d", bias_term)
  634. fprintf_param_value(" 2=%d", weight_data_size)
  635. fprintf_param_value(" 8=%d", int8_scale_term)
  636. fprintf_param_value(" 9=%d", activation_type)
  637. { if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); }
  638. fwrite_weight_tag_data(0, op->weight_data, bp);
  639. fwrite_weight_data(op->bias_data, bp);
  640. // write int8_scale data
  641. if (op->int8_scale_term)
  642. {
  643. std::vector<float> weight_int8scale;
  644. std::vector<float> blob_int8scale;
  645. char key[256];
  646. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  647. if (weight_int8scale_table.find(std::string(key)) != weight_int8scale_table.end())
  648. {
  649. weight_int8scale = weight_int8scale_table[std::string(key)];
  650. }
  651. if (blob_int8scale_table.find(layer->name) != blob_int8scale_table.end())
  652. {
  653. blob_int8scale = blob_int8scale_table[layer->name];
  654. }
  655. // write int8_scale data
  656. fwrite(weight_int8scale.data(), sizeof(float), weight_int8scale.size(), bp);
  657. fwrite(blob_int8scale.data(), sizeof(float), blob_int8scale.size(), bp);
  658. }
  659. }
  660. else if (layer->type == "Input")
  661. {
  662. ncnn::Input* op = (ncnn::Input*)layer;
  663. ncnn::Input* op_default = (ncnn::Input*)layer_default;
  664. fprintf_param_value(" 0=%d", w)
  665. fprintf_param_value(" 1=%d", h)
  666. fprintf_param_value(" 2=%d", c)
  667. }
  668. else if (layer->type == "InstanceNorm")
  669. {
  670. ncnn::InstanceNorm* op = (ncnn::InstanceNorm*)layer;
  671. ncnn::InstanceNorm* op_default = (ncnn::InstanceNorm*)layer_default;
  672. fprintf_param_value(" 0=%d", channels)
  673. fprintf_param_value(" 1=%f", eps)
  674. }
  675. else if (layer->type == "Interp")
  676. {
  677. ncnn::Interp* op = (ncnn::Interp*)layer;
  678. ncnn::Interp* op_default = (ncnn::Interp*)layer_default;
  679. fprintf_param_value(" 0=%d", resize_type)
  680. fprintf_param_value(" 1=%f", height_scale)
  681. fprintf_param_value(" 2=%f", width_scale)
  682. fprintf_param_value(" 3=%d", output_height)
  683. fprintf_param_value(" 4=%d", output_width)
  684. }
  685. else if (layer->type == "Log")
  686. {
  687. ncnn::Log* op = (ncnn::Log*)layer;
  688. ncnn::Log* op_default = (ncnn::Log*)layer_default;
  689. fprintf_param_value(" 0=%f", base)
  690. fprintf_param_value(" 1=%f", scale)
  691. fprintf_param_value(" 2=%f", shift)
  692. }
  693. else if (layer->type == "LRN")
  694. {
  695. ncnn::LRN* op = (ncnn::LRN*)layer;
  696. ncnn::LRN* op_default = (ncnn::LRN*)layer_default;
  697. fprintf_param_value(" 0=%d", region_type)
  698. fprintf_param_value(" 1=%d", local_size)
  699. fprintf_param_value(" 2=%f", alpha)
  700. fprintf_param_value(" 3=%f", beta)
  701. fprintf_param_value(" 4=%f", bias)
  702. }
  703. else if (layer->type == "LSTM")
  704. {
  705. ncnn::LSTM* op = (ncnn::LSTM*)layer;
  706. ncnn::LSTM* op_default = (ncnn::LSTM*)layer_default;
  707. fprintf_param_value(" 0=%d", num_output)
  708. fprintf_param_value(" 1=%d", weight_data_size)
  709. fprintf_param_value(" 2=%d", direction)
  710. fwrite_weight_tag_data(0, op->weight_xc_data, bp);
  711. fwrite_weight_tag_data(0, op->bias_c_data, bp);
  712. fwrite_weight_tag_data(0, op->weight_hc_data, bp);
  713. }
  714. else if (layer->type == "MemoryData")
  715. {
  716. ncnn::MemoryData* op = (ncnn::MemoryData*)layer;
  717. ncnn::MemoryData* op_default = (ncnn::MemoryData*)layer_default;
  718. fprintf_param_value(" 0=%d", w)
  719. fprintf_param_value(" 1=%d", h)
  720. fprintf_param_value(" 2=%d", c)
  721. fwrite_weight_data(op->data, bp);
  722. }
  723. else if (layer->type == "MVN")
  724. {
  725. ncnn::MVN* op = (ncnn::MVN*)layer;
  726. ncnn::MVN* op_default = (ncnn::MVN*)layer_default;
  727. fprintf_param_value(" 0=%d", normalize_variance)
  728. fprintf_param_value(" 1=%d", across_channels)
  729. fprintf_param_value(" 2=%f", eps)
  730. }
  731. else if (layer->type == "Normalize")
  732. {
  733. ncnn::Normalize* op = (ncnn::Normalize*)layer;
  734. ncnn::Normalize* op_default = (ncnn::Normalize*)layer_default;
  735. fprintf_param_value(" 0=%d", across_spatial)
  736. fprintf_param_value(" 1=%d", channel_shared)
  737. fprintf_param_value(" 2=%f", eps)
  738. fprintf_param_value(" 3=%d", scale_data_size)
  739. fprintf_param_value(" 4=%d", across_channel)
  740. fwrite_weight_data(op->scale_data, bp);
  741. }
  742. else if (layer->type == "Padding")
  743. {
  744. ncnn::Padding* op = (ncnn::Padding*)layer;
  745. ncnn::Padding* op_default = (ncnn::Padding*)layer_default;
  746. fprintf_param_value(" 0=%d", top)
  747. fprintf_param_value(" 1=%d", bottom)
  748. fprintf_param_value(" 2=%d", left)
  749. fprintf_param_value(" 3=%d", right)
  750. fprintf_param_value(" 4=%d", type)
  751. fprintf_param_value(" 5=%f", value)
  752. }
  753. else if (layer->type == "Permute")
  754. {
  755. ncnn::Permute* op = (ncnn::Permute*)layer;
  756. ncnn::Permute* op_default = (ncnn::Permute*)layer_default;
  757. fprintf_param_value(" 0=%d", order_type)
  758. }
  759. else if (layer->type == "PixelShuffle")
  760. {
  761. ncnn::PixelShuffle* op = (ncnn::PixelShuffle*)layer;
  762. ncnn::PixelShuffle* op_default = (ncnn::PixelShuffle*)layer_default;
  763. fprintf_param_value(" 0=%d", upscale_factor)
  764. }
  765. else if (layer->type == "Pooling")
  766. {
  767. ncnn::Pooling* op = (ncnn::Pooling*)layer;
  768. ncnn::Pooling* op_default = (ncnn::Pooling*)layer_default;
  769. fprintf_param_value(" 0=%d", pooling_type)
  770. fprintf_param_value(" 1=%d", kernel_w)
  771. { if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); }
  772. fprintf_param_value(" 2=%d", stride_w)
  773. { if (op->stride_h != op->stride_w) fprintf(pp, " 12=%d", op->stride_h); }
  774. fprintf_param_value(" 3=%d", pad_left)
  775. { if (op->pad_top != op->pad_left) fprintf(pp, " 13=%d", op->pad_top); }
  776. { if (op->pad_right != op->pad_left) fprintf(pp, " 14=%d", op->pad_right); }
  777. { if (op->pad_bottom != op->pad_top) fprintf(pp, " 15=%d", op->pad_bottom); }
  778. fprintf_param_value(" 4=%d", global_pooling)
  779. fprintf_param_value(" 5=%d", pad_mode)
  780. }
  781. else if (layer->type == "Power")
  782. {
  783. ncnn::Power* op = (ncnn::Power*)layer;
  784. ncnn::Power* op_default = (ncnn::Power*)layer_default;
  785. fprintf_param_value(" 0=%f", power)
  786. fprintf_param_value(" 1=%f", scale)
  787. fprintf_param_value(" 2=%f", shift)
  788. }
  789. else if (layer->type == "PReLU")
  790. {
  791. ncnn::PReLU* op = (ncnn::PReLU*)layer;
  792. ncnn::PReLU* op_default = (ncnn::PReLU*)layer_default;
  793. fprintf_param_value(" 0=%d", num_slope)
  794. fwrite_weight_data(op->slope_data, bp);
  795. }
  796. else if (layer->type == "PriorBox")
  797. {
  798. ncnn::PriorBox* op = (ncnn::PriorBox*)layer;
  799. ncnn::PriorBox* op_default = (ncnn::PriorBox*)layer_default;
  800. { if (!op->min_sizes.empty()) fprintf_param_float_array(0, op->min_sizes, pp); }
  801. { if (!op->max_sizes.empty()) fprintf_param_float_array(1, op->max_sizes, pp); }
  802. { if (!op->aspect_ratios.empty()) fprintf_param_float_array(2, op->aspect_ratios, pp); }
  803. fprintf_param_value(" 3=%f", variances[0])
  804. fprintf_param_value(" 4=%f", variances[1])
  805. fprintf_param_value(" 5=%f", variances[2])
  806. fprintf_param_value(" 6=%f", variances[3])
  807. fprintf_param_value(" 7=%d", flip)
  808. fprintf_param_value(" 8=%d", clip)
  809. fprintf_param_value(" 9=%d", image_width)
  810. fprintf_param_value(" 10=%d", image_height)
  811. fprintf_param_value(" 11=%f", step_width)
  812. fprintf_param_value(" 12=%f", step_height)
  813. fprintf_param_value(" 13=%f", offset)
  814. }
  815. else if (layer->type == "Proposal")
  816. {
  817. ncnn::Proposal* op = (ncnn::Proposal*)layer;
  818. ncnn::Proposal* op_default = (ncnn::Proposal*)layer_default;
  819. fprintf_param_value(" 0=%d", feat_stride)
  820. fprintf_param_value(" 1=%d", base_size)
  821. fprintf_param_value(" 2=%d", pre_nms_topN)
  822. fprintf_param_value(" 3=%d", after_nms_topN)
  823. fprintf_param_value(" 4=%f", nms_thresh)
  824. fprintf_param_value(" 5=%d", min_size)
  825. }
  826. else if (layer->type == "PSROIPooling")
  827. {
  828. ncnn::PSROIPooling* op = (ncnn::PSROIPooling*)layer;
  829. ncnn::PSROIPooling* op_default = (ncnn::PSROIPooling*)layer_default;
  830. fprintf_param_value(" 0=%d", pooled_width)
  831. fprintf_param_value(" 1=%d", pooled_height)
  832. fprintf_param_value(" 2=%f", spatial_scale)
  833. fprintf_param_value(" 3=%d", output_dim)
  834. }
  835. else if (layer->type == "Quantize")
  836. {
  837. ncnn::Quantize* op = (ncnn::Quantize*)layer;
  838. ncnn::Quantize* op_default = (ncnn::Quantize*)layer_default;
  839. fprintf_param_value(" 0=%f", scale)
  840. }
  841. else if (layer->type == "Reduction")
  842. {
  843. ncnn::Reduction* op = (ncnn::Reduction*)layer;
  844. ncnn::Reduction* op_default = (ncnn::Reduction*)layer_default;
  845. fprintf_param_value(" 0=%d", operation)
  846. fprintf_param_value(" 1=%d", reduce_all)
  847. fprintf_param_value(" 2=%f", coeff)
  848. { if (!op->axes.empty()) fprintf_param_int_array(3, op->axes, pp); }
  849. fprintf_param_value(" 4=%d", keepdims)
  850. }
  851. else if (layer->type == "ReLU")
  852. {
  853. ncnn::ReLU* op = (ncnn::ReLU*)layer;
  854. ncnn::ReLU* op_default = (ncnn::ReLU*)layer_default;
  855. fprintf_param_value(" 0=%f", slope)
  856. }
  857. else if (layer->type == "Reorg")
  858. {
  859. ncnn::Reorg* op = (ncnn::Reorg*)layer;
  860. ncnn::Reorg* op_default = (ncnn::Reorg*)layer_default;
  861. fprintf_param_value(" 0=%d", stride)
  862. }
  863. else if (layer->type == "Requantize")
  864. {
  865. ncnn::Requantize* op = (ncnn::Requantize*)layer;
  866. ncnn::Requantize* op_default = (ncnn::Requantize*)layer_default;
  867. fprintf_param_value(" 0=%f", scale_in)
  868. fprintf_param_value(" 1=%f", scale_out)
  869. fprintf_param_value(" 2=%d", bias_term)
  870. fprintf_param_value(" 3=%d", bias_data_size)
  871. fprintf_param_value(" 4=%d", fusion_relu)
  872. }
  873. else if (layer->type == "Reshape")
  874. {
  875. ncnn::Reshape* op = (ncnn::Reshape*)layer;
  876. ncnn::Reshape* op_default = (ncnn::Reshape*)layer_default;
  877. fprintf_param_value(" 0=%d", w)
  878. fprintf_param_value(" 1=%d", h)
  879. fprintf_param_value(" 2=%d", c)
  880. fprintf_param_value(" 3=%d", permute)
  881. }
  882. else if (layer->type == "ROIAlign")
  883. {
  884. ncnn::ROIAlign* op = (ncnn::ROIAlign*)layer;
  885. ncnn::ROIAlign* op_default = (ncnn::ROIAlign*)layer_default;
  886. fprintf_param_value(" 0=%d", pooled_width)
  887. fprintf_param_value(" 1=%d", pooled_height)
  888. fprintf_param_value(" 2=%f", spatial_scale)
  889. }
  890. else if (layer->type == "ROIPooling")
  891. {
  892. ncnn::ROIPooling* op = (ncnn::ROIPooling*)layer;
  893. ncnn::ROIPooling* op_default = (ncnn::ROIPooling*)layer_default;
  894. fprintf_param_value(" 0=%d", pooled_width)
  895. fprintf_param_value(" 1=%d", pooled_height)
  896. fprintf_param_value(" 2=%f", spatial_scale)
  897. }
  898. else if (layer->type == "Scale")
  899. {
  900. ncnn::Scale* op = (ncnn::Scale*)layer;
  901. ncnn::Scale* op_default = (ncnn::Scale*)layer_default;
  902. fprintf_param_value(" 0=%d", scale_data_size)
  903. fprintf_param_value(" 1=%d", bias_term)
  904. fwrite_weight_data(op->scale_data, bp);
  905. fwrite_weight_data(op->bias_data, bp);
  906. }
  907. else if (layer->type == "ShuffleChannel")
  908. {
  909. ncnn::ShuffleChannel* op = (ncnn::ShuffleChannel*)layer;
  910. ncnn::ShuffleChannel* op_default = (ncnn::ShuffleChannel*)layer_default;
  911. fprintf_param_value(" 0=%d", group)
  912. }
  913. else if (layer->type == "Slice")
  914. {
  915. ncnn::Slice* op = (ncnn::Slice*)layer;
  916. ncnn::Slice* op_default = (ncnn::Slice*)layer_default;
  917. { if (!op->slices.empty()) fprintf_param_int_array(0, op->slices, pp); }
  918. fprintf_param_value(" 1=%d", axis)
  919. }
  920. else if (layer->type == "Softmax")
  921. {
  922. ncnn::Softmax* op = (ncnn::Softmax*)layer;
  923. ncnn::Softmax* op_default = (ncnn::Softmax*)layer_default;
  924. fprintf_param_value(" 0=%d", axis)
  925. // HACK
  926. if (op->axis != 0)
  927. {
  928. int fixbug0 = 1;
  929. fprintf(pp, " 1=%d", fixbug0);
  930. }
  931. }
  932. else if (layer->type == "Threshold")
  933. {
  934. ncnn::Threshold* op = (ncnn::Threshold*)layer;
  935. ncnn::Threshold* op_default = (ncnn::Threshold*)layer_default;
  936. fprintf_param_value(" 0=%f", threshold)
  937. }
  938. else if (layer->type == "UnaryOp")
  939. {
  940. ncnn::UnaryOp* op = (ncnn::UnaryOp*)layer;
  941. ncnn::UnaryOp* op_default = (ncnn::UnaryOp*)layer_default;
  942. fprintf_param_value(" 0=%d", op_type)
  943. }
  944. else if (layer->type == "YoloDetectionOutput")
  945. {
  946. ncnn::YoloDetectionOutput* op = (ncnn::YoloDetectionOutput*)layer;
  947. ncnn::YoloDetectionOutput* op_default = (ncnn::YoloDetectionOutput*)layer_default;
  948. fprintf_param_value(" 0=%d", num_class)
  949. fprintf_param_value(" 1=%d", num_box)
  950. fprintf_param_value(" 2=%f", confidence_threshold)
  951. fprintf_param_value(" 3=%f", nms_threshold)
  952. { if (!op->biases.empty()) fprintf_param_float_array(4, op->biases, pp); }
  953. }
  954. else if (layer->type == "Yolov3DetectionOutput")
  955. {
  956. ncnn::Yolov3DetectionOutput* op = (ncnn::Yolov3DetectionOutput*)layer;
  957. ncnn::Yolov3DetectionOutput* op_default = (ncnn::Yolov3DetectionOutput*)layer_default;
  958. fprintf_param_value(" 0=%d", num_class)
  959. fprintf_param_value(" 1=%d", num_box)
  960. fprintf_param_value(" 2=%f", confidence_threshold)
  961. fprintf_param_value(" 3=%f", nms_threshold)
  962. { if (!op->biases.empty()) fprintf_param_float_array(4, op->biases, pp); }
  963. { if (!op->mask.empty()) fprintf_param_int_array(5, op->mask, pp); }
  964. { if (!op->anchors_scale.empty()) fprintf_param_float_array(6, op->anchors_scale, pp); }
  965. }
  966. #undef fprintf_param_value
  967. fprintf(pp, "\n");
  968. delete layer_default;
  969. }
  970. fclose(pp);
  971. fclose(bp);
  972. return 0;
  973. }
  974. int main(int argc, char** argv)
  975. {
  976. if (argc != 6)
  977. {
  978. fprintf(stderr, "usage: %s [inparam] [inbin] [outparam] [outbin] [calibration table]\n", argv[0]);
  979. return -1;
  980. }
  981. const char* inparam = argv[1];
  982. const char* inbin = argv[2];
  983. const char* outparam = argv[3];
  984. const char* outbin = argv[4];
  985. const char* int8scale_table_path = argv[5];
  986. NetQuantize quantizer;
  987. // parse the calibration scale table
  988. if (int8scale_table_path)
  989. {
  990. bool s2 = read_int8scale_table(int8scale_table_path, quantizer.blob_int8scale_table, quantizer.weight_int8scale_table);
  991. if (!s2)
  992. {
  993. fprintf(stderr, "read_int8scale_table failed\n");
  994. return -1;
  995. }
  996. }
  997. quantizer.load_param(inparam);
  998. quantizer.load_model(inbin);
  999. quantizer.quantize_convolution();
  1000. quantizer.quantize_convolutiondepthwise();
  1001. quantizer.quantize_innerproduct();
  1002. quantizer.save(outparam, outbin);
  1003. return 0;
  1004. }