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