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

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