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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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 <map>
  20. #include <set>
  21. #include <vector>
  22. // ncnn public header
  23. #include "layer.h"
  24. #include "layer_type.h"
  25. #include "net.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/shufflechannel.h"
  72. #include "layer/slice.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. NetQuantize();
  135. std::vector<ncnn::Blob>& blobs;
  136. std::vector<ncnn::Layer*>& layers;
  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. NetQuantize::NetQuantize()
  153. : blobs(mutable_blobs()), layers(mutable_layers())
  154. {
  155. }
  156. int NetQuantize::quantize_convolution()
  157. {
  158. const int layer_count = static_cast<int>(layers.size());
  159. for (int i = 0; i < layer_count; i++)
  160. {
  161. // find convolution layer
  162. if (layers[i]->type != "Convolution")
  163. continue;
  164. // find convolution layer
  165. std::map<std::string, std::vector<float> >::iterator iter_data = blob_int8scale_table.find(layers[i]->name);
  166. if (iter_data == blob_int8scale_table.end())
  167. continue;
  168. char key[256];
  169. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  170. std::map<std::string, std::vector<float> >::iterator iter = weight_int8scale_table.find(key);
  171. if (iter == weight_int8scale_table.end())
  172. {
  173. fprintf(stderr, "this layer need to be quantized, but no scale param!\n");
  174. return -1;
  175. }
  176. // Convolution - quantize weight from fp32 to int8
  177. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[i];
  178. std::vector<float> weight_data_int8_scales = iter->second;
  179. fprintf(stderr, "quantize_convolution %s\n", convolution->name.c_str());
  180. {
  181. ncnn::Mat int8_weight_data(convolution->weight_data_size, (size_t)1u);
  182. if (int8_weight_data.empty())
  183. return -100;
  184. const int weight_data_size_output = convolution->weight_data_size / convolution->num_output;
  185. // quantize weight to int8
  186. for (int n = 0; n < convolution->num_output; n++)
  187. {
  188. ncnn::Layer* op = ncnn::create_layer(ncnn::LayerType::Quantize);
  189. ncnn::ParamDict pd;
  190. pd.set(0, weight_data_int8_scales[n]); // scale
  191. op->load_param(pd);
  192. ncnn::Option opt;
  193. opt.blob_allocator = int8_weight_data.allocator;
  194. const ncnn::Mat weight_data_n = convolution->weight_data.range(weight_data_size_output * n, weight_data_size_output);
  195. ncnn::Mat int8_weight_data_n = int8_weight_data.range(weight_data_size_output * n, weight_data_size_output);
  196. op->forward(weight_data_n, int8_weight_data_n, opt);
  197. delete op;
  198. }
  199. convolution->weight_data = int8_weight_data;
  200. }
  201. convolution->int8_scale_term = 2;
  202. }
  203. return 0;
  204. }
  205. int NetQuantize::quantize_convolutiondepthwise()
  206. {
  207. const int layer_count = static_cast<int>(layers.size());
  208. for (int i = 0; i < layer_count; i++)
  209. {
  210. // find convoultion layer
  211. if (layers[i]->type != "ConvolutionDepthWise")
  212. continue;
  213. // find convolutiondepthwise layer
  214. std::map<std::string, std::vector<float> >::iterator iter_data = blob_int8scale_table.find(layers[i]->name);
  215. if (iter_data == blob_int8scale_table.end())
  216. continue;
  217. char key[256];
  218. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  219. std::map<std::string, std::vector<float> >::iterator iter = weight_int8scale_table.find(key);
  220. if (iter == weight_int8scale_table.end())
  221. {
  222. fprintf(stderr, "this layer need to be quantized, but no scale param!\n");
  223. return -1;
  224. }
  225. // Convolution - quantize weight from fp32 to int8
  226. ncnn::ConvolutionDepthWise* convdw = (ncnn::ConvolutionDepthWise*)layers[i];
  227. std::vector<float> weight_data_int8_scales = iter->second;
  228. fprintf(stderr, "quantize_convolution %s\n", convdw->name.c_str());
  229. {
  230. ncnn::Mat int8_weight_data(convdw->weight_data_size, (size_t)1u);
  231. if (int8_weight_data.empty())
  232. return -100;
  233. const int weight_data_size_output = convdw->weight_data_size / convdw->group;
  234. // quantize weight to int8
  235. for (int n = 0; n < convdw->group; n++)
  236. {
  237. ncnn::Layer* op = ncnn::create_layer(ncnn::LayerType::Quantize);
  238. ncnn::ParamDict pd;
  239. pd.set(0, weight_data_int8_scales[n]); // scale
  240. op->load_param(pd);
  241. ncnn::Option opt;
  242. opt.blob_allocator = int8_weight_data.allocator;
  243. const ncnn::Mat weight_data_n = convdw->weight_data.range(weight_data_size_output * n, weight_data_size_output);
  244. ncnn::Mat int8_weight_data_n = int8_weight_data.range(weight_data_size_output * n, weight_data_size_output);
  245. op->forward(weight_data_n, int8_weight_data_n, opt);
  246. delete op;
  247. }
  248. convdw->weight_data = int8_weight_data;
  249. }
  250. convdw->int8_scale_term = 1;
  251. }
  252. return 0;
  253. }
  254. int NetQuantize::quantize_innerproduct()
  255. {
  256. const int layer_count = static_cast<int>(layers.size());
  257. for (int i = 0; i < layer_count; i++)
  258. {
  259. // find convoultion layer
  260. if (layers[i]->type != "InnerProduct")
  261. continue;
  262. // find InnerProduct layer
  263. std::map<std::string, std::vector<float> >::iterator iter_data = blob_int8scale_table.find(layers[i]->name);
  264. if (iter_data == blob_int8scale_table.end())
  265. continue;
  266. char key[256];
  267. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  268. std::map<std::string, std::vector<float> >::iterator iter = weight_int8scale_table.find(key);
  269. if (iter == weight_int8scale_table.end())
  270. {
  271. fprintf(stderr, "this layer need to be quantized, but no scale param!\n");
  272. return -1;
  273. }
  274. // InnerProduct - quantize weight from fp32 to int8
  275. ncnn::InnerProduct* fc = (ncnn::InnerProduct*)layers[i];
  276. std::vector<float> weight_data_int8_scales = iter->second;
  277. fprintf(stderr, "quantize_convolution %s\n", fc->name.c_str());
  278. {
  279. ncnn::Mat int8_weight_data(fc->weight_data_size, (size_t)1u);
  280. if (int8_weight_data.empty())
  281. return -100;
  282. const int weight_data_size_output = fc->weight_data_size / fc->num_output;
  283. // quantize weight to int8
  284. for (int n = 0; n < fc->num_output; n++)
  285. {
  286. ncnn::Layer* op = ncnn::create_layer(ncnn::LayerType::Quantize);
  287. ncnn::ParamDict pd;
  288. pd.set(0, weight_data_int8_scales[n]); // scale
  289. op->load_param(pd);
  290. ncnn::Option opt;
  291. opt.blob_allocator = int8_weight_data.allocator;
  292. const ncnn::Mat weight_data_n = fc->weight_data.range(weight_data_size_output * n, weight_data_size_output);
  293. ncnn::Mat int8_weight_data_n = int8_weight_data.range(weight_data_size_output * n, weight_data_size_output);
  294. op->forward(weight_data_n, int8_weight_data_n, opt);
  295. delete op;
  296. }
  297. fc->weight_data = int8_weight_data;
  298. }
  299. fc->int8_scale_term = 2;
  300. }
  301. return 0;
  302. }
  303. int NetQuantize::fprintf_param_int_array(int id, const ncnn::Mat& m, FILE* pp)
  304. {
  305. const int count = m.w;
  306. const int* ptr = m;
  307. fprintf(pp, " -%d=%d", 23300 + id, count);
  308. for (int i = 0; i < count; i++)
  309. {
  310. fprintf(pp, ",%d", ptr[i]);
  311. }
  312. return 0;
  313. }
  314. int NetQuantize::fprintf_param_float_array(int id, const ncnn::Mat& m, FILE* pp)
  315. {
  316. const int count = m.w;
  317. const float* ptr = m;
  318. fprintf(pp, " -%d=%d", 23300 + id, count);
  319. for (int i = 0; i < count; i++)
  320. {
  321. fprintf(pp, ",%f", ptr[i]);
  322. }
  323. return 0;
  324. }
  325. static inline size_t alignSize(size_t sz, int n)
  326. {
  327. return (sz + n - 1) & -n;
  328. }
  329. int NetQuantize::fwrite_weight_tag_data(int tag, const ncnn::Mat& data, FILE* bp)
  330. {
  331. int p0 = ftell(bp);
  332. ncnn::Mat data_flattened = data.reshape(data.w * data.h * data.c);
  333. if (data.elemsize == 1)
  334. tag = 0x000D4B38; // int8 magic
  335. fwrite(&tag, sizeof(int), 1, bp);
  336. fwrite(data_flattened.data, data_flattened.elemsize, data_flattened.w, bp);
  337. // padding to 32bit align
  338. int nwrite = ftell(bp) - p0;
  339. int nalign = static_cast<int>(alignSize(nwrite, 4));
  340. unsigned char padding[4] = {0x00, 0x00, 0x00, 0x00};
  341. fwrite(padding, sizeof(unsigned char), nalign - nwrite, bp);
  342. return 0;
  343. }
  344. int NetQuantize::fwrite_weight_data(const ncnn::Mat& data, FILE* bp)
  345. {
  346. int p0 = ftell(bp);
  347. ncnn::Mat data_flattened = data.reshape(data.w * data.h * data.c);
  348. fwrite(data_flattened.data, data_flattened.elemsize, data_flattened.w, bp);
  349. // padding to 32bit align
  350. int nwrite = ftell(bp) - p0;
  351. int nalign = static_cast<int>(alignSize(nwrite, 4));
  352. unsigned char padding[4] = {0x00, 0x00, 0x00, 0x00};
  353. fwrite(padding, sizeof(unsigned char), nalign - nwrite, bp);
  354. return 0;
  355. }
  356. int NetQuantize::save(const char* parampath, const char* binpath)
  357. {
  358. FILE* pp = fopen(parampath, "wb");
  359. FILE* bp = fopen(binpath, "wb");
  360. fprintf(pp, "7767517\n");
  361. const int layer_count = static_cast<int>(layers.size());
  362. int layer_count_fused = 0;
  363. std::set<std::string> blob_names;
  364. for (int i = 0; i < layer_count; i++)
  365. {
  366. const ncnn::Layer* layer = layers[i];
  367. if (layer->type == "ncnnfused")
  368. continue;
  369. layer_count_fused++;
  370. int bottom_count = static_cast<int>(layer->bottoms.size());
  371. for (int j = 0; j < bottom_count; j++)
  372. {
  373. int bottom_blob_index = layer->bottoms[j];
  374. blob_names.insert(blobs[bottom_blob_index].name);
  375. }
  376. int top_count = static_cast<int>(layer->tops.size());
  377. for (int j = 0; j < top_count; j++)
  378. {
  379. int top_blob_index = layer->tops[j];
  380. blob_names.insert(blobs[top_blob_index].name);
  381. }
  382. }
  383. int blob_count_fused = static_cast<int>(blob_names.size());
  384. fprintf(pp, "%d %d\n", layer_count_fused, blob_count_fused);
  385. for (int i = 0; i < layer_count; i++)
  386. {
  387. const ncnn::Layer* layer = layers[i];
  388. if (layer->type == "ncnnfused")
  389. continue;
  390. int bottom_count = static_cast<int>(layer->bottoms.size());
  391. int top_count = static_cast<int>(layer->tops.size());
  392. fprintf(pp, "%-24s %-24s %d %d", layer->type.c_str(), layer->name.c_str(), bottom_count, top_count);
  393. for (int j = 0; j < bottom_count; j++)
  394. {
  395. int bottom_blob_index = layer->bottoms[j];
  396. fprintf(pp, " %s", blobs[bottom_blob_index].name.c_str());
  397. }
  398. for (int j = 0; j < top_count; j++)
  399. {
  400. int top_blob_index = layer->tops[j];
  401. fprintf(pp, " %s", blobs[top_blob_index].name.c_str());
  402. }
  403. ncnn::Layer* layer_default = ncnn::create_layer(layer->typeindex);
  404. ncnn::ParamDict pd;
  405. layer_default->load_param(pd);
  406. #define fprintf_param_value(format, phase) \
  407. { \
  408. if (op->phase != op_default->phase) fprintf(pp, format, op->phase); \
  409. }
  410. if (layer->type == "BatchNorm")
  411. {
  412. ncnn::BatchNorm* op = (ncnn::BatchNorm*)layer;
  413. ncnn::BatchNorm* op_default = (ncnn::BatchNorm*)layer_default;
  414. fprintf_param_value(" 0=%d", channels)
  415. fprintf_param_value(" 1=%f", eps)
  416. fwrite_weight_data(op->slope_data, bp);
  417. fwrite_weight_data(op->mean_data, bp);
  418. fwrite_weight_data(op->var_data, bp);
  419. fwrite_weight_data(op->bias_data, bp);
  420. }
  421. else if (layer->type == "Bias")
  422. {
  423. ncnn::Bias* op = (ncnn::Bias*)layer;
  424. ncnn::Bias* op_default = (ncnn::Bias*)layer_default;
  425. fprintf_param_value(" 0=%d", bias_data_size)
  426. fwrite_weight_data(op->bias_data, bp);
  427. }
  428. else if (layer->type == "BinaryOp")
  429. {
  430. ncnn::BinaryOp* op = (ncnn::BinaryOp*)layer;
  431. ncnn::BinaryOp* op_default = (ncnn::BinaryOp*)layer_default;
  432. fprintf_param_value(" 0=%d", op_type)
  433. fprintf_param_value(" 1=%d", with_scalar)
  434. fprintf_param_value(" 2=%f", b)
  435. }
  436. else if (layer->type == "Clip")
  437. {
  438. ncnn::Clip* op = (ncnn::Clip*)layer;
  439. ncnn::Clip* op_default = (ncnn::Clip*)layer_default;
  440. fprintf_param_value(" 0=%f", min)
  441. fprintf_param_value(" 1=%f", max)
  442. }
  443. else if (layer->type == "Concat")
  444. {
  445. ncnn::Concat* op = (ncnn::Concat*)layer;
  446. ncnn::Concat* op_default = (ncnn::Concat*)layer_default;
  447. fprintf_param_value(" 0=%d", axis)
  448. }
  449. else if (layer->type == "Convolution")
  450. {
  451. ncnn::Convolution* op = (ncnn::Convolution*)layer;
  452. ncnn::Convolution* op_default = (ncnn::Convolution*)layer_default;
  453. fprintf_param_value(" 0=%d", num_output)
  454. fprintf_param_value(" 1=%d", kernel_w)
  455. {
  456. if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h);
  457. }
  458. fprintf_param_value(" 2=%d", dilation_w)
  459. {
  460. if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h);
  461. }
  462. fprintf_param_value(" 3=%d", stride_w)
  463. {
  464. if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h);
  465. }
  466. fprintf_param_value(" 4=%d", pad_left)
  467. {
  468. if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top);
  469. }
  470. {
  471. if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right);
  472. }
  473. {
  474. if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom);
  475. }
  476. fprintf_param_value(" 5=%d", bias_term)
  477. fprintf_param_value(" 6=%d", weight_data_size)
  478. fprintf_param_value(" 8=%d", int8_scale_term)
  479. fprintf_param_value(" 9=%d", activation_type)
  480. {
  481. if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp);
  482. }
  483. fwrite_weight_tag_data(0, op->weight_data, bp);
  484. fwrite_weight_data(op->bias_data, bp);
  485. // write int8_scale data
  486. if (op->int8_scale_term)
  487. {
  488. std::vector<float> weight_int8scale;
  489. std::vector<float> blob_int8scale;
  490. char key[256];
  491. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  492. if (weight_int8scale_table.find(std::string(key)) != weight_int8scale_table.end())
  493. {
  494. weight_int8scale = weight_int8scale_table[std::string(key)];
  495. }
  496. if (blob_int8scale_table.find(layer->name) != blob_int8scale_table.end())
  497. {
  498. blob_int8scale = blob_int8scale_table[layer->name];
  499. }
  500. // write int8_scale data
  501. fwrite(weight_int8scale.data(), sizeof(float), weight_int8scale.size(), bp);
  502. fwrite(blob_int8scale.data(), sizeof(float), blob_int8scale.size(), bp);
  503. }
  504. }
  505. else if (layer->type == "ConvolutionDepthWise")
  506. {
  507. ncnn::ConvolutionDepthWise* op = (ncnn::ConvolutionDepthWise*)layer;
  508. ncnn::ConvolutionDepthWise* op_default = (ncnn::ConvolutionDepthWise*)layer_default;
  509. fprintf_param_value(" 0=%d", num_output)
  510. fprintf_param_value(" 1=%d", kernel_w)
  511. {
  512. if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h);
  513. }
  514. fprintf_param_value(" 2=%d", dilation_w)
  515. {
  516. if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h);
  517. }
  518. fprintf_param_value(" 3=%d", stride_w)
  519. {
  520. if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h);
  521. }
  522. fprintf_param_value(" 4=%d", pad_left)
  523. {
  524. if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top);
  525. }
  526. {
  527. if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right);
  528. }
  529. {
  530. if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom);
  531. }
  532. fprintf_param_value(" 5=%d", bias_term)
  533. fprintf_param_value(" 6=%d", weight_data_size)
  534. fprintf_param_value(" 7=%d", group)
  535. fprintf_param_value(" 8=%d", int8_scale_term)
  536. fprintf_param_value(" 9=%d", activation_type)
  537. {
  538. if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp);
  539. }
  540. fwrite_weight_tag_data(0, op->weight_data, bp);
  541. fwrite_weight_data(op->bias_data, bp);
  542. // write int8_scale data
  543. if (op->int8_scale_term)
  544. {
  545. std::vector<float> weight_int8scale;
  546. std::vector<float> blob_int8scale;
  547. char key[256];
  548. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  549. if (weight_int8scale_table.find(std::string(key)) != weight_int8scale_table.end())
  550. {
  551. weight_int8scale = weight_int8scale_table[std::string(key)];
  552. }
  553. if (blob_int8scale_table.find(layer->name) != blob_int8scale_table.end())
  554. {
  555. blob_int8scale = blob_int8scale_table[layer->name];
  556. }
  557. // write int8_scale data
  558. fwrite(weight_int8scale.data(), sizeof(float), weight_int8scale.size(), bp);
  559. fwrite(blob_int8scale.data(), sizeof(float), blob_int8scale.size(), bp);
  560. }
  561. }
  562. else if (layer->type == "Crop")
  563. {
  564. ncnn::Crop* op = (ncnn::Crop*)layer;
  565. ncnn::Crop* op_default = (ncnn::Crop*)layer_default;
  566. fprintf_param_value(" 0=%d", woffset)
  567. fprintf_param_value(" 1=%d", hoffset)
  568. fprintf_param_value(" 2=%d", coffset)
  569. fprintf_param_value(" 3=%d", outw)
  570. fprintf_param_value(" 4=%d", outh)
  571. fprintf_param_value(" 5=%d", outc)
  572. fprintf_param_value(" 6=%d", woffset2)
  573. fprintf_param_value(" 7=%d", hoffset2)
  574. fprintf_param_value(" 8=%d", coffset2)
  575. {
  576. if (!op->starts.empty()) fprintf_param_int_array(9, op->starts, pp);
  577. }
  578. {
  579. if (!op->ends.empty()) fprintf_param_int_array(10, op->ends, pp);
  580. }
  581. {
  582. if (!op->axes.empty()) fprintf_param_int_array(11, op->axes, pp);
  583. }
  584. }
  585. else if (layer->type == "Deconvolution")
  586. {
  587. ncnn::Deconvolution* op = (ncnn::Deconvolution*)layer;
  588. ncnn::Deconvolution* op_default = (ncnn::Deconvolution*)layer_default;
  589. fprintf_param_value(" 0=%d", num_output)
  590. fprintf_param_value(" 1=%d", kernel_w)
  591. {
  592. if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h);
  593. }
  594. fprintf_param_value(" 2=%d", dilation_w)
  595. {
  596. if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h);
  597. }
  598. fprintf_param_value(" 3=%d", stride_w)
  599. {
  600. if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h);
  601. }
  602. fprintf_param_value(" 4=%d", pad_left)
  603. {
  604. if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top);
  605. }
  606. {
  607. if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right);
  608. }
  609. {
  610. if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom);
  611. }
  612. fprintf_param_value(" 5=%d", bias_term)
  613. fprintf_param_value(" 6=%d", weight_data_size)
  614. fprintf_param_value(" 9=%d", activation_type)
  615. {
  616. if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp);
  617. }
  618. fwrite_weight_tag_data(0, op->weight_data, bp);
  619. fwrite_weight_data(op->bias_data, bp);
  620. }
  621. else if (layer->type == "DeconvolutionDepthWise")
  622. {
  623. ncnn::DeconvolutionDepthWise* op = (ncnn::DeconvolutionDepthWise*)layer;
  624. ncnn::DeconvolutionDepthWise* op_default = (ncnn::DeconvolutionDepthWise*)layer_default;
  625. fprintf_param_value(" 0=%d", num_output)
  626. fprintf_param_value(" 1=%d", kernel_w)
  627. {
  628. if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h);
  629. }
  630. fprintf_param_value(" 2=%d", dilation_w)
  631. {
  632. if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h);
  633. }
  634. fprintf_param_value(" 3=%d", stride_w)
  635. {
  636. if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h);
  637. }
  638. fprintf_param_value(" 4=%d", pad_left)
  639. {
  640. if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top);
  641. }
  642. {
  643. if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right);
  644. }
  645. {
  646. if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom);
  647. }
  648. fprintf_param_value(" 5=%d", bias_term)
  649. fprintf_param_value(" 6=%d", weight_data_size)
  650. fprintf_param_value(" 7=%d", group)
  651. fprintf_param_value(" 9=%d", activation_type)
  652. {
  653. if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp);
  654. }
  655. fwrite_weight_tag_data(0, op->weight_data, bp);
  656. fwrite_weight_data(op->bias_data, bp);
  657. }
  658. else if (layer->type == "DetectionOutput")
  659. {
  660. ncnn::DetectionOutput* op = (ncnn::DetectionOutput*)layer;
  661. ncnn::DetectionOutput* op_default = (ncnn::DetectionOutput*)layer_default;
  662. fprintf_param_value(" 0=%d", num_class)
  663. fprintf_param_value(" 1=%f", nms_threshold)
  664. fprintf_param_value(" 2=%d", nms_top_k)
  665. fprintf_param_value(" 3=%d", keep_top_k)
  666. fprintf_param_value(" 4=%f", confidence_threshold)
  667. fprintf_param_value(" 5=%f", variances[0])
  668. fprintf_param_value(" 6=%f", variances[1])
  669. fprintf_param_value(" 7=%f", variances[2])
  670. fprintf_param_value(" 8=%f", variances[3])
  671. }
  672. else if (layer->type == "Dropout")
  673. {
  674. ncnn::Dropout* op = (ncnn::Dropout*)layer;
  675. ncnn::Dropout* op_default = (ncnn::Dropout*)layer_default;
  676. fprintf_param_value(" 0=%f", scale)
  677. }
  678. else if (layer->type == "Eltwise")
  679. {
  680. ncnn::Eltwise* op = (ncnn::Eltwise*)layer;
  681. ncnn::Eltwise* op_default = (ncnn::Eltwise*)layer_default;
  682. fprintf_param_value(" 0=%d", op_type)
  683. {
  684. if (!op->coeffs.empty()) fprintf_param_float_array(1, op->coeffs, pp);
  685. }
  686. }
  687. else if (layer->type == "ELU")
  688. {
  689. ncnn::ELU* op = (ncnn::ELU*)layer;
  690. ncnn::ELU* op_default = (ncnn::ELU*)layer_default;
  691. fprintf_param_value(" 0=%f", alpha)
  692. }
  693. else if (layer->type == "Exp")
  694. {
  695. ncnn::Exp* op = (ncnn::Exp*)layer;
  696. ncnn::Exp* op_default = (ncnn::Exp*)layer_default;
  697. fprintf_param_value(" 0=%f", base)
  698. fprintf_param_value(" 1=%f", scale)
  699. fprintf_param_value(" 2=%f", shift)
  700. }
  701. else if (layer->type == "InnerProduct")
  702. {
  703. ncnn::InnerProduct* op = (ncnn::InnerProduct*)layer;
  704. ncnn::InnerProduct* op_default = (ncnn::InnerProduct*)layer_default;
  705. fprintf_param_value(" 0=%d", num_output)
  706. fprintf_param_value(" 1=%d", bias_term)
  707. fprintf_param_value(" 2=%d", weight_data_size)
  708. fprintf_param_value(" 8=%d", int8_scale_term)
  709. fprintf_param_value(" 9=%d", activation_type)
  710. {
  711. if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp);
  712. }
  713. fwrite_weight_tag_data(0, op->weight_data, bp);
  714. fwrite_weight_data(op->bias_data, bp);
  715. // write int8_scale data
  716. if (op->int8_scale_term)
  717. {
  718. std::vector<float> weight_int8scale;
  719. std::vector<float> blob_int8scale;
  720. char key[256];
  721. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  722. if (weight_int8scale_table.find(std::string(key)) != weight_int8scale_table.end())
  723. {
  724. weight_int8scale = weight_int8scale_table[std::string(key)];
  725. }
  726. if (blob_int8scale_table.find(layer->name) != blob_int8scale_table.end())
  727. {
  728. blob_int8scale = blob_int8scale_table[layer->name];
  729. }
  730. // write int8_scale data
  731. fwrite(weight_int8scale.data(), sizeof(float), weight_int8scale.size(), bp);
  732. fwrite(blob_int8scale.data(), sizeof(float), blob_int8scale.size(), bp);
  733. }
  734. }
  735. else if (layer->type == "Input")
  736. {
  737. ncnn::Input* op = (ncnn::Input*)layer;
  738. ncnn::Input* op_default = (ncnn::Input*)layer_default;
  739. fprintf_param_value(" 0=%d", w)
  740. fprintf_param_value(" 1=%d", h)
  741. fprintf_param_value(" 2=%d", c)
  742. }
  743. else if (layer->type == "InstanceNorm")
  744. {
  745. ncnn::InstanceNorm* op = (ncnn::InstanceNorm*)layer;
  746. ncnn::InstanceNorm* op_default = (ncnn::InstanceNorm*)layer_default;
  747. fprintf_param_value(" 0=%d", channels)
  748. fprintf_param_value(" 1=%f", eps)
  749. }
  750. else if (layer->type == "Interp")
  751. {
  752. ncnn::Interp* op = (ncnn::Interp*)layer;
  753. ncnn::Interp* op_default = (ncnn::Interp*)layer_default;
  754. fprintf_param_value(" 0=%d", resize_type)
  755. fprintf_param_value(" 1=%f", height_scale)
  756. fprintf_param_value(" 2=%f", width_scale)
  757. fprintf_param_value(" 3=%d", output_height)
  758. fprintf_param_value(" 4=%d", output_width)
  759. }
  760. else if (layer->type == "Log")
  761. {
  762. ncnn::Log* op = (ncnn::Log*)layer;
  763. ncnn::Log* op_default = (ncnn::Log*)layer_default;
  764. fprintf_param_value(" 0=%f", base)
  765. fprintf_param_value(" 1=%f", scale)
  766. fprintf_param_value(" 2=%f", shift)
  767. }
  768. else if (layer->type == "LRN")
  769. {
  770. ncnn::LRN* op = (ncnn::LRN*)layer;
  771. ncnn::LRN* op_default = (ncnn::LRN*)layer_default;
  772. fprintf_param_value(" 0=%d", region_type)
  773. fprintf_param_value(" 1=%d", local_size)
  774. fprintf_param_value(" 2=%f", alpha)
  775. fprintf_param_value(" 3=%f", beta)
  776. fprintf_param_value(" 4=%f", bias)
  777. }
  778. else if (layer->type == "LSTM")
  779. {
  780. ncnn::LSTM* op = (ncnn::LSTM*)layer;
  781. ncnn::LSTM* op_default = (ncnn::LSTM*)layer_default;
  782. fprintf_param_value(" 0=%d", num_output)
  783. fprintf_param_value(" 1=%d", weight_data_size)
  784. fprintf_param_value(" 2=%d", direction)
  785. fwrite_weight_tag_data(0, op->weight_xc_data, bp);
  786. fwrite_weight_tag_data(0, op->bias_c_data, bp);
  787. fwrite_weight_tag_data(0, op->weight_hc_data, bp);
  788. }
  789. else if (layer->type == "MemoryData")
  790. {
  791. ncnn::MemoryData* op = (ncnn::MemoryData*)layer;
  792. ncnn::MemoryData* op_default = (ncnn::MemoryData*)layer_default;
  793. fprintf_param_value(" 0=%d", w)
  794. fprintf_param_value(" 1=%d", h)
  795. fprintf_param_value(" 2=%d", c)
  796. fwrite_weight_data(op->data, bp);
  797. }
  798. else if (layer->type == "MVN")
  799. {
  800. ncnn::MVN* op = (ncnn::MVN*)layer;
  801. ncnn::MVN* op_default = (ncnn::MVN*)layer_default;
  802. fprintf_param_value(" 0=%d", normalize_variance)
  803. fprintf_param_value(" 1=%d", across_channels)
  804. fprintf_param_value(" 2=%f", eps)
  805. }
  806. else if (layer->type == "Normalize")
  807. {
  808. ncnn::Normalize* op = (ncnn::Normalize*)layer;
  809. ncnn::Normalize* op_default = (ncnn::Normalize*)layer_default;
  810. fprintf_param_value(" 0=%d", across_spatial)
  811. fprintf_param_value(" 1=%d", channel_shared)
  812. fprintf_param_value(" 2=%f", eps)
  813. fprintf_param_value(" 3=%d", scale_data_size)
  814. fprintf_param_value(" 4=%d", across_channel)
  815. fwrite_weight_data(op->scale_data, bp);
  816. }
  817. else if (layer->type == "Padding")
  818. {
  819. ncnn::Padding* op = (ncnn::Padding*)layer;
  820. ncnn::Padding* op_default = (ncnn::Padding*)layer_default;
  821. fprintf_param_value(" 0=%d", top)
  822. fprintf_param_value(" 1=%d", bottom)
  823. fprintf_param_value(" 2=%d", left)
  824. fprintf_param_value(" 3=%d", right)
  825. fprintf_param_value(" 4=%d", type)
  826. fprintf_param_value(" 5=%f", value)
  827. }
  828. else if (layer->type == "Permute")
  829. {
  830. ncnn::Permute* op = (ncnn::Permute*)layer;
  831. ncnn::Permute* op_default = (ncnn::Permute*)layer_default;
  832. fprintf_param_value(" 0=%d", order_type)
  833. }
  834. else if (layer->type == "PixelShuffle")
  835. {
  836. ncnn::PixelShuffle* op = (ncnn::PixelShuffle*)layer;
  837. ncnn::PixelShuffle* op_default = (ncnn::PixelShuffle*)layer_default;
  838. fprintf_param_value(" 0=%d", upscale_factor)
  839. }
  840. else if (layer->type == "Pooling")
  841. {
  842. ncnn::Pooling* op = (ncnn::Pooling*)layer;
  843. ncnn::Pooling* op_default = (ncnn::Pooling*)layer_default;
  844. fprintf_param_value(" 0=%d", pooling_type)
  845. fprintf_param_value(" 1=%d", kernel_w)
  846. {
  847. if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h);
  848. }
  849. fprintf_param_value(" 2=%d", stride_w)
  850. {
  851. if (op->stride_h != op->stride_w) fprintf(pp, " 12=%d", op->stride_h);
  852. }
  853. fprintf_param_value(" 3=%d", pad_left)
  854. {
  855. if (op->pad_top != op->pad_left) fprintf(pp, " 13=%d", op->pad_top);
  856. }
  857. {
  858. if (op->pad_right != op->pad_left) fprintf(pp, " 14=%d", op->pad_right);
  859. }
  860. {
  861. if (op->pad_bottom != op->pad_top) fprintf(pp, " 15=%d", op->pad_bottom);
  862. }
  863. fprintf_param_value(" 4=%d", global_pooling)
  864. fprintf_param_value(" 5=%d", pad_mode)
  865. fprintf_param_value(" 6=%d", avgpool_count_include_pad)
  866. fprintf_param_value(" 7=%d", adaptive_pooling)
  867. fprintf_param_value(" 8=%d", out_w)
  868. {
  869. if (op->out_h != op->out_w) fprintf(pp, " 18=%d", op->out_h);
  870. }
  871. }
  872. else if (layer->type == "Power")
  873. {
  874. ncnn::Power* op = (ncnn::Power*)layer;
  875. ncnn::Power* op_default = (ncnn::Power*)layer_default;
  876. fprintf_param_value(" 0=%f", power)
  877. fprintf_param_value(" 1=%f", scale)
  878. fprintf_param_value(" 2=%f", shift)
  879. }
  880. else if (layer->type == "PReLU")
  881. {
  882. ncnn::PReLU* op = (ncnn::PReLU*)layer;
  883. ncnn::PReLU* op_default = (ncnn::PReLU*)layer_default;
  884. fprintf_param_value(" 0=%d", num_slope)
  885. fwrite_weight_data(op->slope_data, bp);
  886. }
  887. else if (layer->type == "PriorBox")
  888. {
  889. ncnn::PriorBox* op = (ncnn::PriorBox*)layer;
  890. ncnn::PriorBox* op_default = (ncnn::PriorBox*)layer_default;
  891. {
  892. if (!op->min_sizes.empty()) fprintf_param_float_array(0, op->min_sizes, pp);
  893. }
  894. {
  895. if (!op->max_sizes.empty()) fprintf_param_float_array(1, op->max_sizes, pp);
  896. }
  897. {
  898. if (!op->aspect_ratios.empty()) fprintf_param_float_array(2, op->aspect_ratios, pp);
  899. }
  900. fprintf_param_value(" 3=%f", variances[0])
  901. fprintf_param_value(" 4=%f", variances[1])
  902. fprintf_param_value(" 5=%f", variances[2])
  903. fprintf_param_value(" 6=%f", variances[3])
  904. fprintf_param_value(" 7=%d", flip)
  905. fprintf_param_value(" 8=%d", clip)
  906. fprintf_param_value(" 9=%d", image_width)
  907. fprintf_param_value(" 10=%d", image_height)
  908. fprintf_param_value(" 11=%f", step_width)
  909. fprintf_param_value(" 12=%f", step_height)
  910. fprintf_param_value(" 13=%f", offset)
  911. }
  912. else if (layer->type == "Proposal")
  913. {
  914. ncnn::Proposal* op = (ncnn::Proposal*)layer;
  915. ncnn::Proposal* op_default = (ncnn::Proposal*)layer_default;
  916. fprintf_param_value(" 0=%d", feat_stride)
  917. fprintf_param_value(" 1=%d", base_size)
  918. fprintf_param_value(" 2=%d", pre_nms_topN)
  919. fprintf_param_value(" 3=%d", after_nms_topN)
  920. fprintf_param_value(" 4=%f", nms_thresh)
  921. fprintf_param_value(" 5=%d", min_size)
  922. }
  923. else if (layer->type == "PSROIPooling")
  924. {
  925. ncnn::PSROIPooling* op = (ncnn::PSROIPooling*)layer;
  926. ncnn::PSROIPooling* op_default = (ncnn::PSROIPooling*)layer_default;
  927. fprintf_param_value(" 0=%d", pooled_width)
  928. fprintf_param_value(" 1=%d", pooled_height)
  929. fprintf_param_value(" 2=%f", spatial_scale)
  930. fprintf_param_value(" 3=%d", output_dim)
  931. }
  932. else if (layer->type == "Quantize")
  933. {
  934. ncnn::Quantize* op = (ncnn::Quantize*)layer;
  935. ncnn::Quantize* op_default = (ncnn::Quantize*)layer_default;
  936. fprintf_param_value(" 0=%f", scale)
  937. }
  938. else if (layer->type == "Reduction")
  939. {
  940. ncnn::Reduction* op = (ncnn::Reduction*)layer;
  941. ncnn::Reduction* op_default = (ncnn::Reduction*)layer_default;
  942. fprintf_param_value(" 0=%d", operation)
  943. fprintf_param_value(" 1=%d", reduce_all)
  944. fprintf_param_value(" 2=%f", coeff)
  945. {
  946. if (!op->axes.empty()) fprintf_param_int_array(3, op->axes, pp);
  947. }
  948. fprintf_param_value(" 4=%d", keepdims)
  949. }
  950. else if (layer->type == "ReLU")
  951. {
  952. ncnn::ReLU* op = (ncnn::ReLU*)layer;
  953. ncnn::ReLU* op_default = (ncnn::ReLU*)layer_default;
  954. fprintf_param_value(" 0=%f", slope)
  955. }
  956. else if (layer->type == "Reorg")
  957. {
  958. ncnn::Reorg* op = (ncnn::Reorg*)layer;
  959. ncnn::Reorg* op_default = (ncnn::Reorg*)layer_default;
  960. fprintf_param_value(" 0=%d", stride)
  961. }
  962. else if (layer->type == "Requantize")
  963. {
  964. ncnn::Requantize* op = (ncnn::Requantize*)layer;
  965. ncnn::Requantize* op_default = (ncnn::Requantize*)layer_default;
  966. fprintf_param_value(" 0=%f", scale_in)
  967. fprintf_param_value(" 1=%f", scale_out)
  968. fprintf_param_value(" 2=%d", bias_term)
  969. fprintf_param_value(" 3=%d", bias_data_size)
  970. fprintf_param_value(" 4=%d", fusion_relu)
  971. }
  972. else if (layer->type == "Reshape")
  973. {
  974. ncnn::Reshape* op = (ncnn::Reshape*)layer;
  975. ncnn::Reshape* op_default = (ncnn::Reshape*)layer_default;
  976. fprintf_param_value(" 0=%d", w)
  977. fprintf_param_value(" 1=%d", h)
  978. fprintf_param_value(" 2=%d", c)
  979. fprintf_param_value(" 3=%d", permute)
  980. }
  981. else if (layer->type == "ROIAlign")
  982. {
  983. ncnn::ROIAlign* op = (ncnn::ROIAlign*)layer;
  984. ncnn::ROIAlign* op_default = (ncnn::ROIAlign*)layer_default;
  985. fprintf_param_value(" 0=%d", pooled_width)
  986. fprintf_param_value(" 1=%d", pooled_height)
  987. fprintf_param_value(" 2=%f", spatial_scale)
  988. fprintf_param_value(" 3=%d", sampling_ratio)
  989. fprintf_param_value(" 4=%d", aligned)
  990. fprintf_param_value(" 5=%d", version)
  991. }
  992. else if (layer->type == "ROIPooling")
  993. {
  994. ncnn::ROIPooling* op = (ncnn::ROIPooling*)layer;
  995. ncnn::ROIPooling* op_default = (ncnn::ROIPooling*)layer_default;
  996. fprintf_param_value(" 0=%d", pooled_width)
  997. fprintf_param_value(" 1=%d", pooled_height)
  998. fprintf_param_value(" 2=%f", spatial_scale)
  999. }
  1000. else if (layer->type == "Scale")
  1001. {
  1002. ncnn::Scale* op = (ncnn::Scale*)layer;
  1003. ncnn::Scale* op_default = (ncnn::Scale*)layer_default;
  1004. fprintf_param_value(" 0=%d", scale_data_size)
  1005. fprintf_param_value(" 1=%d", bias_term)
  1006. fwrite_weight_data(op->scale_data, bp);
  1007. fwrite_weight_data(op->bias_data, bp);
  1008. }
  1009. else if (layer->type == "ShuffleChannel")
  1010. {
  1011. ncnn::ShuffleChannel* op = (ncnn::ShuffleChannel*)layer;
  1012. ncnn::ShuffleChannel* op_default = (ncnn::ShuffleChannel*)layer_default;
  1013. fprintf_param_value(" 0=%d", group)
  1014. }
  1015. else if (layer->type == "Slice")
  1016. {
  1017. ncnn::Slice* op = (ncnn::Slice*)layer;
  1018. ncnn::Slice* op_default = (ncnn::Slice*)layer_default;
  1019. {
  1020. if (!op->slices.empty()) fprintf_param_int_array(0, op->slices, pp);
  1021. }
  1022. fprintf_param_value(" 1=%d", axis)
  1023. }
  1024. else if (layer->type == "Softmax")
  1025. {
  1026. ncnn::Softmax* op = (ncnn::Softmax*)layer;
  1027. ncnn::Softmax* op_default = (ncnn::Softmax*)layer_default;
  1028. fprintf_param_value(" 0=%d", axis)
  1029. // HACK
  1030. if (op->axis != 0)
  1031. {
  1032. int fixbug0 = 1;
  1033. fprintf(pp, " 1=%d", fixbug0);
  1034. }
  1035. }
  1036. else if (layer->type == "Threshold")
  1037. {
  1038. ncnn::Threshold* op = (ncnn::Threshold*)layer;
  1039. ncnn::Threshold* op_default = (ncnn::Threshold*)layer_default;
  1040. fprintf_param_value(" 0=%f", threshold)
  1041. }
  1042. else if (layer->type == "UnaryOp")
  1043. {
  1044. ncnn::UnaryOp* op = (ncnn::UnaryOp*)layer;
  1045. ncnn::UnaryOp* op_default = (ncnn::UnaryOp*)layer_default;
  1046. fprintf_param_value(" 0=%d", op_type)
  1047. }
  1048. else if (layer->type == "YoloDetectionOutput")
  1049. {
  1050. ncnn::YoloDetectionOutput* op = (ncnn::YoloDetectionOutput*)layer;
  1051. ncnn::YoloDetectionOutput* op_default = (ncnn::YoloDetectionOutput*)layer_default;
  1052. fprintf_param_value(" 0=%d", num_class)
  1053. fprintf_param_value(" 1=%d", num_box)
  1054. fprintf_param_value(" 2=%f", confidence_threshold)
  1055. fprintf_param_value(" 3=%f", nms_threshold)
  1056. {
  1057. if (!op->biases.empty()) fprintf_param_float_array(4, op->biases, pp);
  1058. }
  1059. }
  1060. else if (layer->type == "Yolov3DetectionOutput")
  1061. {
  1062. ncnn::Yolov3DetectionOutput* op = (ncnn::Yolov3DetectionOutput*)layer;
  1063. ncnn::Yolov3DetectionOutput* op_default = (ncnn::Yolov3DetectionOutput*)layer_default;
  1064. fprintf_param_value(" 0=%d", num_class)
  1065. fprintf_param_value(" 1=%d", num_box)
  1066. fprintf_param_value(" 2=%f", confidence_threshold)
  1067. fprintf_param_value(" 3=%f", nms_threshold)
  1068. {
  1069. if (!op->biases.empty()) fprintf_param_float_array(4, op->biases, pp);
  1070. }
  1071. {
  1072. if (!op->mask.empty()) fprintf_param_int_array(5, op->mask, pp);
  1073. }
  1074. {
  1075. if (!op->anchors_scale.empty()) fprintf_param_float_array(6, op->anchors_scale, pp);
  1076. }
  1077. }
  1078. #undef fprintf_param_value
  1079. fprintf(pp, "\n");
  1080. delete layer_default;
  1081. }
  1082. fclose(pp);
  1083. fclose(bp);
  1084. return 0;
  1085. }
  1086. int main(int argc, char** argv)
  1087. {
  1088. if (argc != 6)
  1089. {
  1090. fprintf(stderr, "usage: %s [inparam] [inbin] [outparam] [outbin] [calibration table]\n", argv[0]);
  1091. return -1;
  1092. }
  1093. const char* inparam = argv[1];
  1094. const char* inbin = argv[2];
  1095. const char* outparam = argv[3];
  1096. const char* outbin = argv[4];
  1097. const char* int8scale_table_path = argv[5];
  1098. NetQuantize quantizer;
  1099. // parse the calibration scale table
  1100. if (int8scale_table_path)
  1101. {
  1102. bool s2 = read_int8scale_table(int8scale_table_path, quantizer.blob_int8scale_table, quantizer.weight_int8scale_table);
  1103. if (!s2)
  1104. {
  1105. fprintf(stderr, "read_int8scale_table failed\n");
  1106. return -1;
  1107. }
  1108. }
  1109. quantizer.load_param(inparam);
  1110. quantizer.load_model(inbin);
  1111. quantizer.quantize_convolution();
  1112. quantizer.quantize_convolutiondepthwise();
  1113. quantizer.quantize_innerproduct();
  1114. quantizer.save(outparam, outbin);
  1115. return 0;
  1116. }