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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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/mvn.h"
  53. #include "layer/normalize.h"
  54. #include "layer/padding.h"
  55. #include "layer/permute.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, "fopen %s failed\n", filepath);
  86. return false;
  87. }
  88. bool in_scale_vector = false;
  89. std::string keystr;
  90. std::vector<float> scales;
  91. while (!feof(fp))
  92. {
  93. char key[256];
  94. int nscan = fscanf(fp, "%255s", key);
  95. if (nscan != 1)
  96. {
  97. break;
  98. }
  99. if (in_scale_vector)
  100. {
  101. float scale = 1.f;
  102. int nscan = sscanf(key, "%f", &scale);
  103. if (nscan == 1)
  104. {
  105. scales.push_back(scale);
  106. continue;
  107. }
  108. else
  109. {
  110. // XYZ_param_N pattern
  111. if (strstr(keystr.c_str(), "_param_"))
  112. {
  113. weight_int8scale_table[ keystr ] = scales;
  114. }
  115. else
  116. {
  117. blob_int8scale_table[ keystr ] = scales;
  118. }
  119. keystr.clear();
  120. scales.clear();
  121. in_scale_vector = false;
  122. }
  123. }
  124. if (!in_scale_vector)
  125. {
  126. keystr = key;
  127. in_scale_vector = true;
  128. }
  129. }
  130. if (in_scale_vector)
  131. {
  132. // XYZ_param_N pattern
  133. if (strstr(keystr.c_str(), "_param_"))
  134. {
  135. weight_int8scale_table[ keystr ] = scales;
  136. }
  137. else
  138. {
  139. blob_int8scale_table[ keystr ] = scales;
  140. }
  141. }
  142. fclose(fp);
  143. return true;
  144. }
  145. class NetQuantize : public ncnn::Net
  146. {
  147. public:
  148. // 0=fp32 1=fp16 2=int8
  149. int storage_type;
  150. std::map<std::string, std::vector<float> > blob_int8scale_table;
  151. std::map<std::string, std::vector<float> > weight_int8scale_table;
  152. public:
  153. int quantize_convolution();
  154. int quantize_convolutiondepthwise();
  155. int quantize_innerproduct();
  156. public:
  157. int fprintf_param_int_array(int id, const ncnn::Mat& m, FILE* pp);
  158. int fprintf_param_float_array(int id, const ncnn::Mat& m, FILE* pp);
  159. int fwrite_weight_tag_data(int tag, const ncnn::Mat& data, FILE* bp);
  160. int fwrite_weight_data(const ncnn::Mat& data, FILE* bp);
  161. int save(const char* parampath, const char* binpath);
  162. };
  163. int NetQuantize::quantize_convolution()
  164. {
  165. const int layer_count = layers.size();
  166. for (int i=0; i<layer_count; i++)
  167. {
  168. // find convoultion layer
  169. if (layers[i]->type != "Convolution")
  170. continue;
  171. // find convolution layer
  172. std::map<std::string, std::vector<float> >::iterator iter_data = blob_int8scale_table.find(layers[i]->name);
  173. if (iter_data == weight_int8scale_table.end())
  174. continue;
  175. char key[256];
  176. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  177. std::map<std::string, std::vector<float> >::iterator iter = weight_int8scale_table.find(key);
  178. if (iter == weight_int8scale_table.end())
  179. {
  180. fprintf(stderr, "this layer need to be quantized, but no scale param!\n");
  181. return -1;
  182. }
  183. // Convolution - quantize weight from fp32 to int8
  184. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[i];
  185. std::vector<float> weight_data_int8_scales = iter->second;
  186. fprintf(stderr, "quantize_convolution %s\n", convolution->name.c_str());
  187. {
  188. ncnn::Mat int8_weight_data(convolution->weight_data_size, (size_t)1u);
  189. if (int8_weight_data.empty())
  190. return -100;
  191. const int weight_data_size_output = convolution->weight_data_size / convolution->num_output;
  192. // quantize weight to int8
  193. for (int n=0; n<convolution->num_output; n++)
  194. {
  195. ncnn::Layer* op = ncnn::create_layer(ncnn::LayerType::Quantize);
  196. ncnn::ParamDict pd;
  197. pd.set(0, weight_data_int8_scales[n]);// scale
  198. op->load_param(pd);
  199. ncnn::Option opt;
  200. opt.blob_allocator = int8_weight_data.allocator;
  201. const ncnn::Mat weight_data_n = convolution->weight_data.range(weight_data_size_output * n, weight_data_size_output);
  202. ncnn::Mat int8_weight_data_n = int8_weight_data.range(weight_data_size_output * n, weight_data_size_output);
  203. op->forward(weight_data_n, int8_weight_data_n, opt);
  204. delete op;
  205. }
  206. convolution->weight_data = int8_weight_data;
  207. }
  208. convolution->int8_scale_term = 2;
  209. }
  210. return 0;
  211. }
  212. int NetQuantize::quantize_convolutiondepthwise()
  213. {
  214. const int layer_count = layers.size();
  215. for (int i=0; i<layer_count; i++)
  216. {
  217. // find convoultion layer
  218. if (layers[i]->type != "ConvolutionDepthWise")
  219. continue;
  220. // find convolutiondepthwise layer
  221. std::map<std::string, std::vector<float> >::iterator iter_data = blob_int8scale_table.find(layers[i]->name);
  222. if (iter_data == weight_int8scale_table.end())
  223. continue;
  224. char key[256];
  225. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  226. std::map<std::string, std::vector<float> >::iterator iter = weight_int8scale_table.find(key);
  227. if (iter == weight_int8scale_table.end())
  228. {
  229. fprintf(stderr, "this layer need to be quantized, but no scale param!\n");
  230. return -1;
  231. }
  232. // Convolution - quantize weight from fp32 to int8
  233. ncnn::ConvolutionDepthWise* convdw = (ncnn::ConvolutionDepthWise*)layers[i];
  234. std::vector<float> weight_data_int8_scales = iter->second;
  235. fprintf(stderr, "quantize_convolution %s\n", convdw->name.c_str());
  236. {
  237. ncnn::Mat int8_weight_data(convdw->weight_data_size, (size_t)1u);
  238. if (int8_weight_data.empty())
  239. return -100;
  240. const int weight_data_size_output = convdw->weight_data_size / convdw->group;
  241. // quantize weight to int8
  242. for (int n=0; n<convdw->group; n++)
  243. {
  244. ncnn::Layer* op = ncnn::create_layer(ncnn::LayerType::Quantize);
  245. ncnn::ParamDict pd;
  246. pd.set(0, weight_data_int8_scales[n]);// scale
  247. op->load_param(pd);
  248. ncnn::Option opt;
  249. opt.blob_allocator = int8_weight_data.allocator;
  250. const ncnn::Mat weight_data_n = convdw->weight_data.range(weight_data_size_output * n, weight_data_size_output);
  251. ncnn::Mat int8_weight_data_n = int8_weight_data.range(weight_data_size_output * n, weight_data_size_output);
  252. op->forward(weight_data_n, int8_weight_data_n, opt);
  253. delete op;
  254. }
  255. convdw->weight_data = int8_weight_data;
  256. }
  257. convdw->int8_scale_term = 1;
  258. }
  259. return 0;
  260. }
  261. int NetQuantize::quantize_innerproduct()
  262. {
  263. const int layer_count = layers.size();
  264. for (int i=0; i<layer_count; i++)
  265. {
  266. // find convoultion layer
  267. if (layers[i]->type != "InnerProduct")
  268. continue;
  269. // find InnerProduct layer
  270. std::map<std::string, std::vector<float> >::iterator iter_data = blob_int8scale_table.find(layers[i]->name);
  271. if (iter_data == weight_int8scale_table.end())
  272. continue;
  273. char key[256];
  274. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  275. std::map<std::string, std::vector<float> >::iterator iter = weight_int8scale_table.find(key);
  276. if (iter == weight_int8scale_table.end())
  277. {
  278. fprintf(stderr, "this layer need to be quantized, but no scale param!\n");
  279. return -1;
  280. }
  281. // InnerProduct - quantize weight from fp32 to int8
  282. ncnn::InnerProduct* fc = (ncnn::InnerProduct*)layers[i];
  283. std::vector<float> weight_data_int8_scales = iter->second;
  284. fprintf(stderr, "quantize_convolution %s\n", fc->name.c_str());
  285. {
  286. ncnn::Mat int8_weight_data(fc->weight_data_size, (size_t)1u);
  287. if (int8_weight_data.empty())
  288. return -100;
  289. const int weight_data_size_output = fc->weight_data_size / fc->num_output;
  290. // quantize weight to int8
  291. for (int n=0; n<fc->num_output; n++)
  292. {
  293. ncnn::Layer* op = ncnn::create_layer(ncnn::LayerType::Quantize);
  294. ncnn::ParamDict pd;
  295. pd.set(0, weight_data_int8_scales[n]);// scale
  296. op->load_param(pd);
  297. ncnn::Option opt;
  298. opt.blob_allocator = int8_weight_data.allocator;
  299. const ncnn::Mat weight_data_n = fc->weight_data.range(weight_data_size_output * n, weight_data_size_output);
  300. ncnn::Mat int8_weight_data_n = int8_weight_data.range(weight_data_size_output * n, weight_data_size_output);
  301. op->forward(weight_data_n, int8_weight_data_n, opt);
  302. delete op;
  303. }
  304. fc->weight_data = int8_weight_data;
  305. }
  306. fc->int8_scale_term = 2;
  307. }
  308. return 0;
  309. }
  310. int NetQuantize::fprintf_param_int_array(int id, const ncnn::Mat& m, FILE* pp)
  311. {
  312. const int count = m.w;
  313. const int* ptr = m;
  314. fprintf(pp, " -%d=%d", 23300 + id, count);
  315. for (int i=0; i<count; i++)
  316. {
  317. fprintf(pp, ",%d", ptr[i]);
  318. }
  319. return 0;
  320. }
  321. int NetQuantize::fprintf_param_float_array(int id, const ncnn::Mat& m, FILE* pp)
  322. {
  323. const int count = m.w;
  324. const float* ptr = m;
  325. fprintf(pp, " -%d=%d", 23300 + id, count);
  326. for (int i=0; i<count; i++)
  327. {
  328. fprintf(pp, ",%f", ptr[i]);
  329. }
  330. return 0;
  331. }
  332. static inline size_t alignSize(size_t sz, int n)
  333. {
  334. return (sz + n-1) & -n;
  335. }
  336. int NetQuantize::fwrite_weight_tag_data(int tag, const ncnn::Mat& data, FILE* bp)
  337. {
  338. int p0 = ftell(bp);
  339. ncnn::Mat data_flattened = data.reshape(data.w * data.h * data.c);
  340. if (data.elemsize == 1)
  341. tag = 0x000D4B38; // int8 magic
  342. fwrite(&tag, sizeof(int), 1, bp);
  343. fwrite(data_flattened.data, data_flattened.elemsize, data_flattened.w, bp);
  344. // padding to 32bit align
  345. int nwrite = ftell(bp) - p0;
  346. int nalign = alignSize(nwrite, 4);
  347. unsigned char padding[4] = {0x00, 0x00, 0x00, 0x00};
  348. fwrite(padding, sizeof(unsigned char), nalign - nwrite, bp);
  349. return 0;
  350. }
  351. int NetQuantize::fwrite_weight_data(const ncnn::Mat& data, FILE* bp)
  352. {
  353. int p0 = ftell(bp);
  354. ncnn::Mat data_flattened = data.reshape(data.w * data.h * data.c);
  355. fwrite(data_flattened.data, data_flattened.elemsize, data_flattened.w, bp);
  356. // padding to 32bit align
  357. int nwrite = ftell(bp) - p0;
  358. int nalign = alignSize(nwrite, 4);
  359. unsigned char padding[4] = {0x00, 0x00, 0x00, 0x00};
  360. fwrite(padding, sizeof(unsigned char), nalign - nwrite, bp);
  361. return 0;
  362. }
  363. int NetQuantize::save(const char* parampath, const char* binpath)
  364. {
  365. FILE* pp = fopen(parampath, "wb");
  366. FILE* bp = fopen(binpath, "wb");
  367. fprintf(pp, "7767517\n");
  368. const int layer_count = layers.size();
  369. int layer_count_fused = 0;
  370. std::set<std::string> blob_names;
  371. for (int i=0; i<layer_count; i++)
  372. {
  373. const ncnn::Layer* layer = layers[i];
  374. if (layer->type == "ncnnfused")
  375. continue;
  376. layer_count_fused++;
  377. int bottom_count = layer->bottoms.size();
  378. for (int j=0; j<bottom_count; j++)
  379. {
  380. int bottom_blob_index = layer->bottoms[j];
  381. blob_names.insert(blobs[bottom_blob_index].name);
  382. }
  383. int top_count = layer->tops.size();
  384. for (int j=0; j<top_count; j++)
  385. {
  386. int top_blob_index = layer->tops[j];
  387. blob_names.insert(blobs[top_blob_index].name);
  388. }
  389. }
  390. int blob_count_fused = blob_names.size();
  391. fprintf(pp, "%d %d\n", layer_count_fused, blob_count_fused);
  392. for (int i=0; i<layer_count; i++)
  393. {
  394. const ncnn::Layer* layer = layers[i];
  395. if (layer->type == "ncnnfused")
  396. continue;
  397. int bottom_count = layer->bottoms.size();
  398. int top_count = layer->tops.size();
  399. fprintf(pp, "%-24s %-24s %d %d", layer->type.c_str(), layer->name.c_str(), bottom_count, top_count);
  400. for (int j=0; j<bottom_count; j++)
  401. {
  402. int bottom_blob_index = layer->bottoms[j];
  403. fprintf(pp, " %s", blobs[bottom_blob_index].name.c_str());
  404. }
  405. for (int j=0; j<top_count; j++)
  406. {
  407. int top_blob_index = layer->tops[j];
  408. fprintf(pp, " %s", blobs[top_blob_index].name.c_str());
  409. }
  410. ncnn::Layer* layer_default = ncnn::create_layer(layer->typeindex);
  411. ncnn::ParamDict pd;
  412. layer_default->load_param(pd);
  413. #define fprintf_param_value(format, phase) \
  414. { if (op->phase != op_default->phase) fprintf(pp, format, op->phase); }
  415. if (layer->type == "BatchNorm")
  416. {
  417. ncnn::BatchNorm* op = (ncnn::BatchNorm*)layer;
  418. ncnn::BatchNorm* op_default = (ncnn::BatchNorm*)layer_default;
  419. fprintf_param_value(" 0=%d", channels)
  420. fprintf_param_value(" 1=%f", eps)
  421. fwrite_weight_data(op->slope_data, bp);
  422. fwrite_weight_data(op->mean_data, bp);
  423. fwrite_weight_data(op->var_data, bp);
  424. fwrite_weight_data(op->bias_data, bp);
  425. }
  426. else if (layer->type == "Bias")
  427. {
  428. ncnn::Bias* op = (ncnn::Bias*)layer;
  429. ncnn::Bias* op_default = (ncnn::Bias*)layer_default;
  430. fprintf_param_value(" 0=%d", bias_data_size)
  431. fwrite_weight_data(op->bias_data, bp);
  432. }
  433. else if (layer->type == "BinaryOp")
  434. {
  435. ncnn::BinaryOp* op = (ncnn::BinaryOp*)layer;
  436. ncnn::BinaryOp* op_default = (ncnn::BinaryOp*)layer_default;
  437. fprintf_param_value(" 0=%d", op_type)
  438. fprintf_param_value(" 1=%d", with_scalar)
  439. fprintf_param_value(" 2=%f", b)
  440. }
  441. else if (layer->type == "Clip")
  442. {
  443. ncnn::Clip* op = (ncnn::Clip*)layer;
  444. ncnn::Clip* op_default = (ncnn::Clip*)layer_default;
  445. fprintf_param_value(" 0=%f", min)
  446. fprintf_param_value(" 1=%f", max)
  447. }
  448. else if (layer->type == "Concat")
  449. {
  450. ncnn::Concat* op = (ncnn::Concat*)layer;
  451. ncnn::Concat* op_default = (ncnn::Concat*)layer_default;
  452. fprintf_param_value(" 0=%d", axis)
  453. }
  454. else if (layer->type == "Convolution")
  455. {
  456. ncnn::Convolution* op = (ncnn::Convolution*)layer;
  457. ncnn::Convolution* op_default = (ncnn::Convolution*)layer_default;
  458. fprintf_param_value(" 0=%d", num_output)
  459. fprintf_param_value(" 1=%d", kernel_w)
  460. { if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); }
  461. fprintf_param_value(" 2=%d", dilation_w)
  462. { if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); }
  463. fprintf_param_value(" 3=%d", stride_w)
  464. { if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); }
  465. fprintf_param_value(" 4=%d", pad_w)
  466. { if (op->pad_h != op->pad_w) fprintf(pp, " 14=%d", op->pad_h); }
  467. fprintf_param_value(" 5=%d", bias_term)
  468. fprintf_param_value(" 6=%d", weight_data_size)
  469. fprintf_param_value(" 8=%d", int8_scale_term)
  470. fprintf_param_value(" 9=%d", activation_type)
  471. { if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); }
  472. fwrite_weight_tag_data(0, op->weight_data, bp);
  473. fwrite_weight_data(op->bias_data, bp);
  474. // write int8_scale data
  475. if (op->int8_scale_term)
  476. {
  477. std::vector<float> weight_int8scale;
  478. std::vector<float> blob_int8scale;
  479. char key[256];
  480. sprintf(key, "%s_param_0", layer->name.c_str());
  481. if (weight_int8scale_table.find(std::string(key)) != weight_int8scale_table.end())
  482. {
  483. weight_int8scale = weight_int8scale_table[std::string(key)];
  484. }
  485. if (blob_int8scale_table.find(layer->name) != blob_int8scale_table.end())
  486. {
  487. blob_int8scale = blob_int8scale_table[layer->name];
  488. }
  489. // write int8_scale data
  490. fwrite(weight_int8scale.data(), sizeof(float), weight_int8scale.size(), bp);
  491. fwrite(blob_int8scale.data(), sizeof(float), blob_int8scale.size(), bp);
  492. }
  493. }
  494. else if (layer->type == "ConvolutionDepthWise")
  495. {
  496. ncnn::ConvolutionDepthWise* op = (ncnn::ConvolutionDepthWise*)layer;
  497. ncnn::ConvolutionDepthWise* op_default = (ncnn::ConvolutionDepthWise*)layer_default;
  498. fprintf_param_value(" 0=%d", num_output)
  499. fprintf_param_value(" 1=%d", kernel_w)
  500. { if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); }
  501. fprintf_param_value(" 2=%d", dilation_w)
  502. { if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); }
  503. fprintf_param_value(" 3=%d", stride_w)
  504. { if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); }
  505. fprintf_param_value(" 4=%d", pad_w)
  506. { if (op->pad_h != op->pad_w) fprintf(pp, " 14=%d", op->pad_h); }
  507. fprintf_param_value(" 5=%d", bias_term)
  508. fprintf_param_value(" 6=%d", weight_data_size)
  509. fprintf_param_value(" 7=%d", group)
  510. fprintf_param_value(" 8=%d", int8_scale_term)
  511. fprintf_param_value(" 9=%d", activation_type)
  512. { if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); }
  513. fwrite_weight_tag_data(0, op->weight_data, bp);
  514. fwrite_weight_data(op->bias_data, bp);
  515. // write int8_scale data
  516. if (op->int8_scale_term)
  517. {
  518. std::vector<float> weight_int8scale;
  519. std::vector<float> blob_int8scale;
  520. char key[256];
  521. sprintf(key, "%s_param_0", layer->name.c_str());
  522. if (weight_int8scale_table.find(std::string(key)) != weight_int8scale_table.end())
  523. {
  524. weight_int8scale = weight_int8scale_table[std::string(key)];
  525. }
  526. if (blob_int8scale_table.find(layer->name) != blob_int8scale_table.end())
  527. {
  528. blob_int8scale = blob_int8scale_table[layer->name];
  529. }
  530. // write int8_scale data
  531. fwrite(weight_int8scale.data(), sizeof(float), weight_int8scale.size(), bp);
  532. fwrite(blob_int8scale.data(), sizeof(float), blob_int8scale.size(), bp);
  533. }
  534. }
  535. else if (layer->type == "Crop")
  536. {
  537. ncnn::Crop* op = (ncnn::Crop*)layer;
  538. ncnn::Crop* op_default = (ncnn::Crop*)layer_default;
  539. fprintf_param_value(" 0=%d", woffset)
  540. fprintf_param_value(" 1=%d", hoffset)
  541. fprintf_param_value(" 2=%d", coffset)
  542. fprintf_param_value(" 3=%d", outw)
  543. fprintf_param_value(" 4=%d", outh)
  544. fprintf_param_value(" 5=%d", outc)
  545. }
  546. else if (layer->type == "Deconvolution")
  547. {
  548. ncnn::Deconvolution* op = (ncnn::Deconvolution*)layer;
  549. ncnn::Deconvolution* op_default = (ncnn::Deconvolution*)layer_default;
  550. fprintf_param_value(" 0=%d", num_output)
  551. fprintf_param_value(" 1=%d", kernel_w)
  552. { if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); }
  553. fprintf_param_value(" 2=%d", dilation_w)
  554. { if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); }
  555. fprintf_param_value(" 3=%d", stride_w)
  556. { if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); }
  557. fprintf_param_value(" 4=%d", pad_w)
  558. { if (op->pad_h != op->pad_w) fprintf(pp, " 14=%d", op->pad_h); }
  559. fprintf_param_value(" 5=%d", bias_term)
  560. fprintf_param_value(" 6=%d", weight_data_size)
  561. fprintf_param_value(" 9=%d", activation_type)
  562. { if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); }
  563. fwrite_weight_tag_data(0, op->weight_data, bp);
  564. fwrite_weight_data(op->bias_data, bp);
  565. }
  566. else if (layer->type == "DeconvolutionDepthWise")
  567. {
  568. ncnn::DeconvolutionDepthWise* op = (ncnn::DeconvolutionDepthWise*)layer;
  569. ncnn::DeconvolutionDepthWise* op_default = (ncnn::DeconvolutionDepthWise*)layer_default;
  570. fprintf_param_value(" 0=%d", num_output)
  571. fprintf_param_value(" 1=%d", kernel_w)
  572. { if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); }
  573. fprintf_param_value(" 2=%d", dilation_w)
  574. { if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); }
  575. fprintf_param_value(" 3=%d", stride_w)
  576. { if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); }
  577. fprintf_param_value(" 4=%d", pad_w)
  578. { if (op->pad_h != op->pad_w) fprintf(pp, " 14=%d", op->pad_h); }
  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", layer->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 == "MVN")
  704. {
  705. ncnn::MVN* op = (ncnn::MVN*)layer;
  706. ncnn::MVN* op_default = (ncnn::MVN*)layer_default;
  707. fprintf_param_value(" 0=%d", normalize_variance)
  708. fprintf_param_value(" 1=%d", across_channels)
  709. fprintf_param_value(" 2=%f", eps)
  710. }
  711. else if (layer->type == "Normalize")
  712. {
  713. ncnn::Normalize* op = (ncnn::Normalize*)layer;
  714. ncnn::Normalize* op_default = (ncnn::Normalize*)layer_default;
  715. fprintf_param_value(" 0=%d", across_spatial)
  716. fprintf_param_value(" 1=%d", channel_shared)
  717. fprintf_param_value(" 2=%f", eps)
  718. fprintf_param_value(" 3=%d", scale_data_size)
  719. fprintf_param_value(" 4=%d", across_channel)
  720. fwrite_weight_data(op->scale_data, bp);
  721. }
  722. else if (layer->type == "Padding")
  723. {
  724. ncnn::Padding* op = (ncnn::Padding*)layer;
  725. ncnn::Padding* op_default = (ncnn::Padding*)layer_default;
  726. fprintf_param_value(" 0=%d", top)
  727. fprintf_param_value(" 1=%d", bottom)
  728. fprintf_param_value(" 2=%d", left)
  729. fprintf_param_value(" 3=%d", right)
  730. fprintf_param_value(" 4=%d", type)
  731. fprintf_param_value(" 5=%f", value)
  732. }
  733. else if (layer->type == "Permute")
  734. {
  735. ncnn::Permute* op = (ncnn::Permute*)layer;
  736. ncnn::Permute* op_default = (ncnn::Permute*)layer_default;
  737. fprintf_param_value(" 0=%d", order_type)
  738. }
  739. else if (layer->type == "Pooling")
  740. {
  741. ncnn::Pooling* op = (ncnn::Pooling*)layer;
  742. ncnn::Pooling* op_default = (ncnn::Pooling*)layer_default;
  743. fprintf_param_value(" 0=%d", pooling_type)
  744. fprintf_param_value(" 1=%d", kernel_w)
  745. { if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); }
  746. fprintf_param_value(" 2=%d", stride_w)
  747. { if (op->stride_h != op->stride_w) fprintf(pp, " 12=%d", op->stride_h); }
  748. fprintf_param_value(" 3=%d", pad_left)
  749. { if (op->pad_top != op->pad_left) fprintf(pp, " 13=%d", op->pad_top); }
  750. { if (op->pad_right != op->pad_left) fprintf(pp, " 14=%d", op->pad_right); }
  751. { if (op->pad_bottom != op->pad_top) fprintf(pp, " 15=%d", op->pad_bottom); }
  752. fprintf_param_value(" 4=%d", global_pooling)
  753. fprintf_param_value(" 5=%d", pad_mode)
  754. }
  755. else if (layer->type == "Power")
  756. {
  757. ncnn::Power* op = (ncnn::Power*)layer;
  758. ncnn::Power* op_default = (ncnn::Power*)layer_default;
  759. fprintf_param_value(" 0=%f", power)
  760. fprintf_param_value(" 1=%f", scale)
  761. fprintf_param_value(" 2=%f", shift)
  762. }
  763. else if (layer->type == "PReLU")
  764. {
  765. ncnn::PReLU* op = (ncnn::PReLU*)layer;
  766. ncnn::PReLU* op_default = (ncnn::PReLU*)layer_default;
  767. fprintf_param_value(" 0=%d", num_slope)
  768. fwrite_weight_data(op->slope_data, bp);
  769. }
  770. else if (layer->type == "PriorBox")
  771. {
  772. ncnn::PriorBox* op = (ncnn::PriorBox*)layer;
  773. ncnn::PriorBox* op_default = (ncnn::PriorBox*)layer_default;
  774. { if (!op->min_sizes.empty()) fprintf_param_float_array(0, op->min_sizes, pp); }
  775. { if (!op->max_sizes.empty()) fprintf_param_float_array(1, op->max_sizes, pp); }
  776. { if (!op->aspect_ratios.empty()) fprintf_param_float_array(2, op->aspect_ratios, pp); }
  777. fprintf_param_value(" 3=%f", variances[0])
  778. fprintf_param_value(" 4=%f", variances[1])
  779. fprintf_param_value(" 5=%f", variances[2])
  780. fprintf_param_value(" 6=%f", variances[3])
  781. fprintf_param_value(" 7=%d", flip)
  782. fprintf_param_value(" 8=%d", clip)
  783. fprintf_param_value(" 9=%d", image_width)
  784. fprintf_param_value(" 10=%d", image_height)
  785. fprintf_param_value(" 11=%f", step_width)
  786. fprintf_param_value(" 12=%f", step_height)
  787. fprintf_param_value(" 13=%f", offset)
  788. }
  789. else if (layer->type == "Proposal")
  790. {
  791. ncnn::Proposal* op = (ncnn::Proposal*)layer;
  792. ncnn::Proposal* op_default = (ncnn::Proposal*)layer_default;
  793. fprintf_param_value(" 0=%d", feat_stride)
  794. fprintf_param_value(" 1=%d", base_size)
  795. fprintf_param_value(" 2=%d", pre_nms_topN)
  796. fprintf_param_value(" 3=%d", after_nms_topN)
  797. fprintf_param_value(" 4=%f", nms_thresh)
  798. fprintf_param_value(" 5=%d", min_size)
  799. }
  800. else if (layer->type == "PSROIPooling")
  801. {
  802. ncnn::PSROIPooling* op = (ncnn::PSROIPooling*)layer;
  803. ncnn::PSROIPooling* op_default = (ncnn::PSROIPooling*)layer_default;
  804. fprintf_param_value(" 0=%d", pooled_width)
  805. fprintf_param_value(" 1=%d", pooled_height)
  806. fprintf_param_value(" 2=%f", spatial_scale)
  807. fprintf_param_value(" 3=%d", output_dim)
  808. }
  809. else if (layer->type == "Quantize")
  810. {
  811. ncnn::Quantize* op = (ncnn::Quantize*)layer;
  812. ncnn::Quantize* op_default = (ncnn::Quantize*)layer_default;
  813. fprintf_param_value(" 0=%f", scale)
  814. }
  815. else if (layer->type == "Reduction")
  816. {
  817. ncnn::Reduction* op = (ncnn::Reduction*)layer;
  818. ncnn::Reduction* op_default = (ncnn::Reduction*)layer_default;
  819. fprintf_param_value(" 0=%d", operation)
  820. fprintf_param_value(" 1=%d", dim)
  821. fprintf_param_value(" 2=%f", coeff)
  822. }
  823. else if (layer->type == "ReLU")
  824. {
  825. ncnn::ReLU* op = (ncnn::ReLU*)layer;
  826. ncnn::ReLU* op_default = (ncnn::ReLU*)layer_default;
  827. fprintf_param_value(" 0=%f", slope)
  828. }
  829. else if (layer->type == "Reorg")
  830. {
  831. ncnn::Reorg* op = (ncnn::Reorg*)layer;
  832. ncnn::Reorg* op_default = (ncnn::Reorg*)layer_default;
  833. fprintf_param_value(" 0=%d", stride)
  834. }
  835. else if (layer->type == "Requantize")
  836. {
  837. ncnn::Requantize* op = (ncnn::Requantize*)layer;
  838. ncnn::Requantize* op_default = (ncnn::Requantize*)layer_default;
  839. fprintf_param_value(" 0=%f", scale_in)
  840. fprintf_param_value(" 1=%f", scale_out)
  841. fprintf_param_value(" 2=%d", bias_term)
  842. fprintf_param_value(" 3=%d", bias_data_size)
  843. fprintf_param_value(" 4=%d", fusion_relu)
  844. }
  845. else if (layer->type == "Reshape")
  846. {
  847. ncnn::Reshape* op = (ncnn::Reshape*)layer;
  848. ncnn::Reshape* op_default = (ncnn::Reshape*)layer_default;
  849. fprintf_param_value(" 0=%d", w)
  850. fprintf_param_value(" 1=%d", h)
  851. fprintf_param_value(" 2=%d", c)
  852. fprintf_param_value(" 3=%d", permute)
  853. }
  854. else if (layer->type == "ROIAlign")
  855. {
  856. ncnn::ROIAlign* op = (ncnn::ROIAlign*)layer;
  857. ncnn::ROIAlign* op_default = (ncnn::ROIAlign*)layer_default;
  858. fprintf_param_value(" 0=%d", pooled_width)
  859. fprintf_param_value(" 1=%d", pooled_height)
  860. fprintf_param_value(" 2=%f", spatial_scale)
  861. }
  862. else if (layer->type == "ROIPooling")
  863. {
  864. ncnn::ROIPooling* op = (ncnn::ROIPooling*)layer;
  865. ncnn::ROIPooling* op_default = (ncnn::ROIPooling*)layer_default;
  866. fprintf_param_value(" 0=%d", pooled_width)
  867. fprintf_param_value(" 1=%d", pooled_height)
  868. fprintf_param_value(" 2=%f", spatial_scale)
  869. }
  870. else if (layer->type == "Scale")
  871. {
  872. ncnn::Scale* op = (ncnn::Scale*)layer;
  873. ncnn::Scale* op_default = (ncnn::Scale*)layer_default;
  874. fprintf_param_value(" 0=%d", scale_data_size)
  875. fprintf_param_value(" 1=%d", bias_term)
  876. fwrite_weight_data(op->scale_data, bp);
  877. fwrite_weight_data(op->bias_data, bp);
  878. }
  879. else if (layer->type == "ShuffleChannel")
  880. {
  881. ncnn::ShuffleChannel* op = (ncnn::ShuffleChannel*)layer;
  882. ncnn::ShuffleChannel* op_default = (ncnn::ShuffleChannel*)layer_default;
  883. fprintf_param_value(" 0=%d", group)
  884. }
  885. else if (layer->type == "Slice")
  886. {
  887. ncnn::Slice* op = (ncnn::Slice*)layer;
  888. ncnn::Slice* op_default = (ncnn::Slice*)layer_default;
  889. { if (!op->slices.empty()) fprintf_param_int_array(0, op->slices, pp); }
  890. fprintf_param_value(" 1=%d", axis)
  891. }
  892. else if (layer->type == "Softmax")
  893. {
  894. ncnn::Softmax* op = (ncnn::Softmax*)layer;
  895. ncnn::Softmax* op_default = (ncnn::Softmax*)layer_default;
  896. fprintf_param_value(" 0=%d", axis)
  897. // HACK
  898. if (op->axis != 0)
  899. {
  900. int fixbug0 = 1;
  901. fprintf(pp, " 1=%d", fixbug0);
  902. }
  903. }
  904. else if (layer->type == "Threshold")
  905. {
  906. ncnn::Threshold* op = (ncnn::Threshold*)layer;
  907. ncnn::Threshold* op_default = (ncnn::Threshold*)layer_default;
  908. fprintf_param_value(" 0=%f", threshold)
  909. }
  910. else if (layer->type == "UnaryOp")
  911. {
  912. ncnn::UnaryOp* op = (ncnn::UnaryOp*)layer;
  913. ncnn::UnaryOp* op_default = (ncnn::UnaryOp*)layer_default;
  914. fprintf_param_value(" 0=%d", op_type)
  915. }
  916. else if (layer->type == "YoloDetectionOutput")
  917. {
  918. ncnn::YoloDetectionOutput* op = (ncnn::YoloDetectionOutput*)layer;
  919. ncnn::YoloDetectionOutput* op_default = (ncnn::YoloDetectionOutput*)layer_default;
  920. fprintf_param_value(" 0=%d", num_class)
  921. fprintf_param_value(" 1=%d", num_box)
  922. fprintf_param_value(" 2=%f", confidence_threshold)
  923. fprintf_param_value(" 3=%f", nms_threshold)
  924. { if (!op->biases.empty()) fprintf_param_float_array(4, op->biases, pp); }
  925. }
  926. else if (layer->type == "Yolov3DetectionOutput")
  927. {
  928. ncnn::Yolov3DetectionOutput* op = (ncnn::Yolov3DetectionOutput*)layer;
  929. ncnn::Yolov3DetectionOutput* op_default = (ncnn::Yolov3DetectionOutput*)layer_default;
  930. fprintf_param_value(" 0=%d", num_class)
  931. fprintf_param_value(" 1=%d", num_box)
  932. fprintf_param_value(" 2=%f", confidence_threshold)
  933. fprintf_param_value(" 3=%f", nms_threshold)
  934. { if (!op->biases.empty()) fprintf_param_float_array(4, op->biases, pp); }
  935. { if (!op->mask.empty()) fprintf_param_int_array(5, op->mask, pp); }
  936. { if (!op->anchors_scale.empty()) fprintf_param_float_array(6, op->anchors_scale, pp); }
  937. }
  938. #undef fprintf_param_value
  939. fprintf(pp, "\n");
  940. delete layer_default;
  941. }
  942. fclose(pp);
  943. fclose(bp);
  944. return 0;
  945. }
  946. int main(int argc, char** argv)
  947. {
  948. if (argc != 6)
  949. {
  950. fprintf(stderr, "usage: %s [inparam] [inbin] [outparam] [outbin] [calibration table]\n", argv[0]);
  951. return -1;
  952. }
  953. const char* inparam = argv[1];
  954. const char* inbin = argv[2];
  955. const char* outparam = argv[3];
  956. const char* outbin = argv[4];
  957. const char* int8scale_table_path = argv[5];
  958. NetQuantize quantizer;
  959. // parse the calibration scale table
  960. if (int8scale_table_path)
  961. {
  962. bool s2 = read_int8scale_table(int8scale_table_path, quantizer.blob_int8scale_table, quantizer.weight_int8scale_table);
  963. if (!s2)
  964. {
  965. fprintf(stderr, "read_int8scale_table failed\n");
  966. return -1;
  967. }
  968. }
  969. quantizer.load_param(inparam);
  970. quantizer.load_model(inbin);
  971. quantizer.quantize_convolution();
  972. quantizer.quantize_convolutiondepthwise();
  973. quantizer.quantize_innerproduct();
  974. quantizer.save(outparam, outbin);
  975. return 0;
  976. }