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.

ncnnoptimize.cpp 82 kB

6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2019 THL A29 Limited, a Tencent company. 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 <algorithm>
  15. #include <set>
  16. #include <vector>
  17. // ncnn public header
  18. #include "datareader.h"
  19. #include "net.h"
  20. #include "layer.h"
  21. // ncnn private header
  22. #include "layer/batchnorm.h"
  23. #include "layer/bias.h"
  24. #include "layer/binaryop.h"
  25. #include "layer/clip.h"
  26. #include "layer/concat.h"
  27. #include "layer/convolution.h"
  28. #include "layer/convolutiondepthwise.h"
  29. #include "layer/crop.h"
  30. #include "layer/deconvolution.h"
  31. #include "layer/deconvolutiondepthwise.h"
  32. #include "layer/detectionoutput.h"
  33. #include "layer/dropout.h"
  34. #include "layer/eltwise.h"
  35. #include "layer/elu.h"
  36. #include "layer/exp.h"
  37. #include "layer/expanddims.h"
  38. #include "layer/flatten.h"
  39. #include "layer/hardsigmoid.h"
  40. #include "layer/hardswish.h"
  41. #include "layer/innerproduct.h"
  42. #include "layer/input.h"
  43. #include "layer/instancenorm.h"
  44. #include "layer/interp.h"
  45. #include "layer/log.h"
  46. #include "layer/lrn.h"
  47. #include "layer/memorydata.h"
  48. #include "layer/mvn.h"
  49. #include "layer/normalize.h"
  50. #include "layer/padding.h"
  51. #include "layer/permute.h"
  52. #include "layer/pooling.h"
  53. #include "layer/power.h"
  54. #include "layer/prelu.h"
  55. #include "layer/priorbox.h"
  56. #include "layer/proposal.h"
  57. #include "layer/psroipooling.h"
  58. #include "layer/quantize.h"
  59. #include "layer/reduction.h"
  60. #include "layer/relu.h"
  61. #include "layer/reorg.h"
  62. #include "layer/requantize.h"
  63. #include "layer/reshape.h"
  64. #include "layer/roialign.h"
  65. #include "layer/roipooling.h"
  66. #include "layer/scale.h"
  67. #include "layer/slice.h"
  68. #include "layer/shufflechannel.h"
  69. #include "layer/softmax.h"
  70. #include "layer/squeeze.h"
  71. #include "layer/threshold.h"
  72. #include "layer/unaryop.h"
  73. #include "layer/yolodetectionoutput.h"
  74. #include "layer/yolov3detectionoutput.h"
  75. #if defined(__aarch64__) && defined(LINUX)
  76. #include <locale>
  77. #include <chrono>
  78. #include <random>
  79. #include <limits>
  80. #include <cassert>
  81. #define TEXT_GREEN "\033[32m"
  82. #define TEXT_YELLOW "\033[33m"
  83. #define TEXT_RED "\033[31m"
  84. #define CLR "\033[0m"
  85. #endif // defined(__aarch64__) && defined(LINUX)
  86. class DataReaderFromEmpty : public ncnn::DataReader
  87. {
  88. public:
  89. virtual int scan(const char* format, void* p) const { return 0; }
  90. virtual int read(void* /*buf*/, int size) const { return size; }
  91. };
  92. class NetOptimize : public ncnn::Net
  93. {
  94. public:
  95. // 0=fp32 1=fp16
  96. int storage_type;
  97. public:
  98. int fuse_batchnorm_scale();
  99. int fuse_convolution_batchnorm();
  100. int fuse_convolutiondepthwise_batchnorm();
  101. int fuse_deconvolution_batchnorm();
  102. int fuse_deconvolutiondepthwise_batchnorm();
  103. int fuse_innerproduct_batchnorm();
  104. int fuse_innerproduct_dropout();
  105. int fuse_convolution_activation();
  106. int fuse_convolutiondepthwise_activation();
  107. int fuse_deconvolution_activation();
  108. int fuse_deconvolutiondepthwise_activation();
  109. int fuse_innerproduct_activation();
  110. int eliminate_dropout();
  111. int eliminate_noop();
  112. int eliminate_flatten_after_global_pooling();
  113. int eliminate_reshape_after_global_pooling();
  114. int eliminate_flatten_after_innerproduct();
  115. int eliminate_reshape_before_binaryop();
  116. int replace_convolution_with_innerproduct_after_global_pooling();
  117. int replace_convolution_with_innerproduct_after_innerproduct();
  118. public:
  119. int fprintf_param_int_array(int id, const ncnn::Mat& m, FILE* pp);
  120. int fprintf_param_float_array(int id, const ncnn::Mat& m, FILE* pp);
  121. int fwrite_weight_tag_data(int tag, const ncnn::Mat& data, FILE* bp);
  122. int fwrite_weight_data(const ncnn::Mat& data, FILE* bp);
  123. int save(const char* parampath, const char* binpath);
  124. #if defined(__aarch64__) && defined(LINUX)
  125. void gauss_random(ncnn::Mat &m);
  126. void find_fastest_fp32_conv(const char* name, int w, int h, int c);
  127. int support_fp32_conv_type(const ncnn::Convolution* op, const ncnn::Mat& mat, const int type);
  128. #endif
  129. };
  130. #if defined(__aarch64__) && defined(LINUX)
  131. void NetOptimize::gauss_random(ncnn::Mat &m)
  132. {
  133. std::random_device rd;
  134. std::mt19937 gen(rd());
  135. std::normal_distribution<float> d(1.0f, 1.0f);
  136. int size = m.total();
  137. for (int i = 0; i < size; ++i)
  138. {
  139. m[i] = d(gen);
  140. }
  141. }
  142. void NetOptimize::find_fastest_fp32_conv(const char* dataname, int w, int h, int c)
  143. {
  144. ncnn::PoolAllocator allocator;
  145. allocator.clear();
  146. ncnn::Option opt;
  147. // embeded system generally use single thread
  148. opt.num_threads = 1;
  149. const int layer_count = layers.size();
  150. ncnn::Extractor ex = create_extractor();
  151. ncnn::Mat input(w, h, c);
  152. if (ex.input(dataname, input) < 0)
  153. {
  154. fprintf(stderr, "set input failed, check dataname.\n");
  155. return;
  156. }
  157. const char* IMPL_NAME[6] = {"baseline", "winograd", "pointwise", "im2col", "direct", "conv3x3s2"};
  158. for (int i = 0; i < layer_count; ++i)
  159. {
  160. ncnn::Layer* layer = layers[i];
  161. if (layer->type == "Convolution")
  162. {
  163. ncnn::Convolution* op = (ncnn::Convolution*)layer;
  164. ncnn::Mat bottom_blob;
  165. ncnn::Mat top_blob;
  166. ex.extract(layer->bottoms[0], bottom_blob);
  167. ex.extract(layer->tops[0], top_blob);
  168. if (bottom_blob.empty() || top_blob.empty())
  169. {
  170. continue;
  171. }
  172. ncnn::Mat weight_blob(op->kernel_w, op->kernel_h, bottom_blob.c * top_blob.c);
  173. fprintf(stdout, TEXT_GREEN "Input [w h nc]: %d %d %d\n" CLR, bottom_blob.w, bottom_blob.h, bottom_blob.c);
  174. fprintf(stdout, TEXT_GREEN "Kernel [w h nc]: %d %d %d\n" CLR, op->kernel_w, op->kernel_h, bottom_blob.c * top_blob.c);
  175. fprintf(stdout, TEXT_GREEN "Output [w h nc]: %d %d %d\n" CLR, top_blob.w, top_blob.h, top_blob.c);
  176. // randomize input and kernel
  177. gauss_random(bottom_blob);
  178. // try every implementation
  179. double min_cost = std::numeric_limits<double>::max();
  180. int best_type = 0;
  181. // how much conv implementation type ncnn has ?
  182. for (int type = 1; type <= 5; ++type)
  183. {
  184. int support = support_fp32_conv_type(op, bottom_blob, type);
  185. if (support < 1)
  186. {
  187. // implementation type mismatch convolution configuration, skip
  188. continue;
  189. }
  190. op->impl_type = type;
  191. auto start = std::chrono::high_resolution_clock::now();
  192. const int NREPEATS = 20;
  193. op->create_pipeline(opt);
  194. for (int repeat = 0; repeat < NREPEATS; ++repeat)
  195. {
  196. op->forward(top_blob, bottom_blob, opt);
  197. }
  198. op->destroy_pipeline(opt);
  199. auto stop = std::chrono::high_resolution_clock::now();
  200. double cur_cost = std::chrono::duration<double, std::micro>(stop-start).count() / NREPEATS;
  201. fprintf(stdout, TEXT_GREEN "%s cost %0.3lfms \n" CLR, IMPL_NAME[type], cur_cost/1000);
  202. if (cur_cost < min_cost)
  203. {
  204. min_cost = cur_cost;
  205. best_type = type;
  206. }
  207. }
  208. op->impl_type = best_type;
  209. fprintf(stdout, TEXT_YELLOW "%d: %s use %s \n\n" CLR, i, layer->name.c_str(), IMPL_NAME[op->impl_type]);
  210. }
  211. }
  212. }
  213. int NetOptimize::support_fp32_conv_type(const ncnn::Convolution* op, const ncnn::Mat& bottom, const int type)
  214. {
  215. // not baseline, then k_h == k_w and s_h == s_w
  216. // no dilation conv shall be allowed
  217. if (op->kernel_w != op->kernel_h ||
  218. op->stride_w != op->stride_h ||
  219. op->dilation_w != op->dilation_h ||
  220. op->dilation_h != 1)
  221. {
  222. return -1;
  223. }
  224. // (kernel, stride) in {(1, 1), (1, 2), (2, 1), (3, 1), (3, 2), (4, 4), (5, 1), (5, 2), (7, 1), (7, 2)}
  225. const int support_table[7][4] =
  226. {
  227. {1, 1, 0, 0},
  228. {1, 0, 0, 0},
  229. {1, 1, 0, 0},
  230. {0, 0, 0, 1},
  231. {1, 1, 0, 0},
  232. {0, 0, 0, 0},
  233. {1, 1, 0, 0}
  234. };
  235. // kernel_size x stride
  236. const int kernel = op->kernel_h,
  237. stride = op->stride_h;
  238. // if match prequisation
  239. switch(type)
  240. {
  241. case 1:
  242. // winograd
  243. if (kernel != 3 || stride != 1){
  244. return -1;
  245. }
  246. break;
  247. case 2:
  248. // pointwise
  249. // input_h == 1, input_w == 1, dilation == 1, stride == 1
  250. if (bottom.h != 1 || bottom.w != 1 || stride != 1)
  251. {
  252. return -1;
  253. }
  254. break;
  255. case 3:
  256. // im2col
  257. break;
  258. case 4:
  259. // direct conv
  260. if (support_table[kernel-1][stride-1] == 0)
  261. {
  262. return -1;
  263. }
  264. break;
  265. case 5:
  266. // conv3x3s2
  267. // kernel == 3 and stride == 2
  268. if (kernel != 3 || stride != 2)
  269. {
  270. return -1;
  271. }
  272. break;
  273. default:
  274. fprintf(stderr, TEXT_RED "unrecognize convolution impl type: %d" CLR, type);
  275. break;
  276. }
  277. return 1;
  278. }
  279. #endif // defined(__aarch64__) && defined(LINUX)
  280. int NetOptimize::fuse_batchnorm_scale()
  281. {
  282. const int layer_count = layers.size();
  283. for (int i=0; i<layer_count; i++)
  284. {
  285. if (layers[i]->type != "BatchNorm")
  286. continue;
  287. // BatchNorm - Scale
  288. int top_blob_index = layers[i]->tops[0];
  289. int j = i + 1;
  290. for (; j<layer_count; j++)
  291. {
  292. if (layers[j]->type != "Scale")
  293. continue;
  294. if (layers[j]->bottoms.size() != 1)
  295. continue;
  296. if (layers[j]->bottoms[0] == top_blob_index)
  297. break;
  298. }
  299. if (j == layer_count)
  300. continue;
  301. // fuse BatchNorm - Scale to BatchNorm
  302. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[i];
  303. ncnn::Scale* scale = (ncnn::Scale*)layers[j];
  304. fprintf(stderr, "fuse_batchnorm_scale %s %s\n", batchnorm->name.c_str(), scale->name.c_str());
  305. {
  306. // v = ((v - mean) / sqrt(var + eps) * slope + bias) * s + b
  307. // = (v - mean) / sqrt(var + eps) * (slope * s) + (bias * s + b)
  308. int channels = batchnorm->channels;
  309. float* slope = batchnorm->slope_data;
  310. float* bias = batchnorm->bias_data;
  311. for (int q=0; q<channels; q++)
  312. {
  313. slope[q] = slope[q] * scale->scale_data[q];
  314. if (scale->bias_term)
  315. bias[q] = bias[q] * scale->scale_data[q] + scale->bias_data[q];
  316. else
  317. bias[q] = bias[q] * scale->scale_data[q];
  318. }
  319. }
  320. int top_blob_index_final = scale->tops[0];
  321. batchnorm->tops[0] = top_blob_index_final;
  322. blobs[top_blob_index_final].producer = i;
  323. scale->type = "ncnnfused";
  324. }
  325. return 0;
  326. }
  327. int NetOptimize::fuse_convolution_batchnorm()
  328. {
  329. const int layer_count = layers.size();
  330. for (int i=0; i<layer_count; i++)
  331. {
  332. if (layers[i]->type != "Convolution")
  333. continue;
  334. // Convolution - BatchNorm
  335. int top_blob_index = layers[i]->tops[0];
  336. int j = i + 1;
  337. for (; j<layer_count; j++)
  338. {
  339. if (layers[j]->type != "BatchNorm")
  340. continue;
  341. if (layers[j]->bottoms.size() != 1)
  342. continue;
  343. if (layers[j]->bottoms[0] == top_blob_index)
  344. break;
  345. }
  346. if (j == layer_count)
  347. continue;
  348. // fuse Convolution - BatchNorm to Convolution
  349. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[i];
  350. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[j];
  351. fprintf(stderr, "fuse_convolution_batchnorm %s %s\n", convolution->name.c_str(), batchnorm->name.c_str());
  352. {
  353. int channels = batchnorm->channels;
  354. float eps = batchnorm->eps;
  355. // a = bias - slope * mean / sqrt(var + eps)
  356. // b = slope / sqrt(var + eps)
  357. // value = value * b + a
  358. std::vector<float> a(channels);
  359. std::vector<float> b(channels);
  360. for (int i=0; i<channels; i++)
  361. {
  362. float sqrt_var = sqrt(batchnorm->var_data[i] + eps);
  363. a[i] = batchnorm->bias_data[i] - batchnorm->slope_data[i] * batchnorm->mean_data[i] / sqrt_var;
  364. b[i] = batchnorm->slope_data[i] / sqrt_var;
  365. }
  366. if (convolution->bias_term == 0)
  367. {
  368. // init bias as zero
  369. convolution->bias_term = 1;
  370. convolution->bias_data = ncnn::Mat(channels);
  371. convolution->bias_data.fill(0.f);
  372. }
  373. const int weight_per_outch = convolution->weight_data_size / channels;
  374. float* weight = convolution->weight_data;
  375. float* bias = convolution->bias_data;
  376. for (int i=0; i<channels; i++)
  377. {
  378. float* conv_weight_outch = weight + weight_per_outch * i;
  379. for (int j=0; j<weight_per_outch; j++)
  380. {
  381. conv_weight_outch[j] *= b[i];
  382. }
  383. bias[i] = bias[i] * b[i] + a[i];
  384. }
  385. }
  386. int top_blob_index_final = batchnorm->tops[0];
  387. convolution->tops[0] = top_blob_index_final;
  388. blobs[top_blob_index_final].producer = i;
  389. batchnorm->type = "ncnnfused";
  390. }
  391. return 0;
  392. }
  393. int NetOptimize::fuse_convolutiondepthwise_batchnorm()
  394. {
  395. const int layer_count = layers.size();
  396. for (int i=0; i<layer_count; i++)
  397. {
  398. if (layers[i]->type != "ConvolutionDepthWise")
  399. continue;
  400. // ConvolutionDepthWise - BatchNorm
  401. int top_blob_index = layers[i]->tops[0];
  402. int j = i + 1;
  403. for (; j<layer_count; j++)
  404. {
  405. if (layers[j]->type != "BatchNorm")
  406. continue;
  407. if (layers[j]->bottoms.size() != 1)
  408. continue;
  409. if (layers[j]->bottoms[0] == top_blob_index)
  410. break;
  411. }
  412. if (j == layer_count)
  413. continue;
  414. // fuse ConvolutionDepthWise - BatchNorm to ConvolutionDepthWise
  415. ncnn::ConvolutionDepthWise* convolutiondepthwise = (ncnn::ConvolutionDepthWise*)layers[i];
  416. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[j];
  417. fprintf(stderr, "fuse_convolutiondepthwise_batchnorm %s %s\n", convolutiondepthwise->name.c_str(), batchnorm->name.c_str());
  418. {
  419. int channels = batchnorm->channels;
  420. float eps = batchnorm->eps;
  421. // a = bias - slope * mean / sqrt(var + eps)
  422. // b = slope / sqrt(var + eps)
  423. // value = value * b + a
  424. std::vector<float> a(channels);
  425. std::vector<float> b(channels);
  426. for (int i=0; i<channels; i++)
  427. {
  428. float sqrt_var = sqrt(batchnorm->var_data[i] + eps);
  429. a[i] = batchnorm->bias_data[i] - batchnorm->slope_data[i] * batchnorm->mean_data[i] / sqrt_var;
  430. b[i] = batchnorm->slope_data[i] / sqrt_var;
  431. }
  432. if (convolutiondepthwise->bias_term == 0)
  433. {
  434. // init bias as zero
  435. convolutiondepthwise->bias_term = 1;
  436. convolutiondepthwise->bias_data = ncnn::Mat(channels);
  437. convolutiondepthwise->bias_data.fill(0.f);
  438. }
  439. const int weight_per_outch = convolutiondepthwise->weight_data_size / channels;
  440. float* weight = convolutiondepthwise->weight_data;
  441. float* bias = convolutiondepthwise->bias_data;
  442. for (int i=0; i<channels; i++)
  443. {
  444. float* conv_weight_outch = weight + weight_per_outch * i;
  445. for (int j=0; j<weight_per_outch; j++)
  446. {
  447. conv_weight_outch[j] *= b[i];
  448. }
  449. bias[i] = bias[i] * b[i] + a[i];
  450. }
  451. }
  452. int top_blob_index_final = batchnorm->tops[0];
  453. convolutiondepthwise->tops[0] = top_blob_index_final;
  454. blobs[top_blob_index_final].producer = i;
  455. batchnorm->type = "ncnnfused";
  456. }
  457. return 0;
  458. }
  459. int NetOptimize::fuse_deconvolution_batchnorm()
  460. {
  461. const int layer_count = layers.size();
  462. for (int i=0; i<layer_count; i++)
  463. {
  464. if (layers[i]->type != "Deconvolution")
  465. continue;
  466. // Deconvolution - BatchNorm
  467. int top_blob_index = layers[i]->tops[0];
  468. int j = i + 1;
  469. for (; j<layer_count; j++)
  470. {
  471. if (layers[j]->type != "BatchNorm")
  472. continue;
  473. if (layers[j]->bottoms.size() != 1)
  474. continue;
  475. if (layers[j]->bottoms[0] == top_blob_index)
  476. break;
  477. }
  478. if (j == layer_count)
  479. continue;
  480. // fuse Deconvolution - BatchNorm to Deconvolution
  481. ncnn::Deconvolution* deconvolution = (ncnn::Deconvolution*)layers[i];
  482. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[j];
  483. fprintf(stderr, "fuse_deconvolution_batchnorm %s %s\n", deconvolution->name.c_str(), batchnorm->name.c_str());
  484. {
  485. int channels = batchnorm->channels;
  486. float eps = batchnorm->eps;
  487. // a = bias - slope * mean / sqrt(var + eps)
  488. // b = slope / sqrt(var + eps)
  489. // value = value * b + a
  490. std::vector<float> a(channels);
  491. std::vector<float> b(channels);
  492. for (int i=0; i<channels; i++)
  493. {
  494. float sqrt_var = sqrt(batchnorm->var_data[i] + eps);
  495. a[i] = batchnorm->bias_data[i] - batchnorm->slope_data[i] * batchnorm->mean_data[i] / sqrt_var;
  496. b[i] = batchnorm->slope_data[i] / sqrt_var;
  497. }
  498. if (deconvolution->bias_term == 0)
  499. {
  500. // init bias as zero
  501. deconvolution->bias_term = 1;
  502. deconvolution->bias_data = ncnn::Mat(channels);
  503. deconvolution->bias_data.fill(0.f);
  504. }
  505. const int weight_per_outch = deconvolution->weight_data_size / channels;
  506. float* weight = deconvolution->weight_data;
  507. float* bias = deconvolution->bias_data;
  508. for (int i=0; i<channels; i++)
  509. {
  510. float* conv_weight_outch = weight + weight_per_outch * i;
  511. for (int j=0; j<weight_per_outch; j++)
  512. {
  513. conv_weight_outch[j] *= b[i];
  514. }
  515. bias[i] = bias[i] * b[i] + a[i];
  516. }
  517. }
  518. int top_blob_index_final = batchnorm->tops[0];
  519. deconvolution->tops[0] = top_blob_index_final;
  520. blobs[top_blob_index_final].producer = i;
  521. batchnorm->type = "ncnnfused";
  522. }
  523. return 0;
  524. }
  525. int NetOptimize::fuse_deconvolutiondepthwise_batchnorm()
  526. {
  527. const int layer_count = layers.size();
  528. for (int i=0; i<layer_count; i++)
  529. {
  530. if (layers[i]->type != "DeconvolutionDepthWise")
  531. continue;
  532. // DeconvolutionDepthWise - BatchNorm
  533. int top_blob_index = layers[i]->tops[0];
  534. int j = i + 1;
  535. for (; j<layer_count; j++)
  536. {
  537. if (layers[j]->type != "BatchNorm")
  538. continue;
  539. if (layers[j]->bottoms.size() != 1)
  540. continue;
  541. if (layers[j]->bottoms[0] == top_blob_index)
  542. break;
  543. }
  544. if (j == layer_count)
  545. continue;
  546. // fuse DeconvolutionDepthWise - BatchNorm to DeconvolutionDepthWise
  547. ncnn::DeconvolutionDepthWise* deconvolutiondepthwise = (ncnn::DeconvolutionDepthWise*)layers[i];
  548. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[j];
  549. fprintf(stderr, "fuse_deconvolutiondepthwise_batchnorm %s %s\n", deconvolutiondepthwise->name.c_str(), batchnorm->name.c_str());
  550. {
  551. int channels = batchnorm->channels;
  552. float eps = batchnorm->eps;
  553. // a = bias - slope * mean / sqrt(var + eps)
  554. // b = slope / sqrt(var + eps)
  555. // value = value * b + a
  556. std::vector<float> a(channels);
  557. std::vector<float> b(channels);
  558. for (int i=0; i<channels; i++)
  559. {
  560. float sqrt_var = sqrt(batchnorm->var_data[i] + eps);
  561. a[i] = batchnorm->bias_data[i] - batchnorm->slope_data[i] * batchnorm->mean_data[i] / sqrt_var;
  562. b[i] = batchnorm->slope_data[i] / sqrt_var;
  563. }
  564. if (deconvolutiondepthwise->bias_term == 0)
  565. {
  566. // init bias as zero
  567. deconvolutiondepthwise->bias_term = 1;
  568. deconvolutiondepthwise->bias_data = ncnn::Mat(channels);
  569. deconvolutiondepthwise->bias_data.fill(0.f);
  570. }
  571. const int weight_per_outch = deconvolutiondepthwise->weight_data_size / channels;
  572. float* weight = deconvolutiondepthwise->weight_data;
  573. float* bias = deconvolutiondepthwise->bias_data;
  574. for (int i=0; i<channels; i++)
  575. {
  576. float* conv_weight_outch = weight + weight_per_outch * i;
  577. for (int j=0; j<weight_per_outch; j++)
  578. {
  579. conv_weight_outch[j] *= b[i];
  580. }
  581. bias[i] = bias[i] * b[i] + a[i];
  582. }
  583. }
  584. int top_blob_index_final = batchnorm->tops[0];
  585. deconvolutiondepthwise->tops[0] = top_blob_index_final;
  586. blobs[top_blob_index_final].producer = i;
  587. batchnorm->type = "ncnnfused";
  588. }
  589. return 0;
  590. }
  591. int NetOptimize::fuse_innerproduct_batchnorm()
  592. {
  593. const int layer_count = layers.size();
  594. for (int i=0; i<layer_count; i++)
  595. {
  596. if (layers[i]->type != "InnerProduct")
  597. continue;
  598. // InnerProduct - BatchNorm
  599. int top_blob_index = layers[i]->tops[0];
  600. int j = i + 1;
  601. for (; j<layer_count; j++)
  602. {
  603. if (layers[j]->type != "BatchNorm")
  604. continue;
  605. if (layers[j]->bottoms.size() != 1)
  606. continue;
  607. if (layers[j]->bottoms[0] == top_blob_index)
  608. break;
  609. }
  610. if (j == layer_count)
  611. continue;
  612. // fuse InnerProduct - BatchNorm to InnerProduct
  613. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  614. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[j];
  615. fprintf(stderr, "fuse_innerproduct_batchnorm %s %s\n", innerproduct->name.c_str(), batchnorm->name.c_str());
  616. {
  617. int channels = batchnorm->channels;
  618. float eps = batchnorm->eps;
  619. // a = bias - slope * mean / sqrt(var + eps)
  620. // b = slope / sqrt(var + eps)
  621. // value = value * b + a
  622. std::vector<float> a(channels);
  623. std::vector<float> b(channels);
  624. for (int i=0; i<channels; i++)
  625. {
  626. float sqrt_var = sqrt(batchnorm->var_data[i] + eps);
  627. a[i] = batchnorm->bias_data[i] - batchnorm->slope_data[i] * batchnorm->mean_data[i] / sqrt_var;
  628. b[i] = batchnorm->slope_data[i] / sqrt_var;
  629. }
  630. if (innerproduct->bias_term == 0)
  631. {
  632. // init bias as zero
  633. innerproduct->bias_term = 1;
  634. innerproduct->bias_data = ncnn::Mat(channels);
  635. innerproduct->bias_data.fill(0.f);
  636. }
  637. const int weight_per_outch = innerproduct->weight_data_size / channels;
  638. float* weight = innerproduct->weight_data;
  639. float* bias = innerproduct->bias_data;
  640. for (int i=0; i<channels; i++)
  641. {
  642. float* conv_weight_outch = weight + weight_per_outch * i;
  643. for (int j=0; j<weight_per_outch; j++)
  644. {
  645. conv_weight_outch[j] *= b[i];
  646. }
  647. bias[i] = bias[i] * b[i] + a[i];
  648. }
  649. }
  650. int top_blob_index_final = batchnorm->tops[0];
  651. innerproduct->tops[0] = top_blob_index_final;
  652. blobs[top_blob_index_final].producer = i;
  653. batchnorm->type = "ncnnfused";
  654. }
  655. return 0;
  656. }
  657. int NetOptimize::fuse_innerproduct_dropout()
  658. {
  659. const int layer_count = layers.size();
  660. for (int i=0; i<layer_count; i++)
  661. {
  662. if (layers[i]->type != "InnerProduct")
  663. continue;
  664. // InnerProduct - Dropout
  665. int top_blob_index = layers[i]->tops[0];
  666. int j = i + 1;
  667. for (; j<layer_count; j++)
  668. {
  669. if (layers[j]->type != "Dropout")
  670. continue;
  671. if (layers[j]->bottoms.size() != 1)
  672. continue;
  673. if (layers[j]->bottoms[0] == top_blob_index)
  674. break;
  675. }
  676. if (j == layer_count)
  677. continue;
  678. // fuse InnerProduct - Dropout to InnerProduct
  679. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  680. ncnn::Dropout* dropout = (ncnn::Dropout*)layers[j];
  681. fprintf(stderr, "fuse_innerproduct_dropout %s %s\n", innerproduct->name.c_str(), dropout->name.c_str());
  682. float scale = dropout->scale;
  683. if (scale != 1.f)
  684. {
  685. const int num_output = innerproduct->num_output;
  686. const int weight_per_outch = innerproduct->weight_data_size / num_output;
  687. float* weight = innerproduct->weight_data;
  688. for (int i=0; i<num_output; i++)
  689. {
  690. float* conv_weight_outch = weight + weight_per_outch * i;
  691. for (int j=0; j<weight_per_outch; j++)
  692. {
  693. conv_weight_outch[j] *= scale;
  694. }
  695. }
  696. if (innerproduct->bias_term)
  697. {
  698. float* bias = innerproduct->bias_data;
  699. for (int i=0; i<num_output; i++)
  700. {
  701. bias[i] *= scale;
  702. }
  703. }
  704. }
  705. int top_blob_index_final = dropout->tops[0];
  706. innerproduct->tops[0] = top_blob_index_final;
  707. blobs[top_blob_index_final].producer = i;
  708. dropout->type = "ncnnfused";
  709. }
  710. return 0;
  711. }
  712. int NetOptimize::fuse_convolution_activation()
  713. {
  714. const int layer_count = layers.size();
  715. for (int i=0; i<layer_count; i++)
  716. {
  717. if (layers[i]->type != "Convolution")
  718. continue;
  719. // Convolution - Activation
  720. int top_blob_index = layers[i]->tops[0];
  721. int j = i + 1;
  722. for (; j<layer_count; j++)
  723. {
  724. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid")
  725. continue;
  726. if (layers[j]->bottoms.size() != 1)
  727. continue;
  728. if (layers[j]->bottoms[0] == top_blob_index)
  729. break;
  730. }
  731. if (j == layer_count)
  732. continue;
  733. // fuse Convolution - Activation to Convolution
  734. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[i];
  735. ncnn::Layer* activation = layers[j];
  736. fprintf(stderr, "fuse_convolution_activation %s %s\n", convolution->name.c_str(), activation->name.c_str());
  737. if (activation->type == "ReLU")
  738. {
  739. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  740. if (relu->slope == 0.f)
  741. {
  742. convolution->activation_type = 1;
  743. }
  744. else
  745. {
  746. convolution->activation_type = 2;
  747. convolution->activation_params = ncnn::Mat(1);
  748. convolution->activation_params[0] = relu->slope;
  749. }
  750. }
  751. else if (activation->type == "Clip")
  752. {
  753. ncnn::Clip* clip = (ncnn::Clip*)activation;
  754. convolution->activation_type = 3;
  755. convolution->activation_params = ncnn::Mat(2);
  756. convolution->activation_params[0] = clip->min;
  757. convolution->activation_params[1] = clip->max;
  758. }
  759. else if (activation->type == "Sigmoid")
  760. {
  761. convolution->activation_type = 4;
  762. }
  763. int top_blob_index_final = activation->tops[0];
  764. convolution->tops[0] = top_blob_index_final;
  765. blobs[top_blob_index_final].producer = i;
  766. activation->type = "ncnnfused";
  767. }
  768. return 0;
  769. }
  770. int NetOptimize::fuse_convolutiondepthwise_activation()
  771. {
  772. const int layer_count = layers.size();
  773. for (int i=0; i<layer_count; i++)
  774. {
  775. if (layers[i]->type != "ConvolutionDepthWise")
  776. continue;
  777. // ConvolutionDepthWise - Activation
  778. int top_blob_index = layers[i]->tops[0];
  779. int j = i + 1;
  780. for (; j<layer_count; j++)
  781. {
  782. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid")
  783. continue;
  784. if (layers[j]->bottoms.size() != 1)
  785. continue;
  786. if (layers[j]->bottoms[0] == top_blob_index)
  787. break;
  788. }
  789. if (j == layer_count)
  790. continue;
  791. // fuse ConvolutionDepthWise - Activation to ConvolutionDepthWise
  792. ncnn::ConvolutionDepthWise* convolutiondepthwise = (ncnn::ConvolutionDepthWise*)layers[i];
  793. ncnn::Layer* activation = layers[j];
  794. fprintf(stderr, "fuse_convolutiondepthwise_activation %s %s\n", convolutiondepthwise->name.c_str(), activation->name.c_str());
  795. if (activation->type == "ReLU")
  796. {
  797. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  798. if (relu->slope == 0.f)
  799. {
  800. convolutiondepthwise->activation_type = 1;
  801. }
  802. else
  803. {
  804. convolutiondepthwise->activation_type = 2;
  805. convolutiondepthwise->activation_params = ncnn::Mat(1);
  806. convolutiondepthwise->activation_params[0] = relu->slope;
  807. }
  808. }
  809. else if (activation->type == "Clip")
  810. {
  811. ncnn::Clip* clip = (ncnn::Clip*)activation;
  812. convolutiondepthwise->activation_type = 3;
  813. convolutiondepthwise->activation_params = ncnn::Mat(2);
  814. convolutiondepthwise->activation_params[0] = clip->min;
  815. convolutiondepthwise->activation_params[1] = clip->max;
  816. }
  817. else if (activation->type == "Sigmoid")
  818. {
  819. convolutiondepthwise->activation_type = 4;
  820. }
  821. int top_blob_index_final = activation->tops[0];
  822. convolutiondepthwise->tops[0] = top_blob_index_final;
  823. blobs[top_blob_index_final].producer = i;
  824. activation->type = "ncnnfused";
  825. }
  826. return 0;
  827. }
  828. int NetOptimize::fuse_deconvolution_activation()
  829. {
  830. const int layer_count = layers.size();
  831. for (int i=0; i<layer_count; i++)
  832. {
  833. if (layers[i]->type != "Deconvolution")
  834. continue;
  835. // Deconvolution - Activation
  836. int top_blob_index = layers[i]->tops[0];
  837. int j = i + 1;
  838. for (; j<layer_count; j++)
  839. {
  840. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid")
  841. continue;
  842. if (layers[j]->bottoms.size() != 1)
  843. continue;
  844. if (layers[j]->bottoms[0] == top_blob_index)
  845. break;
  846. }
  847. if (j == layer_count)
  848. continue;
  849. // fuse Deconvolution - Activation to Deconvolution
  850. ncnn::Deconvolution* deconvolution = (ncnn::Deconvolution*)layers[i];
  851. ncnn::Layer* activation = layers[j];
  852. fprintf(stderr, "fuse_deconvolution_activation %s %s\n", deconvolution->name.c_str(), activation->name.c_str());
  853. if (activation->type == "ReLU")
  854. {
  855. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  856. if (relu->slope == 0.f)
  857. {
  858. deconvolution->activation_type = 1;
  859. }
  860. else
  861. {
  862. deconvolution->activation_type = 2;
  863. deconvolution->activation_params = ncnn::Mat(1);
  864. deconvolution->activation_params[0] = relu->slope;
  865. }
  866. }
  867. else if (activation->type == "Clip")
  868. {
  869. ncnn::Clip* clip = (ncnn::Clip*)activation;
  870. deconvolution->activation_type = 3;
  871. deconvolution->activation_params = ncnn::Mat(2);
  872. deconvolution->activation_params[0] = clip->min;
  873. deconvolution->activation_params[1] = clip->max;
  874. }
  875. else if (activation->type == "Sigmoid")
  876. {
  877. deconvolution->activation_type = 4;
  878. }
  879. int top_blob_index_final = activation->tops[0];
  880. deconvolution->tops[0] = top_blob_index_final;
  881. blobs[top_blob_index_final].producer = i;
  882. activation->type = "ncnnfused";
  883. }
  884. return 0;
  885. }
  886. int NetOptimize::fuse_deconvolutiondepthwise_activation()
  887. {
  888. const int layer_count = layers.size();
  889. for (int i=0; i<layer_count; i++)
  890. {
  891. if (layers[i]->type != "DeconvolutionDepthWise")
  892. continue;
  893. // DeconvolutionDepthWise - Activation
  894. int top_blob_index = layers[i]->tops[0];
  895. int j = i + 1;
  896. for (; j<layer_count; j++)
  897. {
  898. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid")
  899. continue;
  900. if (layers[j]->bottoms.size() != 1)
  901. continue;
  902. if (layers[j]->bottoms[0] == top_blob_index)
  903. break;
  904. }
  905. if (j == layer_count)
  906. continue;
  907. // fuse DeconvolutionDepthWise - Activation to DeconvolutionDepthWise
  908. ncnn::DeconvolutionDepthWise* deconvolutiondepthwise = (ncnn::DeconvolutionDepthWise*)layers[i];
  909. ncnn::Layer* activation = layers[j];
  910. fprintf(stderr, "fuse_deconvolutiondepthwise_activation %s %s\n", deconvolutiondepthwise->name.c_str(), activation->name.c_str());
  911. if (activation->type == "ReLU")
  912. {
  913. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  914. if (relu->slope == 0.f)
  915. {
  916. deconvolutiondepthwise->activation_type = 1;
  917. }
  918. else
  919. {
  920. deconvolutiondepthwise->activation_type = 2;
  921. deconvolutiondepthwise->activation_params = ncnn::Mat(1);
  922. deconvolutiondepthwise->activation_params[0] = relu->slope;
  923. }
  924. }
  925. else if (activation->type == "Clip")
  926. {
  927. ncnn::Clip* clip = (ncnn::Clip*)activation;
  928. deconvolutiondepthwise->activation_type = 3;
  929. deconvolutiondepthwise->activation_params = ncnn::Mat(2);
  930. deconvolutiondepthwise->activation_params[0] = clip->min;
  931. deconvolutiondepthwise->activation_params[1] = clip->max;
  932. }
  933. else if (activation->type == "Sigmoid")
  934. {
  935. deconvolutiondepthwise->activation_type = 4;
  936. }
  937. int top_blob_index_final = activation->tops[0];
  938. deconvolutiondepthwise->tops[0] = top_blob_index_final;
  939. blobs[top_blob_index_final].producer = i;
  940. activation->type = "ncnnfused";
  941. }
  942. return 0;
  943. }
  944. int NetOptimize::fuse_innerproduct_activation()
  945. {
  946. const int layer_count = layers.size();
  947. for (int i=0; i<layer_count; i++)
  948. {
  949. if (layers[i]->type != "InnerProduct")
  950. continue;
  951. // InnerProduct - Activation
  952. int top_blob_index = layers[i]->tops[0];
  953. int j = i + 1;
  954. for (; j<layer_count; j++)
  955. {
  956. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid")
  957. continue;
  958. if (layers[j]->bottoms.size() != 1)
  959. continue;
  960. if (layers[j]->bottoms[0] == top_blob_index)
  961. break;
  962. }
  963. if (j == layer_count)
  964. continue;
  965. // fuse InnerProduct - Activation to InnerProduct
  966. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  967. ncnn::Layer* activation = layers[j];
  968. fprintf(stderr, "fuse_innerproduct_activation %s %s\n", innerproduct->name.c_str(), activation->name.c_str());
  969. if (activation->type == "ReLU")
  970. {
  971. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  972. if (relu->slope == 0.f)
  973. {
  974. innerproduct->activation_type = 1;
  975. }
  976. else
  977. {
  978. innerproduct->activation_type = 2;
  979. innerproduct->activation_params = ncnn::Mat(1);
  980. innerproduct->activation_params[0] = relu->slope;
  981. }
  982. }
  983. else if (activation->type == "Clip")
  984. {
  985. ncnn::Clip* clip = (ncnn::Clip*)activation;
  986. innerproduct->activation_type = 3;
  987. innerproduct->activation_params = ncnn::Mat(2);
  988. innerproduct->activation_params[0] = clip->min;
  989. innerproduct->activation_params[1] = clip->max;
  990. }
  991. else if (activation->type == "Sigmoid")
  992. {
  993. innerproduct->activation_type = 4;
  994. }
  995. int top_blob_index_final = activation->tops[0];
  996. innerproduct->tops[0] = top_blob_index_final;
  997. blobs[top_blob_index_final].producer = i;
  998. activation->type = "ncnnfused";
  999. }
  1000. return 0;
  1001. }
  1002. int NetOptimize::eliminate_dropout()
  1003. {
  1004. const int layer_count = layers.size();
  1005. for (int i=0; i<layer_count; i++)
  1006. {
  1007. if (layers[i]->type != "Dropout")
  1008. continue;
  1009. ncnn::Dropout* dropout = (ncnn::Dropout*)layers[i];
  1010. if (dropout->scale != 1.f)
  1011. continue;
  1012. // Any - Dropout
  1013. int bottom_blob_index = layers[i]->bottoms[0];
  1014. int j = i - 1;
  1015. for (; j>=0; j--)
  1016. {
  1017. if (layers[j]->type == "ncnnfused")
  1018. continue;
  1019. if (layers[j]->tops.size() != 1)
  1020. continue;
  1021. if (layers[j]->tops[0] == bottom_blob_index)
  1022. break;
  1023. }
  1024. if (j == -1)
  1025. continue;
  1026. ncnn::Layer* any = layers[j];
  1027. fprintf(stderr, "eliminate_dropout %s %s\n", any->name.c_str(), dropout->name.c_str());
  1028. int top_blob_index_final = dropout->tops[0];
  1029. any->tops[0] = top_blob_index_final;
  1030. blobs[top_blob_index_final].producer = j;
  1031. dropout->type = "ncnnfused";
  1032. }
  1033. return 0;
  1034. }
  1035. int NetOptimize::eliminate_noop()
  1036. {
  1037. const int layer_count = layers.size();
  1038. for (int i=0; i<layer_count; i++)
  1039. {
  1040. if (layers[i]->type != "Noop")
  1041. continue;
  1042. ncnn::Layer* noop = layers[i];
  1043. // Any - Noop
  1044. int bottom_blob_index = layers[i]->bottoms[0];
  1045. int j = i - 1;
  1046. for (; j>=0; j--)
  1047. {
  1048. if (layers[j]->type == "ncnnfused")
  1049. continue;
  1050. if (layers[j]->tops.size() != 1)
  1051. continue;
  1052. if (layers[j]->tops[0] == bottom_blob_index)
  1053. break;
  1054. }
  1055. if (j == -1)
  1056. continue;
  1057. ncnn::Layer* any = layers[j];
  1058. fprintf(stderr, "eliminate_noop %s %s\n", any->name.c_str(), noop->name.c_str());
  1059. int top_blob_index_final = noop->tops[0];
  1060. any->tops[0] = top_blob_index_final;
  1061. blobs[top_blob_index_final].producer = j;
  1062. noop->type = "ncnnfused";
  1063. }
  1064. return 0;
  1065. }
  1066. int NetOptimize::eliminate_reshape_after_global_pooling()
  1067. {
  1068. const int layer_count = layers.size();
  1069. for (int i=0; i<layer_count; i++)
  1070. {
  1071. if (layers[i]->type != "Pooling")
  1072. continue;
  1073. ncnn::Pooling* pooling = (ncnn::Pooling*)layers[i];
  1074. if (pooling->global_pooling == 0)
  1075. continue;
  1076. // Pooling - Reshape
  1077. int top_blob_index = layers[i]->tops[0];
  1078. int j = i + 1;
  1079. for (; j<layer_count; j++)
  1080. {
  1081. if (layers[j]->type != "Reshape")
  1082. continue;
  1083. if (layers[j]->bottoms.size() != 1)
  1084. continue;
  1085. if (layers[j]->bottoms[0] == top_blob_index)
  1086. break;
  1087. }
  1088. if (j == layer_count)
  1089. continue;
  1090. ncnn::Reshape* reshape = (ncnn::Reshape*)layers[j];
  1091. if (reshape->h != -233 || reshape->c != -233 || reshape->permute != 0)
  1092. continue;
  1093. fprintf(stderr, "eliminate_reshape_after_global_pooling %s %s\n", pooling->name.c_str(), reshape->name.c_str());
  1094. int top_blob_index_final = reshape->tops[0];
  1095. pooling->tops[0] = top_blob_index_final;
  1096. blobs[top_blob_index_final].producer = i;
  1097. reshape->type = "ncnnfused";
  1098. }
  1099. return 0;
  1100. }
  1101. int NetOptimize::eliminate_flatten_after_global_pooling()
  1102. {
  1103. const int layer_count = layers.size();
  1104. for (int i=0; i<layer_count; i++)
  1105. {
  1106. if (layers[i]->type != "Pooling")
  1107. continue;
  1108. ncnn::Pooling* pooling = (ncnn::Pooling*)layers[i];
  1109. if (pooling->global_pooling == 0)
  1110. continue;
  1111. // Pooling - Flatten
  1112. int top_blob_index = layers[i]->tops[0];
  1113. int j = i + 1;
  1114. for (; j<layer_count; j++)
  1115. {
  1116. if (layers[j]->type != "Flatten")
  1117. continue;
  1118. if (layers[j]->bottoms.size() != 1)
  1119. continue;
  1120. if (layers[j]->bottoms[0] == top_blob_index)
  1121. break;
  1122. }
  1123. if (j == layer_count)
  1124. continue;
  1125. ncnn::Flatten* flatten = (ncnn::Flatten*)layers[j];
  1126. fprintf(stderr, "eliminate_flatten_after_global_pooling %s %s\n", pooling->name.c_str(), flatten->name.c_str());
  1127. int top_blob_index_final = flatten->tops[0];
  1128. pooling->tops[0] = top_blob_index_final;
  1129. blobs[top_blob_index_final].producer = i;
  1130. flatten->type = "ncnnfused";
  1131. }
  1132. return 0;
  1133. }
  1134. int NetOptimize::eliminate_flatten_after_innerproduct()
  1135. {
  1136. const int layer_count = layers.size();
  1137. for (int i=0; i<layer_count; i++)
  1138. {
  1139. if (layers[i]->type != "InnerProduct")
  1140. continue;
  1141. // InnerProduct - Flatten
  1142. int top_blob_index = layers[i]->tops[0];
  1143. int j = i + 1;
  1144. for (; j<layer_count; j++)
  1145. {
  1146. if (layers[j]->type != "Flatten")
  1147. continue;
  1148. if (layers[j]->bottoms.size() != 1)
  1149. continue;
  1150. if (layers[j]->bottoms[0] == top_blob_index)
  1151. break;
  1152. }
  1153. if (j == layer_count)
  1154. continue;
  1155. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  1156. ncnn::Flatten* flatten = (ncnn::Flatten*)layers[j];
  1157. fprintf(stderr, "eliminate_flatten_after_innerproduct %s %s\n", innerproduct->name.c_str(), flatten->name.c_str());
  1158. int top_blob_index_final = flatten->tops[0];
  1159. innerproduct->tops[0] = top_blob_index_final;
  1160. blobs[top_blob_index_final].producer = i;
  1161. flatten->type = "ncnnfused";
  1162. }
  1163. return 0;
  1164. }
  1165. int NetOptimize::eliminate_reshape_before_binaryop()
  1166. {
  1167. const int layer_count = layers.size();
  1168. for (int i=0; i<layer_count; i++)
  1169. {
  1170. if (layers[i]->type != "Reshape")
  1171. continue;
  1172. ncnn::Reshape* reshape = (ncnn::Reshape*)layers[i];
  1173. if (reshape->w != 1 || reshape->h != 1 || reshape->permute != 0)
  1174. continue;
  1175. // Reshape - BinaryOp
  1176. int top_blob_index = layers[i]->tops[0];
  1177. int j = i + 1;
  1178. for (; j<layer_count; j++)
  1179. {
  1180. if (layers[j]->type != "BinaryOp")
  1181. continue;
  1182. if (layers[j]->bottoms.size() != 2)
  1183. continue;
  1184. if (layers[j]->bottoms[0] == top_blob_index || layers[j]->bottoms[1] == top_blob_index)
  1185. break;
  1186. }
  1187. if (j == layer_count)
  1188. continue;
  1189. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  1190. fprintf(stderr, "eliminate_reshape_before_binaryop %s %s\n", reshape->name.c_str(), binaryop->name.c_str());
  1191. int bottom_blob_index_final = reshape->bottoms[0];
  1192. if (layers[j]->bottoms[0] == top_blob_index)
  1193. binaryop->bottoms[0] = bottom_blob_index_final;
  1194. if (layers[j]->bottoms[1] == top_blob_index)
  1195. binaryop->bottoms[1] = bottom_blob_index_final;
  1196. blobs[bottom_blob_index_final].consumers.erase(std::find(blobs[bottom_blob_index_final].consumers.begin(), blobs[bottom_blob_index_final].consumers.end(), i));
  1197. blobs[bottom_blob_index_final].consumers.push_back(j);
  1198. reshape->type = "ncnnfused";
  1199. }
  1200. return 0;
  1201. }
  1202. int NetOptimize::replace_convolution_with_innerproduct_after_global_pooling()
  1203. {
  1204. const int layer_count = layers.size();
  1205. for (int i=0; i<layer_count; i++)
  1206. {
  1207. if (layers[i]->type != "Pooling")
  1208. continue;
  1209. ncnn::Pooling* pooling = (ncnn::Pooling*)layers[i];
  1210. if (pooling->global_pooling == 0)
  1211. continue;
  1212. // Pooling - Convolution
  1213. int top_blob_index = layers[i]->tops[0];
  1214. int j = i + 1;
  1215. for (; j<layer_count; j++)
  1216. {
  1217. if (layers[j]->type != "Convolution")
  1218. continue;
  1219. if (layers[j]->bottoms.size() != 1)
  1220. continue;
  1221. if (layers[j]->bottoms[0] == top_blob_index)
  1222. break;
  1223. }
  1224. if (j == layer_count)
  1225. continue;
  1226. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[j];
  1227. fprintf(stderr, "replace_convolution_with_innerproduct_after_global_pooling %s %s\n", pooling->name.c_str(), convolution->name.c_str());
  1228. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)ncnn::create_layer("InnerProduct");
  1229. innerproduct->type = "InnerProduct";
  1230. innerproduct->name = convolution->name;
  1231. innerproduct->bottoms = convolution->bottoms;
  1232. innerproduct->tops = convolution->tops;
  1233. ncnn::ParamDict pd;
  1234. innerproduct->load_param(pd);
  1235. innerproduct->num_output = convolution->num_output;
  1236. innerproduct->bias_term = convolution->bias_term;
  1237. innerproduct->weight_data_size = convolution->weight_data_size;
  1238. innerproduct->weight_data = convolution->weight_data;
  1239. innerproduct->bias_data = convolution->bias_data;
  1240. innerproduct->activation_type = convolution->activation_type;
  1241. innerproduct->activation_params = convolution->activation_params;
  1242. layers[j] = innerproduct;
  1243. delete convolution;
  1244. }
  1245. return 0;
  1246. }
  1247. int NetOptimize::replace_convolution_with_innerproduct_after_innerproduct()
  1248. {
  1249. const int layer_count = layers.size();
  1250. for (;;)
  1251. {
  1252. bool replaced = false;
  1253. for (int i=0; i<layer_count; i++)
  1254. {
  1255. if (layers[i]->type != "InnerProduct")
  1256. continue;
  1257. // InnerProduct - Convolution
  1258. int top_blob_index = layers[i]->tops[0];
  1259. int j = i + 1;
  1260. for (; j<layer_count; j++)
  1261. {
  1262. if (layers[j]->type != "Convolution")
  1263. continue;
  1264. if (layers[j]->bottoms.size() != 1)
  1265. continue;
  1266. if (layers[j]->bottoms[0] == top_blob_index)
  1267. break;
  1268. }
  1269. if (j == layer_count)
  1270. continue;
  1271. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  1272. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[j];
  1273. fprintf(stderr, "replace_convolution_with_innerproduct_after_innerproduct %s %s\n", innerproduct->name.c_str(), convolution->name.c_str());
  1274. ncnn::InnerProduct* innerproduct2 = (ncnn::InnerProduct*)ncnn::create_layer("InnerProduct");
  1275. innerproduct2->type = "InnerProduct";
  1276. innerproduct2->name = convolution->name;
  1277. innerproduct2->bottoms = convolution->bottoms;
  1278. innerproduct2->tops = convolution->tops;
  1279. ncnn::ParamDict pd;
  1280. innerproduct2->load_param(pd);
  1281. innerproduct2->num_output = convolution->num_output;
  1282. innerproduct2->bias_term = convolution->bias_term;
  1283. innerproduct2->weight_data_size = convolution->weight_data_size;
  1284. innerproduct2->weight_data = convolution->weight_data;
  1285. innerproduct2->bias_data = convolution->bias_data;
  1286. innerproduct2->activation_type = convolution->activation_type;
  1287. innerproduct2->activation_params = convolution->activation_params;
  1288. layers[j] = innerproduct2;
  1289. delete convolution;
  1290. replaced = true;
  1291. }
  1292. if (!replaced)
  1293. break;
  1294. }
  1295. return 0;
  1296. }
  1297. int NetOptimize::fprintf_param_int_array(int id, const ncnn::Mat& m, FILE* pp)
  1298. {
  1299. const int count = m.w;
  1300. const int* ptr = m;
  1301. fprintf(pp, " -%d=%d", 23300 + id, count);
  1302. for (int i=0; i<count; i++)
  1303. {
  1304. fprintf(pp, ",%d", ptr[i]);
  1305. }
  1306. return 0;
  1307. }
  1308. int NetOptimize::fprintf_param_float_array(int id, const ncnn::Mat& m, FILE* pp)
  1309. {
  1310. const int count = m.w;
  1311. const float* ptr = m;
  1312. fprintf(pp, " -%d=%d", 23300 + id, count);
  1313. for (int i=0; i<count; i++)
  1314. {
  1315. fprintf(pp, ",%e", ptr[i]);
  1316. }
  1317. return 0;
  1318. }
  1319. static inline size_t alignSize(size_t sz, int n)
  1320. {
  1321. return (sz + n-1) & -n;
  1322. }
  1323. int NetOptimize::fwrite_weight_tag_data(int tag, const ncnn::Mat& data, FILE* bp)
  1324. {
  1325. int p0 = ftell(bp);
  1326. ncnn::Mat data_flattened = data.reshape(data.w * data.h * data.c);
  1327. if (storage_type == 1 && tag == 0)
  1328. {
  1329. tag = 0x01306B47; // fp16 magic
  1330. fwrite(&tag, sizeof(int), 1, bp);
  1331. ncnn::Mat data_flattened_fp16;
  1332. ncnn::cast_float32_to_float16(data_flattened, data_flattened_fp16);
  1333. fwrite(data_flattened_fp16.data, data_flattened_fp16.elemsize, data_flattened_fp16.w, bp);
  1334. }
  1335. else
  1336. {
  1337. fwrite(&tag, sizeof(int), 1, bp);
  1338. fwrite(data_flattened.data, data_flattened.elemsize, data_flattened.w, bp);
  1339. }
  1340. // padding to 32bit align
  1341. int nwrite = ftell(bp) - p0;
  1342. int nalign = alignSize(nwrite, 4);
  1343. unsigned char padding[4] = {0x00, 0x00, 0x00, 0x00};
  1344. fwrite(padding, sizeof(unsigned char), nalign - nwrite, bp);
  1345. return 0;
  1346. }
  1347. int NetOptimize::fwrite_weight_data(const ncnn::Mat& data, FILE* bp)
  1348. {
  1349. int p0 = ftell(bp);
  1350. ncnn::Mat data_flattened = data.reshape(data.w * data.h * data.c);
  1351. fwrite(data_flattened.data, data_flattened.elemsize, data_flattened.w, bp);
  1352. // padding to 32bit align
  1353. int nwrite = ftell(bp) - p0;
  1354. int nalign = alignSize(nwrite, 4);
  1355. unsigned char padding[4] = {0x00, 0x00, 0x00, 0x00};
  1356. fwrite(padding, sizeof(unsigned char), nalign - nwrite, bp);
  1357. return 0;
  1358. }
  1359. int NetOptimize::save(const char* parampath, const char* binpath)
  1360. {
  1361. FILE* pp = fopen(parampath, "wb");
  1362. FILE* bp = fopen(binpath, "wb");
  1363. fprintf(pp, "7767517\n");
  1364. const int layer_count = layers.size();
  1365. int layer_count_fused = 0;
  1366. std::set<std::string> blob_names;
  1367. for (int i=0; i<layer_count; i++)
  1368. {
  1369. const ncnn::Layer* layer = layers[i];
  1370. if (layer->type == "ncnnfused")
  1371. continue;
  1372. layer_count_fused++;
  1373. int bottom_count = layer->bottoms.size();
  1374. for (int j=0; j<bottom_count; j++)
  1375. {
  1376. int bottom_blob_index = layer->bottoms[j];
  1377. blob_names.insert(blobs[bottom_blob_index].name);
  1378. }
  1379. int top_count = layer->tops.size();
  1380. for (int j=0; j<top_count; j++)
  1381. {
  1382. int top_blob_index = layer->tops[j];
  1383. blob_names.insert(blobs[top_blob_index].name);
  1384. }
  1385. }
  1386. int blob_count_fused = blob_names.size();
  1387. fprintf(pp, "%d %d\n", layer_count_fused, blob_count_fused);
  1388. for (int i=0; i<layer_count; i++)
  1389. {
  1390. const ncnn::Layer* layer = layers[i];
  1391. if (layer->type == "ncnnfused")
  1392. continue;
  1393. int bottom_count = layer->bottoms.size();
  1394. int top_count = layer->tops.size();
  1395. fprintf(pp, "%-24s %-24s %d %d", layer->type.c_str(), layer->name.c_str(), bottom_count, top_count);
  1396. for (int j=0; j<bottom_count; j++)
  1397. {
  1398. int bottom_blob_index = layer->bottoms[j];
  1399. fprintf(pp, " %s", blobs[bottom_blob_index].name.c_str());
  1400. }
  1401. for (int j=0; j<top_count; j++)
  1402. {
  1403. int top_blob_index = layer->tops[j];
  1404. fprintf(pp, " %s", blobs[top_blob_index].name.c_str());
  1405. }
  1406. ncnn::Layer* layer_default = ncnn::create_layer(layer->typeindex);
  1407. ncnn::ParamDict pd;
  1408. layer_default->load_param(pd);
  1409. #define fprintf_param_value(format, phase) \
  1410. { if (op->phase != op_default->phase) fprintf(pp, format, op->phase); }
  1411. if (layer->type == "BatchNorm")
  1412. {
  1413. ncnn::BatchNorm* op = (ncnn::BatchNorm*)layer;
  1414. ncnn::BatchNorm* op_default = (ncnn::BatchNorm*)layer_default;
  1415. fprintf_param_value(" 0=%d", channels)
  1416. fprintf_param_value(" 1=%e", eps)
  1417. fwrite_weight_data(op->slope_data, bp);
  1418. fwrite_weight_data(op->mean_data, bp);
  1419. fwrite_weight_data(op->var_data, bp);
  1420. fwrite_weight_data(op->bias_data, bp);
  1421. }
  1422. else if (layer->type == "Bias")
  1423. {
  1424. ncnn::Bias* op = (ncnn::Bias*)layer;
  1425. ncnn::Bias* op_default = (ncnn::Bias*)layer_default;
  1426. fprintf_param_value(" 0=%d", bias_data_size)
  1427. fwrite_weight_data(op->bias_data, bp);
  1428. }
  1429. else if (layer->type == "BinaryOp")
  1430. {
  1431. ncnn::BinaryOp* op = (ncnn::BinaryOp*)layer;
  1432. ncnn::BinaryOp* op_default = (ncnn::BinaryOp*)layer_default;
  1433. fprintf_param_value(" 0=%d", op_type)
  1434. fprintf_param_value(" 1=%d", with_scalar)
  1435. fprintf_param_value(" 2=%e", b)
  1436. }
  1437. else if (layer->type == "Clip")
  1438. {
  1439. ncnn::Clip* op = (ncnn::Clip*)layer;
  1440. ncnn::Clip* op_default = (ncnn::Clip*)layer_default;
  1441. fprintf_param_value(" 0=%e", min)
  1442. fprintf_param_value(" 1=%e", max)
  1443. }
  1444. else if (layer->type == "Concat")
  1445. {
  1446. ncnn::Concat* op = (ncnn::Concat*)layer;
  1447. ncnn::Concat* op_default = (ncnn::Concat*)layer_default;
  1448. fprintf_param_value(" 0=%d", axis)
  1449. }
  1450. else if (layer->type == "Convolution")
  1451. {
  1452. ncnn::Convolution* op = (ncnn::Convolution*)layer;
  1453. ncnn::Convolution* op_default = (ncnn::Convolution*)layer_default;
  1454. fprintf_param_value(" 0=%d", num_output)
  1455. fprintf_param_value(" 1=%d", kernel_w)
  1456. { if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); }
  1457. fprintf_param_value(" 2=%d", dilation_w)
  1458. { if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); }
  1459. fprintf_param_value(" 3=%d", stride_w)
  1460. { if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); }
  1461. fprintf_param_value(" 4=%d", pad_left)
  1462. { if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); }
  1463. { if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); }
  1464. { if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); }
  1465. fprintf_param_value(" 18=%e", pad_value)
  1466. fprintf_param_value(" 5=%d", bias_term)
  1467. fprintf_param_value(" 6=%d", weight_data_size)
  1468. fprintf_param_value(" 8=%d", int8_scale_term)
  1469. fprintf_param_value(" 9=%d", activation_type)
  1470. { if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); }
  1471. fprintf_param_value(" 17=%d", impl_type)
  1472. fwrite_weight_tag_data(0, op->weight_data, bp);
  1473. fwrite_weight_data(op->bias_data, bp);
  1474. }
  1475. else if (layer->type == "ConvolutionDepthWise")
  1476. {
  1477. ncnn::ConvolutionDepthWise* op = (ncnn::ConvolutionDepthWise*)layer;
  1478. ncnn::ConvolutionDepthWise* op_default = (ncnn::ConvolutionDepthWise*)layer_default;
  1479. fprintf_param_value(" 0=%d", num_output)
  1480. fprintf_param_value(" 1=%d", kernel_w)
  1481. { if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); }
  1482. fprintf_param_value(" 2=%d", dilation_w)
  1483. { if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); }
  1484. fprintf_param_value(" 3=%d", stride_w)
  1485. { if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); }
  1486. fprintf_param_value(" 4=%d", pad_left)
  1487. { if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); }
  1488. { if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); }
  1489. { if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); }
  1490. fprintf_param_value(" 18=%e", pad_value)
  1491. fprintf_param_value(" 5=%d", bias_term)
  1492. fprintf_param_value(" 6=%d", weight_data_size)
  1493. fprintf_param_value(" 7=%d", group)
  1494. fprintf_param_value(" 8=%d", int8_scale_term)
  1495. fprintf_param_value(" 9=%d", activation_type)
  1496. { if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); }
  1497. fwrite_weight_tag_data(0, op->weight_data, bp);
  1498. fwrite_weight_data(op->bias_data, bp);
  1499. }
  1500. else if (layer->type == "Crop")
  1501. {
  1502. ncnn::Crop* op = (ncnn::Crop*)layer;
  1503. ncnn::Crop* op_default = (ncnn::Crop*)layer_default;
  1504. fprintf_param_value(" 0=%d", woffset)
  1505. fprintf_param_value(" 1=%d", hoffset)
  1506. fprintf_param_value(" 2=%d", coffset)
  1507. fprintf_param_value(" 3=%d", outw)
  1508. fprintf_param_value(" 4=%d", outh)
  1509. fprintf_param_value(" 5=%d", outc)
  1510. fprintf_param_value(" 6=%d", woffset2)
  1511. fprintf_param_value(" 7=%d", hoffset2)
  1512. fprintf_param_value(" 8=%d", coffset2)
  1513. { if (!op->starts.empty()) fprintf_param_int_array(9, op->starts, pp); }
  1514. { if (!op->ends.empty()) fprintf_param_int_array(10, op->ends, pp); }
  1515. { if (!op->axes.empty()) fprintf_param_int_array(11, op->axes, pp); }
  1516. }
  1517. else if (layer->type == "Deconvolution")
  1518. {
  1519. ncnn::Deconvolution* op = (ncnn::Deconvolution*)layer;
  1520. ncnn::Deconvolution* op_default = (ncnn::Deconvolution*)layer_default;
  1521. fprintf_param_value(" 0=%d", num_output)
  1522. fprintf_param_value(" 1=%d", kernel_w)
  1523. { if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); }
  1524. fprintf_param_value(" 2=%d", dilation_w)
  1525. { if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); }
  1526. fprintf_param_value(" 3=%d", stride_w)
  1527. { if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); }
  1528. fprintf_param_value(" 4=%d", pad_left)
  1529. { if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); }
  1530. { if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); }
  1531. { if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); }
  1532. fprintf_param_value(" 18=%d", output_pad_right)
  1533. { if (op->output_pad_bottom != op->output_pad_right) fprintf(pp, " 19=%d", op->output_pad_bottom); }
  1534. fprintf_param_value(" 20=%d", output_w)
  1535. { if (op->output_h != op->output_w) fprintf(pp, " 21=%d", op->output_h); }
  1536. fprintf_param_value(" 5=%d", bias_term)
  1537. fprintf_param_value(" 6=%d", weight_data_size)
  1538. fprintf_param_value(" 9=%d", activation_type)
  1539. { if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); }
  1540. fwrite_weight_tag_data(0, op->weight_data, bp);
  1541. fwrite_weight_data(op->bias_data, bp);
  1542. }
  1543. else if (layer->type == "DeconvolutionDepthWise")
  1544. {
  1545. ncnn::DeconvolutionDepthWise* op = (ncnn::DeconvolutionDepthWise*)layer;
  1546. ncnn::DeconvolutionDepthWise* op_default = (ncnn::DeconvolutionDepthWise*)layer_default;
  1547. fprintf_param_value(" 0=%d", num_output)
  1548. fprintf_param_value(" 1=%d", kernel_w)
  1549. { if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); }
  1550. fprintf_param_value(" 2=%d", dilation_w)
  1551. { if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); }
  1552. fprintf_param_value(" 3=%d", stride_w)
  1553. { if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); }
  1554. fprintf_param_value(" 4=%d", pad_left)
  1555. { if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); }
  1556. { if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); }
  1557. { if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); }
  1558. fprintf_param_value(" 18=%d", output_pad_right)
  1559. { if (op->output_pad_bottom != op->output_pad_right) fprintf(pp, " 19=%d", op->output_pad_bottom); }
  1560. fprintf_param_value(" 20=%d", output_w)
  1561. { if (op->output_h != op->output_w) fprintf(pp, " 21=%d", op->output_h); }
  1562. fprintf_param_value(" 5=%d", bias_term)
  1563. fprintf_param_value(" 6=%d", weight_data_size)
  1564. fprintf_param_value(" 7=%d", group)
  1565. fprintf_param_value(" 9=%d", activation_type)
  1566. { if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); }
  1567. fwrite_weight_tag_data(0, op->weight_data, bp);
  1568. fwrite_weight_data(op->bias_data, bp);
  1569. }
  1570. else if (layer->type == "DetectionOutput")
  1571. {
  1572. ncnn::DetectionOutput* op = (ncnn::DetectionOutput*)layer;
  1573. ncnn::DetectionOutput* op_default = (ncnn::DetectionOutput*)layer_default;
  1574. fprintf_param_value(" 0=%d", num_class)
  1575. fprintf_param_value(" 1=%e", nms_threshold)
  1576. fprintf_param_value(" 2=%d", nms_top_k)
  1577. fprintf_param_value(" 3=%d", keep_top_k)
  1578. fprintf_param_value(" 4=%e", confidence_threshold)
  1579. fprintf_param_value(" 5=%e", variances[0])
  1580. fprintf_param_value(" 6=%e", variances[1])
  1581. fprintf_param_value(" 7=%e", variances[2])
  1582. fprintf_param_value(" 8=%e", variances[3])
  1583. }
  1584. else if (layer->type == "Dropout")
  1585. {
  1586. ncnn::Dropout* op = (ncnn::Dropout*)layer;
  1587. ncnn::Dropout* op_default = (ncnn::Dropout*)layer_default;
  1588. fprintf_param_value(" 0=%e", scale)
  1589. }
  1590. else if (layer->type == "Eltwise")
  1591. {
  1592. ncnn::Eltwise* op = (ncnn::Eltwise*)layer;
  1593. ncnn::Eltwise* op_default = (ncnn::Eltwise*)layer_default;
  1594. fprintf_param_value(" 0=%d", op_type)
  1595. { if (!op->coeffs.empty()) fprintf_param_float_array(1, op->coeffs, pp); }
  1596. }
  1597. else if (layer->type == "ELU")
  1598. {
  1599. ncnn::ELU* op = (ncnn::ELU*)layer;
  1600. ncnn::ELU* op_default = (ncnn::ELU*)layer_default;
  1601. fprintf_param_value(" 0=%e", alpha)
  1602. }
  1603. else if (layer->type == "Exp")
  1604. {
  1605. ncnn::Exp* op = (ncnn::Exp*)layer;
  1606. ncnn::Exp* op_default = (ncnn::Exp*)layer_default;
  1607. fprintf_param_value(" 0=%e", base)
  1608. fprintf_param_value(" 1=%e", scale)
  1609. fprintf_param_value(" 2=%e", shift)
  1610. }
  1611. else if (layer->type == "ExpandDims")
  1612. {
  1613. ncnn::ExpandDims* op = (ncnn::ExpandDims*)layer;
  1614. ncnn::ExpandDims* op_default = (ncnn::ExpandDims*)layer_default;
  1615. fprintf_param_value(" 0=%d", expand_w)
  1616. fprintf_param_value(" 1=%d", expand_h)
  1617. fprintf_param_value(" 2=%d", expand_c)
  1618. { if (!op->axes.empty()) fprintf_param_int_array(0, op->axes, pp); }
  1619. }
  1620. else if (layer->type == "HardSigmoid")
  1621. {
  1622. ncnn::HardSigmoid* op = (ncnn::HardSigmoid*)layer;
  1623. ncnn::HardSigmoid* op_default = (ncnn::HardSigmoid*)layer_default;
  1624. fprintf_param_value(" 0=%e", alpha)
  1625. fprintf_param_value(" 1=%e", beta)
  1626. }
  1627. else if (layer->type == "HardSwish")
  1628. {
  1629. ncnn::HardSwish* op = (ncnn::HardSwish*)layer;
  1630. ncnn::HardSwish* op_default = (ncnn::HardSwish*)layer_default;
  1631. fprintf_param_value(" 0=%e", alpha)
  1632. fprintf_param_value(" 1=%e", beta)
  1633. }
  1634. else if (layer->type == "InnerProduct")
  1635. {
  1636. ncnn::InnerProduct* op = (ncnn::InnerProduct*)layer;
  1637. ncnn::InnerProduct* op_default = (ncnn::InnerProduct*)layer_default;
  1638. fprintf_param_value(" 0=%d", num_output)
  1639. fprintf_param_value(" 1=%d", bias_term)
  1640. fprintf_param_value(" 2=%d", weight_data_size)
  1641. fprintf_param_value(" 8=%d", int8_scale_term)
  1642. fprintf_param_value(" 9=%d", activation_type)
  1643. { if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); }
  1644. fwrite_weight_tag_data(0, op->weight_data, bp);
  1645. fwrite_weight_data(op->bias_data, bp);
  1646. }
  1647. else if (layer->type == "Input")
  1648. {
  1649. ncnn::Input* op = (ncnn::Input*)layer;
  1650. ncnn::Input* op_default = (ncnn::Input*)layer_default;
  1651. fprintf_param_value(" 0=%d", w)
  1652. fprintf_param_value(" 1=%d", h)
  1653. fprintf_param_value(" 2=%d", c)
  1654. }
  1655. else if (layer->type == "InstanceNorm")
  1656. {
  1657. ncnn::InstanceNorm* op = (ncnn::InstanceNorm*)layer;
  1658. ncnn::InstanceNorm* op_default = (ncnn::InstanceNorm*)layer_default;
  1659. fprintf_param_value(" 0=%d", channels)
  1660. fprintf_param_value(" 1=%e", eps)
  1661. fwrite_weight_data(op->gamma_data, bp);
  1662. fwrite_weight_data(op->beta_data, bp);
  1663. }
  1664. else if (layer->type == "Interp")
  1665. {
  1666. ncnn::Interp* op = (ncnn::Interp*)layer;
  1667. ncnn::Interp* op_default = (ncnn::Interp*)layer_default;
  1668. fprintf_param_value(" 0=%d", resize_type)
  1669. fprintf_param_value(" 1=%e", height_scale)
  1670. fprintf_param_value(" 2=%e", width_scale)
  1671. fprintf_param_value(" 3=%d", output_height)
  1672. fprintf_param_value(" 4=%d", output_width)
  1673. }
  1674. else if (layer->type == "Log")
  1675. {
  1676. ncnn::Log* op = (ncnn::Log*)layer;
  1677. ncnn::Log* op_default = (ncnn::Log*)layer_default;
  1678. fprintf_param_value(" 0=%e", base)
  1679. fprintf_param_value(" 1=%e", scale)
  1680. fprintf_param_value(" 2=%e", shift)
  1681. }
  1682. else if (layer->type == "LRN")
  1683. {
  1684. ncnn::LRN* op = (ncnn::LRN*)layer;
  1685. ncnn::LRN* op_default = (ncnn::LRN*)layer_default;
  1686. fprintf_param_value(" 0=%d", region_type)
  1687. fprintf_param_value(" 1=%d", local_size)
  1688. fprintf_param_value(" 2=%e", alpha)
  1689. fprintf_param_value(" 3=%e", beta)
  1690. fprintf_param_value(" 4=%e", bias)
  1691. }
  1692. else if (layer->type == "MemoryData")
  1693. {
  1694. ncnn::MemoryData* op = (ncnn::MemoryData*)layer;
  1695. ncnn::MemoryData* op_default = (ncnn::MemoryData*)layer_default;
  1696. fprintf_param_value(" 0=%d", w)
  1697. fprintf_param_value(" 1=%d", h)
  1698. fprintf_param_value(" 2=%d", c)
  1699. fwrite_weight_data(op->data, bp);
  1700. }
  1701. else if (layer->type == "MVN")
  1702. {
  1703. ncnn::MVN* op = (ncnn::MVN*)layer;
  1704. ncnn::MVN* op_default = (ncnn::MVN*)layer_default;
  1705. fprintf_param_value(" 0=%d", normalize_variance)
  1706. fprintf_param_value(" 1=%d", across_channels)
  1707. fprintf_param_value(" 2=%e", eps)
  1708. }
  1709. else if (layer->type == "Normalize")
  1710. {
  1711. ncnn::Normalize* op = (ncnn::Normalize*)layer;
  1712. ncnn::Normalize* op_default = (ncnn::Normalize*)layer_default;
  1713. fprintf_param_value(" 0=%d", across_spatial)
  1714. fprintf_param_value(" 1=%d", channel_shared)
  1715. fprintf_param_value(" 2=%e", eps)
  1716. fprintf_param_value(" 3=%d", scale_data_size)
  1717. fprintf_param_value(" 4=%d", across_channel)
  1718. fwrite_weight_data(op->scale_data, bp);
  1719. }
  1720. else if (layer->type == "Padding")
  1721. {
  1722. ncnn::Padding* op = (ncnn::Padding*)layer;
  1723. ncnn::Padding* op_default = (ncnn::Padding*)layer_default;
  1724. fprintf_param_value(" 0=%d", top)
  1725. fprintf_param_value(" 1=%d", bottom)
  1726. fprintf_param_value(" 2=%d", left)
  1727. fprintf_param_value(" 3=%d", right)
  1728. fprintf_param_value(" 4=%d", type)
  1729. fprintf_param_value(" 5=%e", value)
  1730. }
  1731. else if (layer->type == "Permute")
  1732. {
  1733. ncnn::Permute* op = (ncnn::Permute*)layer;
  1734. ncnn::Permute* op_default = (ncnn::Permute*)layer_default;
  1735. fprintf_param_value(" 0=%d", order_type)
  1736. }
  1737. else if (layer->type == "Pooling")
  1738. {
  1739. ncnn::Pooling* op = (ncnn::Pooling*)layer;
  1740. ncnn::Pooling* op_default = (ncnn::Pooling*)layer_default;
  1741. fprintf_param_value(" 0=%d", pooling_type)
  1742. fprintf_param_value(" 1=%d", kernel_w)
  1743. { if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); }
  1744. fprintf_param_value(" 2=%d", stride_w)
  1745. { if (op->stride_h != op->stride_w) fprintf(pp, " 12=%d", op->stride_h); }
  1746. fprintf_param_value(" 3=%d", pad_left)
  1747. { if (op->pad_top != op->pad_left) fprintf(pp, " 13=%d", op->pad_top); }
  1748. { if (op->pad_right != op->pad_left) fprintf(pp, " 14=%d", op->pad_right); }
  1749. { if (op->pad_bottom != op->pad_top) fprintf(pp, " 15=%d", op->pad_bottom); }
  1750. fprintf_param_value(" 4=%d", global_pooling)
  1751. fprintf_param_value(" 5=%d", pad_mode)
  1752. }
  1753. else if (layer->type == "Power")
  1754. {
  1755. ncnn::Power* op = (ncnn::Power*)layer;
  1756. ncnn::Power* op_default = (ncnn::Power*)layer_default;
  1757. fprintf_param_value(" 0=%e", power)
  1758. fprintf_param_value(" 1=%e", scale)
  1759. fprintf_param_value(" 2=%e", shift)
  1760. }
  1761. else if (layer->type == "PReLU")
  1762. {
  1763. ncnn::PReLU* op = (ncnn::PReLU*)layer;
  1764. ncnn::PReLU* op_default = (ncnn::PReLU*)layer_default;
  1765. fprintf_param_value(" 0=%d", num_slope)
  1766. fwrite_weight_data(op->slope_data, bp);
  1767. }
  1768. else if (layer->type == "PriorBox")
  1769. {
  1770. ncnn::PriorBox* op = (ncnn::PriorBox*)layer;
  1771. ncnn::PriorBox* op_default = (ncnn::PriorBox*)layer_default;
  1772. { if (!op->min_sizes.empty()) fprintf_param_float_array(0, op->min_sizes, pp); }
  1773. { if (!op->max_sizes.empty()) fprintf_param_float_array(1, op->max_sizes, pp); }
  1774. { if (!op->aspect_ratios.empty()) fprintf_param_float_array(2, op->aspect_ratios, pp); }
  1775. fprintf_param_value(" 3=%e", variances[0])
  1776. fprintf_param_value(" 4=%e", variances[1])
  1777. fprintf_param_value(" 5=%e", variances[2])
  1778. fprintf_param_value(" 6=%e", variances[3])
  1779. fprintf_param_value(" 7=%d", flip)
  1780. fprintf_param_value(" 8=%d", clip)
  1781. fprintf_param_value(" 9=%d", image_width)
  1782. fprintf_param_value(" 10=%d", image_height)
  1783. fprintf_param_value(" 11=%e", step_width)
  1784. fprintf_param_value(" 12=%e", step_height)
  1785. fprintf_param_value(" 13=%e", offset)
  1786. }
  1787. else if (layer->type == "Proposal")
  1788. {
  1789. ncnn::Proposal* op = (ncnn::Proposal*)layer;
  1790. ncnn::Proposal* op_default = (ncnn::Proposal*)layer_default;
  1791. fprintf_param_value(" 0=%d", feat_stride)
  1792. fprintf_param_value(" 1=%d", base_size)
  1793. fprintf_param_value(" 2=%d", pre_nms_topN)
  1794. fprintf_param_value(" 3=%d", after_nms_topN)
  1795. fprintf_param_value(" 4=%e", nms_thresh)
  1796. fprintf_param_value(" 5=%d", min_size)
  1797. }
  1798. else if (layer->type == "PSROIPooling")
  1799. {
  1800. ncnn::PSROIPooling* op = (ncnn::PSROIPooling*)layer;
  1801. ncnn::PSROIPooling* op_default = (ncnn::PSROIPooling*)layer_default;
  1802. fprintf_param_value(" 0=%d", pooled_width)
  1803. fprintf_param_value(" 1=%d", pooled_height)
  1804. fprintf_param_value(" 2=%e", spatial_scale)
  1805. fprintf_param_value(" 3=%d", output_dim)
  1806. }
  1807. else if (layer->type == "Quantize")
  1808. {
  1809. ncnn::Quantize* op = (ncnn::Quantize*)layer;
  1810. ncnn::Quantize* op_default = (ncnn::Quantize*)layer_default;
  1811. fprintf_param_value(" 0=%e", scale)
  1812. }
  1813. else if (layer->type == "Reduction")
  1814. {
  1815. ncnn::Reduction* op = (ncnn::Reduction*)layer;
  1816. ncnn::Reduction* op_default = (ncnn::Reduction*)layer_default;
  1817. fprintf_param_value(" 0=%d", operation)
  1818. fprintf_param_value(" 1=%d", reduce_all)
  1819. fprintf_param_value(" 2=%e", coeff)
  1820. }
  1821. else if (layer->type == "ReLU")
  1822. {
  1823. ncnn::ReLU* op = (ncnn::ReLU*)layer;
  1824. ncnn::ReLU* op_default = (ncnn::ReLU*)layer_default;
  1825. fprintf_param_value(" 0=%e", slope)
  1826. }
  1827. else if (layer->type == "Reorg")
  1828. {
  1829. ncnn::Reorg* op = (ncnn::Reorg*)layer;
  1830. ncnn::Reorg* op_default = (ncnn::Reorg*)layer_default;
  1831. fprintf_param_value(" 0=%d", stride)
  1832. }
  1833. else if (layer->type == "Requantize")
  1834. {
  1835. ncnn::Requantize* op = (ncnn::Requantize*)layer;
  1836. ncnn::Requantize* op_default = (ncnn::Requantize*)layer_default;
  1837. fprintf_param_value(" 0=%e", scale_in)
  1838. fprintf_param_value(" 1=%e", scale_out)
  1839. fprintf_param_value(" 2=%d", bias_term)
  1840. fprintf_param_value(" 3=%d", bias_data_size)
  1841. fprintf_param_value(" 4=%d", fusion_relu)
  1842. }
  1843. else if (layer->type == "Reshape")
  1844. {
  1845. ncnn::Reshape* op = (ncnn::Reshape*)layer;
  1846. ncnn::Reshape* op_default = (ncnn::Reshape*)layer_default;
  1847. fprintf_param_value(" 0=%d", w)
  1848. fprintf_param_value(" 1=%d", h)
  1849. fprintf_param_value(" 2=%d", c)
  1850. fprintf_param_value(" 3=%d", permute)
  1851. }
  1852. else if (layer->type == "ROIAlign")
  1853. {
  1854. ncnn::ROIAlign* op = (ncnn::ROIAlign*)layer;
  1855. ncnn::ROIAlign* op_default = (ncnn::ROIAlign*)layer_default;
  1856. fprintf_param_value(" 0=%d", pooled_width)
  1857. fprintf_param_value(" 1=%d", pooled_height)
  1858. fprintf_param_value(" 2=%e", spatial_scale)
  1859. }
  1860. else if (layer->type == "ROIPooling")
  1861. {
  1862. ncnn::ROIPooling* op = (ncnn::ROIPooling*)layer;
  1863. ncnn::ROIPooling* op_default = (ncnn::ROIPooling*)layer_default;
  1864. fprintf_param_value(" 0=%d", pooled_width)
  1865. fprintf_param_value(" 1=%d", pooled_height)
  1866. fprintf_param_value(" 2=%e", spatial_scale)
  1867. }
  1868. else if (layer->type == "Scale")
  1869. {
  1870. ncnn::Scale* op = (ncnn::Scale*)layer;
  1871. ncnn::Scale* op_default = (ncnn::Scale*)layer_default;
  1872. fprintf_param_value(" 0=%d", scale_data_size)
  1873. fprintf_param_value(" 1=%d", bias_term)
  1874. fwrite_weight_data(op->scale_data, bp);
  1875. fwrite_weight_data(op->bias_data, bp);
  1876. }
  1877. else if (layer->type == "ShuffleChannel")
  1878. {
  1879. ncnn::ShuffleChannel* op = (ncnn::ShuffleChannel*)layer;
  1880. ncnn::ShuffleChannel* op_default = (ncnn::ShuffleChannel*)layer_default;
  1881. fprintf_param_value(" 0=%d", group)
  1882. }
  1883. else if (layer->type == "Slice")
  1884. {
  1885. ncnn::Slice* op = (ncnn::Slice*)layer;
  1886. ncnn::Slice* op_default = (ncnn::Slice*)layer_default;
  1887. { if (!op->slices.empty()) fprintf_param_int_array(0, op->slices, pp); }
  1888. fprintf_param_value(" 1=%d", axis)
  1889. }
  1890. else if (layer->type == "Softmax")
  1891. {
  1892. ncnn::Softmax* op = (ncnn::Softmax*)layer;
  1893. ncnn::Softmax* op_default = (ncnn::Softmax*)layer_default;
  1894. fprintf_param_value(" 0=%d", axis)
  1895. // HACK
  1896. if (op->axis != 0)
  1897. {
  1898. int fixbug0 = 1;
  1899. fprintf(pp, " 1=%d", fixbug0);
  1900. }
  1901. }
  1902. else if (layer->type == "Squeeze")
  1903. {
  1904. ncnn::Squeeze* op = (ncnn::Squeeze*)layer;
  1905. ncnn::Squeeze* op_default = (ncnn::Squeeze*)layer_default;
  1906. fprintf_param_value(" 0=%d", squeeze_w)
  1907. fprintf_param_value(" 1=%d", squeeze_h)
  1908. fprintf_param_value(" 2=%d", squeeze_c)
  1909. { if (!op->axes.empty()) fprintf_param_int_array(0, op->axes, pp); }
  1910. }
  1911. else if (layer->type == "Threshold")
  1912. {
  1913. ncnn::Threshold* op = (ncnn::Threshold*)layer;
  1914. ncnn::Threshold* op_default = (ncnn::Threshold*)layer_default;
  1915. fprintf_param_value(" 0=%e", threshold)
  1916. }
  1917. else if (layer->type == "UnaryOp")
  1918. {
  1919. ncnn::UnaryOp* op = (ncnn::UnaryOp*)layer;
  1920. ncnn::UnaryOp* op_default = (ncnn::UnaryOp*)layer_default;
  1921. fprintf_param_value(" 0=%d", op_type)
  1922. }
  1923. else if (layer->type == "YoloDetectionOutput")
  1924. {
  1925. ncnn::YoloDetectionOutput* op = (ncnn::YoloDetectionOutput*)layer;
  1926. ncnn::YoloDetectionOutput* op_default = (ncnn::YoloDetectionOutput*)layer_default;
  1927. fprintf_param_value(" 0=%d", num_class)
  1928. fprintf_param_value(" 1=%d", num_box)
  1929. fprintf_param_value(" 2=%e", confidence_threshold)
  1930. fprintf_param_value(" 3=%e", nms_threshold)
  1931. { if (!op->biases.empty()) fprintf_param_float_array(4, op->biases, pp); }
  1932. }
  1933. else if (layer->type == "Yolov3DetectionOutput")
  1934. {
  1935. ncnn::Yolov3DetectionOutput* op = (ncnn::Yolov3DetectionOutput*)layer;
  1936. ncnn::Yolov3DetectionOutput* op_default = (ncnn::Yolov3DetectionOutput*)layer_default;
  1937. fprintf_param_value(" 0=%d", num_class)
  1938. fprintf_param_value(" 1=%d", num_box)
  1939. fprintf_param_value(" 2=%e", confidence_threshold)
  1940. fprintf_param_value(" 3=%e", nms_threshold)
  1941. { if (!op->biases.empty()) fprintf_param_float_array(4, op->biases, pp); }
  1942. { if (!op->mask.empty()) fprintf_param_int_array(5, op->mask, pp); }
  1943. { if (!op->anchors_scale.empty()) fprintf_param_float_array(6, op->anchors_scale, pp); }
  1944. }
  1945. #undef fprintf_param_value
  1946. fprintf(pp, "\n");
  1947. delete layer_default;
  1948. }
  1949. fclose(pp);
  1950. fclose(bp);
  1951. return 0;
  1952. }
  1953. int main(int argc, char** argv)
  1954. {
  1955. #if defined(__aarch64__) && defined(LINUX)
  1956. if (argc != 10)
  1957. {
  1958. fprintf(stderr, "usage: %s [inparam] [inbin] [outparam] [outbin] [flag] [dataname] [w] [h] [c]\n", argv[0]);
  1959. return -1;
  1960. }
  1961. const char* dataname = argv[6];
  1962. int inw = atoi(argv[7]);
  1963. int inh = atoi(argv[8]);
  1964. int inc = atoi(argv[9]);
  1965. #else
  1966. if (argc != 6)
  1967. {
  1968. fprintf(stderr, "usage: %s [inparam] [inbin] [outparam] [outbin] [flag]\n", argv[0]);
  1969. return -1;
  1970. }
  1971. #endif // defined(__aarch64__) && defined(LINUX)
  1972. const char* inparam = argv[1];
  1973. const char* inbin = argv[2];
  1974. const char* outparam = argv[3];
  1975. const char* outbin = argv[4];
  1976. int flag = atoi(argv[5]);
  1977. NetOptimize optimizer;
  1978. if (flag == 65536)
  1979. {
  1980. optimizer.storage_type = 1;
  1981. }
  1982. else
  1983. {
  1984. optimizer.storage_type = 0;
  1985. }
  1986. optimizer.load_param(inparam);
  1987. if (strcmp(inbin, "null") == 0)
  1988. {
  1989. DataReaderFromEmpty dr;
  1990. optimizer.load_model(dr);
  1991. }
  1992. else
  1993. optimizer.load_model(inbin);
  1994. #if defined(__aarch64__) && defined(LINUX)
  1995. optimizer.find_fastest_fp32_conv(dataname, inw, inh, inc);
  1996. #endif // defined(__aarch64__) && defined(LINUX)
  1997. optimizer.fuse_batchnorm_scale();
  1998. optimizer.fuse_convolution_batchnorm();
  1999. optimizer.fuse_convolutiondepthwise_batchnorm();
  2000. optimizer.fuse_deconvolution_batchnorm();
  2001. optimizer.fuse_deconvolutiondepthwise_batchnorm();
  2002. optimizer.fuse_innerproduct_batchnorm();
  2003. optimizer.fuse_innerproduct_dropout();
  2004. optimizer.fuse_convolution_activation();
  2005. optimizer.fuse_convolutiondepthwise_activation();
  2006. optimizer.fuse_deconvolution_activation();
  2007. optimizer.fuse_deconvolutiondepthwise_activation();
  2008. optimizer.fuse_innerproduct_activation();
  2009. optimizer.eliminate_dropout();
  2010. optimizer.eliminate_noop();
  2011. optimizer.eliminate_flatten_after_global_pooling();
  2012. optimizer.eliminate_reshape_after_global_pooling();
  2013. optimizer.eliminate_reshape_before_binaryop();
  2014. optimizer.replace_convolution_with_innerproduct_after_global_pooling();
  2015. optimizer.replace_convolution_with_innerproduct_after_innerproduct();
  2016. optimizer.eliminate_flatten_after_innerproduct();
  2017. optimizer.save(outparam, outbin);
  2018. return 0;
  2019. }