PaddlePaddle / X2Paddle

Deep learning model converter for PaddlePaddle. (『飞桨』深度学习模型转换工具)
http://www.paddlepaddle.org/
Apache License 2.0
729 stars 164 forks source link

torch转paddle时报错:'PaddleGraph' object has no attribute 'script' #890

Open gyr-kdgc opened 2 years ago

gyr-kdgc commented 2 years ago

问题描述

运行的代码如下(注释部分为torch.save(model.state_dict(), "model_torch16.pt")格式保存的模型,可以转换成功,但是现在想转换torch的script model,转换失败了):

import torch from x2paddle.convert import pytorch2paddle from model import resnet

trained_model = resnet.resnet50(num_classes=2, include_top=True)

trained_model.load_state_dict(torch.load("model_torch16.pt"))

trained_model=torch.jit.load("model_script_torch16.pt") tensor_shape=[1,3,224,224] input_tensor = torch.zeros_like(torch.empty(tuple(tensor_shape))) pytorch2paddle(module=trained_model, save_dir="./output22", jit_type="trace", input_examples=[input_tensor])

具体信息

pytorch==1.6.0 x2paddle==1.2.0

torch模型保存方式(script model): x = torch.randn(1, 3, 224, 224, requires_grad=True).cuda() script_module = torch.jit.trace(model, x, strict=False) torch.jit.save(script_module, "model_script_torch16.pt")

具体报错信息如下: /opt/miniconda3/lib/python3.7/site-packages/setuptools/depends.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses import imp ['1', '6', '0'] Now translating model from pytorch to paddle. /opt/miniconda3/lib/python3.7/site-packages/torch/jit/init.py:948: UserWarning: The input to trace is already a ScriptModule, tracing it is a no-op. Returning the object as is. warnings.warn('The input to trace is already a ScriptModule, tracing it is a no-op. Returning the object as is.') Model optimizing ... trace_fc_fuse_pass done! Model optimized. Traceback (most recent call last): File "pt2pd.py", line 14, in input_examples=[input_tensor]) File "/opt/miniconda3/lib/python3.7/site-packages/x2paddle/convert.py", line 223, in pytorch2paddle mapper.paddle_graph.gen_model(save_dir, jit_type=jit_type) File "/opt/miniconda3/lib/python3.7/site-packages/x2paddle/core/program.py", line 248, in gen_model self.gen_dygraph_model(save_dir, jit_type) File "/opt/miniconda3/lib/python3.7/site-packages/x2paddle/core/program.py", line 280, in gen_dygraph_model hierarchical_tree = HierarchicalTree(self) File "/opt/miniconda3/lib/python3.7/site-packages/x2paddle/optimizer/pytorch_code_optimizer/hierachical_tree.py", line 35, in init self.script = pd_graph.script AttributeError: 'PaddleGraph' object has no attribute 'script'

wjj19950828 commented 2 years ago

@gyr-kdgc 麻烦更新到最新版本X2Paddle试试(1.3.8)

另外,可以描述一下以下两个问题吗: 1、具体业务场景 2、为什么有转到Paddle部署的需求呢?

感谢~