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

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