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 118 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
6 years ago
6 years ago
6 years ago
6 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
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
7 years ago
7 years ago
6 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702
  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. #ifdef _MSC_VER
  15. #define _CRT_SECURE_NO_DEPRECATE
  16. #endif
  17. #include <algorithm>
  18. #include <set>
  19. #include <vector>
  20. // ncnn public header
  21. #include "datareader.h"
  22. #include "layer.h"
  23. #include "net.h"
  24. // ncnn private header
  25. #include "layer/batchnorm.h"
  26. #include "layer/bias.h"
  27. #include "layer/binaryop.h"
  28. #include "layer/clip.h"
  29. #include "layer/concat.h"
  30. #include "layer/convolution.h"
  31. #include "layer/convolutiondepthwise.h"
  32. #include "layer/crop.h"
  33. #include "layer/deconvolution.h"
  34. #include "layer/deconvolutiondepthwise.h"
  35. #include "layer/detectionoutput.h"
  36. #include "layer/dropout.h"
  37. #include "layer/eltwise.h"
  38. #include "layer/elu.h"
  39. #include "layer/exp.h"
  40. #include "layer/expanddims.h"
  41. #include "layer/flatten.h"
  42. #include "layer/hardsigmoid.h"
  43. #include "layer/hardswish.h"
  44. #include "layer/innerproduct.h"
  45. #include "layer/input.h"
  46. #include "layer/instancenorm.h"
  47. #include "layer/interp.h"
  48. #include "layer/log.h"
  49. #include "layer/lrn.h"
  50. #include "layer/lstm.h"
  51. #include "layer/memorydata.h"
  52. #include "layer/mvn.h"
  53. #include "layer/normalize.h"
  54. #include "layer/padding.h"
  55. #include "layer/permute.h"
  56. #include "layer/pixelshuffle.h"
  57. #include "layer/pooling.h"
  58. #include "layer/power.h"
  59. #include "layer/prelu.h"
  60. #include "layer/priorbox.h"
  61. #include "layer/proposal.h"
  62. #include "layer/psroipooling.h"
  63. #include "layer/quantize.h"
  64. #include "layer/reduction.h"
  65. #include "layer/relu.h"
  66. #include "layer/reorg.h"
  67. #include "layer/requantize.h"
  68. #include "layer/reshape.h"
  69. #include "layer/roialign.h"
  70. #include "layer/roipooling.h"
  71. #include "layer/scale.h"
  72. #include "layer/shufflechannel.h"
  73. #include "layer/slice.h"
  74. #include "layer/softmax.h"
  75. #include "layer/squeeze.h"
  76. #include "layer/threshold.h"
  77. #include "layer/unaryop.h"
  78. #include "layer/yolodetectionoutput.h"
  79. #include "layer/yolov3detectionoutput.h"
  80. #if defined(__aarch64__) && defined(LINUX)
  81. #include <cassert>
  82. #include <chrono>
  83. #include <limits>
  84. #include <locale>
  85. #include <random>
  86. #define TEXT_GREEN "\033[32m"
  87. #define TEXT_YELLOW "\033[33m"
  88. #define TEXT_RED "\033[31m"
  89. #define CLR "\033[0m"
  90. #endif // defined(__aarch64__) && defined(LINUX)
  91. class DataReaderFromEmpty : public ncnn::DataReader
  92. {
  93. public:
  94. virtual int scan(const char* format, void* p) const
  95. {
  96. return 0;
  97. }
  98. virtual size_t read(void* /*buf*/, size_t size) const
  99. {
  100. return size;
  101. }
  102. };
  103. class NetOptimize : public ncnn::Net
  104. {
  105. public:
  106. // 0=fp32 1=fp16
  107. int storage_type;
  108. public:
  109. int fuse_batchnorm_scale();
  110. int fuse_convolution_batchnorm();
  111. int fuse_convolution_mul();
  112. int fuse_convolution_add();
  113. int fuse_convolutiondepthwise_batchnorm();
  114. int fuse_convolutiondepthwise_mul();
  115. int fuse_convolutiondepthwise_add();
  116. int fuse_deconvolution_batchnorm();
  117. int fuse_deconvolution_mul();
  118. int fuse_deconvolution_add();
  119. int fuse_deconvolutiondepthwise_batchnorm();
  120. int fuse_innerproduct_batchnorm();
  121. int fuse_innerproduct_add();
  122. int fuse_innerproduct_dropout();
  123. int fuse_convolution_activation();
  124. int fuse_convolutiondepthwise_activation();
  125. int fuse_deconvolution_activation();
  126. int fuse_deconvolutiondepthwise_activation();
  127. int fuse_innerproduct_activation();
  128. int fuse_memorydata_binaryop();
  129. int fuse_binaryop_eltwise();
  130. int eliminate_dropout();
  131. int eliminate_pooling1x1();
  132. int eliminate_noop();
  133. int eliminate_orphaned_memorydata();
  134. int eliminate_flatten_after_global_pooling();
  135. int eliminate_reshape_after_global_pooling();
  136. int eliminate_flatten_after_innerproduct();
  137. int eliminate_reshape_before_binaryop();
  138. int replace_convolution_with_innerproduct_after_global_pooling();
  139. int replace_convolution_with_innerproduct_after_innerproduct();
  140. int shape_inference();
  141. public:
  142. int fprintf_param_int_array(int id, const ncnn::Mat& m, FILE* pp);
  143. int fprintf_param_float_array(int id, const ncnn::Mat& m, FILE* pp);
  144. int fwrite_weight_tag_data(int tag, const ncnn::Mat& data, FILE* bp);
  145. int fwrite_weight_data(const ncnn::Mat& data, FILE* bp);
  146. int save(const char* parampath, const char* binpath);
  147. #if defined(__aarch64__) && defined(LINUX)
  148. void gauss_random(ncnn::Mat& m);
  149. void find_fastest_fp32_conv(const char* name, int w, int h, int c);
  150. int support_fp32_conv_type(const ncnn::Convolution* op, const ncnn::Mat& mat, const int type);
  151. #endif
  152. };
  153. #if defined(__aarch64__) && defined(LINUX)
  154. void NetOptimize::gauss_random(ncnn::Mat& m)
  155. {
  156. std::random_device rd;
  157. std::mt19937 gen(rd());
  158. std::normal_distribution<float> d(1.0f, 1.0f);
  159. int size = m.total();
  160. for (int i = 0; i < size; ++i)
  161. {
  162. m[i] = d(gen);
  163. }
  164. }
  165. void NetOptimize::find_fastest_fp32_conv(const char* dataname, int w, int h, int c)
  166. {
  167. ncnn::PoolAllocator allocator;
  168. allocator.clear();
  169. ncnn::Option opt;
  170. // embeded system generally use single thread
  171. opt.num_threads = 1;
  172. const size_t layer_count = layers.size();
  173. ncnn::Extractor ex = create_extractor();
  174. ncnn::Mat input(w, h, c);
  175. if (ex.input(dataname, input) < 0)
  176. {
  177. fprintf(stderr, "set input failed, check dataname.\n");
  178. return;
  179. }
  180. const char* IMPL_NAME[6] = {"baseline", "winograd", "pointwise", "im2col", "direct", "conv3x3s2"};
  181. for (int i = 0; i < layer_count; ++i)
  182. {
  183. ncnn::Layer* layer = layers[i];
  184. if (layer->type == "Convolution")
  185. {
  186. ncnn::Convolution* op = (ncnn::Convolution*)layer;
  187. ncnn::Mat bottom_blob;
  188. ncnn::Mat top_blob;
  189. ex.extract(layer->bottoms[0], bottom_blob);
  190. ex.extract(layer->tops[0], top_blob);
  191. if (bottom_blob.empty() || top_blob.empty())
  192. {
  193. continue;
  194. }
  195. ncnn::Mat weight_blob(op->kernel_w, op->kernel_h, bottom_blob.c * top_blob.c);
  196. fprintf(stdout, TEXT_GREEN "Input [w h nc]: %d %d %d\n" CLR, bottom_blob.w, bottom_blob.h, bottom_blob.c);
  197. 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);
  198. fprintf(stdout, TEXT_GREEN "Output [w h nc]: %d %d %d\n" CLR, top_blob.w, top_blob.h, top_blob.c);
  199. // randomize input and kernel
  200. gauss_random(bottom_blob);
  201. // try every implementation
  202. double min_cost = std::numeric_limits<double>::max();
  203. int best_type = 0;
  204. // how much conv implementation type ncnn has ?
  205. for (int type = 1; type <= 5; ++type)
  206. {
  207. int support = support_fp32_conv_type(op, bottom_blob, type);
  208. if (support < 1)
  209. {
  210. // implementation type mismatch convolution configuration, skip
  211. continue;
  212. }
  213. op->impl_type = type;
  214. auto start = std::chrono::high_resolution_clock::now();
  215. const int NREPEATS = 20;
  216. op->create_pipeline(opt);
  217. for (int repeat = 0; repeat < NREPEATS; ++repeat)
  218. {
  219. op->forward(top_blob, bottom_blob, opt);
  220. }
  221. op->destroy_pipeline(opt);
  222. auto stop = std::chrono::high_resolution_clock::now();
  223. double cur_cost = std::chrono::duration<double, std::micro>(stop - start).count() / NREPEATS;
  224. fprintf(stdout, TEXT_GREEN "%s cost %0.3lfms \n" CLR, IMPL_NAME[type], cur_cost / 1000);
  225. if (cur_cost < min_cost)
  226. {
  227. min_cost = cur_cost;
  228. best_type = type;
  229. }
  230. }
  231. op->impl_type = best_type;
  232. fprintf(stdout, TEXT_YELLOW "%d: %s use %s \n\n" CLR, i, layer->name.c_str(), IMPL_NAME[op->impl_type]);
  233. }
  234. }
  235. }
  236. int NetOptimize::support_fp32_conv_type(const ncnn::Convolution* op, const ncnn::Mat& bottom, const int type)
  237. {
  238. // not baseline, then k_h == k_w and s_h == s_w
  239. // no dilation conv shall be allowed
  240. if (op->kernel_w != op->kernel_h || op->stride_w != op->stride_h || op->dilation_w != op->dilation_h || op->dilation_h != 1)
  241. {
  242. return -1;
  243. }
  244. // (kernel, stride) in {(1, 1), (1, 2), (2, 1), (3, 1), (3, 2), (4, 4), (5, 1), (5, 2), (7, 1), (7, 2)}
  245. const int support_table[7][4] = {
  246. {1, 1, 0, 0},
  247. {1, 0, 0, 0},
  248. {1, 1, 0, 0},
  249. {0, 0, 0, 1},
  250. {1, 1, 0, 0},
  251. {0, 0, 0, 0},
  252. {1, 1, 0, 0}
  253. };
  254. // kernel_size x stride
  255. const int kernel = op->kernel_h,
  256. stride = op->stride_h;
  257. // if match prequisation
  258. switch (type)
  259. {
  260. case 1:
  261. // winograd
  262. if (kernel != 3 || stride != 1)
  263. {
  264. return -1;
  265. }
  266. break;
  267. case 2:
  268. // pointwise
  269. // input_h == 1, input_w == 1, dilation == 1, stride == 1
  270. if (bottom.h != 1 || bottom.w != 1 || stride != 1)
  271. {
  272. return -1;
  273. }
  274. break;
  275. case 3:
  276. // im2col
  277. break;
  278. case 4:
  279. // direct conv
  280. if (support_table[kernel - 1][stride - 1] == 0)
  281. {
  282. return -1;
  283. }
  284. break;
  285. case 5:
  286. // conv3x3s2
  287. // kernel == 3 and stride == 2
  288. if (kernel != 3 || stride != 2)
  289. {
  290. return -1;
  291. }
  292. break;
  293. default:
  294. fprintf(stderr, TEXT_RED "unrecognize convolution impl type: %d" CLR, type);
  295. break;
  296. }
  297. return 1;
  298. }
  299. #endif // defined(__aarch64__) && defined(LINUX)
  300. int NetOptimize::fuse_batchnorm_scale()
  301. {
  302. const size_t layer_count = layers.size();
  303. for (int i = 0; i < layer_count; i++)
  304. {
  305. if (layers[i]->type != "BatchNorm")
  306. continue;
  307. // BatchNorm - Scale
  308. int top_blob_index = layers[i]->tops[0];
  309. int j = i + 1;
  310. for (; j < layer_count; j++)
  311. {
  312. if (layers[j]->type != "Scale")
  313. continue;
  314. if (layers[j]->bottoms.size() != 1)
  315. continue;
  316. if (layers[j]->bottoms[0] == top_blob_index)
  317. break;
  318. }
  319. if (j == layer_count)
  320. continue;
  321. // fuse BatchNorm - Scale to BatchNorm
  322. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[i];
  323. ncnn::Scale* scale = (ncnn::Scale*)layers[j];
  324. fprintf(stderr, "fuse_batchnorm_scale %s %s\n", batchnorm->name.c_str(), scale->name.c_str());
  325. {
  326. // v = ((v - mean) / sqrt(var + eps) * slope + bias) * s + b
  327. // = (v - mean) / sqrt(var + eps) * (slope * s) + (bias * s + b)
  328. int channels = batchnorm->channels;
  329. float* slope = batchnorm->slope_data;
  330. float* bias = batchnorm->bias_data;
  331. for (int q = 0; q < channels; q++)
  332. {
  333. slope[q] = slope[q] * scale->scale_data[q];
  334. if (scale->bias_term)
  335. bias[q] = bias[q] * scale->scale_data[q] + scale->bias_data[q];
  336. else
  337. bias[q] = bias[q] * scale->scale_data[q];
  338. }
  339. }
  340. int top_blob_index_final = scale->tops[0];
  341. batchnorm->tops[0] = top_blob_index_final;
  342. blobs[top_blob_index_final].producer = i;
  343. scale->type = "ncnnfused";
  344. }
  345. return 0;
  346. }
  347. int NetOptimize::fuse_convolution_batchnorm()
  348. {
  349. const size_t layer_count = layers.size();
  350. for (int i = 0; i < layer_count; i++)
  351. {
  352. if (layers[i]->type != "Convolution")
  353. continue;
  354. // Convolution - BatchNorm
  355. int top_blob_index = layers[i]->tops[0];
  356. int j = i + 1;
  357. for (; j < layer_count; j++)
  358. {
  359. if (layers[j]->type != "BatchNorm")
  360. continue;
  361. if (layers[j]->bottoms.size() != 1)
  362. continue;
  363. if (layers[j]->bottoms[0] == top_blob_index)
  364. break;
  365. }
  366. if (j == layer_count)
  367. continue;
  368. // fuse Convolution - BatchNorm to Convolution
  369. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[i];
  370. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[j];
  371. fprintf(stderr, "fuse_convolution_batchnorm %s %s\n", convolution->name.c_str(), batchnorm->name.c_str());
  372. {
  373. int channels = batchnorm->channels;
  374. float eps = batchnorm->eps;
  375. // a = bias - slope * mean / sqrt(var + eps)
  376. // b = slope / sqrt(var + eps)
  377. // value = value * b + a
  378. std::vector<float> a(channels);
  379. std::vector<float> b(channels);
  380. for (int i = 0; i < channels; i++)
  381. {
  382. float sqrt_var = static_cast<float>(sqrt(batchnorm->var_data[i] + eps));
  383. a[i] = batchnorm->bias_data[i] - batchnorm->slope_data[i] * batchnorm->mean_data[i] / sqrt_var;
  384. b[i] = batchnorm->slope_data[i] / sqrt_var;
  385. }
  386. if (convolution->bias_term == 0)
  387. {
  388. // init bias as zero
  389. convolution->bias_term = 1;
  390. convolution->bias_data = ncnn::Mat(channels);
  391. convolution->bias_data.fill(0.f);
  392. }
  393. const int weight_per_outch = convolution->weight_data_size / channels;
  394. float* weight = convolution->weight_data;
  395. float* bias = convolution->bias_data;
  396. for (int i = 0; i < channels; i++)
  397. {
  398. float* conv_weight_outch = weight + weight_per_outch * i;
  399. for (int j = 0; j < weight_per_outch; j++)
  400. {
  401. conv_weight_outch[j] *= b[i];
  402. }
  403. bias[i] = bias[i] * b[i] + a[i];
  404. }
  405. }
  406. int top_blob_index_final = batchnorm->tops[0];
  407. convolution->tops[0] = top_blob_index_final;
  408. blobs[top_blob_index_final].producer = i;
  409. batchnorm->type = "ncnnfused";
  410. }
  411. return 0;
  412. }
  413. int NetOptimize::fuse_convolution_mul()
  414. {
  415. const size_t layer_count = layers.size();
  416. for (int i = 0; i < layer_count; i++)
  417. {
  418. if (layers[i]->type != "Convolution")
  419. continue;
  420. // Convolution - BinaryOp
  421. int top_blob_index = layers[i]->tops[0];
  422. int j = i + 1;
  423. for (; j < layer_count; j++)
  424. {
  425. if (layers[j]->type != "BinaryOp")
  426. continue;
  427. if (layers[j]->bottoms.size() != 2)
  428. continue;
  429. if (layers[j]->bottoms[0] == top_blob_index)
  430. break;
  431. }
  432. if (j == layer_count)
  433. continue;
  434. // fuse Convolution - BinaryOp to Convolution
  435. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[i];
  436. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  437. if (binaryop->op_type != 2 || binaryop->with_scalar)
  438. continue;
  439. // MemoryData - ..... - BinaryOp
  440. int k = 0;
  441. for (; k < j; k++)
  442. {
  443. if (layers[k]->type != "MemoryData")
  444. continue;
  445. if (layers[k]->tops[0] == binaryop->bottoms[1])
  446. break;
  447. }
  448. if (k == j)
  449. continue;
  450. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[k];
  451. int channels = convolution->num_output;
  452. if (memorydata->w != channels || memorydata->h != 0 || memorydata->c != 0)
  453. {
  454. // not bias-like broadcasting type
  455. continue;
  456. }
  457. fprintf(stderr, "fuse_convolution_mul %s %s\n", convolution->name.c_str(), binaryop->name.c_str());
  458. {
  459. const int weight_per_outch = convolution->weight_data_size / channels;
  460. float* weight = convolution->weight_data;
  461. float* bias = convolution->bias_data;
  462. for (int i = 0; i < channels; i++)
  463. {
  464. float* conv_weight_outch = weight + weight_per_outch * i;
  465. for (int j = 0; j < weight_per_outch; j++)
  466. {
  467. conv_weight_outch[j] *= memorydata->data[i];
  468. }
  469. if (bias)
  470. {
  471. bias[i] = bias[i] * memorydata->data[i];
  472. }
  473. }
  474. }
  475. int top_blob_index_final = binaryop->tops[0];
  476. convolution->tops[0] = top_blob_index_final;
  477. blobs[top_blob_index_final].producer = i;
  478. binaryop->type = "ncnnfused";
  479. }
  480. return 0;
  481. }
  482. int NetOptimize::fuse_convolution_add()
  483. {
  484. const size_t layer_count = layers.size();
  485. for (int i = 0; i < layer_count; i++)
  486. {
  487. if (layers[i]->type != "Convolution")
  488. continue;
  489. // Convolution - BinaryOp
  490. int top_blob_index = layers[i]->tops[0];
  491. int j = i + 1;
  492. for (; j < layer_count; j++)
  493. {
  494. if (layers[j]->type != "BinaryOp")
  495. continue;
  496. if (layers[j]->bottoms.size() != 2)
  497. continue;
  498. if (layers[j]->bottoms[0] == top_blob_index)
  499. break;
  500. }
  501. if (j == layer_count)
  502. continue;
  503. // fuse Convolution - BinaryOp to Convolution
  504. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[i];
  505. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  506. if (binaryop->op_type != 0 || binaryop->with_scalar)
  507. continue;
  508. // MemoryData - ..... - BinaryOp
  509. int k = 0;
  510. for (; k < j; k++)
  511. {
  512. if (layers[k]->type != "MemoryData")
  513. continue;
  514. if (layers[k]->tops[0] == binaryop->bottoms[1])
  515. break;
  516. }
  517. if (k == j)
  518. continue;
  519. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[k];
  520. int channels = convolution->num_output;
  521. if (memorydata->w != channels || memorydata->h != 0 || memorydata->c != 0)
  522. {
  523. // not bias-like broadcasting type
  524. continue;
  525. }
  526. fprintf(stderr, "fuse_convolution_add %s %s\n", convolution->name.c_str(), binaryop->name.c_str());
  527. {
  528. if (convolution->bias_term == 0)
  529. {
  530. // init bias
  531. convolution->bias_term = 1;
  532. convolution->bias_data = memorydata->data;
  533. }
  534. else
  535. {
  536. float* bias = convolution->bias_data;
  537. for (int i = 0; i < channels; i++)
  538. {
  539. bias[i] = bias[i] + memorydata->data[i];
  540. }
  541. }
  542. }
  543. int top_blob_index_final = binaryop->tops[0];
  544. convolution->tops[0] = top_blob_index_final;
  545. blobs[top_blob_index_final].producer = i;
  546. binaryop->type = "ncnnfused";
  547. }
  548. return 0;
  549. }
  550. int NetOptimize::fuse_convolutiondepthwise_batchnorm()
  551. {
  552. const size_t layer_count = layers.size();
  553. for (int i = 0; i < layer_count; i++)
  554. {
  555. if (layers[i]->type != "ConvolutionDepthWise")
  556. continue;
  557. // ConvolutionDepthWise - BatchNorm
  558. int top_blob_index = layers[i]->tops[0];
  559. int j = i + 1;
  560. for (; j < layer_count; j++)
  561. {
  562. if (layers[j]->type != "BatchNorm")
  563. continue;
  564. if (layers[j]->bottoms.size() != 1)
  565. continue;
  566. if (layers[j]->bottoms[0] == top_blob_index)
  567. break;
  568. }
  569. if (j == layer_count)
  570. continue;
  571. // fuse ConvolutionDepthWise - BatchNorm to ConvolutionDepthWise
  572. ncnn::ConvolutionDepthWise* convolutiondepthwise = (ncnn::ConvolutionDepthWise*)layers[i];
  573. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[j];
  574. fprintf(stderr, "fuse_convolutiondepthwise_batchnorm %s %s\n", convolutiondepthwise->name.c_str(), batchnorm->name.c_str());
  575. {
  576. int channels = batchnorm->channels;
  577. float eps = batchnorm->eps;
  578. // a = bias - slope * mean / sqrt(var + eps)
  579. // b = slope / sqrt(var + eps)
  580. // value = value * b + a
  581. std::vector<float> a(channels);
  582. std::vector<float> b(channels);
  583. for (int i = 0; i < channels; i++)
  584. {
  585. float sqrt_var = static_cast<float>(sqrt(batchnorm->var_data[i] + eps));
  586. a[i] = batchnorm->bias_data[i] - batchnorm->slope_data[i] * batchnorm->mean_data[i] / sqrt_var;
  587. b[i] = batchnorm->slope_data[i] / sqrt_var;
  588. }
  589. if (convolutiondepthwise->bias_term == 0)
  590. {
  591. // init bias as zero
  592. convolutiondepthwise->bias_term = 1;
  593. convolutiondepthwise->bias_data = ncnn::Mat(channels);
  594. convolutiondepthwise->bias_data.fill(0.f);
  595. }
  596. const int weight_per_outch = convolutiondepthwise->weight_data_size / channels;
  597. float* weight = convolutiondepthwise->weight_data;
  598. float* bias = convolutiondepthwise->bias_data;
  599. for (int i = 0; i < channels; i++)
  600. {
  601. float* conv_weight_outch = weight + weight_per_outch * i;
  602. for (int j = 0; j < weight_per_outch; j++)
  603. {
  604. conv_weight_outch[j] *= b[i];
  605. }
  606. bias[i] = bias[i] * b[i] + a[i];
  607. }
  608. }
  609. int top_blob_index_final = batchnorm->tops[0];
  610. convolutiondepthwise->tops[0] = top_blob_index_final;
  611. blobs[top_blob_index_final].producer = i;
  612. batchnorm->type = "ncnnfused";
  613. }
  614. return 0;
  615. }
  616. int NetOptimize::fuse_convolutiondepthwise_mul()
  617. {
  618. const size_t layer_count = layers.size();
  619. for (int i = 0; i < layer_count; i++)
  620. {
  621. if (layers[i]->type != "ConvolutionDepthWise")
  622. continue;
  623. // ConvolutionDepthWise - BinaryOp
  624. int top_blob_index = layers[i]->tops[0];
  625. int j = i + 1;
  626. for (; j < layer_count; j++)
  627. {
  628. if (layers[j]->type != "BinaryOp")
  629. continue;
  630. if (layers[j]->bottoms.size() != 2)
  631. continue;
  632. if (layers[j]->bottoms[0] == top_blob_index)
  633. break;
  634. }
  635. if (j == layer_count)
  636. continue;
  637. // fuse ConvolutionDepthWise - BinaryOp to ConvolutionDepthWise
  638. ncnn::ConvolutionDepthWise* convolutiondepthwise = (ncnn::ConvolutionDepthWise*)layers[i];
  639. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  640. if (binaryop->op_type != 2 || binaryop->with_scalar)
  641. continue;
  642. // MemoryData - ..... - BinaryOp
  643. int k = 0;
  644. for (; k < j; k++)
  645. {
  646. if (layers[k]->type != "MemoryData")
  647. continue;
  648. if (layers[k]->tops[0] == binaryop->bottoms[1])
  649. break;
  650. }
  651. if (k == j)
  652. continue;
  653. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[k];
  654. int channels = convolutiondepthwise->num_output;
  655. if (memorydata->w != channels || memorydata->h != 0 || memorydata->c != 0)
  656. {
  657. // not bias-like broadcasting type
  658. continue;
  659. }
  660. fprintf(stderr, "fuse_convolutiondepthwise_mul %s %s\n", convolutiondepthwise->name.c_str(), binaryop->name.c_str());
  661. {
  662. const int weight_per_outch = convolutiondepthwise->weight_data_size / channels;
  663. float* weight = convolutiondepthwise->weight_data;
  664. float* bias = convolutiondepthwise->bias_data;
  665. for (int i = 0; i < channels; i++)
  666. {
  667. float* conv_weight_outch = weight + weight_per_outch * i;
  668. for (int j = 0; j < weight_per_outch; j++)
  669. {
  670. conv_weight_outch[j] *= memorydata->data[i];
  671. }
  672. if (bias)
  673. {
  674. bias[i] = bias[i] * memorydata->data[i];
  675. }
  676. }
  677. }
  678. int top_blob_index_final = binaryop->tops[0];
  679. convolutiondepthwise->tops[0] = top_blob_index_final;
  680. blobs[top_blob_index_final].producer = i;
  681. binaryop->type = "ncnnfused";
  682. }
  683. return 0;
  684. }
  685. int NetOptimize::fuse_convolutiondepthwise_add()
  686. {
  687. const size_t layer_count = layers.size();
  688. for (int i = 0; i < layer_count; i++)
  689. {
  690. if (layers[i]->type != "ConvolutionDepthWise")
  691. continue;
  692. // ConvolutionDepthWise - BinaryOp
  693. int top_blob_index = layers[i]->tops[0];
  694. int j = i + 1;
  695. for (; j < layer_count; j++)
  696. {
  697. if (layers[j]->type != "BinaryOp")
  698. continue;
  699. if (layers[j]->bottoms.size() != 2)
  700. continue;
  701. if (layers[j]->bottoms[0] == top_blob_index)
  702. break;
  703. }
  704. if (j == layer_count)
  705. continue;
  706. // fuse ConvolutionDepthWise - BinaryOp to ConvolutionDepthWise
  707. ncnn::ConvolutionDepthWise* convolutiondepthwise = (ncnn::ConvolutionDepthWise*)layers[i];
  708. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  709. if (binaryop->op_type != 0 || binaryop->with_scalar)
  710. continue;
  711. // MemoryData - ..... - BinaryOp
  712. int k = 0;
  713. for (; k < j; k++)
  714. {
  715. if (layers[k]->type != "MemoryData")
  716. continue;
  717. if (layers[k]->tops[0] == binaryop->bottoms[1])
  718. break;
  719. }
  720. if (k == j)
  721. continue;
  722. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[k];
  723. int channels = convolutiondepthwise->num_output;
  724. if (memorydata->w != channels || memorydata->h != 0 || memorydata->c != 0)
  725. {
  726. // not bias-like broadcasting type
  727. continue;
  728. }
  729. fprintf(stderr, "fuse_convolutiondepthwise_add %s %s\n", convolutiondepthwise->name.c_str(), binaryop->name.c_str());
  730. {
  731. if (convolutiondepthwise->bias_term == 0)
  732. {
  733. // init bias
  734. convolutiondepthwise->bias_term = 1;
  735. convolutiondepthwise->bias_data = memorydata->data;
  736. }
  737. else
  738. {
  739. float* bias = convolutiondepthwise->bias_data;
  740. for (int i = 0; i < channels; i++)
  741. {
  742. bias[i] = bias[i] + memorydata->data[i];
  743. }
  744. }
  745. }
  746. int top_blob_index_final = binaryop->tops[0];
  747. convolutiondepthwise->tops[0] = top_blob_index_final;
  748. blobs[top_blob_index_final].producer = i;
  749. binaryop->type = "ncnnfused";
  750. }
  751. return 0;
  752. }
  753. int NetOptimize::fuse_deconvolution_batchnorm()
  754. {
  755. const size_t layer_count = layers.size();
  756. for (int i = 0; i < layer_count; i++)
  757. {
  758. if (layers[i]->type != "Deconvolution")
  759. continue;
  760. // Deconvolution - BatchNorm
  761. int top_blob_index = layers[i]->tops[0];
  762. int j = i + 1;
  763. for (; j < layer_count; j++)
  764. {
  765. if (layers[j]->type != "BatchNorm")
  766. continue;
  767. if (layers[j]->bottoms.size() != 1)
  768. continue;
  769. if (layers[j]->bottoms[0] == top_blob_index)
  770. break;
  771. }
  772. if (j == layer_count)
  773. continue;
  774. // fuse Deconvolution - BatchNorm to Deconvolution
  775. ncnn::Deconvolution* deconvolution = (ncnn::Deconvolution*)layers[i];
  776. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[j];
  777. fprintf(stderr, "fuse_deconvolution_batchnorm %s %s\n", deconvolution->name.c_str(), batchnorm->name.c_str());
  778. {
  779. int channels = batchnorm->channels;
  780. float eps = batchnorm->eps;
  781. // a = bias - slope * mean / sqrt(var + eps)
  782. // b = slope / sqrt(var + eps)
  783. // value = value * b + a
  784. std::vector<float> a(channels);
  785. std::vector<float> b(channels);
  786. for (int i = 0; i < channels; i++)
  787. {
  788. float sqrt_var = static_cast<float>(sqrt(batchnorm->var_data[i] + eps));
  789. a[i] = batchnorm->bias_data[i] - batchnorm->slope_data[i] * batchnorm->mean_data[i] / sqrt_var;
  790. b[i] = batchnorm->slope_data[i] / sqrt_var;
  791. }
  792. if (deconvolution->bias_term == 0)
  793. {
  794. // init bias as zero
  795. deconvolution->bias_term = 1;
  796. deconvolution->bias_data = ncnn::Mat(channels);
  797. deconvolution->bias_data.fill(0.f);
  798. }
  799. const int weight_per_outch = deconvolution->weight_data_size / channels;
  800. float* weight = deconvolution->weight_data;
  801. float* bias = deconvolution->bias_data;
  802. for (int i = 0; i < channels; i++)
  803. {
  804. float* conv_weight_outch = weight + weight_per_outch * i;
  805. for (int j = 0; j < weight_per_outch; j++)
  806. {
  807. conv_weight_outch[j] *= b[i];
  808. }
  809. bias[i] = bias[i] * b[i] + a[i];
  810. }
  811. }
  812. int top_blob_index_final = batchnorm->tops[0];
  813. deconvolution->tops[0] = top_blob_index_final;
  814. blobs[top_blob_index_final].producer = i;
  815. batchnorm->type = "ncnnfused";
  816. }
  817. return 0;
  818. }
  819. int NetOptimize::fuse_deconvolution_mul()
  820. {
  821. const size_t layer_count = layers.size();
  822. for (int i = 0; i < layer_count; i++)
  823. {
  824. if (layers[i]->type != "Deconvolution")
  825. continue;
  826. // Deconvolution - BinaryOp
  827. int top_blob_index = layers[i]->tops[0];
  828. int j = i + 1;
  829. for (; j < layer_count; j++)
  830. {
  831. if (layers[j]->type != "BinaryOp")
  832. continue;
  833. if (layers[j]->bottoms.size() != 2)
  834. continue;
  835. if (layers[j]->bottoms[0] == top_blob_index)
  836. break;
  837. }
  838. if (j == layer_count)
  839. continue;
  840. // fuse Deconvolution - BinaryOp to Deconvolution
  841. ncnn::Deconvolution* deconvolution = (ncnn::Deconvolution*)layers[i];
  842. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  843. if (binaryop->op_type != 2 || binaryop->with_scalar)
  844. continue;
  845. // MemoryData - ..... - BinaryOp
  846. int k = 0;
  847. for (; k < j; k++)
  848. {
  849. if (layers[k]->type != "MemoryData")
  850. continue;
  851. if (layers[k]->tops[0] == binaryop->bottoms[1])
  852. break;
  853. }
  854. if (k == j)
  855. continue;
  856. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[k];
  857. int channels = deconvolution->num_output;
  858. if (memorydata->w != channels || memorydata->h != 0 || memorydata->c != 0)
  859. {
  860. // not bias-like broadcasting type
  861. continue;
  862. }
  863. fprintf(stderr, "fuse_deconvolution_mul %s %s\n", deconvolution->name.c_str(), binaryop->name.c_str());
  864. {
  865. const int weight_per_outch = deconvolution->weight_data_size / channels;
  866. float* weight = deconvolution->weight_data;
  867. float* bias = deconvolution->bias_data;
  868. for (int i = 0; i < channels; i++)
  869. {
  870. float* conv_weight_outch = weight + weight_per_outch * i;
  871. for (int j = 0; j < weight_per_outch; j++)
  872. {
  873. conv_weight_outch[j] *= memorydata->data[i];
  874. }
  875. if (bias)
  876. {
  877. bias[i] = bias[i] * memorydata->data[i];
  878. }
  879. }
  880. }
  881. int top_blob_index_final = binaryop->tops[0];
  882. deconvolution->tops[0] = top_blob_index_final;
  883. blobs[top_blob_index_final].producer = i;
  884. binaryop->type = "ncnnfused";
  885. }
  886. return 0;
  887. }
  888. int NetOptimize::fuse_deconvolution_add()
  889. {
  890. const size_t layer_count = layers.size();
  891. for (int i = 0; i < layer_count; i++)
  892. {
  893. if (layers[i]->type != "Deconvolution")
  894. continue;
  895. // Deconvolution - BinaryOp
  896. int top_blob_index = layers[i]->tops[0];
  897. int j = i + 1;
  898. for (; j < layer_count; j++)
  899. {
  900. if (layers[j]->type != "BinaryOp")
  901. continue;
  902. if (layers[j]->bottoms.size() != 2)
  903. continue;
  904. if (layers[j]->bottoms[0] == top_blob_index)
  905. break;
  906. }
  907. if (j == layer_count)
  908. continue;
  909. // fuse Deconvolution - BinaryOp to Deconvolution
  910. ncnn::Deconvolution* deconvolution = (ncnn::Deconvolution*)layers[i];
  911. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  912. if (binaryop->op_type != 0 || binaryop->with_scalar)
  913. continue;
  914. // MemoryData - ..... - BinaryOp
  915. int k = 0;
  916. for (; k < j; k++)
  917. {
  918. if (layers[k]->type != "MemoryData")
  919. continue;
  920. if (layers[k]->tops[0] == binaryop->bottoms[1])
  921. break;
  922. }
  923. if (k == j)
  924. continue;
  925. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[k];
  926. int channels = deconvolution->num_output;
  927. if (memorydata->w != channels || memorydata->h != 0 || memorydata->c != 0)
  928. {
  929. // not bias-like broadcasting type
  930. continue;
  931. }
  932. fprintf(stderr, "fuse_deconvolution_add %s %s\n", deconvolution->name.c_str(), binaryop->name.c_str());
  933. {
  934. if (deconvolution->bias_term == 0)
  935. {
  936. // init bias
  937. deconvolution->bias_term = 1;
  938. deconvolution->bias_data = memorydata->data;
  939. }
  940. else
  941. {
  942. float* bias = deconvolution->bias_data;
  943. for (int i = 0; i < channels; i++)
  944. {
  945. bias[i] = bias[i] + memorydata->data[i];
  946. }
  947. }
  948. }
  949. int top_blob_index_final = binaryop->tops[0];
  950. deconvolution->tops[0] = top_blob_index_final;
  951. blobs[top_blob_index_final].producer = i;
  952. binaryop->type = "ncnnfused";
  953. }
  954. return 0;
  955. }
  956. int NetOptimize::fuse_deconvolutiondepthwise_batchnorm()
  957. {
  958. const size_t layer_count = layers.size();
  959. for (int i = 0; i < layer_count; i++)
  960. {
  961. if (layers[i]->type != "DeconvolutionDepthWise")
  962. continue;
  963. // DeconvolutionDepthWise - BatchNorm
  964. int top_blob_index = layers[i]->tops[0];
  965. int j = i + 1;
  966. for (; j < layer_count; j++)
  967. {
  968. if (layers[j]->type != "BatchNorm")
  969. continue;
  970. if (layers[j]->bottoms.size() != 1)
  971. continue;
  972. if (layers[j]->bottoms[0] == top_blob_index)
  973. break;
  974. }
  975. if (j == layer_count)
  976. continue;
  977. // fuse DeconvolutionDepthWise - BatchNorm to DeconvolutionDepthWise
  978. ncnn::DeconvolutionDepthWise* deconvolutiondepthwise = (ncnn::DeconvolutionDepthWise*)layers[i];
  979. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[j];
  980. fprintf(stderr, "fuse_deconvolutiondepthwise_batchnorm %s %s\n", deconvolutiondepthwise->name.c_str(), batchnorm->name.c_str());
  981. {
  982. int channels = batchnorm->channels;
  983. float eps = batchnorm->eps;
  984. // a = bias - slope * mean / sqrt(var + eps)
  985. // b = slope / sqrt(var + eps)
  986. // value = value * b + a
  987. std::vector<float> a(channels);
  988. std::vector<float> b(channels);
  989. for (int i = 0; i < channels; i++)
  990. {
  991. float sqrt_var = static_cast<float>(sqrt(batchnorm->var_data[i] + eps));
  992. a[i] = batchnorm->bias_data[i] - batchnorm->slope_data[i] * batchnorm->mean_data[i] / sqrt_var;
  993. b[i] = batchnorm->slope_data[i] / sqrt_var;
  994. }
  995. if (deconvolutiondepthwise->bias_term == 0)
  996. {
  997. // init bias as zero
  998. deconvolutiondepthwise->bias_term = 1;
  999. deconvolutiondepthwise->bias_data = ncnn::Mat(channels);
  1000. deconvolutiondepthwise->bias_data.fill(0.f);
  1001. }
  1002. const int weight_per_outch = deconvolutiondepthwise->weight_data_size / channels;
  1003. float* weight = deconvolutiondepthwise->weight_data;
  1004. float* bias = deconvolutiondepthwise->bias_data;
  1005. for (int i = 0; i < channels; i++)
  1006. {
  1007. float* conv_weight_outch = weight + weight_per_outch * i;
  1008. for (int j = 0; j < weight_per_outch; j++)
  1009. {
  1010. conv_weight_outch[j] *= b[i];
  1011. }
  1012. bias[i] = bias[i] * b[i] + a[i];
  1013. }
  1014. }
  1015. int top_blob_index_final = batchnorm->tops[0];
  1016. deconvolutiondepthwise->tops[0] = top_blob_index_final;
  1017. blobs[top_blob_index_final].producer = i;
  1018. batchnorm->type = "ncnnfused";
  1019. }
  1020. return 0;
  1021. }
  1022. int NetOptimize::fuse_innerproduct_batchnorm()
  1023. {
  1024. const size_t layer_count = layers.size();
  1025. for (int i = 0; i < layer_count; i++)
  1026. {
  1027. if (layers[i]->type != "InnerProduct")
  1028. continue;
  1029. // InnerProduct - BatchNorm
  1030. int top_blob_index = layers[i]->tops[0];
  1031. int j = i + 1;
  1032. for (; j < layer_count; j++)
  1033. {
  1034. if (layers[j]->type != "BatchNorm")
  1035. continue;
  1036. if (layers[j]->bottoms.size() != 1)
  1037. continue;
  1038. if (layers[j]->bottoms[0] == top_blob_index)
  1039. break;
  1040. }
  1041. if (j == layer_count)
  1042. continue;
  1043. // fuse InnerProduct - BatchNorm to InnerProduct
  1044. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  1045. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[j];
  1046. fprintf(stderr, "fuse_innerproduct_batchnorm %s %s\n", innerproduct->name.c_str(), batchnorm->name.c_str());
  1047. {
  1048. int channels = batchnorm->channels;
  1049. float eps = batchnorm->eps;
  1050. // a = bias - slope * mean / sqrt(var + eps)
  1051. // b = slope / sqrt(var + eps)
  1052. // value = value * b + a
  1053. std::vector<float> a(channels);
  1054. std::vector<float> b(channels);
  1055. for (int i = 0; i < channels; i++)
  1056. {
  1057. float sqrt_var = static_cast<float>(sqrt(batchnorm->var_data[i] + eps));
  1058. a[i] = batchnorm->bias_data[i] - batchnorm->slope_data[i] * batchnorm->mean_data[i] / sqrt_var;
  1059. b[i] = batchnorm->slope_data[i] / sqrt_var;
  1060. }
  1061. if (innerproduct->bias_term == 0)
  1062. {
  1063. // init bias as zero
  1064. innerproduct->bias_term = 1;
  1065. innerproduct->bias_data = ncnn::Mat(channels);
  1066. innerproduct->bias_data.fill(0.f);
  1067. }
  1068. const int weight_per_outch = innerproduct->weight_data_size / channels;
  1069. float* weight = innerproduct->weight_data;
  1070. float* bias = innerproduct->bias_data;
  1071. for (int i = 0; i < channels; i++)
  1072. {
  1073. float* conv_weight_outch = weight + weight_per_outch * i;
  1074. for (int j = 0; j < weight_per_outch; j++)
  1075. {
  1076. conv_weight_outch[j] *= b[i];
  1077. }
  1078. bias[i] = bias[i] * b[i] + a[i];
  1079. }
  1080. }
  1081. int top_blob_index_final = batchnorm->tops[0];
  1082. innerproduct->tops[0] = top_blob_index_final;
  1083. blobs[top_blob_index_final].producer = i;
  1084. batchnorm->type = "ncnnfused";
  1085. }
  1086. return 0;
  1087. }
  1088. int NetOptimize::fuse_innerproduct_add()
  1089. {
  1090. const size_t layer_count = layers.size();
  1091. for (int i = 0; i < layer_count; i++)
  1092. {
  1093. if (layers[i]->type != "InnerProduct")
  1094. continue;
  1095. // InnerProduct - BinaryOp
  1096. int top_blob_index = layers[i]->tops[0];
  1097. int j = i + 1;
  1098. for (; j < layer_count; j++)
  1099. {
  1100. if (layers[j]->type != "BinaryOp")
  1101. continue;
  1102. if (layers[j]->bottoms.size() != 2)
  1103. continue;
  1104. if (layers[j]->bottoms[0] == top_blob_index)
  1105. break;
  1106. }
  1107. if (j == layer_count)
  1108. continue;
  1109. // fuse InnerProduct - BinaryOp to InnerProduct
  1110. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  1111. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  1112. if (binaryop->op_type != 0 || binaryop->with_scalar)
  1113. continue;
  1114. // MemoryData - ..... - BinaryOp
  1115. int k = 0;
  1116. for (; k < j; k++)
  1117. {
  1118. if (layers[k]->type != "MemoryData")
  1119. continue;
  1120. if (layers[k]->tops[0] == binaryop->bottoms[1])
  1121. break;
  1122. }
  1123. if (k == j)
  1124. continue;
  1125. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[k];
  1126. int channels = innerproduct->num_output;
  1127. if (memorydata->w != channels || memorydata->h != 0 || memorydata->c != 0)
  1128. {
  1129. // not bias-like broadcasting type
  1130. continue;
  1131. }
  1132. fprintf(stderr, "fuse_innerproduct_add %s %s\n", innerproduct->name.c_str(), binaryop->name.c_str());
  1133. {
  1134. if (innerproduct->bias_term == 0)
  1135. {
  1136. // init bias
  1137. innerproduct->bias_term = 1;
  1138. innerproduct->bias_data = memorydata->data;
  1139. }
  1140. else
  1141. {
  1142. float* bias = innerproduct->bias_data;
  1143. for (int i = 0; i < channels; i++)
  1144. {
  1145. bias[i] = bias[i] + memorydata->data[i];
  1146. }
  1147. }
  1148. }
  1149. int top_blob_index_final = binaryop->tops[0];
  1150. innerproduct->tops[0] = top_blob_index_final;
  1151. blobs[top_blob_index_final].producer = i;
  1152. binaryop->type = "ncnnfused";
  1153. }
  1154. return 0;
  1155. }
  1156. int NetOptimize::fuse_innerproduct_dropout()
  1157. {
  1158. const size_t layer_count = layers.size();
  1159. for (int i = 0; i < layer_count; i++)
  1160. {
  1161. if (layers[i]->type != "InnerProduct")
  1162. continue;
  1163. // InnerProduct - Dropout
  1164. int top_blob_index = layers[i]->tops[0];
  1165. int j = i + 1;
  1166. for (; j < layer_count; j++)
  1167. {
  1168. if (layers[j]->type != "Dropout")
  1169. continue;
  1170. if (layers[j]->bottoms.size() != 1)
  1171. continue;
  1172. if (layers[j]->bottoms[0] == top_blob_index)
  1173. break;
  1174. }
  1175. if (j == layer_count)
  1176. continue;
  1177. // fuse InnerProduct - Dropout to InnerProduct
  1178. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  1179. ncnn::Dropout* dropout = (ncnn::Dropout*)layers[j];
  1180. fprintf(stderr, "fuse_innerproduct_dropout %s %s\n", innerproduct->name.c_str(), dropout->name.c_str());
  1181. float scale = dropout->scale;
  1182. if (scale != 1.f)
  1183. {
  1184. const int num_output = innerproduct->num_output;
  1185. const int weight_per_outch = innerproduct->weight_data_size / num_output;
  1186. float* weight = innerproduct->weight_data;
  1187. for (int i = 0; i < num_output; i++)
  1188. {
  1189. float* conv_weight_outch = weight + weight_per_outch * i;
  1190. for (int j = 0; j < weight_per_outch; j++)
  1191. {
  1192. conv_weight_outch[j] *= scale;
  1193. }
  1194. }
  1195. if (innerproduct->bias_term)
  1196. {
  1197. float* bias = innerproduct->bias_data;
  1198. for (int i = 0; i < num_output; i++)
  1199. {
  1200. bias[i] *= scale;
  1201. }
  1202. }
  1203. }
  1204. int top_blob_index_final = dropout->tops[0];
  1205. innerproduct->tops[0] = top_blob_index_final;
  1206. blobs[top_blob_index_final].producer = i;
  1207. dropout->type = "ncnnfused";
  1208. }
  1209. return 0;
  1210. }
  1211. int NetOptimize::fuse_convolution_activation()
  1212. {
  1213. const size_t layer_count = layers.size();
  1214. for (int i = 0; i < layer_count; i++)
  1215. {
  1216. if (layers[i]->type != "Convolution")
  1217. continue;
  1218. // Convolution - Activation
  1219. int top_blob_index = layers[i]->tops[0];
  1220. int j = i + 1;
  1221. for (; j < layer_count; j++)
  1222. {
  1223. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid" && layers[j]->type != "Mish")
  1224. continue;
  1225. if (layers[j]->bottoms.size() != 1)
  1226. continue;
  1227. if (layers[j]->bottoms[0] == top_blob_index)
  1228. break;
  1229. }
  1230. if (j == layer_count)
  1231. continue;
  1232. // fuse Convolution - Activation to Convolution
  1233. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[i];
  1234. ncnn::Layer* activation = layers[j];
  1235. fprintf(stderr, "fuse_convolution_activation %s %s\n", convolution->name.c_str(), activation->name.c_str());
  1236. if (activation->type == "ReLU")
  1237. {
  1238. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  1239. if (relu->slope == 0.f)
  1240. {
  1241. convolution->activation_type = 1;
  1242. }
  1243. else
  1244. {
  1245. convolution->activation_type = 2;
  1246. convolution->activation_params = ncnn::Mat(1);
  1247. convolution->activation_params[0] = relu->slope;
  1248. }
  1249. }
  1250. else if (activation->type == "Clip")
  1251. {
  1252. ncnn::Clip* clip = (ncnn::Clip*)activation;
  1253. convolution->activation_type = 3;
  1254. convolution->activation_params = ncnn::Mat(2);
  1255. convolution->activation_params[0] = clip->min;
  1256. convolution->activation_params[1] = clip->max;
  1257. }
  1258. else if (activation->type == "Sigmoid")
  1259. {
  1260. convolution->activation_type = 4;
  1261. }
  1262. else if (activation->type == "Mish")
  1263. {
  1264. convolution->activation_type = 5;
  1265. }
  1266. int top_blob_index_final = activation->tops[0];
  1267. convolution->tops[0] = top_blob_index_final;
  1268. blobs[top_blob_index_final].producer = i;
  1269. activation->type = "ncnnfused";
  1270. }
  1271. return 0;
  1272. }
  1273. int NetOptimize::fuse_convolutiondepthwise_activation()
  1274. {
  1275. const size_t layer_count = layers.size();
  1276. for (int i = 0; i < layer_count; i++)
  1277. {
  1278. if (layers[i]->type != "ConvolutionDepthWise")
  1279. continue;
  1280. // ConvolutionDepthWise - Activation
  1281. int top_blob_index = layers[i]->tops[0];
  1282. int j = i + 1;
  1283. for (; j < layer_count; j++)
  1284. {
  1285. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid" && layers[j]->type != "Mish")
  1286. continue;
  1287. if (layers[j]->bottoms.size() != 1)
  1288. continue;
  1289. if (layers[j]->bottoms[0] == top_blob_index)
  1290. break;
  1291. }
  1292. if (j == layer_count)
  1293. continue;
  1294. // fuse ConvolutionDepthWise - Activation to ConvolutionDepthWise
  1295. ncnn::ConvolutionDepthWise* convolutiondepthwise = (ncnn::ConvolutionDepthWise*)layers[i];
  1296. ncnn::Layer* activation = layers[j];
  1297. fprintf(stderr, "fuse_convolutiondepthwise_activation %s %s\n", convolutiondepthwise->name.c_str(), activation->name.c_str());
  1298. if (activation->type == "ReLU")
  1299. {
  1300. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  1301. if (relu->slope == 0.f)
  1302. {
  1303. convolutiondepthwise->activation_type = 1;
  1304. }
  1305. else
  1306. {
  1307. convolutiondepthwise->activation_type = 2;
  1308. convolutiondepthwise->activation_params = ncnn::Mat(1);
  1309. convolutiondepthwise->activation_params[0] = relu->slope;
  1310. }
  1311. }
  1312. else if (activation->type == "Clip")
  1313. {
  1314. ncnn::Clip* clip = (ncnn::Clip*)activation;
  1315. convolutiondepthwise->activation_type = 3;
  1316. convolutiondepthwise->activation_params = ncnn::Mat(2);
  1317. convolutiondepthwise->activation_params[0] = clip->min;
  1318. convolutiondepthwise->activation_params[1] = clip->max;
  1319. }
  1320. else if (activation->type == "Sigmoid")
  1321. {
  1322. convolutiondepthwise->activation_type = 4;
  1323. }
  1324. else if (activation->type == "Mish")
  1325. {
  1326. convolutiondepthwise->activation_type = 5;
  1327. }
  1328. int top_blob_index_final = activation->tops[0];
  1329. convolutiondepthwise->tops[0] = top_blob_index_final;
  1330. blobs[top_blob_index_final].producer = i;
  1331. activation->type = "ncnnfused";
  1332. }
  1333. return 0;
  1334. }
  1335. int NetOptimize::fuse_deconvolution_activation()
  1336. {
  1337. const size_t layer_count = layers.size();
  1338. for (int i = 0; i < layer_count; i++)
  1339. {
  1340. if (layers[i]->type != "Deconvolution")
  1341. continue;
  1342. // Deconvolution - Activation
  1343. int top_blob_index = layers[i]->tops[0];
  1344. int j = i + 1;
  1345. for (; j < layer_count; j++)
  1346. {
  1347. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid")
  1348. continue;
  1349. if (layers[j]->bottoms.size() != 1)
  1350. continue;
  1351. if (layers[j]->bottoms[0] == top_blob_index)
  1352. break;
  1353. }
  1354. if (j == layer_count)
  1355. continue;
  1356. // fuse Deconvolution - Activation to Deconvolution
  1357. ncnn::Deconvolution* deconvolution = (ncnn::Deconvolution*)layers[i];
  1358. ncnn::Layer* activation = layers[j];
  1359. fprintf(stderr, "fuse_deconvolution_activation %s %s\n", deconvolution->name.c_str(), activation->name.c_str());
  1360. if (activation->type == "ReLU")
  1361. {
  1362. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  1363. if (relu->slope == 0.f)
  1364. {
  1365. deconvolution->activation_type = 1;
  1366. }
  1367. else
  1368. {
  1369. deconvolution->activation_type = 2;
  1370. deconvolution->activation_params = ncnn::Mat(1);
  1371. deconvolution->activation_params[0] = relu->slope;
  1372. }
  1373. }
  1374. else if (activation->type == "Clip")
  1375. {
  1376. ncnn::Clip* clip = (ncnn::Clip*)activation;
  1377. deconvolution->activation_type = 3;
  1378. deconvolution->activation_params = ncnn::Mat(2);
  1379. deconvolution->activation_params[0] = clip->min;
  1380. deconvolution->activation_params[1] = clip->max;
  1381. }
  1382. else if (activation->type == "Sigmoid")
  1383. {
  1384. deconvolution->activation_type = 4;
  1385. }
  1386. int top_blob_index_final = activation->tops[0];
  1387. deconvolution->tops[0] = top_blob_index_final;
  1388. blobs[top_blob_index_final].producer = i;
  1389. activation->type = "ncnnfused";
  1390. }
  1391. return 0;
  1392. }
  1393. int NetOptimize::fuse_deconvolutiondepthwise_activation()
  1394. {
  1395. const size_t layer_count = layers.size();
  1396. for (int i = 0; i < layer_count; i++)
  1397. {
  1398. if (layers[i]->type != "DeconvolutionDepthWise")
  1399. continue;
  1400. // DeconvolutionDepthWise - Activation
  1401. int top_blob_index = layers[i]->tops[0];
  1402. int j = i + 1;
  1403. for (; j < layer_count; j++)
  1404. {
  1405. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid")
  1406. continue;
  1407. if (layers[j]->bottoms.size() != 1)
  1408. continue;
  1409. if (layers[j]->bottoms[0] == top_blob_index)
  1410. break;
  1411. }
  1412. if (j == layer_count)
  1413. continue;
  1414. // fuse DeconvolutionDepthWise - Activation to DeconvolutionDepthWise
  1415. ncnn::DeconvolutionDepthWise* deconvolutiondepthwise = (ncnn::DeconvolutionDepthWise*)layers[i];
  1416. ncnn::Layer* activation = layers[j];
  1417. fprintf(stderr, "fuse_deconvolutiondepthwise_activation %s %s\n", deconvolutiondepthwise->name.c_str(), activation->name.c_str());
  1418. if (activation->type == "ReLU")
  1419. {
  1420. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  1421. if (relu->slope == 0.f)
  1422. {
  1423. deconvolutiondepthwise->activation_type = 1;
  1424. }
  1425. else
  1426. {
  1427. deconvolutiondepthwise->activation_type = 2;
  1428. deconvolutiondepthwise->activation_params = ncnn::Mat(1);
  1429. deconvolutiondepthwise->activation_params[0] = relu->slope;
  1430. }
  1431. }
  1432. else if (activation->type == "Clip")
  1433. {
  1434. ncnn::Clip* clip = (ncnn::Clip*)activation;
  1435. deconvolutiondepthwise->activation_type = 3;
  1436. deconvolutiondepthwise->activation_params = ncnn::Mat(2);
  1437. deconvolutiondepthwise->activation_params[0] = clip->min;
  1438. deconvolutiondepthwise->activation_params[1] = clip->max;
  1439. }
  1440. else if (activation->type == "Sigmoid")
  1441. {
  1442. deconvolutiondepthwise->activation_type = 4;
  1443. }
  1444. int top_blob_index_final = activation->tops[0];
  1445. deconvolutiondepthwise->tops[0] = top_blob_index_final;
  1446. blobs[top_blob_index_final].producer = i;
  1447. activation->type = "ncnnfused";
  1448. }
  1449. return 0;
  1450. }
  1451. int NetOptimize::fuse_innerproduct_activation()
  1452. {
  1453. const size_t layer_count = layers.size();
  1454. for (int i = 0; i < layer_count; i++)
  1455. {
  1456. if (layers[i]->type != "InnerProduct")
  1457. continue;
  1458. // InnerProduct - Activation
  1459. int top_blob_index = layers[i]->tops[0];
  1460. int j = i + 1;
  1461. for (; j < layer_count; j++)
  1462. {
  1463. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid")
  1464. continue;
  1465. if (layers[j]->bottoms.size() != 1)
  1466. continue;
  1467. if (layers[j]->bottoms[0] == top_blob_index)
  1468. break;
  1469. }
  1470. if (j == layer_count)
  1471. continue;
  1472. // fuse InnerProduct - Activation to InnerProduct
  1473. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  1474. ncnn::Layer* activation = layers[j];
  1475. fprintf(stderr, "fuse_innerproduct_activation %s %s\n", innerproduct->name.c_str(), activation->name.c_str());
  1476. if (activation->type == "ReLU")
  1477. {
  1478. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  1479. if (relu->slope == 0.f)
  1480. {
  1481. innerproduct->activation_type = 1;
  1482. }
  1483. else
  1484. {
  1485. innerproduct->activation_type = 2;
  1486. innerproduct->activation_params = ncnn::Mat(1);
  1487. innerproduct->activation_params[0] = relu->slope;
  1488. }
  1489. }
  1490. else if (activation->type == "Clip")
  1491. {
  1492. ncnn::Clip* clip = (ncnn::Clip*)activation;
  1493. innerproduct->activation_type = 3;
  1494. innerproduct->activation_params = ncnn::Mat(2);
  1495. innerproduct->activation_params[0] = clip->min;
  1496. innerproduct->activation_params[1] = clip->max;
  1497. }
  1498. else if (activation->type == "Sigmoid")
  1499. {
  1500. innerproduct->activation_type = 4;
  1501. }
  1502. int top_blob_index_final = activation->tops[0];
  1503. innerproduct->tops[0] = top_blob_index_final;
  1504. blobs[top_blob_index_final].producer = i;
  1505. activation->type = "ncnnfused";
  1506. }
  1507. return 0;
  1508. }
  1509. int NetOptimize::fuse_memorydata_binaryop()
  1510. {
  1511. const size_t layer_count = layers.size();
  1512. for (int i = 0; i < layer_count; i++)
  1513. {
  1514. if (layers[i]->type != "MemoryData")
  1515. continue;
  1516. // MemoryData - BinaryOp
  1517. int top_blob_index = layers[i]->tops[0];
  1518. int j = i + 1;
  1519. for (; j < layer_count; j++)
  1520. {
  1521. if (layers[j]->type != "BinaryOp")
  1522. continue;
  1523. if (layers[j]->bottoms.size() != 2)
  1524. continue;
  1525. if (layers[j]->bottoms[0] == top_blob_index || layers[j]->bottoms[1] == top_blob_index)
  1526. break;
  1527. }
  1528. if (j == layer_count)
  1529. continue;
  1530. // fuse MemoryData - BinaryOp to BinaryOp
  1531. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[i];
  1532. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  1533. int memorydata_index = 1;
  1534. if (binaryop->bottoms[0] == top_blob_index)
  1535. {
  1536. int op_type = binaryop->op_type;
  1537. if (op_type == ncnn::BinaryOp::Operation_ADD
  1538. || op_type == ncnn::BinaryOp::Operation_MUL
  1539. || op_type == ncnn::BinaryOp::Operation_MAX
  1540. || op_type == ncnn::BinaryOp::Operation_MIN)
  1541. {
  1542. memorydata_index = 0;
  1543. }
  1544. else if (op_type == ncnn::BinaryOp::Operation_SUB)
  1545. {
  1546. binaryop->op_type = ncnn::BinaryOp::Operation_RSUB;
  1547. memorydata_index = 0;
  1548. }
  1549. else if (op_type == ncnn::BinaryOp::Operation_DIV)
  1550. {
  1551. binaryop->op_type = ncnn::BinaryOp::Operation_RDIV;
  1552. memorydata_index = 0;
  1553. }
  1554. else
  1555. {
  1556. // non interchangeable binaryop
  1557. continue;
  1558. }
  1559. }
  1560. if (memorydata->w != 1 || memorydata->h != 0 || memorydata->c != 0)
  1561. {
  1562. // not a scalar
  1563. continue;
  1564. }
  1565. float scalar = memorydata->data[0];
  1566. binaryop->with_scalar = 1;
  1567. binaryop->b = scalar;
  1568. fprintf(stderr, "fuse_memorydata_binaryop %s %s\n", memorydata->name.c_str(), binaryop->name.c_str());
  1569. binaryop->bottoms.erase(binaryop->bottoms.begin() + memorydata_index);
  1570. memorydata->type = "ncnnfused";
  1571. }
  1572. return 0;
  1573. }
  1574. int NetOptimize::fuse_binaryop_eltwise()
  1575. {
  1576. const size_t layer_count = layers.size();
  1577. for (int i = 0; i < layer_count; i++)
  1578. {
  1579. if (layers[i]->type != "BinaryOp")
  1580. continue;
  1581. if (layers[i]->bottoms.size() != 2)
  1582. continue;
  1583. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[i];
  1584. if (binaryop->op_type != ncnn::BinaryOp::Operation_ADD)
  1585. continue;
  1586. if (binaryop->with_scalar)
  1587. continue;
  1588. // BinaryOp - BinaryOp - BinaryOp
  1589. int bottom_blob_index_0 = binaryop->bottoms[0];
  1590. int bottom_blob_index_1 = binaryop->bottoms[1];
  1591. int j0 = 0;
  1592. for (; j0 < i; j0++)
  1593. {
  1594. if (layers[j0]->type != "BinaryOp")
  1595. continue;
  1596. if (layers[j0]->bottoms.size() != 1)
  1597. continue;
  1598. if (((ncnn::BinaryOp*)layers[j0])->op_type != ncnn::BinaryOp::Operation_MUL)
  1599. continue;
  1600. if (layers[j0]->tops[0] == bottom_blob_index_0)
  1601. break;
  1602. }
  1603. int j1 = 0;
  1604. for (; j1 < i; j1++)
  1605. {
  1606. if (layers[j1]->type != "BinaryOp")
  1607. continue;
  1608. if (layers[j1]->bottoms.size() != 1)
  1609. continue;
  1610. if (((ncnn::BinaryOp*)layers[j1])->op_type != ncnn::BinaryOp::Operation_MUL)
  1611. continue;
  1612. if (layers[j1]->tops[0] == bottom_blob_index_1)
  1613. break;
  1614. }
  1615. if (j0 == i && j1 == i)
  1616. continue;
  1617. ncnn::BinaryOp* binaryop0 = (ncnn::BinaryOp*)layers[j0];
  1618. ncnn::BinaryOp* binaryop1 = (ncnn::BinaryOp*)layers[j1];
  1619. fprintf(stderr, "fuse_binaryop_eltwise %s %s %s\n", binaryop0->name.c_str(), binaryop1->name.c_str(), binaryop->name.c_str());
  1620. ncnn::Eltwise* eltwise = (ncnn::Eltwise*)ncnn::create_layer("Eltwise");
  1621. eltwise->type = "Eltwise";
  1622. eltwise->name = binaryop->name;
  1623. eltwise->bottoms = binaryop->bottoms;
  1624. eltwise->tops = binaryop->tops;
  1625. ncnn::ParamDict pd;
  1626. eltwise->load_param(pd);
  1627. eltwise->op_type = ncnn::Eltwise::Operation_SUM;
  1628. eltwise->coeffs = ncnn::Mat(2);
  1629. if (j0 != i && j1 != i)
  1630. {
  1631. // fuse BinaryOp - BinaryOp - BinaryOp to Eltwise
  1632. eltwise->coeffs[0] = binaryop0->b;
  1633. eltwise->coeffs[1] = binaryop1->b;
  1634. eltwise->bottoms[0] = binaryop0->bottoms[0];
  1635. eltwise->bottoms[1] = binaryop1->bottoms[0];
  1636. binaryop0->type = "ncnnfused";
  1637. binaryop1->type = "ncnnfused";
  1638. }
  1639. if (j0 != i && j1 == i)
  1640. {
  1641. // fuse BinaryOp - X - BinaryOp to Eltwise
  1642. eltwise->coeffs[0] = binaryop0->b;
  1643. eltwise->coeffs[1] = 1.f;
  1644. eltwise->bottoms[0] = binaryop0->bottoms[0];
  1645. binaryop0->type = "ncnnfused";
  1646. }
  1647. if (j0 == i && j1 != i)
  1648. {
  1649. // fuse X - BinaryOp - BinaryOp to Eltwise
  1650. eltwise->coeffs[0] = 1.f;
  1651. eltwise->coeffs[1] = binaryop1->b;
  1652. eltwise->bottoms[1] = binaryop1->bottoms[0];
  1653. binaryop1->type = "ncnnfused";
  1654. }
  1655. layers[i] = eltwise;
  1656. delete binaryop;
  1657. }
  1658. return 0;
  1659. }
  1660. int NetOptimize::eliminate_dropout()
  1661. {
  1662. const size_t layer_count = layers.size();
  1663. for (int i = 0; i < layer_count; i++)
  1664. {
  1665. if (layers[i]->type != "Dropout")
  1666. continue;
  1667. ncnn::Dropout* dropout = (ncnn::Dropout*)layers[i];
  1668. if (dropout->scale != 1.f)
  1669. continue;
  1670. // Any - Dropout
  1671. int bottom_blob_index = layers[i]->bottoms[0];
  1672. int j = i - 1;
  1673. for (; j >= 0; j--)
  1674. {
  1675. if (layers[j]->type == "ncnnfused")
  1676. continue;
  1677. if (layers[j]->tops.size() != 1)
  1678. continue;
  1679. if (layers[j]->tops[0] == bottom_blob_index)
  1680. break;
  1681. }
  1682. if (j == -1)
  1683. continue;
  1684. ncnn::Layer* any = layers[j];
  1685. fprintf(stderr, "eliminate_dropout %s %s\n", any->name.c_str(), dropout->name.c_str());
  1686. int top_blob_index_final = dropout->tops[0];
  1687. any->tops[0] = top_blob_index_final;
  1688. blobs[top_blob_index_final].producer = j;
  1689. dropout->type = "ncnnfused";
  1690. }
  1691. return 0;
  1692. }
  1693. int NetOptimize::eliminate_pooling1x1()
  1694. {
  1695. const size_t layer_count = layers.size();
  1696. for (int i = 0; i < layer_count; i++)
  1697. {
  1698. if (layers[i]->type != "Pooling")
  1699. continue;
  1700. ncnn::Pooling* pooling = (ncnn::Pooling*)layers[i];
  1701. if (pooling->pad_left != 0 || pooling->pad_right != 0 || pooling->pad_top != 0 || pooling->pad_bottom != 0)
  1702. continue;
  1703. if (pooling->kernel_w != 1 || pooling->kernel_h != 1 || pooling->stride_w != 1 || pooling->stride_h != 1)
  1704. continue;
  1705. if (pooling->global_pooling != 0)
  1706. continue;
  1707. // Any - Pooling
  1708. int bottom_blob_index = layers[i]->bottoms[0];
  1709. int top_i = -1;
  1710. int j = i - 1;
  1711. for (; j >= 0; j--)
  1712. {
  1713. if (layers[j]->type == "ncnnfused")
  1714. continue;
  1715. for (int k = 0; k < layers[j]->tops.size(); k++)
  1716. {
  1717. if (layers[j]->tops[k] == bottom_blob_index)
  1718. {
  1719. top_i = k;
  1720. break;
  1721. }
  1722. }
  1723. if (top_i != -1)
  1724. break;
  1725. }
  1726. if (j == -1)
  1727. continue;
  1728. ncnn::Layer* any = layers[j];
  1729. fprintf(stderr, "eliminate_pooling1x1 %s %s\n", any->name.c_str(), pooling->name.c_str());
  1730. int top_blob_index_final = pooling->tops[0];
  1731. any->tops[top_i] = top_blob_index_final;
  1732. blobs[top_blob_index_final].producer = j;
  1733. pooling->type = "ncnnfused";
  1734. }
  1735. return 0;
  1736. }
  1737. int NetOptimize::eliminate_noop()
  1738. {
  1739. const size_t layer_count = layers.size();
  1740. for (int i = 0; i < layer_count; i++)
  1741. {
  1742. if (layers[i]->type != "Noop")
  1743. continue;
  1744. ncnn::Layer* noop = layers[i];
  1745. if (noop->bottoms.empty())
  1746. {
  1747. // Noop
  1748. fprintf(stderr, "eliminate_noop %s\n", noop->name.c_str());
  1749. size_t top_blob_count = noop->tops.size();
  1750. for (int k = 0; k < top_blob_count; k++)
  1751. {
  1752. int top_blob_index_final = noop->tops[k];
  1753. blobs[top_blob_index_final].producer = -1;
  1754. }
  1755. noop->type = "ncnnfused";
  1756. continue;
  1757. }
  1758. // Any - Noop
  1759. int bottom_blob_index = layers[i]->bottoms[0];
  1760. int j = i - 1;
  1761. for (; j >= 0; j--)
  1762. {
  1763. if (layers[j]->type == "ncnnfused")
  1764. continue;
  1765. if (layers[j]->tops.size() != 1)
  1766. continue;
  1767. if (layers[j]->tops[0] == bottom_blob_index)
  1768. break;
  1769. }
  1770. if (j == -1)
  1771. continue;
  1772. ncnn::Layer* any = layers[j];
  1773. fprintf(stderr, "eliminate_noop %s %s\n", any->name.c_str(), noop->name.c_str());
  1774. size_t top_blob_count = std::min(noop->tops.size(), any->tops.size());
  1775. for (int k = 0; k < top_blob_count; k++)
  1776. {
  1777. int top_blob_index_final = noop->tops[k];
  1778. any->tops[k] = top_blob_index_final;
  1779. blobs[top_blob_index_final].producer = j;
  1780. }
  1781. noop->type = "ncnnfused";
  1782. }
  1783. return 0;
  1784. }
  1785. int NetOptimize::eliminate_orphaned_memorydata()
  1786. {
  1787. const size_t layer_count = layers.size();
  1788. for (int i = 0; i < layer_count; i++)
  1789. {
  1790. if (layers[i]->type != "MemoryData")
  1791. continue;
  1792. // MemoryData - X
  1793. int top_blob_index = layers[i]->tops[0];
  1794. int j = i + 1;
  1795. for (; j < layer_count; j++)
  1796. {
  1797. if (layers[j]->type == "ncnnfused")
  1798. continue;
  1799. bool orphaned = true;
  1800. for (int k = 0; k < layers[j]->bottoms.size(); k++)
  1801. {
  1802. if (layers[j]->bottoms[k] == top_blob_index)
  1803. {
  1804. orphaned = false;
  1805. break;
  1806. }
  1807. }
  1808. if (!orphaned)
  1809. break;
  1810. }
  1811. if (j < layer_count)
  1812. continue;
  1813. // assert orphaned == true
  1814. fprintf(stderr, "eliminate_orphaned_memorydata %s\n", layers[i]->name.c_str());
  1815. layers[i]->type = "ncnnfused";
  1816. }
  1817. return 0;
  1818. }
  1819. int NetOptimize::eliminate_reshape_after_global_pooling()
  1820. {
  1821. const size_t layer_count = layers.size();
  1822. for (int i = 0; i < layer_count; i++)
  1823. {
  1824. if (layers[i]->type != "Pooling")
  1825. continue;
  1826. ncnn::Pooling* pooling = (ncnn::Pooling*)layers[i];
  1827. if (pooling->global_pooling == 0)
  1828. continue;
  1829. // Pooling - Reshape
  1830. int top_blob_index = layers[i]->tops[0];
  1831. int j = i + 1;
  1832. for (; j < layer_count; j++)
  1833. {
  1834. if (layers[j]->type != "Reshape")
  1835. continue;
  1836. if (layers[j]->bottoms.size() != 1)
  1837. continue;
  1838. if (layers[j]->bottoms[0] == top_blob_index)
  1839. break;
  1840. }
  1841. if (j == layer_count)
  1842. continue;
  1843. ncnn::Reshape* reshape = (ncnn::Reshape*)layers[j];
  1844. if (reshape->h != -233 || reshape->c != -233 || reshape->permute != 0)
  1845. continue;
  1846. fprintf(stderr, "eliminate_reshape_after_global_pooling %s %s\n", pooling->name.c_str(), reshape->name.c_str());
  1847. int top_blob_index_final = reshape->tops[0];
  1848. pooling->tops[0] = top_blob_index_final;
  1849. blobs[top_blob_index_final].producer = i;
  1850. reshape->type = "ncnnfused";
  1851. }
  1852. return 0;
  1853. }
  1854. int NetOptimize::eliminate_flatten_after_global_pooling()
  1855. {
  1856. const size_t layer_count = layers.size();
  1857. for (int i = 0; i < layer_count; i++)
  1858. {
  1859. if (layers[i]->type != "Pooling")
  1860. continue;
  1861. ncnn::Pooling* pooling = (ncnn::Pooling*)layers[i];
  1862. if (pooling->global_pooling == 0)
  1863. continue;
  1864. // Pooling - Flatten
  1865. int top_blob_index = layers[i]->tops[0];
  1866. int j = i + 1;
  1867. for (; j < layer_count; j++)
  1868. {
  1869. if (layers[j]->type != "Flatten")
  1870. continue;
  1871. if (layers[j]->bottoms.size() != 1)
  1872. continue;
  1873. if (layers[j]->bottoms[0] == top_blob_index)
  1874. break;
  1875. }
  1876. if (j == layer_count)
  1877. continue;
  1878. ncnn::Flatten* flatten = (ncnn::Flatten*)layers[j];
  1879. fprintf(stderr, "eliminate_flatten_after_global_pooling %s %s\n", pooling->name.c_str(), flatten->name.c_str());
  1880. int top_blob_index_final = flatten->tops[0];
  1881. pooling->tops[0] = top_blob_index_final;
  1882. blobs[top_blob_index_final].producer = i;
  1883. flatten->type = "ncnnfused";
  1884. }
  1885. return 0;
  1886. }
  1887. int NetOptimize::eliminate_flatten_after_innerproduct()
  1888. {
  1889. const size_t layer_count = layers.size();
  1890. for (int i = 0; i < layer_count; i++)
  1891. {
  1892. if (layers[i]->type != "InnerProduct")
  1893. continue;
  1894. // InnerProduct - Flatten
  1895. int top_blob_index = layers[i]->tops[0];
  1896. int j = i + 1;
  1897. for (; j < layer_count; j++)
  1898. {
  1899. if (layers[j]->type != "Flatten")
  1900. continue;
  1901. if (layers[j]->bottoms.size() != 1)
  1902. continue;
  1903. if (layers[j]->bottoms[0] == top_blob_index)
  1904. break;
  1905. }
  1906. if (j == layer_count)
  1907. continue;
  1908. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  1909. ncnn::Flatten* flatten = (ncnn::Flatten*)layers[j];
  1910. fprintf(stderr, "eliminate_flatten_after_innerproduct %s %s\n", innerproduct->name.c_str(), flatten->name.c_str());
  1911. int top_blob_index_final = flatten->tops[0];
  1912. innerproduct->tops[0] = top_blob_index_final;
  1913. blobs[top_blob_index_final].producer = i;
  1914. flatten->type = "ncnnfused";
  1915. }
  1916. return 0;
  1917. }
  1918. int NetOptimize::eliminate_reshape_before_binaryop()
  1919. {
  1920. const size_t layer_count = layers.size();
  1921. for (int i = 0; i < layer_count; i++)
  1922. {
  1923. if (layers[i]->type != "Reshape")
  1924. continue;
  1925. ncnn::Reshape* reshape = (ncnn::Reshape*)layers[i];
  1926. if (reshape->w != 1 || reshape->h != 1 || reshape->permute != 0)
  1927. continue;
  1928. // Reshape - BinaryOp
  1929. int top_blob_index = layers[i]->tops[0];
  1930. int j = i + 1;
  1931. for (; j < layer_count; j++)
  1932. {
  1933. if (layers[j]->type != "BinaryOp")
  1934. continue;
  1935. if (layers[j]->bottoms.size() != 2)
  1936. continue;
  1937. if (layers[j]->bottoms[0] == top_blob_index || layers[j]->bottoms[1] == top_blob_index)
  1938. break;
  1939. }
  1940. if (j == layer_count)
  1941. continue;
  1942. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  1943. fprintf(stderr, "eliminate_reshape_before_binaryop %s %s\n", reshape->name.c_str(), binaryop->name.c_str());
  1944. int bottom_blob_index_final = reshape->bottoms[0];
  1945. if (layers[j]->bottoms[0] == top_blob_index)
  1946. binaryop->bottoms[0] = bottom_blob_index_final;
  1947. if (layers[j]->bottoms[1] == top_blob_index)
  1948. binaryop->bottoms[1] = bottom_blob_index_final;
  1949. blobs[bottom_blob_index_final].consumers.erase(std::find(blobs[bottom_blob_index_final].consumers.begin(), blobs[bottom_blob_index_final].consumers.end(), i));
  1950. blobs[bottom_blob_index_final].consumers.push_back(j);
  1951. reshape->type = "ncnnfused";
  1952. }
  1953. return 0;
  1954. }
  1955. int NetOptimize::replace_convolution_with_innerproduct_after_global_pooling()
  1956. {
  1957. const size_t layer_count = layers.size();
  1958. for (int i = 0; i < layer_count; i++)
  1959. {
  1960. if (layers[i]->type != "Pooling")
  1961. continue;
  1962. ncnn::Pooling* pooling = (ncnn::Pooling*)layers[i];
  1963. if (pooling->global_pooling == 0)
  1964. continue;
  1965. // Pooling - Convolution
  1966. int top_blob_index = layers[i]->tops[0];
  1967. int j = i + 1;
  1968. for (; j < layer_count; j++)
  1969. {
  1970. if (layers[j]->type != "Convolution")
  1971. continue;
  1972. if (layers[j]->bottoms.size() != 1)
  1973. continue;
  1974. if (layers[j]->bottoms[0] == top_blob_index)
  1975. break;
  1976. }
  1977. if (j == layer_count)
  1978. continue;
  1979. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[j];
  1980. fprintf(stderr, "replace_convolution_with_innerproduct_after_global_pooling %s %s\n", pooling->name.c_str(), convolution->name.c_str());
  1981. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)ncnn::create_layer("InnerProduct");
  1982. innerproduct->type = "InnerProduct";
  1983. innerproduct->name = convolution->name;
  1984. innerproduct->bottoms = convolution->bottoms;
  1985. innerproduct->tops = convolution->tops;
  1986. ncnn::ParamDict pd;
  1987. innerproduct->load_param(pd);
  1988. innerproduct->num_output = convolution->num_output;
  1989. innerproduct->bias_term = convolution->bias_term;
  1990. innerproduct->weight_data_size = convolution->weight_data_size;
  1991. innerproduct->int8_scale_term = convolution->int8_scale_term;
  1992. innerproduct->weight_data = convolution->weight_data;
  1993. innerproduct->bias_data = convolution->bias_data;
  1994. innerproduct->weight_data_int8_scales = convolution->weight_data_int8_scales;
  1995. innerproduct->bottom_blob_int8_scale = convolution->bottom_blob_int8_scale;
  1996. innerproduct->activation_type = convolution->activation_type;
  1997. innerproduct->activation_params = convolution->activation_params;
  1998. layers[j] = innerproduct;
  1999. delete convolution;
  2000. }
  2001. return 0;
  2002. }
  2003. int NetOptimize::replace_convolution_with_innerproduct_after_innerproduct()
  2004. {
  2005. const size_t layer_count = layers.size();
  2006. for (;;)
  2007. {
  2008. bool replaced = false;
  2009. for (int i = 0; i < layer_count; i++)
  2010. {
  2011. if (layers[i]->type != "InnerProduct")
  2012. continue;
  2013. // InnerProduct - Convolution
  2014. int top_blob_index = layers[i]->tops[0];
  2015. int j = i + 1;
  2016. for (; j < layer_count; j++)
  2017. {
  2018. if (layers[j]->type != "Convolution")
  2019. continue;
  2020. if (layers[j]->bottoms.size() != 1)
  2021. continue;
  2022. if (layers[j]->bottoms[0] == top_blob_index)
  2023. break;
  2024. }
  2025. if (j == layer_count)
  2026. continue;
  2027. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  2028. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[j];
  2029. fprintf(stderr, "replace_convolution_with_innerproduct_after_innerproduct %s %s\n", innerproduct->name.c_str(), convolution->name.c_str());
  2030. ncnn::InnerProduct* innerproduct2 = (ncnn::InnerProduct*)ncnn::create_layer("InnerProduct");
  2031. innerproduct2->type = "InnerProduct";
  2032. innerproduct2->name = convolution->name;
  2033. innerproduct2->bottoms = convolution->bottoms;
  2034. innerproduct2->tops = convolution->tops;
  2035. ncnn::ParamDict pd;
  2036. innerproduct2->load_param(pd);
  2037. innerproduct2->num_output = convolution->num_output;
  2038. innerproduct2->bias_term = convolution->bias_term;
  2039. innerproduct2->weight_data_size = convolution->weight_data_size;
  2040. innerproduct->int8_scale_term = convolution->int8_scale_term;
  2041. innerproduct2->weight_data = convolution->weight_data;
  2042. innerproduct2->bias_data = convolution->bias_data;
  2043. innerproduct->weight_data_int8_scales = convolution->weight_data_int8_scales;
  2044. innerproduct->bottom_blob_int8_scale = convolution->bottom_blob_int8_scale;
  2045. innerproduct2->activation_type = convolution->activation_type;
  2046. innerproduct2->activation_params = convolution->activation_params;
  2047. layers[j] = innerproduct2;
  2048. delete convolution;
  2049. replaced = true;
  2050. }
  2051. if (!replaced)
  2052. break;
  2053. }
  2054. return 0;
  2055. }
  2056. int NetOptimize::shape_inference()
  2057. {
  2058. const size_t layer_count = layers.size();
  2059. const size_t blob_count = blobs.size();
  2060. ncnn::Extractor ex = create_extractor();
  2061. // prepare Input blobs
  2062. for (size_t i = 0; i < layer_count; i++)
  2063. {
  2064. const ncnn::Layer* layer = layers[i];
  2065. if (layer->type == "ncnnfused")
  2066. continue;
  2067. if (layer->type != "Input")
  2068. continue;
  2069. ncnn::Input* input = (ncnn::Input*)layer;
  2070. int w = input->w;
  2071. int h = input->h;
  2072. int c = input->c;
  2073. int dims = 0;
  2074. if (w == 0 && h == 0 && c == 0) dims = 0;
  2075. if (w != 0 && h == 0 && c == 0) dims = 1;
  2076. if (w != 0 && h != 0 && c == 0) dims = 2;
  2077. if (w != 0 && h != 0 && c != 0) dims = 3;
  2078. if (dims == 0)
  2079. {
  2080. fprintf(stderr, "Input layer %s without shape info, shape_inference aborted\n", layer->name.c_str());
  2081. return -1;
  2082. }
  2083. ncnn::Mat m;
  2084. if (dims == 1) m.create(w);
  2085. if (dims == 2) m.create(w, h);
  2086. if (dims == 3) m.create(w, h, c);
  2087. ex.input(layer->tops[0], m);
  2088. }
  2089. // prepare blobs with predefined shape
  2090. for (size_t i = 0; i < blob_count; i++)
  2091. {
  2092. const ncnn::Blob blob = blobs[i];
  2093. int dims = blob.shape.dims;
  2094. int w = blob.shape.w;
  2095. int h = blob.shape.h;
  2096. int c = blob.shape.c;
  2097. if (dims == 0)
  2098. continue;
  2099. ncnn::Mat m;
  2100. if (dims == 1) m.create(w);
  2101. if (dims == 2) m.create(w, h);
  2102. if (dims == 3) m.create(w, h, c);
  2103. ex.input(int(i), m);
  2104. }
  2105. fprintf(stderr, "shape_inference\n");
  2106. // resolve all layer output blob shape
  2107. for (size_t i = 0; i < layer_count; i++)
  2108. {
  2109. const ncnn::Layer* layer = layers[i];
  2110. if (layer->type == "ncnnfused")
  2111. continue;
  2112. for (size_t j = 0; j < layer->tops.size(); j++)
  2113. {
  2114. int top_blob_index = layer->tops[j];
  2115. ncnn::Mat m;
  2116. ex.extract(top_blob_index, m);
  2117. blobs[top_blob_index].shape = m;
  2118. }
  2119. }
  2120. // assign all layer blob shape
  2121. for (size_t i = 0; i < layer_count; i++)
  2122. {
  2123. ncnn::Layer* layer = layers[i];
  2124. if (layer->type == "ncnnfused")
  2125. continue;
  2126. layer->bottom_shapes.resize(layer->bottoms.size());
  2127. for (size_t j = 0; j < layer->bottoms.size(); j++)
  2128. {
  2129. int bottom_blob_index = layer->bottoms[j];
  2130. layer->bottom_shapes[j] = blobs[bottom_blob_index].shape;
  2131. }
  2132. layer->top_shapes.resize(layer->tops.size());
  2133. for (size_t j = 0; j < layer->tops.size(); j++)
  2134. {
  2135. int top_blob_index = layer->tops[j];
  2136. layer->top_shapes[j] = blobs[top_blob_index].shape;
  2137. // fprintf(stderr, "%d %4d %4d %4d | %2d %s\n", blobs[top_blob_index].shape.dims, blobs[top_blob_index].shape.w, blobs[top_blob_index].shape.h, blobs[top_blob_index].shape.c, top_blob_index, blobs[top_blob_index].name.c_str());
  2138. }
  2139. }
  2140. return 0;
  2141. }
  2142. int NetOptimize::fprintf_param_int_array(int id, const ncnn::Mat& m, FILE* pp)
  2143. {
  2144. const int count = m.w;
  2145. const int* ptr = m;
  2146. fprintf(pp, " -%d=%d", 23300 + id, count);
  2147. for (int i = 0; i < count; i++)
  2148. {
  2149. fprintf(pp, ",%d", ptr[i]);
  2150. }
  2151. return 0;
  2152. }
  2153. int NetOptimize::fprintf_param_float_array(int id, const ncnn::Mat& m, FILE* pp)
  2154. {
  2155. const int count = m.w;
  2156. const float* ptr = m;
  2157. fprintf(pp, " -%d=%d", 23300 + id, count);
  2158. for (int i = 0; i < count; i++)
  2159. {
  2160. fprintf(pp, ",%e", ptr[i]);
  2161. }
  2162. return 0;
  2163. }
  2164. static inline size_t alignSize(size_t sz, int n)
  2165. {
  2166. return (sz + n - 1) & -n;
  2167. }
  2168. int NetOptimize::fwrite_weight_tag_data(int tag, const ncnn::Mat& data, FILE* bp)
  2169. {
  2170. int p0 = ftell(bp);
  2171. ncnn::Mat data_flattened = data.reshape(data.w * data.h * data.c);
  2172. if (storage_type == 1 && tag == 0)
  2173. {
  2174. tag = 0x01306B47; // fp16 magic
  2175. fwrite(&tag, sizeof(int), 1, bp);
  2176. ncnn::Mat data_flattened_fp16;
  2177. ncnn::cast_float32_to_float16(data_flattened, data_flattened_fp16);
  2178. fwrite(data_flattened_fp16.data, data_flattened_fp16.elemsize, data_flattened_fp16.w, bp);
  2179. }
  2180. else
  2181. {
  2182. fwrite(&tag, sizeof(int), 1, bp);
  2183. fwrite(data_flattened.data, data_flattened.elemsize, data_flattened.w, bp);
  2184. }
  2185. // padding to 32bit align
  2186. int nwrite = ftell(bp) - p0;
  2187. size_t nalign = alignSize(nwrite, 4);
  2188. unsigned char padding[4] = {0x00, 0x00, 0x00, 0x00};
  2189. fwrite(padding, sizeof(unsigned char), nalign - nwrite, bp);
  2190. return 0;
  2191. }
  2192. int NetOptimize::fwrite_weight_data(const ncnn::Mat& data, FILE* bp)
  2193. {
  2194. int p0 = ftell(bp);
  2195. ncnn::Mat data_flattened = data.reshape(data.w * data.h * data.c);
  2196. fwrite(data_flattened.data, data_flattened.elemsize, data_flattened.w, bp);
  2197. // padding to 32bit align
  2198. int nwrite = ftell(bp) - p0;
  2199. size_t nalign = alignSize(nwrite, 4);
  2200. unsigned char padding[4] = {0x00, 0x00, 0x00, 0x00};
  2201. fwrite(padding, sizeof(unsigned char), nalign - nwrite, bp);
  2202. return 0;
  2203. }
  2204. int NetOptimize::save(const char* parampath, const char* binpath)
  2205. {
  2206. FILE* pp = fopen(parampath, "wb");
  2207. FILE* bp = fopen(binpath, "wb");
  2208. fprintf(pp, "7767517\n");
  2209. const size_t layer_count = layers.size();
  2210. int layer_count_fused = 0;
  2211. std::set<std::string> blob_names;
  2212. for (int i = 0; i < layer_count; i++)
  2213. {
  2214. const ncnn::Layer* layer = layers[i];
  2215. if (layer->type == "ncnnfused")
  2216. continue;
  2217. layer_count_fused++;
  2218. size_t bottom_count = layer->bottoms.size();
  2219. for (int j = 0; j < bottom_count; j++)
  2220. {
  2221. int bottom_blob_index = layer->bottoms[j];
  2222. blob_names.insert(blobs[bottom_blob_index].name);
  2223. }
  2224. size_t top_count = layer->tops.size();
  2225. for (int j = 0; j < top_count; j++)
  2226. {
  2227. int top_blob_index = layer->tops[j];
  2228. blob_names.insert(blobs[top_blob_index].name);
  2229. }
  2230. }
  2231. size_t blob_count_fused = blob_names.size();
  2232. fprintf(pp, "%d %zd\n", layer_count_fused, blob_count_fused);
  2233. for (int i = 0; i < layer_count; i++)
  2234. {
  2235. const ncnn::Layer* layer = layers[i];
  2236. if (layer->type == "ncnnfused")
  2237. continue;
  2238. size_t bottom_count = layer->bottoms.size();
  2239. size_t top_count = layer->tops.size();
  2240. fprintf(pp, "%-24s %-24s %zd %zd", layer->type.c_str(), layer->name.c_str(), bottom_count, top_count);
  2241. for (int j = 0; j < bottom_count; j++)
  2242. {
  2243. int bottom_blob_index = layer->bottoms[j];
  2244. fprintf(pp, " %s", blobs[bottom_blob_index].name.c_str());
  2245. }
  2246. for (int j = 0; j < top_count; j++)
  2247. {
  2248. int top_blob_index = layer->tops[j];
  2249. fprintf(pp, " %s", blobs[top_blob_index].name.c_str());
  2250. }
  2251. // write shape hints
  2252. bool shape_ready = true;
  2253. for (int j = 0; j < top_count; j++)
  2254. {
  2255. int top_blob_index = layer->tops[j];
  2256. int dims = blobs[top_blob_index].shape.dims;
  2257. if (dims == 0)
  2258. {
  2259. shape_ready = false;
  2260. break;
  2261. }
  2262. }
  2263. if (shape_ready)
  2264. {
  2265. fprintf(pp, " -23330=%zd", top_count * 4);
  2266. for (int j = 0; j < top_count; j++)
  2267. {
  2268. int top_blob_index = layer->tops[j];
  2269. int dims = blobs[top_blob_index].shape.dims;
  2270. int w = blobs[top_blob_index].shape.w;
  2271. int h = blobs[top_blob_index].shape.h;
  2272. int c = blobs[top_blob_index].shape.c;
  2273. fprintf(pp, ",%d,%d,%d,%d", dims, w, h, c);
  2274. }
  2275. }
  2276. ncnn::Layer* layer_default = ncnn::create_layer(layer->typeindex);
  2277. ncnn::ParamDict pd;
  2278. layer_default->load_param(pd);
  2279. #define fprintf_param_value(format, phase) \
  2280. { \
  2281. if (op->phase != op_default->phase) fprintf(pp, format, op->phase); \
  2282. }
  2283. if (layer->type == "BatchNorm")
  2284. {
  2285. ncnn::BatchNorm* op = (ncnn::BatchNorm*)layer;
  2286. ncnn::BatchNorm* op_default = (ncnn::BatchNorm*)layer_default;
  2287. fprintf_param_value(" 0=%d", channels)
  2288. fprintf_param_value(" 1=%e", eps)
  2289. fwrite_weight_data(op->slope_data, bp);
  2290. fwrite_weight_data(op->mean_data, bp);
  2291. fwrite_weight_data(op->var_data, bp);
  2292. fwrite_weight_data(op->bias_data, bp);
  2293. }
  2294. else if (layer->type == "Bias")
  2295. {
  2296. ncnn::Bias* op = (ncnn::Bias*)layer;
  2297. ncnn::Bias* op_default = (ncnn::Bias*)layer_default;
  2298. fprintf_param_value(" 0=%d", bias_data_size)
  2299. fwrite_weight_data(op->bias_data, bp);
  2300. }
  2301. else if (layer->type == "BinaryOp")
  2302. {
  2303. ncnn::BinaryOp* op = (ncnn::BinaryOp*)layer;
  2304. ncnn::BinaryOp* op_default = (ncnn::BinaryOp*)layer_default;
  2305. fprintf_param_value(" 0=%d", op_type)
  2306. fprintf_param_value(" 1=%d", with_scalar)
  2307. fprintf_param_value(" 2=%e", b)
  2308. }
  2309. else if (layer->type == "Clip")
  2310. {
  2311. ncnn::Clip* op = (ncnn::Clip*)layer;
  2312. ncnn::Clip* op_default = (ncnn::Clip*)layer_default;
  2313. fprintf_param_value(" 0=%e", min)
  2314. fprintf_param_value(" 1=%e", max)
  2315. }
  2316. else if (layer->type == "Concat")
  2317. {
  2318. ncnn::Concat* op = (ncnn::Concat*)layer;
  2319. ncnn::Concat* op_default = (ncnn::Concat*)layer_default;
  2320. fprintf_param_value(" 0=%d", axis)
  2321. }
  2322. else if (layer->type == "Convolution")
  2323. {
  2324. ncnn::Convolution* op = (ncnn::Convolution*)layer;
  2325. ncnn::Convolution* op_default = (ncnn::Convolution*)layer_default;
  2326. fprintf_param_value(" 0=%d", num_output)
  2327. fprintf_param_value(" 1=%d", kernel_w)
  2328. {
  2329. if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h);
  2330. }
  2331. fprintf_param_value(" 2=%d", dilation_w)
  2332. {
  2333. if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h);
  2334. }
  2335. fprintf_param_value(" 3=%d", stride_w)
  2336. {
  2337. if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h);
  2338. }
  2339. fprintf_param_value(" 4=%d", pad_left)
  2340. {
  2341. if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top);
  2342. }
  2343. {
  2344. if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right);
  2345. }
  2346. {
  2347. if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom);
  2348. }
  2349. fprintf_param_value(" 18=%e", pad_value)
  2350. fprintf_param_value(" 5=%d", bias_term)
  2351. fprintf_param_value(" 6=%d", weight_data_size)
  2352. fprintf_param_value(" 8=%d", int8_scale_term)
  2353. fprintf_param_value(" 9=%d", activation_type)
  2354. {
  2355. if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp);
  2356. }
  2357. fprintf_param_value(" 17=%d", impl_type)
  2358. fwrite_weight_tag_data(0, op->weight_data, bp);
  2359. fwrite_weight_data(op->bias_data, bp);
  2360. }
  2361. else if (layer->type == "ConvolutionDepthWise")
  2362. {
  2363. ncnn::ConvolutionDepthWise* op = (ncnn::ConvolutionDepthWise*)layer;
  2364. ncnn::ConvolutionDepthWise* op_default = (ncnn::ConvolutionDepthWise*)layer_default;
  2365. fprintf_param_value(" 0=%d", num_output)
  2366. fprintf_param_value(" 1=%d", kernel_w)
  2367. {
  2368. if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h);
  2369. }
  2370. fprintf_param_value(" 2=%d", dilation_w)
  2371. {
  2372. if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h);
  2373. }
  2374. fprintf_param_value(" 3=%d", stride_w)
  2375. {
  2376. if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h);
  2377. }
  2378. fprintf_param_value(" 4=%d", pad_left)
  2379. {
  2380. if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top);
  2381. }
  2382. {
  2383. if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right);
  2384. }
  2385. {
  2386. if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom);
  2387. }
  2388. fprintf_param_value(" 18=%e", pad_value)
  2389. fprintf_param_value(" 5=%d", bias_term)
  2390. fprintf_param_value(" 6=%d", weight_data_size)
  2391. fprintf_param_value(" 7=%d", group)
  2392. fprintf_param_value(" 8=%d", int8_scale_term)
  2393. fprintf_param_value(" 9=%d", activation_type)
  2394. {
  2395. if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp);
  2396. }
  2397. fwrite_weight_tag_data(0, op->weight_data, bp);
  2398. fwrite_weight_data(op->bias_data, bp);
  2399. }
  2400. else if (layer->type == "Crop")
  2401. {
  2402. ncnn::Crop* op = (ncnn::Crop*)layer;
  2403. ncnn::Crop* op_default = (ncnn::Crop*)layer_default;
  2404. fprintf_param_value(" 0=%d", woffset)
  2405. fprintf_param_value(" 1=%d", hoffset)
  2406. fprintf_param_value(" 2=%d", coffset)
  2407. fprintf_param_value(" 3=%d", outw)
  2408. fprintf_param_value(" 4=%d", outh)
  2409. fprintf_param_value(" 5=%d", outc)
  2410. fprintf_param_value(" 6=%d", woffset2)
  2411. fprintf_param_value(" 7=%d", hoffset2)
  2412. fprintf_param_value(" 8=%d", coffset2)
  2413. {
  2414. if (!op->starts.empty()) fprintf_param_int_array(9, op->starts, pp);
  2415. }
  2416. {
  2417. if (!op->ends.empty()) fprintf_param_int_array(10, op->ends, pp);
  2418. }
  2419. {
  2420. if (!op->axes.empty()) fprintf_param_int_array(11, op->axes, pp);
  2421. }
  2422. }
  2423. else if (layer->type == "Deconvolution")
  2424. {
  2425. ncnn::Deconvolution* op = (ncnn::Deconvolution*)layer;
  2426. ncnn::Deconvolution* op_default = (ncnn::Deconvolution*)layer_default;
  2427. fprintf_param_value(" 0=%d", num_output)
  2428. fprintf_param_value(" 1=%d", kernel_w)
  2429. {
  2430. if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h);
  2431. }
  2432. fprintf_param_value(" 2=%d", dilation_w)
  2433. {
  2434. if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h);
  2435. }
  2436. fprintf_param_value(" 3=%d", stride_w)
  2437. {
  2438. if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h);
  2439. }
  2440. fprintf_param_value(" 4=%d", pad_left)
  2441. {
  2442. if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top);
  2443. }
  2444. {
  2445. if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right);
  2446. }
  2447. {
  2448. if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom);
  2449. }
  2450. fprintf_param_value(" 18=%d", output_pad_right)
  2451. {
  2452. if (op->output_pad_bottom != op->output_pad_right) fprintf(pp, " 19=%d", op->output_pad_bottom);
  2453. }
  2454. fprintf_param_value(" 20=%d", output_w)
  2455. {
  2456. if (op->output_h != op->output_w) fprintf(pp, " 21=%d", op->output_h);
  2457. }
  2458. fprintf_param_value(" 5=%d", bias_term)
  2459. fprintf_param_value(" 6=%d", weight_data_size)
  2460. fprintf_param_value(" 9=%d", activation_type)
  2461. {
  2462. if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp);
  2463. }
  2464. fwrite_weight_tag_data(0, op->weight_data, bp);
  2465. fwrite_weight_data(op->bias_data, bp);
  2466. }
  2467. else if (layer->type == "DeconvolutionDepthWise")
  2468. {
  2469. ncnn::DeconvolutionDepthWise* op = (ncnn::DeconvolutionDepthWise*)layer;
  2470. ncnn::DeconvolutionDepthWise* op_default = (ncnn::DeconvolutionDepthWise*)layer_default;
  2471. fprintf_param_value(" 0=%d", num_output)
  2472. fprintf_param_value(" 1=%d", kernel_w)
  2473. {
  2474. if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h);
  2475. }
  2476. fprintf_param_value(" 2=%d", dilation_w)
  2477. {
  2478. if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h);
  2479. }
  2480. fprintf_param_value(" 3=%d", stride_w)
  2481. {
  2482. if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h);
  2483. }
  2484. fprintf_param_value(" 4=%d", pad_left)
  2485. {
  2486. if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top);
  2487. }
  2488. {
  2489. if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right);
  2490. }
  2491. {
  2492. if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom);
  2493. }
  2494. fprintf_param_value(" 18=%d", output_pad_right)
  2495. {
  2496. if (op->output_pad_bottom != op->output_pad_right) fprintf(pp, " 19=%d", op->output_pad_bottom);
  2497. }
  2498. fprintf_param_value(" 20=%d", output_w)
  2499. {
  2500. if (op->output_h != op->output_w) fprintf(pp, " 21=%d", op->output_h);
  2501. }
  2502. fprintf_param_value(" 5=%d", bias_term)
  2503. fprintf_param_value(" 6=%d", weight_data_size)
  2504. fprintf_param_value(" 7=%d", group)
  2505. fprintf_param_value(" 9=%d", activation_type)
  2506. {
  2507. if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp);
  2508. }
  2509. fwrite_weight_tag_data(0, op->weight_data, bp);
  2510. fwrite_weight_data(op->bias_data, bp);
  2511. }
  2512. else if (layer->type == "DetectionOutput")
  2513. {
  2514. ncnn::DetectionOutput* op = (ncnn::DetectionOutput*)layer;
  2515. ncnn::DetectionOutput* op_default = (ncnn::DetectionOutput*)layer_default;
  2516. fprintf_param_value(" 0=%d", num_class)
  2517. fprintf_param_value(" 1=%e", nms_threshold)
  2518. fprintf_param_value(" 2=%d", nms_top_k)
  2519. fprintf_param_value(" 3=%d", keep_top_k)
  2520. fprintf_param_value(" 4=%e", confidence_threshold)
  2521. fprintf_param_value(" 5=%e", variances[0])
  2522. fprintf_param_value(" 6=%e", variances[1])
  2523. fprintf_param_value(" 7=%e", variances[2])
  2524. fprintf_param_value(" 8=%e", variances[3])
  2525. }
  2526. else if (layer->type == "Dropout")
  2527. {
  2528. ncnn::Dropout* op = (ncnn::Dropout*)layer;
  2529. ncnn::Dropout* op_default = (ncnn::Dropout*)layer_default;
  2530. fprintf_param_value(" 0=%e", scale)
  2531. }
  2532. else if (layer->type == "Eltwise")
  2533. {
  2534. ncnn::Eltwise* op = (ncnn::Eltwise*)layer;
  2535. ncnn::Eltwise* op_default = (ncnn::Eltwise*)layer_default;
  2536. fprintf_param_value(" 0=%d", op_type)
  2537. {
  2538. if (!op->coeffs.empty()) fprintf_param_float_array(1, op->coeffs, pp);
  2539. }
  2540. }
  2541. else if (layer->type == "ELU")
  2542. {
  2543. ncnn::ELU* op = (ncnn::ELU*)layer;
  2544. ncnn::ELU* op_default = (ncnn::ELU*)layer_default;
  2545. fprintf_param_value(" 0=%e", alpha)
  2546. }
  2547. else if (layer->type == "Exp")
  2548. {
  2549. ncnn::Exp* op = (ncnn::Exp*)layer;
  2550. ncnn::Exp* op_default = (ncnn::Exp*)layer_default;
  2551. fprintf_param_value(" 0=%e", base)
  2552. fprintf_param_value(" 1=%e", scale)
  2553. fprintf_param_value(" 2=%e", shift)
  2554. }
  2555. else if (layer->type == "ExpandDims")
  2556. {
  2557. ncnn::ExpandDims* op = (ncnn::ExpandDims*)layer;
  2558. ncnn::ExpandDims* op_default = (ncnn::ExpandDims*)layer_default;
  2559. fprintf_param_value(" 0=%d", expand_w)
  2560. fprintf_param_value(" 1=%d", expand_h)
  2561. fprintf_param_value(" 2=%d", expand_c)
  2562. {
  2563. if (!op->axes.empty()) fprintf_param_int_array(0, op->axes, pp);
  2564. }
  2565. }
  2566. else if (layer->type == "HardSigmoid")
  2567. {
  2568. ncnn::HardSigmoid* op = (ncnn::HardSigmoid*)layer;
  2569. ncnn::HardSigmoid* op_default = (ncnn::HardSigmoid*)layer_default;
  2570. fprintf_param_value(" 0=%e", alpha)
  2571. fprintf_param_value(" 1=%e", beta)
  2572. }
  2573. else if (layer->type == "HardSwish")
  2574. {
  2575. ncnn::HardSwish* op = (ncnn::HardSwish*)layer;
  2576. ncnn::HardSwish* op_default = (ncnn::HardSwish*)layer_default;
  2577. fprintf_param_value(" 0=%e", alpha)
  2578. fprintf_param_value(" 1=%e", beta)
  2579. }
  2580. else if (layer->type == "InnerProduct")
  2581. {
  2582. ncnn::InnerProduct* op = (ncnn::InnerProduct*)layer;
  2583. ncnn::InnerProduct* op_default = (ncnn::InnerProduct*)layer_default;
  2584. fprintf_param_value(" 0=%d", num_output)
  2585. fprintf_param_value(" 1=%d", bias_term)
  2586. fprintf_param_value(" 2=%d", weight_data_size)
  2587. fprintf_param_value(" 8=%d", int8_scale_term)
  2588. fprintf_param_value(" 9=%d", activation_type)
  2589. {
  2590. if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp);
  2591. }
  2592. fwrite_weight_tag_data(0, op->weight_data, bp);
  2593. fwrite_weight_data(op->bias_data, bp);
  2594. }
  2595. else if (layer->type == "Input")
  2596. {
  2597. ncnn::Input* op = (ncnn::Input*)layer;
  2598. ncnn::Input* op_default = (ncnn::Input*)layer_default;
  2599. fprintf_param_value(" 0=%d", w)
  2600. fprintf_param_value(" 1=%d", h)
  2601. fprintf_param_value(" 2=%d", c)
  2602. }
  2603. else if (layer->type == "InstanceNorm")
  2604. {
  2605. ncnn::InstanceNorm* op = (ncnn::InstanceNorm*)layer;
  2606. ncnn::InstanceNorm* op_default = (ncnn::InstanceNorm*)layer_default;
  2607. fprintf_param_value(" 0=%d", channels)
  2608. fprintf_param_value(" 1=%e", eps)
  2609. fwrite_weight_data(op->gamma_data, bp);
  2610. fwrite_weight_data(op->beta_data, bp);
  2611. }
  2612. else if (layer->type == "Interp")
  2613. {
  2614. ncnn::Interp* op = (ncnn::Interp*)layer;
  2615. ncnn::Interp* op_default = (ncnn::Interp*)layer_default;
  2616. fprintf_param_value(" 0=%d", resize_type)
  2617. fprintf_param_value(" 1=%e", height_scale)
  2618. fprintf_param_value(" 2=%e", width_scale)
  2619. fprintf_param_value(" 3=%d", output_height)
  2620. fprintf_param_value(" 4=%d", output_width)
  2621. }
  2622. else if (layer->type == "Log")
  2623. {
  2624. ncnn::Log* op = (ncnn::Log*)layer;
  2625. ncnn::Log* op_default = (ncnn::Log*)layer_default;
  2626. fprintf_param_value(" 0=%e", base)
  2627. fprintf_param_value(" 1=%e", scale)
  2628. fprintf_param_value(" 2=%e", shift)
  2629. }
  2630. else if (layer->type == "LRN")
  2631. {
  2632. ncnn::LRN* op = (ncnn::LRN*)layer;
  2633. ncnn::LRN* op_default = (ncnn::LRN*)layer_default;
  2634. fprintf_param_value(" 0=%d", region_type)
  2635. fprintf_param_value(" 1=%d", local_size)
  2636. fprintf_param_value(" 2=%e", alpha)
  2637. fprintf_param_value(" 3=%e", beta)
  2638. fprintf_param_value(" 4=%e", bias)
  2639. }
  2640. else if (layer->type == "LSTM")
  2641. {
  2642. ncnn::LSTM* op = (ncnn::LSTM*)layer;
  2643. ncnn::LSTM* op_default = (ncnn::LSTM*)layer_default;
  2644. fprintf_param_value(" 0=%d", num_output)
  2645. fprintf_param_value(" 1=%d", weight_data_size)
  2646. fprintf_param_value(" 2=%d", direction)
  2647. fwrite_weight_tag_data(0, op->weight_xc_data, bp);
  2648. fwrite_weight_tag_data(0, op->bias_c_data, bp);
  2649. fwrite_weight_tag_data(0, op->weight_hc_data, bp);
  2650. }
  2651. else if (layer->type == "MemoryData")
  2652. {
  2653. ncnn::MemoryData* op = (ncnn::MemoryData*)layer;
  2654. ncnn::MemoryData* op_default = (ncnn::MemoryData*)layer_default;
  2655. fprintf_param_value(" 0=%d", w)
  2656. fprintf_param_value(" 1=%d", h)
  2657. fprintf_param_value(" 2=%d", c)
  2658. fwrite_weight_data(op->data, bp);
  2659. }
  2660. else if (layer->type == "MVN")
  2661. {
  2662. ncnn::MVN* op = (ncnn::MVN*)layer;
  2663. ncnn::MVN* op_default = (ncnn::MVN*)layer_default;
  2664. fprintf_param_value(" 0=%d", normalize_variance)
  2665. fprintf_param_value(" 1=%d", across_channels)
  2666. fprintf_param_value(" 2=%e", eps)
  2667. }
  2668. else if (layer->type == "Normalize")
  2669. {
  2670. ncnn::Normalize* op = (ncnn::Normalize*)layer;
  2671. ncnn::Normalize* op_default = (ncnn::Normalize*)layer_default;
  2672. fprintf_param_value(" 0=%d", across_spatial)
  2673. fprintf_param_value(" 1=%d", channel_shared)
  2674. fprintf_param_value(" 2=%e", eps)
  2675. fprintf_param_value(" 3=%d", scale_data_size)
  2676. fprintf_param_value(" 4=%d", across_channel)
  2677. fprintf_param_value(" 9=%d", eps_mode)
  2678. fwrite_weight_data(op->scale_data, bp);
  2679. }
  2680. else if (layer->type == "Padding")
  2681. {
  2682. ncnn::Padding* op = (ncnn::Padding*)layer;
  2683. ncnn::Padding* op_default = (ncnn::Padding*)layer_default;
  2684. fprintf_param_value(" 0=%d", top)
  2685. fprintf_param_value(" 1=%d", bottom)
  2686. fprintf_param_value(" 2=%d", left)
  2687. fprintf_param_value(" 3=%d", right)
  2688. fprintf_param_value(" 4=%d", type)
  2689. fprintf_param_value(" 5=%e", value)
  2690. fprintf_param_value(" 6=%d", per_channel_pad_data_size)
  2691. fprintf_param_value(" 7=%d", front)
  2692. fprintf_param_value(" 8=%d", behind)
  2693. }
  2694. else if (layer->type == "Permute")
  2695. {
  2696. ncnn::Permute* op = (ncnn::Permute*)layer;
  2697. ncnn::Permute* op_default = (ncnn::Permute*)layer_default;
  2698. fprintf_param_value(" 0=%d", order_type)
  2699. }
  2700. else if (layer->type == "PixelShuffle")
  2701. {
  2702. ncnn::PixelShuffle* op = (ncnn::PixelShuffle*)layer;
  2703. ncnn::PixelShuffle* op_default = (ncnn::PixelShuffle*)layer_default;
  2704. fprintf_param_value(" 0=%d", upscale_factor)
  2705. }
  2706. else if (layer->type == "Pooling")
  2707. {
  2708. ncnn::Pooling* op = (ncnn::Pooling*)layer;
  2709. ncnn::Pooling* op_default = (ncnn::Pooling*)layer_default;
  2710. fprintf_param_value(" 0=%d", pooling_type)
  2711. fprintf_param_value(" 1=%d", kernel_w)
  2712. {
  2713. if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h);
  2714. }
  2715. fprintf_param_value(" 2=%d", stride_w)
  2716. {
  2717. if (op->stride_h != op->stride_w) fprintf(pp, " 12=%d", op->stride_h);
  2718. }
  2719. fprintf_param_value(" 3=%d", pad_left)
  2720. {
  2721. if (op->pad_top != op->pad_left) fprintf(pp, " 13=%d", op->pad_top);
  2722. }
  2723. {
  2724. if (op->pad_right != op->pad_left) fprintf(pp, " 14=%d", op->pad_right);
  2725. }
  2726. {
  2727. if (op->pad_bottom != op->pad_top) fprintf(pp, " 15=%d", op->pad_bottom);
  2728. }
  2729. fprintf_param_value(" 4=%d", global_pooling)
  2730. fprintf_param_value(" 5=%d", pad_mode)
  2731. }
  2732. else if (layer->type == "Power")
  2733. {
  2734. ncnn::Power* op = (ncnn::Power*)layer;
  2735. ncnn::Power* op_default = (ncnn::Power*)layer_default;
  2736. fprintf_param_value(" 0=%e", power)
  2737. fprintf_param_value(" 1=%e", scale)
  2738. fprintf_param_value(" 2=%e", shift)
  2739. }
  2740. else if (layer->type == "PReLU")
  2741. {
  2742. ncnn::PReLU* op = (ncnn::PReLU*)layer;
  2743. ncnn::PReLU* op_default = (ncnn::PReLU*)layer_default;
  2744. fprintf_param_value(" 0=%d", num_slope)
  2745. fwrite_weight_data(op->slope_data, bp);
  2746. }
  2747. else if (layer->type == "PriorBox")
  2748. {
  2749. ncnn::PriorBox* op = (ncnn::PriorBox*)layer;
  2750. ncnn::PriorBox* op_default = (ncnn::PriorBox*)layer_default;
  2751. {
  2752. if (!op->min_sizes.empty()) fprintf_param_float_array(0, op->min_sizes, pp);
  2753. }
  2754. {
  2755. if (!op->max_sizes.empty()) fprintf_param_float_array(1, op->max_sizes, pp);
  2756. }
  2757. {
  2758. if (!op->aspect_ratios.empty()) fprintf_param_float_array(2, op->aspect_ratios, pp);
  2759. }
  2760. fprintf_param_value(" 3=%e", variances[0])
  2761. fprintf_param_value(" 4=%e", variances[1])
  2762. fprintf_param_value(" 5=%e", variances[2])
  2763. fprintf_param_value(" 6=%e", variances[3])
  2764. fprintf_param_value(" 7=%d", flip)
  2765. fprintf_param_value(" 8=%d", clip)
  2766. fprintf_param_value(" 9=%d", image_width)
  2767. fprintf_param_value(" 10=%d", image_height)
  2768. fprintf_param_value(" 11=%e", step_width)
  2769. fprintf_param_value(" 12=%e", step_height)
  2770. fprintf_param_value(" 13=%e", offset)
  2771. }
  2772. else if (layer->type == "Proposal")
  2773. {
  2774. ncnn::Proposal* op = (ncnn::Proposal*)layer;
  2775. ncnn::Proposal* op_default = (ncnn::Proposal*)layer_default;
  2776. fprintf_param_value(" 0=%d", feat_stride)
  2777. fprintf_param_value(" 1=%d", base_size)
  2778. fprintf_param_value(" 2=%d", pre_nms_topN)
  2779. fprintf_param_value(" 3=%d", after_nms_topN)
  2780. fprintf_param_value(" 4=%e", nms_thresh)
  2781. fprintf_param_value(" 5=%d", min_size)
  2782. }
  2783. else if (layer->type == "PSROIPooling")
  2784. {
  2785. ncnn::PSROIPooling* op = (ncnn::PSROIPooling*)layer;
  2786. ncnn::PSROIPooling* op_default = (ncnn::PSROIPooling*)layer_default;
  2787. fprintf_param_value(" 0=%d", pooled_width)
  2788. fprintf_param_value(" 1=%d", pooled_height)
  2789. fprintf_param_value(" 2=%e", spatial_scale)
  2790. fprintf_param_value(" 3=%d", output_dim)
  2791. }
  2792. else if (layer->type == "Quantize")
  2793. {
  2794. ncnn::Quantize* op = (ncnn::Quantize*)layer;
  2795. ncnn::Quantize* op_default = (ncnn::Quantize*)layer_default;
  2796. fprintf_param_value(" 0=%e", scale)
  2797. }
  2798. else if (layer->type == "Reduction")
  2799. {
  2800. ncnn::Reduction* op = (ncnn::Reduction*)layer;
  2801. ncnn::Reduction* op_default = (ncnn::Reduction*)layer_default;
  2802. fprintf_param_value(" 0=%d", operation)
  2803. fprintf_param_value(" 1=%d", reduce_all)
  2804. fprintf_param_value(" 2=%e", coeff)
  2805. {
  2806. if (!op->axes.empty()) fprintf_param_int_array(3, op->axes, pp);
  2807. }
  2808. fprintf_param_value(" 4=%d", keepdims)
  2809. }
  2810. else if (layer->type == "ReLU")
  2811. {
  2812. ncnn::ReLU* op = (ncnn::ReLU*)layer;
  2813. ncnn::ReLU* op_default = (ncnn::ReLU*)layer_default;
  2814. fprintf_param_value(" 0=%e", slope)
  2815. }
  2816. else if (layer->type == "Reorg")
  2817. {
  2818. ncnn::Reorg* op = (ncnn::Reorg*)layer;
  2819. ncnn::Reorg* op_default = (ncnn::Reorg*)layer_default;
  2820. fprintf_param_value(" 0=%d", stride)
  2821. }
  2822. else if (layer->type == "Requantize")
  2823. {
  2824. ncnn::Requantize* op = (ncnn::Requantize*)layer;
  2825. ncnn::Requantize* op_default = (ncnn::Requantize*)layer_default;
  2826. fprintf_param_value(" 0=%e", scale_in)
  2827. fprintf_param_value(" 1=%e", scale_out)
  2828. fprintf_param_value(" 2=%d", bias_term)
  2829. fprintf_param_value(" 3=%d", bias_data_size)
  2830. fprintf_param_value(" 4=%d", fusion_relu)
  2831. }
  2832. else if (layer->type == "Reshape")
  2833. {
  2834. ncnn::Reshape* op = (ncnn::Reshape*)layer;
  2835. ncnn::Reshape* op_default = (ncnn::Reshape*)layer_default;
  2836. fprintf_param_value(" 0=%d", w)
  2837. fprintf_param_value(" 1=%d", h)
  2838. fprintf_param_value(" 2=%d", c)
  2839. fprintf_param_value(" 3=%d", permute)
  2840. }
  2841. else if (layer->type == "ROIAlign")
  2842. {
  2843. ncnn::ROIAlign* op = (ncnn::ROIAlign*)layer;
  2844. ncnn::ROIAlign* op_default = (ncnn::ROIAlign*)layer_default;
  2845. fprintf_param_value(" 0=%d", pooled_width)
  2846. fprintf_param_value(" 1=%d", pooled_height)
  2847. fprintf_param_value(" 2=%e", spatial_scale)
  2848. fprintf_param_value(" 3=%d", sampling_ratio)
  2849. fprintf_param_value(" 4=%d", aligned)
  2850. fprintf_param_value(" 5=%d", version)
  2851. }
  2852. else if (layer->type == "ROIPooling")
  2853. {
  2854. ncnn::ROIPooling* op = (ncnn::ROIPooling*)layer;
  2855. ncnn::ROIPooling* op_default = (ncnn::ROIPooling*)layer_default;
  2856. fprintf_param_value(" 0=%d", pooled_width)
  2857. fprintf_param_value(" 1=%d", pooled_height)
  2858. fprintf_param_value(" 2=%e", spatial_scale)
  2859. }
  2860. else if (layer->type == "Scale")
  2861. {
  2862. ncnn::Scale* op = (ncnn::Scale*)layer;
  2863. ncnn::Scale* op_default = (ncnn::Scale*)layer_default;
  2864. fprintf_param_value(" 0=%d", scale_data_size)
  2865. fprintf_param_value(" 1=%d", bias_term)
  2866. fwrite_weight_data(op->scale_data, bp);
  2867. fwrite_weight_data(op->bias_data, bp);
  2868. }
  2869. else if (layer->type == "ShuffleChannel")
  2870. {
  2871. ncnn::ShuffleChannel* op = (ncnn::ShuffleChannel*)layer;
  2872. ncnn::ShuffleChannel* op_default = (ncnn::ShuffleChannel*)layer_default;
  2873. fprintf_param_value(" 0=%d", group)
  2874. }
  2875. else if (layer->type == "Slice")
  2876. {
  2877. ncnn::Slice* op = (ncnn::Slice*)layer;
  2878. ncnn::Slice* op_default = (ncnn::Slice*)layer_default;
  2879. {
  2880. if (!op->slices.empty()) fprintf_param_int_array(0, op->slices, pp);
  2881. }
  2882. fprintf_param_value(" 1=%d", axis)
  2883. }
  2884. else if (layer->type == "Softmax")
  2885. {
  2886. ncnn::Softmax* op = (ncnn::Softmax*)layer;
  2887. ncnn::Softmax* op_default = (ncnn::Softmax*)layer_default;
  2888. fprintf_param_value(" 0=%d", axis)
  2889. // HACK
  2890. if (op->axis != 0)
  2891. {
  2892. int fixbug0 = 1;
  2893. fprintf(pp, " 1=%d", fixbug0);
  2894. }
  2895. }
  2896. else if (layer->type == "Squeeze")
  2897. {
  2898. ncnn::Squeeze* op = (ncnn::Squeeze*)layer;
  2899. ncnn::Squeeze* op_default = (ncnn::Squeeze*)layer_default;
  2900. fprintf_param_value(" 0=%d", squeeze_w)
  2901. fprintf_param_value(" 1=%d", squeeze_h)
  2902. fprintf_param_value(" 2=%d", squeeze_c)
  2903. {
  2904. if (!op->axes.empty()) fprintf_param_int_array(0, op->axes, pp);
  2905. }
  2906. }
  2907. else if (layer->type == "Threshold")
  2908. {
  2909. ncnn::Threshold* op = (ncnn::Threshold*)layer;
  2910. ncnn::Threshold* op_default = (ncnn::Threshold*)layer_default;
  2911. fprintf_param_value(" 0=%e", threshold)
  2912. }
  2913. else if (layer->type == "UnaryOp")
  2914. {
  2915. ncnn::UnaryOp* op = (ncnn::UnaryOp*)layer;
  2916. ncnn::UnaryOp* op_default = (ncnn::UnaryOp*)layer_default;
  2917. fprintf_param_value(" 0=%d", op_type)
  2918. }
  2919. else if (layer->type == "YoloDetectionOutput")
  2920. {
  2921. ncnn::YoloDetectionOutput* op = (ncnn::YoloDetectionOutput*)layer;
  2922. ncnn::YoloDetectionOutput* op_default = (ncnn::YoloDetectionOutput*)layer_default;
  2923. fprintf_param_value(" 0=%d", num_class)
  2924. fprintf_param_value(" 1=%d", num_box)
  2925. fprintf_param_value(" 2=%e", confidence_threshold)
  2926. fprintf_param_value(" 3=%e", nms_threshold)
  2927. {
  2928. if (!op->biases.empty()) fprintf_param_float_array(4, op->biases, pp);
  2929. }
  2930. }
  2931. else if (layer->type == "Yolov3DetectionOutput")
  2932. {
  2933. ncnn::Yolov3DetectionOutput* op = (ncnn::Yolov3DetectionOutput*)layer;
  2934. ncnn::Yolov3DetectionOutput* op_default = (ncnn::Yolov3DetectionOutput*)layer_default;
  2935. fprintf_param_value(" 0=%d", num_class)
  2936. fprintf_param_value(" 1=%d", num_box)
  2937. fprintf_param_value(" 2=%e", confidence_threshold)
  2938. fprintf_param_value(" 3=%e", nms_threshold)
  2939. {
  2940. if (!op->biases.empty()) fprintf_param_float_array(4, op->biases, pp);
  2941. }
  2942. {
  2943. if (!op->mask.empty()) fprintf_param_int_array(5, op->mask, pp);
  2944. }
  2945. {
  2946. if (!op->anchors_scale.empty()) fprintf_param_float_array(6, op->anchors_scale, pp);
  2947. }
  2948. }
  2949. #undef fprintf_param_value
  2950. fprintf(pp, "\n");
  2951. delete layer_default;
  2952. }
  2953. fclose(pp);
  2954. fclose(bp);
  2955. return 0;
  2956. }
  2957. int main(int argc, char** argv)
  2958. {
  2959. #if defined(__aarch64__) && defined(LINUX)
  2960. if (argc != 10)
  2961. {
  2962. fprintf(stderr, "usage: %s [inparam] [inbin] [outparam] [outbin] [flag] [dataname] [w] [h] [c]\n", argv[0]);
  2963. return -1;
  2964. }
  2965. const char* dataname = argv[6];
  2966. int inw = atoi(argv[7]);
  2967. int inh = atoi(argv[8]);
  2968. int inc = atoi(argv[9]);
  2969. #else
  2970. if (argc != 6)
  2971. {
  2972. fprintf(stderr, "usage: %s [inparam] [inbin] [outparam] [outbin] [flag]\n", argv[0]);
  2973. return -1;
  2974. }
  2975. #endif // defined(__aarch64__) && defined(LINUX)
  2976. const char* inparam = argv[1];
  2977. const char* inbin = argv[2];
  2978. const char* outparam = argv[3];
  2979. const char* outbin = argv[4];
  2980. int flag = atoi(argv[5]);
  2981. NetOptimize optimizer;
  2982. if (flag == 65536)
  2983. {
  2984. optimizer.storage_type = 1;
  2985. }
  2986. else
  2987. {
  2988. optimizer.storage_type = 0;
  2989. }
  2990. optimizer.load_param(inparam);
  2991. if (strcmp(inbin, "null") == 0)
  2992. {
  2993. DataReaderFromEmpty dr;
  2994. optimizer.load_model(dr);
  2995. }
  2996. else
  2997. optimizer.load_model(inbin);
  2998. #if defined(__aarch64__) && defined(LINUX)
  2999. optimizer.find_fastest_fp32_conv(dataname, inw, inh, inc);
  3000. #endif // defined(__aarch64__) && defined(LINUX)
  3001. optimizer.fuse_batchnorm_scale();
  3002. optimizer.fuse_convolution_batchnorm();
  3003. optimizer.fuse_convolution_mul();
  3004. optimizer.fuse_convolution_add();
  3005. optimizer.fuse_convolutiondepthwise_batchnorm();
  3006. optimizer.fuse_convolutiondepthwise_mul();
  3007. optimizer.fuse_convolutiondepthwise_add();
  3008. optimizer.fuse_deconvolution_batchnorm();
  3009. optimizer.fuse_deconvolution_mul();
  3010. optimizer.fuse_deconvolution_add();
  3011. optimizer.fuse_deconvolutiondepthwise_batchnorm();
  3012. optimizer.fuse_innerproduct_batchnorm();
  3013. optimizer.fuse_innerproduct_add();
  3014. optimizer.fuse_innerproduct_dropout();
  3015. optimizer.fuse_convolution_activation();
  3016. optimizer.fuse_convolutiondepthwise_activation();
  3017. optimizer.fuse_deconvolution_activation();
  3018. optimizer.fuse_deconvolutiondepthwise_activation();
  3019. optimizer.fuse_innerproduct_activation();
  3020. optimizer.fuse_memorydata_binaryop();
  3021. optimizer.fuse_binaryop_eltwise();
  3022. optimizer.eliminate_dropout();
  3023. optimizer.eliminate_pooling1x1();
  3024. optimizer.eliminate_noop();
  3025. optimizer.eliminate_flatten_after_global_pooling();
  3026. optimizer.eliminate_reshape_after_global_pooling();
  3027. optimizer.eliminate_reshape_before_binaryop();
  3028. optimizer.replace_convolution_with_innerproduct_after_global_pooling();
  3029. optimizer.replace_convolution_with_innerproduct_after_innerproduct();
  3030. optimizer.eliminate_flatten_after_innerproduct();
  3031. optimizer.eliminate_orphaned_memorydata();
  3032. optimizer.shape_inference();
  3033. optimizer.save(outparam, outbin);
  3034. return 0;
  3035. }