具体报错信息如下:
/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'
问题描述
运行的代码如下(注释部分为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'