1.系统环境
硬件环境(Ascend/GPU/CPU): GPU
MindSpore版本: mindspore=2.0
执行模式(PyNative/ Graph):不限
Python版本:3.7
操作系统平台:Linux
2. 问题描述
迁移网络任务-tacotron2时mindspore的权重初始化与torch的不一致
代码:
ts.migrator.compare_pth_and_ckpt(
weight_map_path="/home/hodia/python/Pytorch-fasterRCNN/pt_net_info/torch_net_map.json",
pt_file_path="/home/hodia/python/Pytorch-fasterRCNN/pt_net_info/torch_troubleshooter_create.pth",
ms_file_path="/home/hodia/python/MindSpore-fasterRCNN/ms_net_info/mindspore_troubleshooter_create.ckpt",
compare_value=True)
3. 解决方案
将pytorch的权重转化为mindspore,进行训练
ts.migrator.convert_weight_and_load(weight_map_path="/home/hodia/python/Pytorch-fasterRCNN/pt_net_info/torch_net_map.json",
pt_file_path="/home/hodia/python/Pytorch-fasterRCNN/pt_net_info/torch_troubleshooter_create.pth",
net=model)
主要表现在初始化中
Torch的初始化
for layer in self.children():
if isinstance(layer,nn.Conv2d):
torch.nn.init.normal_(layer.weight,std=0.01)
torch.nn.init.constant_(layer.bias,0)
MindSpore的初始化
for _, layer in self.cells_and_names():
if isinstance(layer,nn.Conv2d):
layer.weight.set_data(ms.common.initializer.initializer(ms.common.initializer.Normal(sigma=0.01,mean=0.0),layer.weight.shape,layer.weight.dtype))
layer.bias.set_data(ms.common.initializer.initializer("zeros",layer.bias.shape,layer.bias.dtype))