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

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