import torch
import paddle
import os
from collections import OrderedDict
import numpy as np
def export_weight_names(net):
print(net.state_dict().keys())
with open('paddle.txt', 'w') as f:
for key in net.state_dict().keys():
f.write(key + '\n')
res2net_paddle_implement=paddle.vision.models.resnet50(pretrained=False)
export_weight_names(res2net_paddle_implement) # 将自己paddle模型的keys存为txt
paddle_list = open('paddle.txt') # paddle的keys
state_dict = torch.load('C:\\Users\\1\\Downloads\\resnet50.pth')
paddle_state_dict = OrderedDict()
paddle_list = paddle_list.readlines()
torch_list = state_dict.keys()
for p in paddle_list:
p = p.strip()
t = p
if "mean" in p:
t = p.replace("_mean", "running_mean")
if "variance" in p:
t = p.replace("_variance", "running_var")
if t in torch_list:
if 'fc' not in p:
paddle_state_dict[p] = state_dict[t].detach().cpu().numpy()
else:
paddle_state_dict[p] = state_dict[t].detach().cpu().numpy().T
else:
print(p)
f = open('resnet502.pdparams', 'wb')
import pickle
pickle.dump(paddle_state_dict, f)
f.close()
torch预训练权重转换为paddle模型格式:Resnet50模型
最新推荐文章于 2024-05-24 11:26:12 发布