|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370 |
- // 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");
-
- // magic
- fprintf(pp, "7767517\n");
-
- 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, " 0=%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;
- int pad_mode = 1;
-
- 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_mode = 1;
- }
- else if (value_padding.s() == "SAME")
- {
- pad_mode = 2;
- }
- }
-
- fprintf(pp, " 0=%d", pooling_type);
- fprintf(pp, " 1=%d", kernel_size_w);
- fprintf(pp, " 11=%d", kernel_size_h);
- fprintf(pp, " 2=%d", stride_w);
- fprintf(pp, " 12=%d", stride_h);
- fprintf(pp, " 3=%d", pad);
- fprintf(pp, " 4=%d", global_pooling);
- fprintf(pp, " 5=%d", pad_mode);
- }
- 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, " 0=%d", w);
- fprintf(pp, " 1=%d", h);
- fprintf(pp, " 2=%d", 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, " 0=%d", num_output);
- fprintf(pp, " 1=%d", kernel_size_w);
- fprintf(pp, " 11=%d", kernel_size_h);
- fprintf(pp, " 2=%d", dilation_w);
- fprintf(pp, " 12=%d", dilation_h);
- fprintf(pp, " 3=%d", stride_w);
- fprintf(pp, " 13=%d", stride_h);
- fprintf(pp, " 4=%d", pad);
- fprintf(pp, " 5=%d", bias_term);
- fprintf(pp, " 6=%d", 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, " 0=%d", num_output);
- fprintf(pp, " 1=%d", kernel_size_w);
- fprintf(pp, " 11=%d", kernel_size_h);
- fprintf(pp, " 2=%d", dilation_w);
- fprintf(pp, " 12=%d", dilation_h);
- fprintf(pp, " 3=%d", stride_w);
- fprintf(pp, " 13=%d", stride_h);
- fprintf(pp, " 4=%d", pad);
- fprintf(pp, " 5=%d", bias_term);
- fprintf(pp, " 6=%d", weight_data_size);
- fprintf(pp, " 7=%d", group);
- }
- else if (node.op() == "Div" || node.op() == "RealDiv")
- {
- int op_type = 3;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (node.op() == "Exp")
- {
- int op_type = 7;
- fprintf(pp, " 0=%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, " 0=%d", expand_w);
- fprintf(pp, " 1=%d", expand_h);
- fprintf(pp, " 2=%d", expand_c);
- }
- else if (node.op() == "Floor")
- {
- int op_type = 2;
- fprintf(pp, " 0=%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, " 0=%d", norm_region);
- fprintf(pp, " 1=%d", local_size);
- fprintf(pp, " 2=%f", alpha);
- fprintf(pp, " 3=%f", 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, " 0=%d", num_output);
- fprintf(pp, " 1=%d", bias_term);
- fprintf(pp, " 2=%d", 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, " 0=%d", operation);
- fprintf(pp, " 1=%d", dim);
- fprintf(pp, " 2=%f", coeff);
- }
- else
- {
- int op_type = 4;
- fprintf(pp, " 0=%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;
- int pad_mode = 1;
-
- 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_mode = 1;
- }
- else if (value_padding.s() == "SAME")
- {
- pad_mode = 2;
- }
- }
-
- fprintf(pp, " 0=%d", pooling_type);
- fprintf(pp, " 1=%d", kernel_size_w);
- fprintf(pp, " 11=%d", kernel_size_h);
- fprintf(pp, " 2=%d", stride_w);
- fprintf(pp, " 12=%d", stride_h);
- fprintf(pp, " 3=%d", pad);
- fprintf(pp, " 4=%d", global_pooling);
- fprintf(pp, " 5=%d", pad_mode);
- }
- 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, " 0=%d", operation);
- fprintf(pp, " 1=%d", dim);
- fprintf(pp, " 2=%f", coeff);
- }
- else
- {
- int op_type = 5;
- fprintf(pp, " 0=%d", op_type);
- }
- }
- else if (node.op() == "Mul")
- {
- int op_type = 2;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (node.op() == "Neg")
- {
- int op_type = 1;
- fprintf(pp, " 0=%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, " 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", value);
- }
- else if (node.op() == "Placeholder")
- {
- // TODO pass through
- fprintf(pp, " 0=0 1=0 2=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, " 0=%d", operation);
- fprintf(pp, " 1=%d", dim);
- fprintf(pp, " 2=%f", coeff);
- }
- else if (node.op() == "Reciprocal")
- {
- int op_type = 15;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (node.op() == "Relu")
- {
- float slope = 0.f;
- fprintf(pp, " 0=%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, " 0=%d 1=%d 2=%d 3=0", data[2], data[1], data[3]);
- }
- if (size == 3)
- {
- fprintf(pp, " 0=%d 1=%d 2=-233 3=1", data[2], data[1]);
- }
- if (size == 2)
- {
- fprintf(pp, " 0=%d 1=-233 2=-233 3=1", data[1]);
- }
- }
- }
- else
- {
- // pass through
- fprintf(pp, " 0=0 1=0 2=0 3=0");
- }
- }
- else if (node.op() == "Rsqrt")
- {
- int op_type = 6;
- fprintf(pp, " 0=%d", op_type);
- }
- else if (node.op() == "Sigmoid")
- {
- }
- else if (node.op() == "Softmax")
- {
- }
- else if (node.op() == "Square")
- {
- int op_type = 4;
- fprintf(pp, " 0=%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, " 0=%d", squeeze_w);
- fprintf(pp, " 1=%d", squeeze_h);
- fprintf(pp, " 2=%d", squeeze_c);
- }
- else if (node.op() == "Sub")
- {
- int op_type = 1;
- fprintf(pp, " 0=%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, " 0=%d", operation);
- fprintf(pp, " 1=%d", dim);
- fprintf(pp, " 2=%f", 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;
- }
|