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