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

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