Skip to content

[PyTorch2Paddle] nn.PReLU模型参数名不匹配 #654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
youqingxiaozhua opened this issue Aug 10, 2021 · 7 comments
Open

[PyTorch2Paddle] nn.PReLU模型参数名不匹配 #654

youqingxiaozhua opened this issue Aug 10, 2021 · 7 comments
Assignees
Labels

Comments

@youqingxiaozhua
Copy link

发现了宝藏包!落地Paddle比想象中丝滑的多,非常感谢大家的辛苦付出!

模型来源: PyTorch
模型说明: 模型中使用了nn.PReLU,API映射表中表示功能一致,实际上他们保存的weight名称不一致。torch 中为 “weight",paddle中为"_weight"

@wjj19950828
Copy link
Collaborator

感谢您的支持,您说的是预测模型还是训练代码转换,可以描述一下应用场景吗,感谢~

@wjj19950828 wjj19950828 self-assigned this Aug 10, 2021
@youqingxiaozhua
Copy link
Author

感谢您的支持,您说的是预测模型还是训练代码转换,可以描述一下应用场景吗,感谢~

已经训练好的torch的模型参数(.pth)需要转成paddle的格式(.pdparams)。
使用x2paddle --convert_torch_project --pretrain_model=model.pth, model里有PReLU层,他的参数在torch的文件(.pth)里的key是weight,而paddle的PReLU需要的key是_weight。

具体可见:
pytorch:https://pytorch.org/docs/stable/_modules/torch/nn/modules/activation.html#PReLU
paddle:https://github.com/PaddlePaddle/Paddle/blob/e04b66f2d272d68f77dcd94cb2956938475411d8/python/paddle/nn/layer/activation.py#L364

@wjj19950828
Copy link
Collaborator

wjj19950828 commented Aug 10, 2021

训练好的PyTorch模型转为Paddle参考文档:https://github.com/PaddlePaddle/X2Paddle/blob/develop/docs/inference_model_convertor/pytorch2paddle.md
您说的命令是训练代码转换即将PyTorch的组网以及损失函数等代码转为Paddle,包括预训练模型。

@youqingxiaozhua
Copy link
Author

不是……这是报bug,不是求助贴,我已经转成功了;这个bug是用上面命令时遇到的,bug的内容就是转换后的模型参数(.pdparams)并不能直接load进转换后的paddle模型中,原因是nn.PReLU的可训练参数(param)在torch中定义为self.weight,而在paddle中定义为self._weight,导致他们存储的时候的key不同(模型参数存储的key-value对)。

@youqingxiaozhua
Copy link
Author

如果直接在paddle里load转化后的model.pdparams,会报以下warning:

/lib/python3.6/site-packages/paddle/fluid/dygraph/layers.py:1301: UserWarning: Skip loading for extractor.body.20.res_layer.2._weight. extractor.body.20.res_layer.2._weight is not found in the provided dict.
  warnings.warn(("Skip loading for {}. ".format(key) + str(err)))

@wjj19950828
Copy link
Collaborator

我的意思是您的命令使用错了,训练好的PyTorch模型转为Paddle需要参考文档:https://github.com/PaddlePaddle/X2Paddle/blob/develop/docs/inference_model_convertor/pytorch2paddle.md,
而不是x2paddle --convert_torch_project --pretrain_model=model.pth,另外您的应用场景是什么,请具体描述一下吧~

@youqingxiaozhua
Copy link
Author

😂 我的场景就是需要使用 x2paddle --convert_torch_project把代码和参数文件(.pth)都从torch转成paddle,x2paddle命令行的参数--pretrain_model不就是用来转换预训练模型吗……

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants