diff --git a/x2paddle/core/program.py b/x2paddle/core/program.py index 7b155bd8b634c19ff6a519640c764228bdfb969f..097f1f51ce172b816eb700817c5dfa91c7b8d96a 100644 --- a/x2paddle/core/program.py +++ b/x2paddle/core/program.py @@ -220,6 +220,7 @@ class PaddleGraph(object): self.layers.pop(layer_id) self.get_inputs() + if len(self.outputs) == 0: self.get_outputs() @@ -293,8 +294,13 @@ class PaddleGraph(object): self.inputs.extend(block.inputs) update(self.layers) - self.inputs = list(set(self.inputs)) - if self.inputs is not None: + new_inputs = list() + for input_name in self.inputs: + if input_name in new_inputs: + continue + new_inputs.append(input_name) + self.inputs = new_inputs + if self.source_type == "pytorch" and self.inputs is not None: self.inputs.sort() def get_outputs(self): @@ -313,6 +319,15 @@ class PaddleGraph(object): self.outputs.append(output_name) def gen_code(self, code_dir=None, indent=2): + # 去除to_tensor的layer + invalid_list = list() + for layer_id, layer in self.layers.items(): + if layer.kernel == "paddle.to_tensor": + if layer.attrs["data"] in self.inputs: + invalid_list.append(layer_id) + for layer_id in invalid_list: + self.layers.pop(layer_id) + def gen_codes(code_list, indent=0): indent_blank = " " * indent codes = [] diff --git a/x2paddle/op_mapper/caffe2paddle/__pycache__/__init__.cpython-37.pyc b/x2paddle/op_mapper/caffe2paddle/__pycache__/__init__.cpython-37.pyc deleted file mode 100644 index 9b70e2ea71a7fa5c6502d5ae2fd79be148ce6ac6..0000000000000000000000000000000000000000 Binary files a/x2paddle/op_mapper/caffe2paddle/__pycache__/__init__.cpython-37.pyc and /dev/null differ diff --git a/x2paddle/op_mapper/caffe2paddle/__pycache__/caffe_op_mapper.cpython-37.pyc b/x2paddle/op_mapper/caffe2paddle/__pycache__/caffe_op_mapper.cpython-37.pyc deleted file mode 100644 index af303fb725b43e314b8cf0f33f9a760b4973bd09..0000000000000000000000000000000000000000 Binary files a/x2paddle/op_mapper/caffe2paddle/__pycache__/caffe_op_mapper.cpython-37.pyc and /dev/null differ diff --git a/x2paddle/op_mapper/caffe2paddle/caffe_op_mapper.py b/x2paddle/op_mapper/caffe2paddle/caffe_op_mapper.py index 582f90ca9a1e209db10a473f4056c00db802634c..bf120b7d3c3cc81bf2d7ec34bc025d3d51944b4b 100644 --- a/x2paddle/op_mapper/caffe2paddle/caffe_op_mapper.py +++ b/x2paddle/op_mapper/caffe2paddle/caffe_op_mapper.py @@ -128,7 +128,6 @@ class CaffeOpMapper(): self.params = dict() self.paddle_graph = PaddleGraph(parent_layer=None, source_type="caffe") self.paddle_graph.outputs = self.graph.output_nodes - self.input_index = 0 self.inputs_info = {} self.nn_name2id = {} print("Total nodes: {}".format( @@ -193,11 +192,10 @@ class CaffeOpMapper(): "paddle.to_tensor", inputs={}, outputs=[node.layer_name], - data="x{}".format(self.input_index)) + data=node.name) shape = list(node.layer.input_param.shape[0].dim)[1:] - self.inputs_info["x{}".format(self.input_index)] = [[-1] + shape, + self.inputs_info[node.name] = [[-1] + shape, "float32"] - self.input_index += 1 def MemoryData(self, node): params = node.layer.memory_data_param @@ -206,7 +204,7 @@ class CaffeOpMapper(): "paddle.to_tensor", inputs={}, outputs=[node.layer_name], - data="x{}".format(self.input_index)) + data=node.layer_name) shape = list() shape.append(params.batch_size) shape.append(params.channels) @@ -216,8 +214,7 @@ class CaffeOpMapper(): else: shape.append(params.width) shape.append(params.height) - self.inputs_info["x{}".format(self.input_index)] = [shape, "float32"] - self.input_index += 1 + self.inputs_info[node.layer_name] = [shape, "float32"] def Convolution(self, node): conv2d_name = name_generator("conv", self.nn_name2id) diff --git a/x2paddle/op_mapper/onnx2paddle/opset9/opset.py b/x2paddle/op_mapper/onnx2paddle/opset9/opset.py index 28dab15bc2e63e78c540cb3f0b7b9e8c5ce913e2..59cd56e275ce4c72a0c182d83ef155f5e1311d58 100755 --- a/x2paddle/op_mapper/onnx2paddle/opset9/opset.py +++ b/x2paddle/op_mapper/onnx2paddle/opset9/opset.py @@ -180,7 +180,6 @@ class OpSet9(): super(OpSet9, self).__init__() self.graph = decoder.graph self.paddle_graph = paddle_graph - self.input_index = 0 self.inputs_info = dict() self.weights = dict() self.nn_name2id = dict() @@ -245,9 +244,8 @@ class OpSet9(): kernel="paddle.to_tensor", inputs={}, outputs=[node.name], - data="x{}".format(self.input_index)) - self.inputs_info["x{}".format(self.input_index)] = [shape, node.dtype] - self.input_index += 1 + data=node.name) + self.inputs_info[node.name] = [shape, node.dtype] @print_mapping_info def create_parameter(self, node, parameter=None): diff --git a/x2paddle/op_mapper/tf2paddle/tf_op_mapper.py b/x2paddle/op_mapper/tf2paddle/tf_op_mapper.py index 819c5e5457e93f9daf0807823a48cf7666077962..b049e96a06083f89610ba8679da537062f7abdb3 100644 --- a/x2paddle/op_mapper/tf2paddle/tf_op_mapper.py +++ b/x2paddle/op_mapper/tf2paddle/tf_op_mapper.py @@ -91,7 +91,6 @@ class TFOpMapper(): raise Exception("Model is not supported yet.") self.params = dict() self.nn_name2id = dict() - self.input_index = 0 self.inputs_info = dict() self.paddle_graph = PaddleGraph(parent_layer=None, source_type="tf") self.paddle_graph.outputs = self.graph.output_nodes @@ -213,9 +212,8 @@ class TFOpMapper(): kernel="paddle.to_tensor", inputs={}, outputs=[node.name], - data="x{}".format(self.input_index)) - self.inputs_info["x{}".format(self.input_index)] = [shape, node.dtype] - self.input_index += 1 + data=node.name) + self.inputs_info[node.name] = [shape, node.dtype] def Const(self, node): shape = node.out_shapes[0]