|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292 |
- // Tencent is pleased to support the open source community by making ncnn available.
- //
- // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
- //
- // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
- // in compliance with the License. You may obtain a copy of the License at
- //
- // https://opensource.org/licenses/BSD-3-Clause
- //
- // Unless required by applicable law or agreed to in writing, software distributed
- // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- // CONDITIONS OF ANY KIND, either express or implied. See the License for the
- // specific language governing permissions and limitations under the License.
-
- #include <stdio.h>
- #include <stdint.h>
- #include <string.h>
-
- #include <map>
- #include <set>
- #include <string>
- #include <vector>
-
- class MXNetParam;
- class MXNetNode
- {
- public:
- bool has_attr(const char* key) const;
-
- class AttrProxy
- {
- MXNetNode const* _n;
- const char* const _key;
- public:
- AttrProxy( MXNetNode const* n, const char* key ) : _n(n), _key(key) {}
- operator int() const { return _n->attr_i(_key); }
- operator float() const { return _n->attr_f(_key); }
- operator std::string() const { return _n->attr_s(_key); }
- operator std::vector<int>() const { return _n->attr_ai(_key); }
- operator std::vector<float>() const { return _n->attr_af(_key); }
- };
-
- AttrProxy attr(const char* key) const { return AttrProxy(this, key); }
-
- int attr_i(const char* key) const;
- float attr_f(const char* key) const;
- std::string attr_s(const char* key) const;
- std::vector<int> attr_ai(const char* key) const;
- std::vector<float> attr_af(const char* key) const;
-
- public:
- bool is_weight() const;
- bool has_weight(int i) const;
- std::vector<float> weight(int i, int init_len = 0) const;
-
- std::vector<MXNetNode>* nodes;// reference
- std::vector<MXNetParam>* params;// reference
-
- public:
- std::string op;
- std::string name;
- int output_size;
- std::map<std::string, std::string> attrs;
- std::vector<int> inputs;
- std::vector<int> subinputs;
- std::vector<int> weights;
- };
-
- class MXNetParam
- {
- public:
- std::string name;
- std::vector<float> data;
- std::string init;
- };
-
- bool MXNetNode::has_attr(const char* key) const
- {
- const std::map<std::string, std::string>::const_iterator it = attrs.find(key);
- return it != attrs.end();
- }
-
- int MXNetNode::attr_i(const char* key) const
- {
- const std::map<std::string, std::string>::const_iterator it = attrs.find(key);
- if (it == attrs.end())
- return 0;
-
- if (it->second == "False")
- return 0;
-
- if (it->second == "True")
- return 1;
-
- int i = 0;
- int nscan = sscanf(it->second.c_str(), "%d", &i);
- if (nscan != 1)
- return 0;
-
- return i;
- }
-
- float MXNetNode::attr_f(const char* key) const
- {
- const std::map<std::string, std::string>::const_iterator it = attrs.find(key);
- if (it == attrs.end())
- return 0.f;
-
- float f = 0;
- int nscan = sscanf(it->second.c_str(), "%f", &f);
- if (nscan != 1)
- return 0.f;
-
- return f;
- }
-
- std::string MXNetNode::attr_s(const char* key) const
- {
- const std::map<std::string, std::string>::const_iterator it = attrs.find(key);
- if (it == attrs.end())
- return std::string();
-
- return it->second;
- }
-
- std::vector<int> MXNetNode::attr_ai(const char* key) const
- {
- const std::map<std::string, std::string>::const_iterator it = attrs.find(key);
- if (it == attrs.end())
- return std::vector<int>();
-
- // (1,2,3)
- std::vector<int> list;
-
- int i = 0;
- int c = 0;
- int nconsumed = 0;
- int nscan = sscanf(it->second.c_str() + c, "%*[(,]%d%n", &i, &nconsumed);
- while (nscan == 1)
- {
- list.push_back(i);
- // fprintf(stderr, "%d\n", i);
-
- i = 0;
- c += nconsumed;
- nscan = sscanf(it->second.c_str() + c, "%*[(,]%d%n", &i, &nconsumed);
- }
-
- return list;
- }
-
- std::vector<float> MXNetNode::attr_af(const char* key) const
- {
- const std::map<std::string, std::string>::const_iterator it = attrs.find(key);
- if (it == attrs.end())
- return std::vector<float>();
-
- // (0.1,0.2,0.3)
- std::vector<float> list;
-
- float i = 0.f;
- int c = 0;
- int nconsumed = 0;
- int nscan = sscanf(it->second.c_str() + c, "%*[(,]%f%n", &i, &nconsumed);
- while (nscan == 1)
- {
- list.push_back(i);
- // fprintf(stderr, "%f\n", i);
-
- i = 0.f;
- c += nconsumed;
- nscan = sscanf(it->second.c_str() + c, "%*[(,]%f%n", &i, &nconsumed);
- }
-
- return list;
- }
-
- bool MXNetNode::is_weight() const
- {
- for (int i=0; i<(int)(*params).size(); i++)
- {
- const MXNetParam& p = (*params)[i];
- if (p.name == name)
- return true;
- }
-
- return false;
- }
-
- bool MXNetNode::has_weight(int i) const
- {
- if (i < 0 || i >= (int)weights.size())
- return false;
-
- const std::string& name = (*nodes)[ weights[i] ].name;
-
- for (int i=0; i<(int)(*params).size(); i++)
- {
- const MXNetParam& p = (*params)[i];
- if (p.name == name)
- return true;
- }
-
- return false;
- }
-
- std::vector<float> MXNetNode::weight(int i, int init_len) const
- {
- if (i < 0 || i >= (int)weights.size())
- return std::vector<float>();
-
- const std::string& name = (*nodes)[ weights[i] ].name;
-
- for (int i=0; i<(int)(*params).size(); i++)
- {
- const MXNetParam& p = (*params)[i];
- if (p.name != name)
- continue;
-
- if (!p.data.empty())
- return p.data;
-
- std::vector<float> data;
-
- if (!p.init.empty() && init_len != 0)
- {
- if (p.init == "[\\$zero\\$, {}]" || p.init == "[\\\"zero\\\", {}]" || p.init == "zeros")
- {
- data.resize(init_len, 0.f);
- }
- else if (p.init == "[\\$one\\$, {}]" || p.init == "[\\\"one\\\", {}]" || p.init == "ones")
- {
- data.resize(init_len, 1.f);
- }
- }
-
- return data;
- }
-
- return std::vector<float>();
- }
-
- static void replace_backslash_doublequote_dollar(char* s)
- {
- char* a = s;
- char* b = s+1;
- while (*a && *b)
- {
- if (*a == '\\' && *b == '\"')
- {
- *b = '$';
- }
-
- a++;
- b++;
- }
- }
-
- static void parse_input_list(const char* s, std::vector<int>& inputs, std::vector<int>& subinputs)
- {
- inputs.clear();
- subinputs.clear();
-
- if (memcmp(s, "[]", 2) == 0)
- return;
-
- int nscan = 0;
- int nconsumed = 0;
-
- int id;
- int subid;
-
- int c = 1;// skip leading [
- nscan = sscanf(s + c, "[%d, %d%n", &id, &subid, &nconsumed);
- while (nscan == 2)
- {
- inputs.push_back(id);
- subinputs.push_back(subid);
- // fprintf(stderr, "%d %d\n", id, subid);
-
- c += nconsumed;
- nscan = sscanf(s + c, "%*[^[][%d, %d%n", &id, &subid, &nconsumed);
- }
- }
-
- static bool read_mxnet_json(const char* jsonpath, std::vector<MXNetNode>& nodes)
- {
- FILE* fp = fopen(jsonpath, "rb");
- if (!fp)
- {
- fprintf(stderr, "fopen %s failed\n", jsonpath);
- return false;
- }
-
- int internal_unknown = 0;
-
- char line[1024];
-
- //{
- (void)fgets(line, 1024, fp);
-
- MXNetNode n;
-
- bool in_nodes_list = false;
- bool in_node_block = false;
- bool in_attr_block = false;
- bool in_inputs_block = false;
- while (!feof(fp))
- {
- char* s = fgets(line, 1024, fp);
- if (!s)
- break;
-
- if (in_inputs_block)
- {
- // ]
- if (memcmp(line, " ]", 7) == 0)
- {
- in_inputs_block = false;
- continue;
- }
-
- // [439, 0, 0],
- int id;
- int subid;
- int nscan = sscanf(line, " [%d, %d", &id, &subid);
- if (nscan == 2)
- {
- n.inputs.push_back(id);
- n.subinputs.push_back(subid);
- continue;
- }
- }
-
- if (in_attr_block)
- {
- // },
- if (memcmp(line, " }", 7) == 0)
- {
- in_attr_block = false;
- continue;
- }
-
- // replace \" with \$
- replace_backslash_doublequote_dollar(line);
-
- // "kernel": "(7,7)",
- char key[256] = {0};
- char value[256] = {0};
- int nscan = sscanf(line, " \"%255[^\"]\": \"%255[^\"]\"", key, value);
- if (nscan == 2)
- {
- n.attrs[key] = value;
- // fprintf(stderr, "# %s = %s\n", key, value);
- continue;
- }
- }
-
- if (in_node_block)
- {
- // },
- if (memcmp(line, " }", 5) == 0)
- {
- // new node
- if (n.name.empty())
- {
- // assign default unknown name
- char unknownname[256];
- sprintf(unknownname, "unknownncnn_%d", internal_unknown);
-
- n.name = unknownname;
-
- internal_unknown++;
- }
- nodes.push_back(n);
-
- in_node_block = false;
- continue;
- }
-
- int nscan;
-
- // "op": "Convolution",
- char op[256] = {0};
- nscan = sscanf(line, " \"op\": \"%255[^\"]\",", op);
- if (nscan == 1)
- {
- n.op = op;
- // fprintf(stderr, "op = %s\n", op);
- continue;
- }
-
- // "name": "conv0",
- char name[256] = {0};
- nscan = sscanf(line, " \"name\": \"%255[^\"]\",", name);
- if (nscan == 1)
- {
- n.name = name;
- // fprintf(stderr, "name = %s\n", name);
- continue;
- }
-
- // "inputs": [
- if (memcmp(line, " \"inputs\": [\n", 18) == 0)
- {
- in_inputs_block = true;
- continue;
- }
-
- // "inputs": []
- char inputs[256] = {0};
- nscan = sscanf(line, " \"inputs\": %255[^\n]", inputs);
- if (nscan == 1)
- {
- parse_input_list(inputs, n.inputs, n.subinputs);
- // fprintf(stderr, "inputs = %s\n", inputs);
- continue;
- }
-
- // "param": {},
- if (memcmp(line, " \"param\": {}", 17) == 0)
- {
- continue;
- }
-
- // replace \" with \$
- replace_backslash_doublequote_dollar(line);
-
- // "attr": {"__init__": "[\"zero\", {}]"},
- char key[256] = {0};
- char value[256] = {0};
- nscan = sscanf(line, " \"attr\": {\"%255[^\"]\": \"%255[^\"]\"}", key, value);
- if (nscan == 2)
- {
- n.attrs[key] = value;
- // fprintf(stderr, "# %s = %s\n", key, value);
- continue;
- }
-
- // "attrs": {"__init__": "[\"zero\", {}]"},
- nscan = sscanf(line, " \"attrs\": {\"%255[^\"]\": \"%255[^\"]\"}", key, value);
- if (nscan == 2)
- {
- n.attrs[key] = value;
- // fprintf(stderr, "# %s = %s\n", key, value);
- continue;
- }
-
- // "param": {"p": "0.5"},
- nscan = sscanf(line, " \"param\": {\"%255[^\"]\": \"%255[^\"]\"}", key, value);
- if (nscan == 2)
- {
- n.attrs[key] = value;
- // fprintf(stderr, "# %s = %s\n", key, value);
- continue;
- }
-
- // "attr": {
- if (memcmp(line, " \"attr\": {", 15) == 0)
- {
- in_attr_block = true;
- continue;
- }
-
- // "attrs": {
- if (memcmp(line, " \"attrs\": {", 16) == 0)
- {
- in_attr_block = true;
- continue;
- }
-
- // "param": {
- if (memcmp(line, " \"param\": {", 16) == 0)
- {
- in_attr_block = true;
- continue;
- }
-
- }
-
- if (in_nodes_list)
- {
- // ],
- if (memcmp(line, " ],", 4) == 0)
- {
- in_nodes_list = false;
- // all nodes parsed
- break;
- }
-
- // {
- if (memcmp(line, " {", 5) == 0)
- {
- n = MXNetNode();
-
- in_node_block = true;
- continue;
- }
-
- }
-
- // "nodes": [
- if (memcmp(line, " \"nodes\": [", 12) == 0)
- {
- in_nodes_list = true;
- continue;
- }
-
- }
-
- fclose(fp);
-
- return true;
- }
-
- static bool read_mxnet_param(const char* parampath, std::vector<MXNetParam>& params)
- {
- FILE* fp = fopen(parampath, "rb");
- if (!fp)
- {
- fprintf(stderr, "fopen %s failed\n", parampath);
- return false;
- }
-
- uint64_t header;
- uint64_t reserved;
- fread(&header, 1, sizeof(uint64_t), fp);
- fread(&reserved, 1, sizeof(uint64_t), fp);
-
- // NDArray vec
-
- // each data
- uint64_t data_count;
- fread(&data_count, 1, sizeof(uint64_t), fp);
-
- // fprintf(stderr, "data count = %d\n", (int)data_count);
-
- for (int i = 0; i < (int)data_count; i++)
- {
- uint32_t magic;// 0xF993FAC9
- fread(&magic, 1, sizeof(uint32_t), fp);
-
- // shape
- uint32_t ndim;
- std::vector<int64_t> shape;
-
- if (magic == 0xF993FAC9)
- {
- int32_t stype;
- fread(&stype, 1, sizeof(int32_t), fp);
-
- fread(&ndim, 1, sizeof(uint32_t), fp);
-
- shape.resize(ndim);
- fread(&shape[0], 1, ndim * sizeof(int64_t), fp);
- }
- else if (magic == 0xF993FAC8)
- {
- fread(&ndim, 1, sizeof(uint32_t), fp);
-
- shape.resize(ndim);
- fread(&shape[0], 1, ndim * sizeof(int64_t), fp);
- }
- else
- {
- ndim = magic;
-
- shape.resize(ndim);
-
- std::vector<uint32_t> shape32;
- shape32.resize(ndim);
- fread(&shape32[0], 1, ndim * sizeof(uint32_t), fp);
-
- for (int j=0; j<(int)ndim; j++)
- {
- shape[j] = shape32[j];
- }
- }
-
- // context
- int32_t dev_type;
- int32_t dev_id;
- fread(&dev_type, 1, sizeof(int32_t), fp);
- fread(&dev_id, 1, sizeof(int32_t), fp);
-
- int32_t type_flag;
- fread(&type_flag, 1, sizeof(int32_t), fp);
-
- // data
- size_t len = 0;
- if (shape.size() == 1) len = shape[0];
- if (shape.size() == 2) len = shape[0] * shape[1];
- if (shape.size() == 3) len = shape[0] * shape[1] * shape[2];
- if (shape.size() == 4) len = shape[0] * shape[1] * shape[2] * shape[3];
-
- MXNetParam p;
-
- p.data.resize(len);
- fread(&p.data[0], 1, len * sizeof(float), fp);
-
- params.push_back(p);
-
- // fprintf(stderr, "%u read\n", len);
- }
-
- // each name
- uint64_t name_count;
- fread(&name_count, 1, sizeof(uint64_t), fp);
-
- // fprintf(stderr, "name count = %d\n", (int)name_count);
-
- for (int i = 0; i < (int)name_count; i++)
- {
- uint64_t len;
- fread(&len, 1, sizeof(uint64_t), fp);
-
- MXNetParam& p = params[i];
-
- p.name.resize(len);
- fread((char*)p.name.data(), 1, len, fp);
-
- // cut leading arg:
- if (memcmp(p.name.c_str(), "arg:", 4) == 0)
- {
- p.name = std::string(p.name.c_str() + 4);
- }
- if (memcmp(p.name.c_str(), "aux:", 4) == 0)
- {
- p.name = std::string(p.name.c_str() + 4);
- }
-
- // fprintf(stderr, "%s read\n", p.name.c_str());
- }
-
- fclose(fp);
-
- return true;
- }
-
- int main(int argc, char** argv)
- {
- const char* jsonpath = argv[1];
- const char* parampath = argv[2];
- const char* ncnn_prototxt = argc >= 5 ? argv[3] : "ncnn.param";
- const char* ncnn_modelbin = argc >= 5 ? argv[4] : "ncnn.bin";
-
- std::vector<MXNetNode> nodes;
- std::vector<MXNetParam> params;
-
- read_mxnet_json(jsonpath, nodes);
- read_mxnet_param(parampath, params);
-
- FILE* pp = fopen(ncnn_prototxt, "wb");
- FILE* bp = fopen(ncnn_modelbin, "wb");
-
- // magic
- fprintf(pp, "7767517\n");
-
- int node_count = nodes.size();
-
- // node reference
- std::map<int, int> node_reference;
-
- // weight node
- std::vector<int> weight_nodes;
-
- // global definition line
- // [layer count] [blob count]
- std::set<std::string> blob_names;
- for (int i=0; i<node_count; i++)
- {
- MXNetNode& n = nodes[i];
-
- // assign global param reference
- n.nodes = &nodes;
- n.params = ¶ms;
-
- const std::string& output_name = n.name;
- n.output_size = 1;
-
- if (n.op == "null")
- {
- if (n.is_weight())
- {
- weight_nodes.push_back(i);
- }
- else
- {
- if (n.has_attr("__init__"))
- {
- // init weight param
- MXNetParam pi;
- pi.name = n.name;
- pi.init = (std::string)n.attr("__init__");
- params.push_back(pi);
-
- weight_nodes.push_back(i);
- }
- else
- {
- // null node without data, treat it as network input
- }
- }
- continue;
- }
- else if (n.op == "_contrib_MultiBoxTarget")
- {
- n.output_size = 3;
- }
- else if (n.op == "SliceChannel")
- {
- n.output_size = n.attr("num_outputs");
- }
-
- // distinguish weights and inputs
- std::vector<int> weights;
- std::vector<int> inputs;
- for (int j=0; j<(int)n.inputs.size(); j++)
- {
- int input_index = n.inputs[j];
- if (nodes[input_index].is_weight())
- {
- weights.push_back(input_index);
- continue;
- }
-
- inputs.push_back(input_index);
- }
- n.inputs = inputs;
- n.weights = weights;
-
- if (n.op == "_contrib_MultiBoxDetection")
- {
- // reorder input blob
- int temp = n.inputs[0];
- n.inputs[0] = n.inputs[1];
- n.inputs[1] = temp;
- }
-
- // input
- for (int j=0; j<(int)n.inputs.size(); j++)
- {
- int input_index = n.inputs[j];
- int subinput_index = n.subinputs[j];
-
- std::string input_name = nodes[input_index].name;
- // fprintf(stderr, "input = %s\n", input_name.c_str());
-
- if (subinput_index != 0)
- {
- char subinputsuffix[256];
- sprintf(subinputsuffix, "_subncnn_%d", subinput_index);
- input_name = input_name + subinputsuffix;
- }
-
- blob_names.insert(input_name);
-
- int input_uid = input_index | (subinput_index << 16);
- if (node_reference.find(input_uid) == node_reference.end())
- {
- node_reference[input_uid] = 1;
- }
- else
- {
- node_reference[input_uid] = node_reference[input_uid] + 1;
- }
- }
-
- // output
- // fprintf(stderr, "output = %s\n", output_name.c_str());
- blob_names.insert(output_name);
-
- for (int j=1; j<n.output_size; j++)
- {
- char subinputsuffix[256];
- sprintf(subinputsuffix, "_%d", j);
- std::string output_name_j = output_name + subinputsuffix;
- blob_names.insert(output_name_j);
- }
- }
-
- // for (std::map<int, int>::iterator it = node_reference.begin(); it != node_reference.end(); it++)
- // {
- // fprintf(stderr, "ref %d %d\n", it->first, it->second);
- // }
-
- // op chain fusion
- int reduced_node_count = 0;
- for (int i=0; i<node_count; i++)
- {
- const MXNetNode& n = nodes[i];
-
- if (n.is_weight())
- continue;
-
- // ShuffleChannel <= Reshape - SwapAxis - Reshape
- if (n.op == "Reshape")
- {
- if (node_reference[i] != 1)
- continue;
-
- // "shape": "(0, -4, X, -1, -2)"
- std::vector<int> shape = n.attr("shape");
- if (shape.size() != 5)
- continue;
- if (shape[0] != 0 || shape[1] != -4 || shape[3] != -1 || shape[4] != -2)
- continue;
-
- if (i+2 >= node_count)
- continue;
-
- const MXNetNode& n2 = nodes[i+1];
- const MXNetNode& n3 = nodes[i+2];
-
- if (n2.op != "SwapAxis" || n3.op != "Reshape")
- continue;
-
- if (node_reference[i+1] != 1)
- continue;
-
- // "dim1": "1", "dim2": "2"
- int dim1 = n2.attr("dim1");
- int dim2 = n2.attr("dim2");
- if (dim1 != 1 || dim2 != 2)
- continue;
-
- // "shape": "(0, -3, -2)"
- std::vector<int> shape3 = n3.attr("shape");
- if (shape3.size() != 3)
- continue;
- if (shape3[0] != 0 || shape3[1] != -3 || shape3[2] != -2)
- continue;
-
- // reduce
- nodes[i].op = "noop_reducedncnn";
- nodes[i+1].op = "noop_reducedncnn";
-
- node_reference.erase(node_reference.find(i));
- node_reference.erase(node_reference.find(i+1));
- blob_names.erase(n.name);
- blob_names.erase(n2.name);
-
- MXNetNode new_node;
- new_node.nodes = &nodes;
- new_node.params = ¶ms;
- new_node.op = "ShuffleChannel";
- // new_node.name = n.name + "_" + n2.name + "_" + n3.name;
- new_node.name = n3.name;
- new_node.output_size = n3.output_size;
- char group[16];
- sprintf(group, "%d", shape[2]);
- new_node.attrs["group"] = group;
- new_node.inputs = n.inputs;
- new_node.subinputs = n.subinputs;
-
- nodes[i+2] = new_node;
-
- reduced_node_count += 2;
- i += 2;
- }
- }
-
- // remove node_reference entry with reference equals to one
- int splitncnn_blob_count = 0;
- std::map<int, int>::iterator it = node_reference.begin();
- while (it != node_reference.end())
- {
- if (it->second == 1)
- {
- node_reference.erase(it++);
- }
- else
- {
- splitncnn_blob_count += it->second;
- // fprintf(stderr, "%s %d\n", it->first.c_str(), it->second);
- ++it;
- }
- }
-
- // fprintf(stderr, "%d %d %d %d, %d %d\n", node_count, reduced_node_count, node_reference.size(), weight_nodes.size(), blob_names.size(), splitncnn_blob_count);
-
- fprintf(pp, "%lu %lu\n", node_count - reduced_node_count + node_reference.size() - weight_nodes.size(), blob_names.size() + splitncnn_blob_count);
-
- int internal_split = 0;
-
- for (int i=0; i<node_count; i++)
- {
- const MXNetNode& n = nodes[i];
-
- if (n.op == "noop_reducedncnn")
- {
- continue;
- }
-
- if (n.op == "null")
- {
- if (n.is_weight())
- {
- continue;
- }
-
- fprintf(pp, "%-16s", "Input");
- }
- else if (n.op == "_contrib_MultiBoxDetection")
- {
- fprintf(pp, "%-16s", "DetectionOutput");
- }
- else if (n.op == "_contrib_MultiBoxPrior")
- {
- fprintf(pp, "%-16s", "PriorBox");
- }
- else if (n.op == "_div_scalar")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "_maximum_scalar")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "_minimum_scalar")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "_minus_scalar")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "_mul_scalar")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "_plus_scalar")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "_power_scalar")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "_rdiv_scalar")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "_rminus_scalar")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "abs")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (n.op == "Activation")
- {
- std::string type = n.attr("act_type");
- if (type == "relu")
- {
- fprintf(pp, "%-16s", "ReLU");
- }
- else if (type == "sigmoid")
- {
- fprintf(pp, "%-16s", "Sigmoid");
- }
- else if (type == "tanh")
- {
- fprintf(pp, "%-16s", "TanH");
- }
- }
- else if (n.op == "arccos")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (n.op == "arcsin")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (n.op == "arctan")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (n.op == "BatchNorm")
- {
- fprintf(pp, "%-16s", "BatchNorm");
- }
- else if (n.op == "broadcast_add")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "broadcast_div")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "broadcast_mul")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "broadcast_sub")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "ceil")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (n.op == "clip")
- {
- fprintf(pp, "%-16s", "Clip");
- }
- else if (n.op == "Concat")
- {
- fprintf(pp, "%-16s", "Concat");
- }
- else if (n.op == "Convolution")
- {
- int num_group = n.attr("num_group");
- if (num_group > 1) {
- fprintf(pp, "%-16s", "ConvolutionDepthWise");
- } else {
- fprintf(pp, "%-16s", "Convolution");
- }
- }
- else if (n.op == "cos")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (n.op == "Crop")
- {
- fprintf(pp, "%-16s", "Crop");
- }
- else if (n.op == "Deconvolution")
- {
- int num_group = n.attr("num_group");
- if (num_group > 1) {
- fprintf(pp, "%-16s", "DeconvolutionDepthWise");
- } else {
- fprintf(pp, "%-16s", "Deconvolution");
- }
- }
- else if (n.op == "Dropout")
- {
- fprintf(pp, "%-16s", "Dropout");
- }
- else if (n.op == "elemwise_add" || n.op == "_add" || n.op == "_plus" || n.op == "_Plus")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "elemwise_div" || n.op == "_div" || n.op == "_Div")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "elemwise_mul" || n.op == "_mul" || n.op == "_Mul")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "elemwise_sub" || n.op == "_sub" || n.op == "_minus" || n.op == "_Minus")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "Embedding")
- {
- fprintf(pp, "%-16s", "Embed");
- }
- else if (n.op == "exp")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (n.op == "expand_dims")
- {
- fprintf(pp, "%-16s", "ExpandDims");
- }
- else if (n.op == "Flatten")
- {
- fprintf(pp, "%-16s", "Flatten");
- }
- else if (n.op == "floor")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (n.op == "FullyConnected")
- {
- fprintf(pp, "%-16s", "InnerProduct");
- }
- else if (n.op == "InstanceNorm")
- {
- fprintf(pp, "%-16s", "InstanceNorm");
- }
- else if (n.op == "L2Normalization")
- {
- fprintf(pp, "%-16s", "Normalize");
- }
- else if (n.op == "LeakyReLU")
- {
- std::string type = n.attr("act_type");
- if (type == "elu")
- {
- fprintf(pp, "%-16s", "ELU");
- }
- else if (type == "leaky" || type.empty())
- {
- fprintf(pp, "%-16s", "ReLU");
- }
- else if (type == "prelu")
- {
- fprintf(pp, "%-16s", "PReLU");
- }
- }
- else if (n.op == "log")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (n.op == "LogisticRegressionOutput")
- {
- fprintf(pp, "%-16s", "Sigmoid");
- }
- else if (n.op == "max")
- {
- fprintf(pp, "%-16s", "Reduction");
- }
- else if (n.op == "maximum")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "mean")
- {
- fprintf(pp, "%-16s", "Reduction");
- }
- else if (n.op == "min")
- {
- fprintf(pp, "%-16s", "Reduction");
- }
- else if (n.op == "minimum")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (n.op == "negative")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (n.op == "Pad")
- {
- fprintf(pp, "%-16s", "Padding");
- }
- else if (n.op == "Pooling")
- {
- fprintf(pp, "%-16s", "Pooling");
- }
- else if (n.op == "prod")
- {
- fprintf(pp, "%-16s", "Reduction");
- }
- else if (n.op == "reciprocal")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (n.op == "relu")
- {
- fprintf(pp, "%-16s", "ReLU");
- }
- else if (n.op == "Reshape")
- {
- fprintf(pp, "%-16s", "Reshape");
- }
- else if (n.op == "ShuffleChannel")
- {
- fprintf(pp, "%-16s", "ShuffleChannel");
- }
- else if (n.op == "sigmoid")
- {
- fprintf(pp, "%-16s", "Sigmoid");
- }
- else if (n.op == "sin")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (n.op == "slice")
- {
- fprintf(pp, "%-16s", "Crop");
- }
- else if (n.op == "SliceChannel")
- {
- fprintf(pp, "%-16s", "Slice");
- }
- else if (n.op == "SoftmaxActivation")
- {
- fprintf(pp, "%-16s", "Softmax");
- }
- else if (n.op == "SoftmaxOutput")
- {
- fprintf(pp, "%-16s", "Softmax");
- }
- else if (n.op == "softmax")
- {
- fprintf(pp, "%-16s", "Softmax");
- }
- else if (n.op == "sqrt")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (n.op == "square")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (n.op == "sum")
- {
- fprintf(pp, "%-16s", "Reduction");
- }
- else if (n.op == "tan")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (n.op == "tanh")
- {
- fprintf(pp, "%-16s", "TanH");
- }
- else if (n.op == "Transpose" || n.op == "transpose")
- {
- fprintf(pp, "%-16s", "Permute");
- }
- else if (n.op == "UpSampling")
- {
- std::string sample_type = n.attr("sample_type");
- if (sample_type == "nearest")
- {
- fprintf(pp, "%-16s", "Interp");
- }
- else if (sample_type == "bilinear")
- {
- fprintf(pp, "%-16s", "DeconvolutionDepthWise");
- }
- }
- else
- {
- fprintf(stderr, "%s not supported yet!\n", n.op.c_str());
- fprintf(pp, "%-16s", n.op.c_str());
- }
-
- int input_size = n.inputs.size();
- for (int j=0; j<(int)n.inputs.size(); j++)
- {
- int input_index = n.inputs[j];
- if (nodes[input_index].is_weight())
- {
- input_size--;
- }
- }
-
- if (n.op == "SoftmaxOutput" || n.op == "LogisticRegressionOutput")
- {
- // drop label
- input_size--;
- }
-
- fprintf(pp, " %-32s %d %d", n.name.c_str(), input_size, n.output_size);
-
- for (int j=0; j<(int)n.inputs.size(); j++)
- {
- int input_index = n.inputs[j];
- int subinput_index = n.subinputs[j];
- if (nodes[input_index].is_weight())
- {
- continue;
- }
-
- if (n.op == "SoftmaxOutput" || n.op == "LogisticRegressionOutput")
- {
- // drop label
- if (j == 1)
- continue;
- }
-
- std::string input_name = nodes[input_index].name;
-
- if (subinput_index != 0)
- {
- char subinputsuffix[256];
- sprintf(subinputsuffix, "_subncnn_%d", subinput_index);
- input_name = input_name + subinputsuffix;
- }
-
- int input_uid = input_index | (subinput_index << 16);
- if (node_reference.find(input_uid) != node_reference.end())
- {
- int refidx = node_reference[input_uid] - 1;
- node_reference[input_uid] = refidx;
-
- char splitsuffix[256];
- sprintf(splitsuffix, "_splitncnn_%d", refidx);
- input_name = input_name + splitsuffix;
- }
-
- fprintf(pp, " %s", input_name.c_str());
- }
-
- fprintf(pp, " %s", n.name.c_str());
- for (int j=1; j<n.output_size; j++)
- {
- fprintf(pp, " %s_subncnn_%d", n.name.c_str(), j);
- }
-
- if (n.op == "null")
- {
- // dummy input shape
- // fprintf(pp, " 0 0 0");
- }
- else if (n.op == "_contrib_MultiBoxDetection")
- {
- float threshold = n.has_attr("threshold") ? n.attr("threshold") : 0.01f;
- float nms_threshold = n.has_attr("nms_threshold") ? n.attr("nms_threshold") : 0.5f;
- int nms_topk = n.has_attr("nms_topk") ? n.attr("nms_topk") : 300;
-
- fprintf(pp, " 0=-233");
- fprintf(pp, " 1=%f", nms_threshold);
- fprintf(pp, " 2=%d", nms_topk);
-
- int keep_top_k = 100;
- fprintf(pp, " 3=%d", keep_top_k);
- fprintf(pp, " 4=%f", threshold);
-
- std::vector<float> variances = n.attr("variances");
- if (variances.empty())
- {
- fprintf(pp, " 5=0.1");
- fprintf(pp, " 6=0.1");
- fprintf(pp, " 7=0.2");
- fprintf(pp, " 8=0.2");
- }
- else
- {
- fprintf(pp, " 5=%f", variances[0]);
- fprintf(pp, " 6=%f", variances[1]);
- fprintf(pp, " 7=%f", variances[2]);
- fprintf(pp, " 8=%f", variances[3]);
- }
- }
- else if (n.op == "_contrib_MultiBoxPrior")
- {
- // mxnet-ssd encode size as scale factor, fill min_size
- std::vector<float> sizes = n.attr("sizes");
- fprintf(pp, " -23300=%d", (int)sizes.size());
- for (int j=0; j<(int)sizes.size(); j++)
- {
- fprintf(pp, ",%f", sizes[j]);
- }
-
- std::vector<float> aspect_ratios = n.attr("ratios");
- fprintf(pp, " -23302=%d", (int)aspect_ratios.size());
- for (int j=0; j<(int)aspect_ratios.size(); j++)
- {
- fprintf(pp, ",%f", aspect_ratios[j]);
- }
-
- int flip = 0;
- fprintf(pp, " 7=%d", flip);
-
- int clip = n.attr("clip");
- fprintf(pp, " 8=%d", clip);
-
- // auto image size
- fprintf(pp, " 9=-233");
- fprintf(pp, " 10=-233");
-
- std::vector<float> steps = n.attr("steps");
- if (steps.empty() || (steps[0] == -1.f && steps[1] == -1.f))
- {
- // auto step
- fprintf(pp, " 11=-233.0");
- fprintf(pp, " 12=-233.0");
- }
- else
- {
- fprintf(pp, " 11=%f", steps[1]);
- fprintf(pp, " 12=%f", steps[0]);
- }
-
- std::vector<float> offsets = n.attr("offsets");
- if (offsets.empty() || (offsets[0] == 0.5f && offsets[1] == 0.5f))
- {
- fprintf(pp, " 13=0.5");
- }
- else
- {
- fprintf(stderr, "Unsupported offsets param! %f %f\n", offsets[0], offsets[1]);
- }
- }
- else if (n.op == "_div_scalar")
- {
- int op_type = 3;
- int with_scalar = 1;
- float scalar = n.attr("scalar");
- fprintf(pp, " 0=%d", op_type);
- fprintf(pp, " 1=%d", with_scalar);
- fprintf(pp, " 2=%f", scalar);
- }
- else if (n.op == "_maximum_scalar")
- {
- int op_type = 4;
- int with_scalar = 1;
- float scalar = n.attr("scalar");
- fprintf(pp, " 0=%d", op_type);
- fprintf(pp, " 1=%d", with_scalar);
- fprintf(pp, " 2=%f", scalar);
- }
- else if (n.op == "_minimum_scalar")
- {
- int op_type = 5;
- int with_scalar = 1;
- float scalar = n.attr("scalar");
- fprintf(pp, " 0=%d", op_type);
- fprintf(pp, " 1=%d", with_scalar);
- fprintf(pp, " 2=%f", scalar);
- }
- else if (n.op == "_minus_scalar")
- {
- int op_type = 1;
- int with_scalar = 1;
- float scalar = n.attr("scalar");
- fprintf(pp, " 0=%d", op_type);
- fprintf(pp, " 1=%d", with_scalar);
- fprintf(pp, " 2=%f", scalar);
- }
- else if (n.op == "_mul_scalar")
- {
- int op_type = 2;
- int with_scalar = 1;
- float scalar = n.attr("scalar");
- fprintf(pp, " 0=%d", op_type);
- fprintf(pp, " 1=%d", with_scalar);
- fprintf(pp, " 2=%f", scalar);
- }
- else if (n.op == "_plus_scalar")
- {
- int op_type = 0;
- int with_scalar = 1;
- float scalar = n.attr("scalar");
- fprintf(pp, " 0=%d", op_type);
- fprintf(pp, " 1=%d", with_scalar);
- fprintf(pp, " 2=%f", scalar);
- }
- else if (n.op == "_power_scalar")
- {
- int op_type = 6;
- int with_scalar = 1;
- float scalar = n.attr("scalar");
- fprintf(pp, " 0=%d", op_type);
- fprintf(pp, " 1=%d", with_scalar);
- fprintf(pp, " 2=%f", scalar);
- }
- else if (n.op == "_rdiv_scalar")
- {
- int op_type = 8;
- int with_scalar = 1;
- float scalar = n.attr("scalar");
- fprintf(pp, " 0=%d", op_type);
- fprintf(pp, " 1=%d", with_scalar);
- fprintf(pp, " 2=%f", scalar);
- }
- else if (n.op == "_rminus_scalar")
- {
- int op_type = 7;
- int with_scalar = 1;
- float scalar = n.attr("scalar");
- fprintf(pp, " 0=%d", op_type);
- fprintf(pp, " 1=%d", with_scalar);
- fprintf(pp, " 2=%f", scalar);
- }
- else if (n.op == "abs")
- {
- int op_type = 0;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "Activation")
- {
- std::string type = n.attr("act_type");
- if (type == "relu")
- {
- // fprintf(pp, " 0=%f", 0.f);
- }
- }
- else if (n.op == "arccos")
- {
- int op_type = 13;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "arcsin")
- {
- int op_type = 12;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "arctan")
- {
- int op_type = 14;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "BatchNorm")
- {
- float eps = 1e-3;
- if (n.has_attr("eps")) {
- eps = n.attr("eps");
- }
-
- std::vector<float> slope_data = n.weight(0);
- std::vector<float> bias_data = n.weight(1);
-
- int channels = slope_data.size();
-
- std::vector<float> mean_data = n.weight(2, channels);
- std::vector<float> var_data = n.weight(3, channels);
-
- for (int j=0; j<(int)var_data.size(); j++)
- {
- var_data[j] += eps;
- }
-
- fprintf(pp, " 0=%d", channels);
-
- int fix_gamma = n.has_attr("fix_gamma") ? n.attr("fix_gamma") : 0;
- if (fix_gamma)
- {
- // slope data are all 0 here, force set 1
- for (int j=0; j<channels; j++)
- {
- slope_data[j] = 1.f;
- }
- }
-
- fwrite(slope_data.data(), sizeof(float), slope_data.size(), bp);
- fwrite(mean_data.data(), sizeof(float), mean_data.size(), bp);
- fwrite(var_data.data(), sizeof(float), var_data.size(), bp);
- fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp);
- }
- else if (n.op == "broadcast_add")
- {
- int op_type = 0;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "broadcast_div")
- {
- int op_type = 3;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "broadcast_mul")
- {
- int op_type = 2;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "broadcast_sub")
- {
- int op_type = 1;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "ceil")
- {
- int op_type = 3;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "clip")
- {
- float min = n.attr("a_min");
- float max = n.attr("a_max");
- fprintf(pp, " 0=%f", min);
- fprintf(pp, " 1=%f", max);
- }
- else if (n.op == "Concat")
- {
- int dim = n.has_attr("dim") ? n.attr("dim") : 1;
- fprintf(pp, " 0=%d", dim-1);
- }
- else if (n.op == "Convolution")
- {
- int num_filter = n.attr("num_filter");
- std::vector<int> kernel = n.attr("kernel");
- std::vector<int> dilate = n.attr("dilate");
- std::vector<int> stride = n.attr("stride");
- std::vector<int> pad = n.attr("pad");
- int no_bias = n.attr("no_bias");
- int num_group = n.attr("num_group");
-
- std::vector<float> weight_data = n.weight(0);
- std::vector<float> bias_data = n.weight(1);
-
- fprintf(pp, " 0=%d", num_filter);
- if (kernel.size() == 1) {
- fprintf(pp, " 1=%d", kernel[0]);
- } else if (kernel.size() == 2) {
- fprintf(pp, " 1=%d", kernel[1]);
- fprintf(pp, " 11=%d", kernel[0]);
- }
-
- if (dilate.size() == 1) {
- fprintf(pp, " 2=%d", dilate[0]);
- } else if (dilate.size() == 2) {
- fprintf(pp, " 2=%d", dilate[1]);
- fprintf(pp, " 12=%d", dilate[0]);
- }
-
- if (stride.size() == 1) {
- fprintf(pp, " 3=%d", stride[0]);
- } else if (stride.size() == 2) {
- fprintf(pp, " 3=%d", stride[1]);
- fprintf(pp, " 13=%d", stride[0]);
- }
-
- if (pad.size() == 1) {
- fprintf(pp, " 4=%d", pad[0]);
- } else if (pad.size() == 2) {
- fprintf(pp, " 4=%d", pad[1]);
- fprintf(pp, " 14=%d", pad[0]);
- }
-
- fprintf(pp, " 5=%d", no_bias == 1 ? 0 : 1);
- fprintf(pp, " 6=%d", (int)weight_data.size());
- if (num_group > 1) {
- fprintf(pp, " 7=%d", num_group);
- }
-
- int quantize_tag = 0;
- fwrite(&quantize_tag, sizeof(int), 1, bp);
- fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp);
- fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp);
- }
- else if (n.op == "Deconvolution")
- {
- int num_filter = n.attr("num_filter");
- std::vector<int> kernel = n.attr("kernel");
- std::vector<int> dilate = n.attr("dilate");
- std::vector<int> stride = n.attr("stride");
- std::vector<int> pad = n.attr("pad");
- int no_bias = n.attr("no_bias");
- int num_group = n.attr("num_group");
-
- std::vector<float> weight_data = n.weight(0);
- std::vector<float> bias_data = n.weight(1);
-
- fprintf(pp, " 0=%d", num_filter);
- if (kernel.size() == 1) {
- fprintf(pp, " 1=%d", kernel[0]);
- } else if (kernel.size() == 2) {
- fprintf(pp, " 1=%d", kernel[1]);
- fprintf(pp, " 11=%d", kernel[0]);
- }
-
- if (dilate.size() == 1) {
- fprintf(pp, " 2=%d", dilate[0]);
- } else if (dilate.size() == 2) {
- fprintf(pp, " 2=%d", dilate[1]);
- fprintf(pp, " 12=%d", dilate[0]);
- }
-
- if (stride.size() == 1) {
- fprintf(pp, " 3=%d", stride[0]);
- } else if (stride.size() == 2) {
- fprintf(pp, " 3=%d", stride[1]);
- fprintf(pp, " 13=%d", stride[0]);
- }
-
- if (pad.size() == 1) {
- fprintf(pp, " 4=%d", pad[0]);
- } else if (pad.size() == 2) {
- fprintf(pp, " 4=%d", pad[1]);
- fprintf(pp, " 14=%d", pad[0]);
- }
-
- fprintf(pp, " 5=%d", no_bias == 1 ? 0 : 1);
- fprintf(pp, " 6=%d", (int)weight_data.size());
- if (num_group > 1) {
- fprintf(pp, " 7=%d", num_group);
- }
-
- int quantize_tag = 0;
- fwrite(&quantize_tag, sizeof(int), 1, bp);
-
- int maxk = 0;
- if (kernel.size() == 2)
- {
- maxk = kernel[1] * kernel[0];
- }
- else
- {
- maxk = kernel[0] * kernel[0];
- }
- for (int g=0; g<num_group; g++)
- {
- // reorder weight from inch-outch to outch-inch
- int num_filter_g = num_filter / num_group;
- int num_input = weight_data.size() / maxk / num_filter_g / num_group;
- const float* weight_data_ptr = weight_data.data() + g * maxk * num_filter_g * num_input;
- for (int k=0; k<num_filter_g; k++)
- {
- for (int j=0; j<num_input; j++)
- {
- fwrite(weight_data_ptr + (j*num_filter_g + k) * maxk, sizeof(float), maxk, bp);
- }
- }
- }
-
- fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp);
- }
- else if (n.op == "cos")
- {
- int op_type = 10;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "Crop")
- {
- int num_args = n.attr("num_args");
- std::vector<int> offset = n.attr("offset");
-
- int woffset = 0;
- int hoffset = 0;
- if (offset.size() == 2)
- {
- woffset = offset[1];
- hoffset = offset[0];
- }
-
- fprintf(pp, " 0=%d", woffset);
- fprintf(pp, " 1=%d", hoffset);
- fprintf(pp, " 2=0");
-
- if (num_args == 1)
- {
- std::vector<int> h_w = n.attr("h_w");
- fprintf(pp, " 3=%d", h_w[1]);
- fprintf(pp, " 4=%d", h_w[0]);
- fprintf(pp, " 5=0");
- }
- }
- else if (n.op == "Dropout")
- {
- // float p = n.attr("p");
- // fprintf(pp, " 0=%d", p);
- }
- else if (n.op == "elemwise_add" || n.op == "_add" || n.op == "_plus" || n.op == "_Plus")
- {
- int op_type = 0;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "elemwise_div" || n.op == "_div" || n.op == "_Div")
- {
- int op_type = 3;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "elemwise_mul" || n.op == "_mul" || n.op == "_Mul")
- {
- int op_type = 2;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "elemwise_sub" || n.op == "_sub" || n.op == "_minus" || n.op == "_Minus")
- {
- int op_type = 1;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "Embedding")
- {
- int input_dim = n.attr("input_dim");
- int output_dim = n.attr("output_dim");
-
- std::vector<float> weight_data = n.weight(0);
-
- fprintf(pp, " 0=%d", output_dim);
- fprintf(pp, " 1=%d", input_dim);
- fprintf(pp, " 3=%d", (int)weight_data.size());
-
- int quantize_tag = 0;
- fwrite(&quantize_tag, sizeof(int), 1, bp);
- fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp);
- }
- else if (n.op == "exp")
- {
- int op_type = 7;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "expand_dims")
- {
- int axis = n.attr("axis");
-
- int expand_w = 0;
- int expand_h = 0;
- int expand_c = 0;
-
- if (axis == 0)
- expand_c = 1;
- if (axis == 1)
- expand_h = 1;
- if (axis == 2)
- expand_w = 1;
-
- fprintf(pp, " 0=%d", expand_w);
- fprintf(pp, " 1=%d", expand_h);
- fprintf(pp, " 2=%d", expand_c);
- }
- else if (n.op == "Flatten")
- {
- }
- else if (n.op == "floor")
- {
- int op_type = 2;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "FullyConnected")
- {
- int num_hidden = n.attr("num_hidden");
- int no_bias = n.attr("no_bias");
- // int flatten = n.attr("flatten");
-
- // TODO flatten
-
- std::vector<float> weight_data = n.weight(0);
- std::vector<float> bias_data = n.weight(1);
-
- fprintf(pp, " 0=%d", num_hidden);
- fprintf(pp, " 1=%d", no_bias == 1 ? 0 : 1);
- fprintf(pp, " 2=%d", (int)weight_data.size());
-
- int quantize_tag = 0;
- fwrite(&quantize_tag, sizeof(int), 1, bp);
- fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp);
- fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp);
- }
- else if (n.op == "InstanceNorm")
- {
- float eps = n.has_attr("eps") ? n.attr("eps") : 0.001f;
-
- std::vector<float> gamma_data = n.weight(0);
- std::vector<float> beta_data = n.weight(1);
-
- fprintf(pp, " 0=%d", (int)gamma_data.size());
- fprintf(pp, " 1=%f", eps);
-
- fwrite(gamma_data.data(), sizeof(float), gamma_data.size(), bp);
- fwrite(beta_data.data(), sizeof(float), beta_data.size(), bp);
- }
- else if (n.op == "L2Normalization")
- {
- std::string mode = n.attr("mode");
- float eps = n.has_attr("eps") ? n.attr("eps") : 1e-10;
-
- int across_spatial = 0;
- int across_channel = 1;
- int channel_shared = 1;
- int scale_data_size = 1;
-
- if (mode == "instance")
- {
- across_spatial = 1;
- across_channel = 1;
- }
- else if (mode == "channel")
- {
- across_spatial = 0;
- across_channel = 1;
- }
- else if (mode == "spatial")
- {
- across_spatial = 1;
- across_channel = 0;
- }
-
- fprintf(pp, " 0=%d", across_spatial);
- fprintf(pp, " 4=%d", across_channel);
- fprintf(pp, " 1=%d", channel_shared);
- fprintf(pp, " 2=%f", eps);
- fprintf(pp, " 3=%d", scale_data_size);
-
- const float scale_data[1] = { 1.f };
- fwrite(scale_data, sizeof(float), 1, bp);
- }
- else if (n.op == "LeakyReLU")
- {
- std::string type = n.attr("act_type");
- if (type == "elu")
- {
- float slope = n.has_attr("slope") ? n.attr("slope") : 0.25f;
- fprintf(pp, " 0=%f", slope);
- }
- else if (type == "leaky" || type.empty())
- {
- float slope = n.has_attr("slope") ? n.attr("slope") : 0.25f;
- fprintf(pp, " 0=%f", slope);
- }
- else if (type == "prelu")
- {
- std::vector<float> weight_data = n.weight(0);
-
- fprintf(pp, " 0=%d", (int)weight_data.size());
-
- fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp);
- }
- }
- else if (n.op == "log")
- {
- int op_type = 8;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "LogisticRegressionOutput")
- {
- }
- else if (n.op == "max")
- {
- int operation = 4;
- fprintf(pp, " 0=%d", operation);
- }
- else if (n.op == "maximum")
- {
- int op_type = 4;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "mean")
- {
- int operation = 3;
- fprintf(pp, " 0=%d", operation);
- }
- else if (n.op == "min")
- {
- int operation = 5;
- fprintf(pp, " 0=%d", operation);
- }
- else if (n.op == "minimum")
- {
- int op_type = 5;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "negative")
- {
- int op_type = 1;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "Pad")
- {
- std::string mode = n.attr("mode");
- std::vector<int> pad_width = n.attr("pad_width");
- float constant_value = n.attr("constant_value");
-
- int type = 0;
- if (mode == "constant")
- {
- type = 0;
- }
- else if (mode == "edge")
- {
- type = 1;
- }
- else if (mode == "reflect")
- {
- // FIXME
- }
-
- if (pad_width.size() != 8)
- {
- fprintf(stderr, "Unsupported pad_width !\n");
- }
-
- int channel_before = pad_width[2];
- int channel_after = pad_width[3];
- if (channel_before != 0 || channel_after != 0)
- {
- // FIXME
- fprintf(stderr, "Unsupported pad_width on channel axis !\n");
- }
-
- int top = pad_width[4];
- int bottom = pad_width[5];
- int left = pad_width[6];
- int right = pad_width[7];
-
- fprintf(pp, " 0=%d", top);
- fprintf(pp, " 1=%d", bottom);
- fprintf(pp, " 2=%d", left);
- fprintf(pp, " 3=%d", right);
- fprintf(pp, " 4=%d", type);
- fprintf(pp, " 5=%f", constant_value);
- }
- else if (n.op == "Pooling")
- {
- std::string pool_type = n.attr("pool_type");
- std::vector<int> kernel = n.attr("kernel");
- std::vector<int> stride = n.attr("stride");
- std::vector<int> pad = n.attr("pad");
- std::string pooling_convention = n.attr("pooling_convention");
- int global_pool = n.attr("global_pool");
-
- int pool = 0;
- if (pool_type == "max")
- {
- pool = 0;
- }
- else if (pool_type == "avg")
- {
- pool = 1;
- }
-
- int pad_mode = 1;
- if (pooling_convention == "valid")
- {
- pad_mode = 1;
- }
- else if (pooling_convention == "full")
- {
- pad_mode = 0;
- }
-
- fprintf(pp, " 0=%d", pool);
- if (!kernel.empty())
- fprintf(pp, " 1=%d", kernel[0]);
- if (!stride.empty())
- fprintf(pp, " 2=%d", stride[0]);
- if (!pad.empty())
- fprintf(pp, " 3=%d", pad[0]);
- fprintf(pp, " 4=%d", global_pool);
- fprintf(pp, " 5=%d", pad_mode);
- }
- else if (n.op == "prod")
- {
- int operation = 6;
- fprintf(pp, " 0=%d", operation);
- }
- else if (n.op == "reciprocal")
- {
- int op_type = 15;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "relu")
- {
- }
- else if (n.op == "Reshape")
- {
- std::vector<int> shape = n.attr("shape");
-
- if (shape.size() == 1) {
- fprintf(pp, " 0=%d", shape[0]);// should never reach here
- } else if (shape.size() == 2) {
- fprintf(pp, " 0=%d", shape[1]);
- } else if (shape.size() == 3) {
- fprintf(pp, " 0=%d", shape[2]);
- fprintf(pp, " 1=%d", shape[1]);
- } else if (shape.size() == 4) {
- fprintf(pp, " 0=%d", shape[3]);
- fprintf(pp, " 1=%d", shape[2]);
- fprintf(pp, " 2=%d", shape[1]);
- } else if (shape.size() == 5) {
- fprintf(pp, " 0=%d", shape[4] * shape[3]);
- fprintf(pp, " 1=%d", shape[2]);
- fprintf(pp, " 2=%d", shape[1]);
- }
- }
- else if (n.op == "ShuffleChannel")
- {
- int group = n.attr("group");
- fprintf(pp, " 0=%d", group);
- }
- else if (n.op == "sigmoid")
- {
- }
- else if (n.op == "sin")
- {
- int op_type = 9;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "slice")
- {
- std::vector<int> begin = n.attr("begin");
- std::vector<int> end = n.attr("end");
- std::vector<int> step = n.attr("step");// TODO
-
- // assert step == 1
- for (int i=0; i<(int)step.size(); i++)
- {
- if (step[i] != 1)
- fprintf(stderr, "Unsupported slice step !\n");
- }
-
- int woffset = 0;
- int hoffset = 0;
- int coffset = 0;
- int outw = -233;
- int outh = -233;
- int outc = -233;
-
- if (begin.size() == 2)
- {
- woffset = begin[1];
- outw = end[1] == -1 ? -234 : end[1] - begin[1];
- }
- else if (begin.size() == 3)
- {
- woffset = begin[2];
- hoffset = begin[1];
- outw = end[2] == -1 ? -234 : end[2] - begin[2];
- outh = end[1] == -1 ? -234 : end[1] - begin[1];
- }
- else if (begin.size() == 4)
- {
- woffset = begin[3];
- hoffset = begin[2];
- coffset = begin[1];
- outw = end[3] == -1 ? -234 : end[3] - begin[3];
- outh = end[2] == -1 ? -234 : end[2] - begin[2];
- outc = end[1] == -1 ? -234 : end[1] - begin[1];
- }
-
- fprintf(pp, " 0=%d", woffset);
- fprintf(pp, " 1=%d", hoffset);
- fprintf(pp, " 2=%d", coffset);
- fprintf(pp, " 3=%d", outw);
- fprintf(pp, " 4=%d", outh);
- fprintf(pp, " 5=%d", outc);
- }
- else if (n.op == "SliceChannel")
- {
- int num_outputs = n.attr("num_outputs");
- int squeeze_axis = n.attr("squeeze_axis");// TODO
-
- fprintf(pp, " -23300=%d", num_outputs);
- for (int j=0; j<num_outputs; j++)
- {
- fprintf(pp, ",-233");
- }
- }
- else if (n.op == "SoftmaxActivation")
- {
- fprintf(pp, " 1=1");
- }
- else if (n.op == "SoftmaxOutput")
- {
- fprintf(pp, " 1=1");
- }
- else if (n.op == "softmax")
- {
- fprintf(pp, " 1=1");
- }
- else if (n.op == "sqrt")
- {
- int op_type = 5;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "square")
- {
- int op_type = 4;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "sum")
- {
- int operation = 0;
- fprintf(pp, " 0=%d", operation);
- }
- else if (n.op == "tan")
- {
- int op_type = 11;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (n.op == "tanh")
- {
- }
- else if (n.op == "Transpose" || n.op == "transpose")
- {
- std::vector<int> axes = n.attr("axes");
-
- if (axes.size() == 3) {
- if (axes[1] == 2 && axes[2] == 1)
- fprintf(pp, " 0=1");// h w c
- else
- fprintf(stderr, "Unsupported transpose type !\n");
- }
- else if (axes.size() == 4) {
- if (axes[1] == 1 && axes[2] == 2 && axes[3] == 3)
- fprintf(pp, " 0=0");// w h c
- else if (axes[1] == 1 && axes[2] == 3 && axes[3] == 2)
- fprintf(pp, " 0=1");// h w c
- else if (axes[1] == 2 && axes[2] == 1 && axes[3] == 3)
- fprintf(pp, " 0=2");// w c h
- else if (axes[1] == 2 && axes[2] == 3 && axes[3] == 1)
- fprintf(pp, " 0=3");// c w h
- else if (axes[1] == 3 && axes[2] == 1 && axes[3] == 2)
- fprintf(pp, " 0=4");// h c w
- else if (axes[1] == 3 && axes[2] == 2 && axes[3] == 1)
- fprintf(pp, " 0=5");// c h w
- } else if (axes.size() == 5) {
- if (axes[1] == 1 && axes[2] == 2 && axes[3] == 3 && axes[4] == 4)
- fprintf(pp, " 0=0");// wx h c
- else if (axes[1] == 1 && axes[2] == 3 && axes[3] == 4 && axes[4] == 2)
- fprintf(pp, " 0=1");// h wx c
- else if (axes[1] == 2 && axes[2] == 1 && axes[3] == 3 && axes[4] == 4)
- fprintf(pp, " 0=2");// wx c h
- else if (axes[1] == 2 && axes[2] == 3 && axes[3] == 4 && axes[4] == 1)
- fprintf(pp, " 0=3");// c wx h
- else if (axes[1] == 3 && axes[2] == 4 && axes[3] == 1 && axes[4] == 2)
- fprintf(pp, " 0=4");// h c wx
- else if (axes[1] == 3 && axes[2] == 4 && axes[3] == 2 && axes[4] == 1)
- fprintf(pp, " 0=5");// c h wx
- else
- fprintf(stderr, "Unsupported transpose type !\n");
- }
- else
- {
- fprintf(stderr, "Unsupported transpose type !\n");
- }
- }
- else if (n.op == "UpSampling")
- {
- int scale = n.attr("scale");
- std::string sample_type = n.attr("sample_type");
-
- if (sample_type == "nearest")
- {
- fprintf(pp, " 0=1");
- fprintf(pp, " 1=%f", (float)scale);
- fprintf(pp, " 2=%f", (float)scale);
- }
- else if (sample_type == "bilinear")
- {
- // DeconvolutionDepthWise
- int num_filter = n.attr("num_filter");
-
- std::vector<float> weight_data = n.weight(0);
-
- int kernel = scale * 2 - scale % 2;
- int stride = scale;
- int pad = (scale - 1) / 2;
-
- fprintf(pp, " 0=%d", num_filter);
- fprintf(pp, " 1=%d", kernel);
- fprintf(pp, " 2=1");
- fprintf(pp, " 3=%d", stride);
- fprintf(pp, " 4=%d", pad);
- fprintf(pp, " 5=0");
- fprintf(pp, " 6=%d", (int)weight_data.size());
- fprintf(pp, " 7=%d", num_filter);
-
- int quantize_tag = 0;
- fwrite(&quantize_tag, sizeof(int), 1, bp);
-
- fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp);
- }
- }
- else
- {
- // TODO op specific params
- std::map<std::string, std::string>::const_iterator it = n.attrs.begin();
- for (; it != n.attrs.end(); it++)
- {
- fprintf(stderr, "# %s=%s\n", it->first.c_str(), it->second.c_str());
- // fprintf(pp, " %s=%s", it->first.c_str(), it->second.c_str());
- }
- }
-
- fprintf(pp, "\n");
-
- for (int j=0; j<n.output_size; j++)
- {
- int input_uid = i | (j << 16);
- if (node_reference.find(input_uid) != node_reference.end())
- {
- int refcount = node_reference[input_uid];
- if (refcount > 1)
- {
- std::string output_name = n.name;
-
- char splitname[256];
- sprintf(splitname, "splitncnn_%d", internal_split);
- fprintf(pp, "%-16s %-32s %d %d", "Split", splitname, 1, refcount);
- if (j == 0)
- {
- fprintf(pp, " %s", output_name.c_str());
- }
- else
- {
- fprintf(pp, " %s_subncnn_%d", output_name.c_str(), j);
- }
-
- for (int k=0; k<refcount; k++)
- {
- if (j == 0)
- {
- fprintf(pp, " %s_splitncnn_%d", output_name.c_str(), k);
- }
- else
- {
- fprintf(pp, " %s_subncnn_%d_splitncnn_%d", output_name.c_str(), j, k);
- }
- }
- fprintf(pp, "\n");
-
- internal_split++;
- }
- }
- }
- }
-
- fclose(pp);
- fclose(bp);
-
- return 0;
- }
|