From 0c3d8fe57c4f5167c7929cb0f13283c87c494fec Mon Sep 17 00:00:00 2001 From: SunAhong1993 Date: Tue, 2 Feb 2021 11:51:06 +0800 Subject: [PATCH] add caffe memory data --- x2paddle/decoder/caffe_decoder.py | 2 +- .../dygraph/caffe2paddle/caffe_op_mapper.py | 20 +++++++++++++ .../static/caffe2paddle/caffe_op_mapper.py | 29 ++++++++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/x2paddle/decoder/caffe_decoder.py b/x2paddle/decoder/caffe_decoder.py index 99f3853..dc125fa 100644 --- a/x2paddle/decoder/caffe_decoder.py +++ b/x2paddle/decoder/caffe_decoder.py @@ -258,7 +258,7 @@ class CaffeGraph(Graph): assert input_node_name in self.node_map, 'The {} isn\'t a valid node'.format( name) input_node = self.node_map[input_node_name] - if len(input_node.layer.top) > 1 and input_node.layer_type != "Input": + if len(input_node.layer.top) > 1 and input_node.layer_type not in ["Input", "MemoryData"]: need_idx = list(input_node.layer.top).index(node.layer.bottom[idx]) name = input_node_name + ':' + str(need_idx) else: diff --git a/x2paddle/op_mapper/dygraph/caffe2paddle/caffe_op_mapper.py b/x2paddle/op_mapper/dygraph/caffe2paddle/caffe_op_mapper.py index 888d809..25bfce5 100644 --- a/x2paddle/op_mapper/dygraph/caffe2paddle/caffe_op_mapper.py +++ b/x2paddle/op_mapper/dygraph/caffe2paddle/caffe_op_mapper.py @@ -198,6 +198,26 @@ class CaffeOpMapper(OpMapper): shape = list(node.layer.input_param.shape[0].dim)[1:] self.inputs_info["x{}".format(self.input_index)] = [[-1] + shape, "float32"] self.input_index += 1 + + def MemoryData(self, node): + params = node.layer.memory_data_param + transform_params = node.layer.transform_param + self.paddle_graph.add_layer( + "paddle.to_tensor", + inputs={}, + outputs=[node.layer_name], + data="x{}".format(self.input_index)) + shape = list() + shape.append(params.batch_size) + shape.append(params.channels) + if hasattr(transform_params, "crop_size"): + shape.append(transform_params.crop_size) + shape.append(transform_params.crop_size) + else: + shape.append(params.width) + shape.append(params.height) + self.inputs_info["x{}".format(self.input_index)] = [shape, "float32"] + self.input_index += 1 def Convolution(self, node): conv2d_name = name_generator("conv", self.nn_name2id) diff --git a/x2paddle/op_mapper/static/caffe2paddle/caffe_op_mapper.py b/x2paddle/op_mapper/static/caffe2paddle/caffe_op_mapper.py index 68ce95c..0a4119a 100644 --- a/x2paddle/op_mapper/static/caffe2paddle/caffe_op_mapper.py +++ b/x2paddle/op_mapper/static/caffe2paddle/caffe_op_mapper.py @@ -124,6 +124,8 @@ class CaffeOpMapper(OpMapper): def __init__(self, decoder): super(CaffeOpMapper, self).__init__() self.graph = decoder.caffe_graph + if not self.op_checker(): + raise Exception("Model is not supported yet.") self.params = dict() resolver = decoder.resolver self.used_custom_layers = {} @@ -190,7 +192,32 @@ class CaffeOpMapper(OpMapper): inputs={}, outputs=[node.name], **layer_attrs) - + + def MemoryData(self, node): + params = node.layer.memory_data_param + transform_params = node.layer.transform_param + + shape = list() + shape.append(params.batch_size) + shape.append(params.channels) + if hasattr(transform_params, "crop_size"): + shape.append(transform_params.crop_size) + shape.append(transform_params.crop_size) + else: + shape.append(params.width) + shape.append(params.height) + dtype = 'float32' + layer_attrs = { + "dtype": string(dtype), + "shape": shape, + "name": string(node.name) + } + self.paddle_graph.add_layer( + kernel="paddle.static.data", + inputs={}, + outputs=[node.name], + **layer_attrs) + def Convolution(self, node): data = node.data params = node.layer.convolution_param -- GitLab