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

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