提交 e6ac5a2a 编写于 作者: xiebaiyuan's avatar xiebaiyuan

add mdl loader and paddle loader

todo convert
上级 2cada5ed
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
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. */
syntax = "proto2";
option optimize_for = LITE_RUNTIME;
package paddle_mobile.framework.proto;
enum AttrType {
INT = 0;
FLOAT = 1;
STRING = 2;
INTS = 3;
FLOATS = 4;
STRINGS = 5;
BOOLEAN = 6;
BOOLEANS = 7;
BLOCK = 8;
LONG = 9;
}
// OpDesc describes an instance of a C++ framework::OperatorBase
// derived class type.
message OpDesc {
message Attr {
required string name = 1;
required AttrType type = 2;
optional int32 i = 3;
optional float f = 4;
optional string s = 5;
repeated int32 ints = 6;
repeated float floats = 7;
repeated string strings = 8;
optional bool b = 10;
repeated bool bools = 11;
optional int32 block_idx = 12;
optional int64 l = 13;
};
message Var {
required string parameter = 1;
repeated string arguments = 2;
};
required string type = 3;
repeated Var inputs = 1;
repeated Var outputs = 2;
repeated Attr attrs = 4;
optional bool is_target = 5 [ default = false ];
};
// OpProto describes a C++ framework::OperatorBase derived class.
message OpProto {
// VarProto describes the C++ type framework::Variable.
message Var {
required string name = 1;
required string comment = 2;
optional bool duplicable = 3 [ default = false ];
optional bool intermediate = 4 [ default = false ];
optional bool dispensable = 5 [ default = false ];
}
// AttrProto describes the C++ type Attribute.
message Attr {
required string name = 1;
required AttrType type = 2;
required string comment = 3;
// If that attribute is generated, it means the Paddle third
// language binding has responsibility to fill that
// attribute. End-User should not set that attribute.
optional bool generated = 4 [ default = false ];
}
required string type = 1;
repeated Var inputs = 2;
repeated Var outputs = 3;
repeated Attr attrs = 4;
required string comment = 5;
}
message VarType {
enum Type {
// Pod Types
BOOL = 0;
INT16 = 1;
INT32 = 2;
INT64 = 3;
FP16 = 4;
FP32 = 5;
FP64 = 6;
// Other types that may need additional descriptions
LOD_TENSOR = 7;
SELECTED_ROWS = 8;
FEED_MINIBATCH = 9;
FETCH_LIST = 10;
STEP_SCOPES = 11;
LOD_RANK_TABLE = 12;
LOD_TENSOR_ARRAY = 13;
PLACE_LIST = 14;
READER = 15;
CHANNEL = 16;
// Any runtime decided variable type is raw
// raw variables should manage their own allocations
// in operators like nccl_op
RAW = 17;
TUPLE = 18;
}
required Type type = 1;
message TensorDesc {
// Should only be PODType. Is enforced in C++
required Type data_type = 1;
repeated int64 dims = 2; // [UNK, 640, 480] is saved as [-1, 640, 480]
}
optional TensorDesc selected_rows = 2;
message LoDTensorDesc {
required TensorDesc tensor = 1;
optional int32 lod_level = 2 [ default = 0 ];
}
optional LoDTensorDesc lod_tensor = 3;
message LoDTensorArrayDesc {
required TensorDesc tensor = 1;
optional int32 lod_level = 2 [ default = 0 ];
}
optional LoDTensorArrayDesc tensor_array = 4;
message ReaderDesc { repeated LoDTensorDesc lod_tensor = 1; }
optional ReaderDesc reader = 5;
message ChannelDesc {
required Type data_type = 1;
required int64 capacity = 2;
}
optional ChannelDesc channel = 6;
message Tuple { repeated Type element_type = 1; }
optional Tuple tuple = 7;
}
message VarDesc {
required string name = 1;
required VarType type = 2;
optional bool persistable = 3 [ default = false ];
}
message BlockDesc {
required int32 idx = 1;
required int32 parent_idx = 2;
repeated VarDesc vars = 3;
repeated OpDesc ops = 4;
optional int32 forward_block_idx = 5 [ default = -1 ];
}
// Please refer to
// https://github.com/PaddlePaddle/Paddle/blob/develop/doc/design/program.md
// for more details.
// TODO(panyx0718): A model can have multiple programs. Need a
// way to distinguish them. Maybe ID or name?
message ProgramDesc { repeated BlockDesc blocks = 1; }
此差异已折叠。
import datetime
import json
import os
import google.protobuf as pbg
import framework_pb2 as framework_pb2
def loadmdl(json_path):
print('mdl json path : ' + json_path)
with open(json_path, 'r') as f:
json_dick = json.load(f)
# print(json_dick)
layers = (json_dick['layer'])
for layer in layers:
print(layer)
import json
import framework_pb2 as framework_pb2
import op_types as types
def load_mdl(mdl_json_path):
# print('mdl json path : ' + mdl_json_path)
with open(mdl_json_path, 'r') as f:
return json.load(f)
class Converter:
'convert mdlmodel to fluidmodel'
def __init__(self, mdl_json_path):
self.mdl_json_path = mdl_json_path
self.mdl_json = load_mdl(self.mdl_json_path)
self.program_desc = framework_pb2.ProgramDesc()
# print(json_dick)
# layers = (json_dick['layer'])
# for layer in layers:
# print(layer)
def convert(self):
print 'convert begin.....'
# add block_desc
block_desc = self.program_desc.blocks.add()
block_desc.idx = 0
block_desc.parent_idx = -1
self.package_ops(block_desc)
print 'blocks: '
print self.program_desc.blocks
def package_ops(self, block_desc):
# add ops with layer
if 'layer' in self.mdl_json:
layers_ = self.mdl_json['layer']
for layer in layers_:
desc_ops = block_desc.ops.add()
# print layer
# for i in layer:
# print i
if 'name' in layer:
l_name = layer['name']
if 'weight' in layer:
l_weights = layer['weight']
if 'param' in layer:
l_params = layer['param']
if 'output' in layer:
l_outputs = layer['output']
if 'input' in layer:
l_inputs = layer['input']
inputs_add = desc_ops.inputs.add()
for i in l_inputs:
# print i
inputs_add.parameter = ''
inputs_add.arguments.append(i)
if 'type' in layer:
l_type = layer['type']
# print l_type
# print mdl2fluid_op_layer_dict.get(l_type)
desc_ops.type = types.mdl2fluid_op_layer_dict.get(l_type)
mdl_path = "multiobjects/YOLO_Universal.json"
# print mdl_path
# # model
# mdl_model = load_mdl(mdl_path)
# for key in mdl_model:
# print key
#
# # layer
# layers = mdl_model['layer']
# print layers
#
# for layer in layers:
# print layer
# for i in layer:
# print i
# if 'name' in layer:
# l_name = layer['name']
#
# if 'weight' in layer:
# l_weights = layer['weight']
#
# if 'param' in layer:
# l_params = layer['param']
#
# if 'output' in layer:
# l_outputs = layer['output']
#
# if 'input' in layer:
# l_inputs = layer['input']
#
# if 'type' in layer:
# l_type = layer['type']
#
# print mdl_model['matrix']
#
# package()
converter = Converter(mdl_path)
converter.convert()
import os
import framework_pb2 as framework_pb2
def read_model(model_path):
print('read_model.')
path_8 = unicode(model_path, 'utf8')
try:
with open(path_8, "rb") as f_model:
print get_file_size(model_path)
desc = framework_pb2.ProgramDesc()
desc.ParseFromString(f_model.read())
print desc
# print desc.blocks
except IOError:
print ": File not found. Creating a new file."
def get_file_size(file_path):
file_path = unicode(file_path, 'utf8')
fsize = os.path.getsize(file_path)
fsize = fsize / float(1024 * 1024)
return round(fsize, 2)
path = "/Users/xiebaiyuan/PaddleProject/paddle-mobile/python/tools/mdl2fluid/yolo/__model__"
read_model(path)
mdl2fluid_op_layer_dict = {
'ConvolutionLayer': 'fusion_conv_add',
'DepthwiseConvolutionLayer': 'fusion_conv_add',
'ReluLayer': 'relu',
'PointwiseConvolutionLayer': 'fusion_conv_add'
}
fusion_conv_add_dict = {
'inputs': 'Input',
'outputs': 'Out'
}
relu_dict = {
'inputs': 'X',
'outputs': 'Out'
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册