|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308 |
- // 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 <limits.h>
-
- #include <iostream>
-
- #include <fstream>
- #include <set>
- #include <limits>
- #include <algorithm>
-
- #include <google/protobuf/io/coded_stream.h>
- #include <google/protobuf/io/zero_copy_stream_impl.h>
- #include <google/protobuf/text_format.h>
- #include <google/protobuf/message.h>
-
- #include "graph.pb.h"
-
- static bool read_proto_from_binary(const char* filepath, google::protobuf::Message* message)
- {
- std::ifstream fs(filepath, std::ifstream::in | std::ifstream::binary);
- if (!fs.is_open())
- {
- fprintf(stderr, "open failed %s\n", filepath);
- return false;
- }
-
- google::protobuf::io::IstreamInputStream input(&fs);
- google::protobuf::io::CodedInputStream codedstr(&input);
-
- codedstr.SetTotalBytesLimit(INT_MAX, INT_MAX / 2);
-
- bool success = message->ParseFromCodedStream(&codedstr);
-
- fs.close();
-
- return success;
- }
-
- static bool find_tensor_proto(const std::map<std::string, tensorflow::TensorProto>& weights,
- const tensorflow::NodeDef& node, tensorflow::TensorProto& tensor)
- {
- for (int j=0; j<node.input_size(); j++)
- {
- const std::string& input_name = node.input(j);
-
- const std::map<std::string, tensorflow::TensorProto>::const_iterator it = weights.find(input_name);
- if (it != weights.end())
- {
- tensor = it->second;
- return true;
- }
- }
-
- return false;
- }
-
- static bool get_tensor_proto(const std::map<std::string, tensorflow::TensorProto>& consts,
- const tensorflow::NodeDef& node, tensorflow::TensorProto& tensor)
- {
- const std::string& output_name = node.name();
-
- const std::map<std::string, tensorflow::TensorProto>::const_iterator it = consts.find(output_name);
- if (it != consts.end())
- {
- tensor = it->second;
- return true;
- }
-
- return false;
- }
-
- static bool find_attr_value(const tensorflow::NodeDef& node, const char* key, tensorflow::AttrValue& value)
- {
- const google::protobuf::Map<std::string, tensorflow::AttrValue>& attr = node.attr();
-
- const google::protobuf::Map<std::string, tensorflow::AttrValue>::const_iterator it = attr.find(key);
- if (it != attr.end())
- {
- value = it->second;
- return true;
- }
-
- return false;
- }
-
- static int parse_tensor_reduction_dim(const tensorflow::TensorProto& tensor)
- {
- int dim = 0;
-
- // dim == 0 // w h c -> X X X
- // dim == 1 // w h c -> X X c
- // dim == 2 // w h c -> X h c
- // dim == -1 // w h c -> w X X
- // dim == -2 // w h c -> w h X
-
- if (!tensor.tensor_content().empty() && tensor.dtype() == 3)// int32
- {
- const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
- int size = tensor.tensor_content().size() / sizeof(int);
-
- // n h w c
- // n h w
- // n w
- // TODO investigate two stage / three stage reduction
- if (size == 2)
- {
- if (data[0] == 1 && data[1] == 2)
- {
- dim = 1;
- }
- }
- }
- else
- {
- int axis = tensor.int_val(0);
- if (axis == 1)
- dim = 0;
- else if (axis == 3)
- dim = -2;
- }
-
- return dim;
- }
-
- int main(int argc, char** argv)
- {
- const char* tensorflowpb = argv[1];
- const char* ncnn_prototxt = argc >= 4 ? argv[2] : "ncnn.proto";
- const char* ncnn_modelbin = argc >= 4 ? argv[3] : "ncnn.bin";
-
- tensorflow::GraphDef graph;
-
- // load
- bool s1 = read_proto_from_binary(tensorflowpb, &graph);
- if (!s1)
- {
- fprintf(stderr, "read_proto_from_binary failed\n");
- return -1;
- }
-
- FILE* pp = fopen(ncnn_prototxt, "wb");
- FILE* bp = fopen(ncnn_modelbin, "wb");
-
- int node_count = graph.node_size();
-
- // fprintf(stderr, "node_count = %d\n\n", node_count);
-
- // node reference
- std::map<std::string, int> node_reference;
-
- // mapping for Const and Const-Identity
- std::map<std::string, tensorflow::TensorProto> weights;
-
- // Dropout like Identity
- std::set<std::string> dropouts;
-
- // Const before BinaryOp
- std::map<std::string, tensorflow::TensorProto> binaryop_consts;
-
- // global definition line
- // [layer count] [blob count]
- std::set<std::string> blob_names;
- for (int i=0; i<node_count; i++)
- {
- const tensorflow::NodeDef& node = graph.node(i);
-
- const std::string& output_name = node.name();
-
- if (node.op() == "Const")
- {
- tensorflow::AttrValue value;
- if (find_attr_value(node, "value", value))
- {
- const tensorflow::TensorProto& tensor = value.tensor();
- weights[output_name] = tensor;
- }
- continue;
- }
- else if (node.op() == "Identity")
- {
- const std::string& input_name = node.input(0);
- if (weights.find(input_name) != weights.end())
- {
- weights[output_name] = weights[input_name];
- continue;
- }
- else
- {
- dropouts.insert(output_name);
- }
- }
- else if (node.op() == "NoOp")
- {
- weights[output_name] = tensorflow::TensorProto();
- continue;
- }
- else
- {
- bool isBinaryOp = false;
- if (node.op() == "Add" || node.op() == "BiasAdd" || node.op() == "Div"
- || node.op() == "Mul" || node.op() == "RealDiv" || node.op() == "Sub")
- {
- isBinaryOp = true;
- }
- if (node.op() == "Max" || node.op() == "Maximum" || node.op() == "Min" || node.op() == "Minimum")
- {
- // check weights
- tensorflow::TensorProto tensor;
- if (!find_tensor_proto(weights, node, tensor))
- {
- isBinaryOp = true;
- }
- }
-
- if (isBinaryOp)
- {
- // check weights
- for (int j=0; j<node.input_size(); j++)
- {
- const std::string& input_name = node.input(j);
-
- std::map<std::string, tensorflow::TensorProto>::iterator it = weights.find(input_name);
- if (it != weights.end())
- {
- // binary op with const, insert MemoryData layer and const blob
- binaryop_consts[input_name] = it->second;
- weights.erase(it);
- }
- }
- }
- }
-
- // input
- for (int j=0; j<node.input_size(); j++)
- {
- const std::string& input_name = node.input(j);
- // fprintf(stderr, "input = %s\n", input_name.c_str());
-
- if (weights.find(input_name) != weights.end())
- {
- continue;
- }
-
- blob_names.insert(input_name);
-
- if (node_reference.find(input_name) == node_reference.end())
- {
- node_reference[input_name] = 1;
- }
- else
- {
- node_reference[input_name] = node_reference[input_name] + 1;
- }
- }
-
- // output
- // fprintf(stderr, "output = %s\n", output_name.c_str());
- blob_names.insert(output_name);
- }
-
- // remove node_reference entry with reference equals to one
- int splitncnn_blob_count = 0;
- std::map<std::string, 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(pp, "%lu %lu\n", node_count + node_reference.size() - weights.size(), blob_names.size() + splitncnn_blob_count);
-
- int internal_split = 0;
-
- for (int i=0; i<node_count; i++)
- {
- const tensorflow::NodeDef& node = graph.node(i);
-
- // layer definition line, repeated
- // [type] [name] [bottom blob count] [top blob count] [bottom blobs] [top blobs] [layer specific params]
- // fprintf(pp, "%-16s %-16s %d %d", layer.type().c_str(), layer.name().c_str(), node.input_size(), layer.top_size());
-
- if (node.op() == "Add" || node.op() == "BiasAdd")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (node.op() == "AvgPool")
- {
- fprintf(pp, "%-16s", "Pooling");
- }
- else if (node.op() == "Concat" || node.op() == "ConcatV2")
- {
- fprintf(pp, "%-16s", "Concat");
- }
- else if (node.op() == "Const")
- {
- // check before binaryop
- tensorflow::TensorProto tensor;
- if (get_tensor_proto(binaryop_consts, node, tensor))
- {
- fprintf(pp, "%-16s", "MemoryData");
- }
- else
- {
- continue;
- }
- }
- else if (node.op() == "Conv2D")
- {
- fprintf(pp, "%-16s", "Convolution");
- }
- else if (node.op() == "DepthwiseConv2dNative")
- {
- fprintf(pp, "%-16s", "ConvolutionDepthWise");
- }
- else if (node.op() == "Div" || node.op() == "RealDiv")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (node.op() == "Exp")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (node.op() == "ExpandDims")
- {
- fprintf(pp, "%-16s", "ExpandDims");
- }
- else if (node.op() == "Floor")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (node.op() == "Identity")
- {
- // check before binaryop
- tensorflow::TensorProto tensor;
- if (get_tensor_proto(binaryop_consts, node, tensor))
- {
- fprintf(pp, "%-16s", "MemoryData");
- }
- else if (dropouts.find(node.name()) != dropouts.end())
- {
- fprintf(pp, "%-16s", "Dropout");
- }
- else
- {
- continue;
- }
- }
- else if (node.op() == "LRN")
- {
- fprintf(pp, "%-16s", "LRN");
- }
- else if (node.op() == "MatMul")
- {
- fprintf(pp, "%-16s", "InnerProduct");
- }
- else if (node.op() == "Max" || node.op() == "Maximum")
- {
- // check weights
- tensorflow::TensorProto tensor;
- if (find_tensor_proto(weights, node, tensor))
- {
- fprintf(pp, "%-16s", "Reduction");
- }
- else
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- }
- else if (node.op() == "MaxPool")
- {
- fprintf(pp, "%-16s", "Pooling");
- }
- else if (node.op() == "Min" || node.op() == "Minimum")
- {
- // check weights
- tensorflow::TensorProto tensor;
- if (find_tensor_proto(weights, node, tensor))
- {
- fprintf(pp, "%-16s", "Reduction");
- }
- else
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- }
- else if (node.op() == "Mul")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (node.op() == "Neg")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (node.op() == "NoOp")
- {
- continue;
- }
- else if (node.op() == "Pad")
- {
- fprintf(pp, "%-16s", "Padding");
- }
- else if (node.op() == "Placeholder")
- {
- fprintf(pp, "%-16s", "Input");
- }
- else if (node.op() == "Prod")
- {
- fprintf(pp, "%-16s", "Reduction");
- }
- else if (node.op() == "Reciprocal")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (node.op() == "Relu")
- {
- fprintf(pp, "%-16s", "ReLU");
- }
- else if (node.op() == "Reshape")
- {
- fprintf(pp, "%-16s", "Reshape");
- }
- else if (node.op() == "Rsqrt")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (node.op() == "Sigmoid")
- {
- fprintf(pp, "%-16s", "Sigmoid");
- }
- else if (node.op() == "Softmax")
- {
- fprintf(pp, "%-16s", "Softmax");
- }
- else if (node.op() == "Square")
- {
- fprintf(pp, "%-16s", "UnaryOp");
- }
- else if (node.op() == "Squeeze")
- {
- fprintf(pp, "%-16s", "Squeeze");
- }
- else if (node.op() == "Sub")
- {
- fprintf(pp, "%-16s", "BinaryOp");
- }
- else if (node.op() == "Sum")
- {
- fprintf(pp, "%-16s", "Reduction");
- }
- else
- {
- fprintf(pp, "%-16s", node.op().c_str());
- fprintf(stderr, "%s not supported yet !\nn", node.op().c_str());
- }
-
- int input_size = node.input_size();
- for (int j=0; j<node.input_size(); j++)
- {
- const std::string& input_name = node.input(j);
- if (weights.find(input_name) != weights.end())
- {
- input_size--;
- }
- }
-
- fprintf(pp, " %-32s %d 1", node.name().c_str(), input_size);
-
- for (int j=0; j<node.input_size(); j++)
- {
- std::string input_name = node.input(j);
-
- if (weights.find(input_name) != weights.end())
- {
- continue;
- }
-
- if (node_reference.find(input_name) != node_reference.end())
- {
- int refidx = node_reference[input_name] - 1;
- node_reference[input_name] = refidx;
-
- char splitsuffix[256];
- sprintf(splitsuffix, "_splitncnn_%d", refidx);
- input_name = input_name + splitsuffix;
- }
-
- fprintf(pp, " %s", input_name.c_str());
- }
-
- fprintf(pp, " %s", node.name().c_str());
-
- if (node.op() == "Add" || node.op() == "BiasAdd")
- {
- int op_type = 0;
- fprintf(pp, " %d", op_type);
- }
- else if (node.op() == "AvgPool")
- {
- int pooling_type = 1;
-
- int kernel_size_h = 1;
- int kernel_size_w = 1;
- int stride_h = 1;
- int stride_w = 1;
- int pad = 0;
-
- int global_pooling = 0;
-
- tensorflow::AttrValue value_ksize;
- if (find_attr_value(node, "ksize", value_ksize))
- {
- // batch, height, width, channels
- kernel_size_h = value_ksize.list().i(1);
- kernel_size_w = value_ksize.list().i(2);
- }
-
- tensorflow::AttrValue value_strides;
- if (find_attr_value(node, "strides", value_strides))
- {
- // batch, height, width, channels
- stride_h = value_strides.list().i(1);
- stride_w = value_strides.list().i(2);
- }
-
- tensorflow::AttrValue value_padding;
- if (find_attr_value(node, "padding", value_padding))
- {
- if (value_padding.s() == "VALID")
- {
- pad = 0;
- }
- else if (value_padding.s() == "SAME")
- {
- pad = -233;
- }
- }
-
- fprintf(pp, " %d %d %d %d %d", pooling_type, kernel_size_w, stride_w, pad, global_pooling);
- }
- else if (node.op() == "Concat" || node.op() == "ConcatV2")
- {
- tensorflow::TensorProto tensor;
- if (find_tensor_proto(weights, node, tensor))
- {
- // TODO
- int axis = tensor.int_val(0);
- }
- }
- else if (node.op() == "Const" || node.op() == "Identity")
- {
- // check before binaryop
- tensorflow::TensorProto tensor;
- if (get_tensor_proto(binaryop_consts, node, tensor))
- {
- const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
-
- int w = 0;
- int h = 0;
- int c = 0;
-
- if (shape.dim_size() == 1)
- {
- w = shape.dim(0).size();
- }
- else if (shape.dim_size() == 2)
- {
- h = shape.dim(0).size();
- w = shape.dim(1).size();
- }
- else if (shape.dim_size() == 3)
- {
- c = shape.dim(2).size();
- h = shape.dim(0).size();
- w = shape.dim(1).size();
- }
-
- int weight_data_size = 0;
-
- if (!tensor.tensor_content().empty())
- {
- if (tensor.dtype() == 1)// float
- {
- const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
- weight_data_size = tensor.tensor_content().size() / sizeof(float);
-
- if (c == 0)
- fwrite(data, sizeof(float), weight_data_size, bp);
- else
- {
- float tmp;
- // h-w-c to c-h-w
- for (int p=0; p<c; p++)
- {
- for (int i=0; i<h; i++)
- {
- for (int j=0; j<w; j++)
- {
- tmp = data[i*w*c + j*c + p];
- fwrite(&tmp, sizeof(float), 1, bp);
- }
- }
- }
- }
- }
- else if (tensor.dtype() == 3)// int32
- {
- const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
- weight_data_size = tensor.tensor_content().size() / sizeof(int);
-
- float tmp;
- if (c == 0)
- {
- for (int i=0; i<weight_data_size; i++)
- {
- tmp = data[i];
- fwrite(&tmp, sizeof(float), 1, bp);
- }
- }
- else
- {
- // h-w-c to c-h-w
- for (int p=0; p<c; p++)
- {
- for (int i=0; i<h; i++)
- {
- for (int j=0; j<w; j++)
- {
- tmp = data[i*w*c + j*c + p];
- fwrite(&tmp, sizeof(float), 1, bp);
- }
- }
- }
- }
- }
- }
- else
- {
- if (tensor.dtype() == 1)// float
- {
- float val = tensor.float_val(0);
- fwrite(&val, sizeof(float), 1, bp);
- }
- else if (tensor.dtype() == 3)// int32
- {
- float val = tensor.int_val(0);
- fwrite(&val, sizeof(float), 1, bp);
- }
- }
-
- fprintf(pp, " %d %d %d", w, h, c);
- }
- }
- else if (node.op() == "Conv2D")
- {
- // weights
- tensorflow::TensorProto tensor;
- find_tensor_proto(weights, node, tensor);
-
- const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
-
- int kernel_size_h = shape.dim(0).size();
- int kernel_size_w = shape.dim(1).size();
- int num_input = shape.dim(2).size();
- int num_output = shape.dim(3).size();
-
- int stride_h = 1;
- int stride_w = 1;
- int dilation_h = 1;
- int dilation_w = 1;
- int pad = 0;
-
- tensorflow::AttrValue value_strides;
- if (find_attr_value(node, "strides", value_strides))
- {
- // batch, height, width, channels
- stride_h = value_strides.list().i(1);
- stride_w = value_strides.list().i(2);
- }
-
- tensorflow::AttrValue value_padding;
- if (find_attr_value(node, "padding", value_padding))
- {
- if (value_padding.s() == "VALID")
- {
- pad = 0;
- }
- else if (value_padding.s() == "SAME")
- {
- pad = -233;
- }
- }
-
- tensorflow::AttrValue value_rate;
- if (find_attr_value(node, "rate", value_rate))
- {
- // height, width
- dilation_h = value_rate.list().i(0);
- dilation_w = value_rate.list().i(1);
- }
-
- int bias_term = 0;
- int weight_data_size = 0;
-
- // reorder h-w-i-o to o-i-h-w
- if (!tensor.tensor_content().empty())
- {
- int quantize_tag = 0;
- fwrite(&quantize_tag, sizeof(int), 1, bp);
-
- if (tensor.dtype() == 1)// float
- {
- const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
- weight_data_size = tensor.tensor_content().size() / sizeof(float);
-
- float tmp;
- for (int p=0; p<num_output; p++)
- {
- for (int q=0; q<num_input; q++)
- {
- for (int i=0; i<kernel_size_h; i++)
- {
- for (int j=0; j<kernel_size_w; j++)
- {
- tmp = data[i*kernel_size_w*num_input*num_output + j*num_input*num_output + q*num_output + p];
- fwrite(&tmp, sizeof(float), 1, bp);
- }
- }
- }
- }
- }
- else if (tensor.dtype() == 3)// int32
- {
- const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
- weight_data_size = tensor.tensor_content().size() / sizeof(int);
-
- float tmp;
- for (int p=0; p<num_output; p++)
- {
- for (int q=0; q<num_input; q++)
- {
- for (int i=0; i<kernel_size_h; i++)
- {
- for (int j=0; j<kernel_size_w; j++)
- {
- tmp = data[i*kernel_size_w*num_input*num_output + j*num_input*num_output + q*num_output + p];
- fwrite(&tmp, sizeof(float), 1, bp);
- }
- }
- }
- }
- }
- }
-
- fprintf(pp, " %d %d %d %d %d %d %d", num_output, kernel_size_w, dilation_w, stride_w, pad, bias_term, weight_data_size);
- }
- else if (node.op() == "DepthwiseConv2dNative")
- {
- // weights
- tensorflow::TensorProto tensor;
- find_tensor_proto(weights, node, tensor);
-
- const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
-
- int kernel_size_h = shape.dim(0).size();
- int kernel_size_w = shape.dim(1).size();
- int num_input = shape.dim(2).size();
- int channel_multiplier = shape.dim(3).size();
-
- int num_output = num_input * channel_multiplier;
- int group = num_input;
-
- int stride_h = 1;
- int stride_w = 1;
- int dilation_h = 1;
- int dilation_w = 1;
- int pad = 0;
-
- tensorflow::AttrValue value_strides;
- if (find_attr_value(node, "strides", value_strides))
- {
- // batch, height, width, channels
- stride_h = value_strides.list().i(1);
- stride_w = value_strides.list().i(2);
- }
-
- tensorflow::AttrValue value_padding;
- if (find_attr_value(node, "padding", value_padding))
- {
- if (value_padding.s() == "VALID")
- {
- pad = 0;
- }
- else if (value_padding.s() == "SAME")
- {
- pad = -233;
- }
- }
-
- tensorflow::AttrValue value_rate;
- if (find_attr_value(node, "rate", value_rate))
- {
- // height, width
- dilation_h = value_rate.list().i(0);
- dilation_w = value_rate.list().i(1);
- }
-
- int bias_term = 0;
- int weight_data_size = 0;
-
- // reorder h-w-i-cm to i-cm-h-w
- if (!tensor.tensor_content().empty())
- {
- int quantize_tag = 0;
- fwrite(&quantize_tag, sizeof(int), 1, bp);
-
- if (tensor.dtype() == 1)// float
- {
- const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
- weight_data_size = tensor.tensor_content().size() / sizeof(float);
-
- float tmp;
- for (int p=0; p<num_input; p++)
- {
- for (int q=0; q<channel_multiplier; q++)
- {
- for (int i=0; i<kernel_size_h; i++)
- {
- for (int j=0; j<kernel_size_w; j++)
- {
- tmp = data[i*kernel_size_w*channel_multiplier*num_input + j*channel_multiplier*num_input + p*channel_multiplier + q];
- fwrite(&tmp, sizeof(float), 1, bp);
- }
- }
- }
- }
- }
- else if (tensor.dtype() == 3)// int32
- {
- const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
- weight_data_size = tensor.tensor_content().size() / sizeof(int);
-
- float tmp;
- for (int p=0; p<num_input; p++)
- {
- for (int q=0; q<channel_multiplier; q++)
- {
- for (int i=0; i<kernel_size_h; i++)
- {
- for (int j=0; j<kernel_size_w; j++)
- {
- tmp = data[i*kernel_size_w*channel_multiplier*num_input + j*channel_multiplier*num_input + p*channel_multiplier + q];
- fwrite(&tmp, sizeof(float), 1, bp);
- }
- }
- }
- }
- }
- }
-
- fprintf(pp, " %d %d %d %d %d %d %d %d", num_output, kernel_size_w, dilation_w, stride_w, pad, bias_term, weight_data_size, group);
- }
- else if (node.op() == "Div" || node.op() == "RealDiv")
- {
- int op_type = 3;
- fprintf(pp, " %d", op_type);
- }
- else if (node.op() == "Exp")
- {
- int op_type = 7;
- fprintf(pp, " %d", op_type);
- }
- else if (node.op() == "ExpandDims")
- {
- int expand_w = 0;
- int expand_h = 0;
- int expand_c = 0;
-
- tensorflow::AttrValue value_dim;
- if (find_attr_value(node, "Tdim", value_dim))
- {
- int dim = value_dim.i();
- if (dim == 0)
- expand_w = 1;
- if (dim == 1)
- expand_h = 1;
- if (dim == 2)
- expand_c = 1;
- }
-
- fprintf(pp, " %d %d %d", expand_w, expand_h, expand_c);
- }
- else if (node.op() == "Floor")
- {
- int op_type = 2;
- fprintf(pp, " %d", op_type);
- }
- else if (node.op() == "LRN")
- {
- int norm_region = 0;
- int local_size = 1;
- float alpha = 1.f;
- float beta = 0.5f;
-
- tensorflow::AttrValue value_depth_radius;
- if (find_attr_value(node, "depth_radius", value_depth_radius))
- {
- local_size = value_depth_radius.i() * 2 + 1;
- }
-
- tensorflow::AttrValue value_alpha;
- if (find_attr_value(node, "alpha", value_alpha))
- {
- alpha = value_alpha.f();
- }
-
- tensorflow::AttrValue value_beta;
- if (find_attr_value(node, "beta", value_beta))
- {
- beta = value_beta.f();
- }
-
- // TODO
- float bias = 1.f;
- tensorflow::AttrValue value_bias;
- if (find_attr_value(node, "bias", value_bias))
- {
- bias = value_bias.f();
- }
-
- fprintf(pp, " %d %d %f %f", norm_region, local_size, alpha, beta);
- }
- else if (node.op() == "MatMul")
- {
- // weights
- tensorflow::TensorProto tensor;
- find_tensor_proto(weights, node, tensor);
-
- const tensorflow::TensorShapeProto& shape = tensor.tensor_shape();
-
- int num_input = shape.dim(0).size();
- int num_output = shape.dim(1).size();
-
- int bias_term = 0;
- int weight_data_size = 0;
-
- // reorder i-o to o-i
- if (!tensor.tensor_content().empty())
- {
- int quantize_tag = 0;
- fwrite(&quantize_tag, sizeof(int), 1, bp);
-
- if (tensor.dtype() == 1)// float
- {
- const float* data = reinterpret_cast<const float*>(tensor.tensor_content().c_str());
- weight_data_size = tensor.tensor_content().size() / sizeof(float);
-
- float tmp;
- for (int p=0; p<num_output; p++)
- {
- for (int q=0; q<num_input; q++)
- {
- tmp = data[q*num_output + p];
- fwrite(&tmp, sizeof(float), 1, bp);
- }
- }
- }
- else if (tensor.dtype() == 3)// int32
- {
- const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
- weight_data_size = tensor.tensor_content().size() / sizeof(int);
-
- float tmp;
- for (int p=0; p<num_output; p++)
- {
- for (int q=0; q<num_input; q++)
- {
- tmp = data[q*num_output + p];
- fwrite(&tmp, sizeof(float), 1, bp);
- }
- }
- }
- }
-
- fprintf(pp, " %d %d %d", num_output, bias_term, weight_data_size);
- }
- else if (node.op() == "Max" || node.op() == "Maximum")
- {
- // check weights
- tensorflow::TensorProto tensor;
- if (find_tensor_proto(weights, node, tensor))
- {
- int operation = 4;
- int dim = 0;
- float coeff = 1.f;
-
- dim = parse_tensor_reduction_dim(tensor);
-
- fprintf(pp, " %d %d %f", operation, dim, coeff);
- }
- else
- {
- int op_type = 4;
- fprintf(pp, " %d", op_type);
- }
- }
- else if (node.op() == "MaxPool")
- {
- int pooling_type = 0;
-
- int kernel_size_h = 1;
- int kernel_size_w = 1;
- int stride_h = 1;
- int stride_w = 1;
- int pad = 0;
-
- int global_pooling = 0;
-
- tensorflow::AttrValue value_ksize;
- if (find_attr_value(node, "ksize", value_ksize))
- {
- // batch, height, width, channels
- kernel_size_h = value_ksize.list().i(1);
- kernel_size_w = value_ksize.list().i(2);
- }
-
- tensorflow::AttrValue value_strides;
- if (find_attr_value(node, "strides", value_strides))
- {
- // batch, height, width, channels
- stride_h = value_strides.list().i(1);
- stride_w = value_strides.list().i(2);
- }
-
- tensorflow::AttrValue value_padding;
- if (find_attr_value(node, "padding", value_padding))
- {
- if (value_padding.s() == "VALID")
- {
- pad = -2333;
- }
- else if (value_padding.s() == "SAME")
- {
- pad = -233;
- }
- }
-
- fprintf(pp, " %d %d %d %d %d", pooling_type, kernel_size_w, stride_w, pad, global_pooling);
- }
- else if (node.op() == "Min" || node.op() == "Minimum")
- {
- // check weights
- tensorflow::TensorProto tensor;
- if (find_tensor_proto(weights, node, tensor))
- {
- int operation = 5;
- int dim = 0;
- float coeff = 1.f;
-
- dim = parse_tensor_reduction_dim(tensor);
-
- fprintf(pp, " %d %d %f", operation, dim, coeff);
- }
- else
- {
- int op_type = 5;
- fprintf(pp, " %d", op_type);
- }
- }
- else if (node.op() == "Mul")
- {
- int op_type = 2;
- fprintf(pp, " %d", op_type);
- }
- else if (node.op() == "Neg")
- {
- int op_type = 1;
- fprintf(pp, " %d", op_type);
- }
- else if (node.op() == "NoOp")
- {
- }
- else if (node.op() == "Pad")
- {
- int top = 0;
- int bottom = 0;
- int left = 0;
- int right = 0;
- int type = 0;
- float value = 0.f;
-
- // check weights
- tensorflow::TensorProto tensor;
- if (find_tensor_proto(weights, node, tensor))
- {
- if (!tensor.tensor_content().empty() && tensor.dtype() == 3)// int32
- {
- const int *data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
- int size = tensor.tensor_content().size() / sizeof(int);
-
- if (size == 8)
- {
- // n h w c
- top = data[2];
- bottom = data[3];
- left = data[4];
- right = data[5];
- }
- }
- }
-
- tensorflow::AttrValue value_Tpaddings;
- if (find_attr_value(node, "Tpaddings", value_Tpaddings))
- {
- type = value_Tpaddings.i();
- }
-
- tensorflow::AttrValue value_T;
- if (find_attr_value(node, "T", value_T))
- {
- value = value_T.f();
- }
-
- fprintf(pp, " %d %d %d %d %d %f", top, bottom, left, right, type, value);
- }
- else if (node.op() == "Placeholder")
- {
- // TODO pass through
- fprintf(pp, " 0 0 0");
- }
- else if (node.op() == "Prod")
- {
- int operation = 6;
- int dim = 0;
- float coeff = 1.f;
-
- // check weights
- tensorflow::TensorProto tensor;
- if (find_tensor_proto(weights, node, tensor))
- {
- dim = parse_tensor_reduction_dim(tensor);
- }
-
- fprintf(pp, " %d %d %f", operation, dim, coeff);
- }
- else if (node.op() == "Reciprocal")
- {
- int op_type = 15;
- fprintf(pp, " %d", op_type);
- }
- else if (node.op() == "Relu")
- {
- float slope = 0.f;
- fprintf(pp, " %f", slope);
- }
- else if (node.op() == "Reshape")
- {
- tensorflow::TensorProto tensor;
- if (find_tensor_proto(weights, node, tensor))
- {
- if (!tensor.tensor_content().empty() && tensor.dtype() == 3)// int32
- {
- const int* data = reinterpret_cast<const int*>(tensor.tensor_content().c_str());
- int size = tensor.tensor_content().size() / sizeof(int);
-
- // n h w c
- // n h w
- // n w
- if (size == 4)
- {
- fprintf(pp, " %d %d %d 0", data[2], data[1], data[3]);
- }
- if (size == 3)
- {
- fprintf(pp, " %d %d -233 1", data[2], data[1]);
- }
- if (size == 2)
- {
- fprintf(pp, " %d -233 -233 1", data[1]);
- }
- }
- }
- else
- {
- // pass through
- fprintf(pp, " 0 0 0");
- }
- }
- else if (node.op() == "Rsqrt")
- {
- int op_type = 6;
- fprintf(pp, " %d", op_type);
- }
- else if (node.op() == "Sigmoid")
- {
- }
- else if (node.op() == "Softmax")
- {
- }
- else if (node.op() == "Square")
- {
- int op_type = 4;
- fprintf(pp, " %d", op_type);
- }
- else if (node.op() == "Squeeze")
- {
- int squeeze_w = 0;
- int squeeze_h = 0;
- int squeeze_c = 0;
-
- tensorflow::AttrValue value_squeeze_dims;
- if (find_attr_value(node, "squeeze_dims", value_squeeze_dims))
- {
- for (int i = 0; i<value_squeeze_dims.list().i_size(); i++)
- {
- int dim = value_squeeze_dims.list().i(i);
- if (dim == 0)
- squeeze_w = 1;
- if (dim == 1)
- squeeze_h = 1;
- if (dim == 2)
- squeeze_c = 1;
- }
- }
-
- fprintf(pp, " %d %d %d", squeeze_w, squeeze_h, squeeze_c);
- }
- else if (node.op() == "Sub")
- {
- int op_type = 1;
- fprintf(pp, " %d", op_type);
- }
- else if (node.op() == "Sum")
- {
- int operation = 0;
- int dim = 0;
- float coeff = 1.f;
-
- // check weights
- tensorflow::TensorProto tensor;
- if (find_tensor_proto(weights, node, tensor))
- {
- dim = parse_tensor_reduction_dim(tensor);
- }
-
- fprintf(pp, " %d %d %f", operation, dim, coeff);
- }
- else
- {
- const google::protobuf::Map<std::string, tensorflow::AttrValue>& attr = node.attr();
-
- google::protobuf::Map<std::string, tensorflow::AttrValue>::const_iterator it = attr.begin();
- for (; it != attr.end(); it++)
- {
- std::cerr << it->first << " #" << it->second.type() << std::endl;
- }
- }
-
- fprintf(pp, "\n");
-
- std::string output_name = node.name();
- if (node_reference.find(output_name) != node_reference.end())
- {
- int refcount = node_reference[output_name];
- if (refcount > 1)
- {
- char splitname[256];
- sprintf(splitname, "splitncnn_%d", internal_split);
- fprintf(pp, "%-16s %-32s %d %d", "Split", splitname, 1, refcount);
- fprintf(pp, " %s", output_name.c_str());
-
- for (int j=0; j<refcount; j++)
- {
- fprintf(pp, " %s_splitncnn_%d", output_name.c_str(), j);
- }
- fprintf(pp, "\n");
-
- internal_split++;
- }
- }
- }
-
- fclose(pp);
- fclose(bp);
-
- return 0;
- }
|