You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ncnn2int8.cpp 44 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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. { if (!op->axes.empty()) fprintf_param_int_array(3, op->axes, pp); }
  835. fprintf_param_value(" 4=%d", keepdims)
  836. }
  837. else if (layer->type == "ReLU")
  838. {
  839. ncnn::ReLU* op = (ncnn::ReLU*)layer;
  840. ncnn::ReLU* op_default = (ncnn::ReLU*)layer_default;
  841. fprintf_param_value(" 0=%f", slope)
  842. }
  843. else if (layer->type == "Reorg")
  844. {
  845. ncnn::Reorg* op = (ncnn::Reorg*)layer;
  846. ncnn::Reorg* op_default = (ncnn::Reorg*)layer_default;
  847. fprintf_param_value(" 0=%d", stride)
  848. }
  849. else if (layer->type == "Requantize")
  850. {
  851. ncnn::Requantize* op = (ncnn::Requantize*)layer;
  852. ncnn::Requantize* op_default = (ncnn::Requantize*)layer_default;
  853. fprintf_param_value(" 0=%f", scale_in)
  854. fprintf_param_value(" 1=%f", scale_out)
  855. fprintf_param_value(" 2=%d", bias_term)
  856. fprintf_param_value(" 3=%d", bias_data_size)
  857. fprintf_param_value(" 4=%d", fusion_relu)
  858. }
  859. else if (layer->type == "Reshape")
  860. {
  861. ncnn::Reshape* op = (ncnn::Reshape*)layer;
  862. ncnn::Reshape* op_default = (ncnn::Reshape*)layer_default;
  863. fprintf_param_value(" 0=%d", w)
  864. fprintf_param_value(" 1=%d", h)
  865. fprintf_param_value(" 2=%d", c)
  866. fprintf_param_value(" 3=%d", permute)
  867. }
  868. else if (layer->type == "ROIAlign")
  869. {
  870. ncnn::ROIAlign* op = (ncnn::ROIAlign*)layer;
  871. ncnn::ROIAlign* op_default = (ncnn::ROIAlign*)layer_default;
  872. fprintf_param_value(" 0=%d", pooled_width)
  873. fprintf_param_value(" 1=%d", pooled_height)
  874. fprintf_param_value(" 2=%f", spatial_scale)
  875. }
  876. else if (layer->type == "ROIPooling")
  877. {
  878. ncnn::ROIPooling* op = (ncnn::ROIPooling*)layer;
  879. ncnn::ROIPooling* op_default = (ncnn::ROIPooling*)layer_default;
  880. fprintf_param_value(" 0=%d", pooled_width)
  881. fprintf_param_value(" 1=%d", pooled_height)
  882. fprintf_param_value(" 2=%f", spatial_scale)
  883. }
  884. else if (layer->type == "Scale")
  885. {
  886. ncnn::Scale* op = (ncnn::Scale*)layer;
  887. ncnn::Scale* op_default = (ncnn::Scale*)layer_default;
  888. fprintf_param_value(" 0=%d", scale_data_size)
  889. fprintf_param_value(" 1=%d", bias_term)
  890. fwrite_weight_data(op->scale_data, bp);
  891. fwrite_weight_data(op->bias_data, bp);
  892. }
  893. else if (layer->type == "ShuffleChannel")
  894. {
  895. ncnn::ShuffleChannel* op = (ncnn::ShuffleChannel*)layer;
  896. ncnn::ShuffleChannel* op_default = (ncnn::ShuffleChannel*)layer_default;
  897. fprintf_param_value(" 0=%d", group)
  898. }
  899. else if (layer->type == "Slice")
  900. {
  901. ncnn::Slice* op = (ncnn::Slice*)layer;
  902. ncnn::Slice* op_default = (ncnn::Slice*)layer_default;
  903. { if (!op->slices.empty()) fprintf_param_int_array(0, op->slices, pp); }
  904. fprintf_param_value(" 1=%d", axis)
  905. }
  906. else if (layer->type == "Softmax")
  907. {
  908. ncnn::Softmax* op = (ncnn::Softmax*)layer;
  909. ncnn::Softmax* op_default = (ncnn::Softmax*)layer_default;
  910. fprintf_param_value(" 0=%d", axis)
  911. // HACK
  912. if (op->axis != 0)
  913. {
  914. int fixbug0 = 1;
  915. fprintf(pp, " 1=%d", fixbug0);
  916. }
  917. }
  918. else if (layer->type == "Threshold")
  919. {
  920. ncnn::Threshold* op = (ncnn::Threshold*)layer;
  921. ncnn::Threshold* op_default = (ncnn::Threshold*)layer_default;
  922. fprintf_param_value(" 0=%f", threshold)
  923. }
  924. else if (layer->type == "UnaryOp")
  925. {
  926. ncnn::UnaryOp* op = (ncnn::UnaryOp*)layer;
  927. ncnn::UnaryOp* op_default = (ncnn::UnaryOp*)layer_default;
  928. fprintf_param_value(" 0=%d", op_type)
  929. }
  930. else if (layer->type == "YoloDetectionOutput")
  931. {
  932. ncnn::YoloDetectionOutput* op = (ncnn::YoloDetectionOutput*)layer;
  933. ncnn::YoloDetectionOutput* op_default = (ncnn::YoloDetectionOutput*)layer_default;
  934. fprintf_param_value(" 0=%d", num_class)
  935. fprintf_param_value(" 1=%d", num_box)
  936. fprintf_param_value(" 2=%f", confidence_threshold)
  937. fprintf_param_value(" 3=%f", nms_threshold)
  938. { if (!op->biases.empty()) fprintf_param_float_array(4, op->biases, pp); }
  939. }
  940. else if (layer->type == "Yolov3DetectionOutput")
  941. {
  942. ncnn::Yolov3DetectionOutput* op = (ncnn::Yolov3DetectionOutput*)layer;
  943. ncnn::Yolov3DetectionOutput* op_default = (ncnn::Yolov3DetectionOutput*)layer_default;
  944. fprintf_param_value(" 0=%d", num_class)
  945. fprintf_param_value(" 1=%d", num_box)
  946. fprintf_param_value(" 2=%f", confidence_threshold)
  947. fprintf_param_value(" 3=%f", nms_threshold)
  948. { if (!op->biases.empty()) fprintf_param_float_array(4, op->biases, pp); }
  949. { if (!op->mask.empty()) fprintf_param_int_array(5, op->mask, pp); }
  950. { if (!op->anchors_scale.empty()) fprintf_param_float_array(6, op->anchors_scale, pp); }
  951. }
  952. #undef fprintf_param_value
  953. fprintf(pp, "\n");
  954. delete layer_default;
  955. }
  956. fclose(pp);
  957. fclose(bp);
  958. return 0;
  959. }
  960. int main(int argc, char** argv)
  961. {
  962. if (argc != 6)
  963. {
  964. fprintf(stderr, "usage: %s [inparam] [inbin] [outparam] [outbin] [calibration table]\n", argv[0]);
  965. return -1;
  966. }
  967. const char* inparam = argv[1];
  968. const char* inbin = argv[2];
  969. const char* outparam = argv[3];
  970. const char* outbin = argv[4];
  971. const char* int8scale_table_path = argv[5];
  972. NetQuantize quantizer;
  973. // parse the calibration scale table
  974. if (int8scale_table_path)
  975. {
  976. bool s2 = read_int8scale_table(int8scale_table_path, quantizer.blob_int8scale_table, quantizer.weight_int8scale_table);
  977. if (!s2)
  978. {
  979. fprintf(stderr, "read_int8scale_table failed\n");
  980. return -1;
  981. }
  982. }
  983. quantizer.load_param(inparam);
  984. quantizer.load_model(inbin);
  985. quantizer.quantize_convolution();
  986. quantizer.quantize_convolutiondepthwise();
  987. quantizer.quantize_innerproduct();
  988. quantizer.save(outparam, outbin);
  989. return 0;
  990. }