当前位置:网站首页>请问想加载torch预训练好的模型用于mindspore模型finetune有什么方法?
请问想加载torch预训练好的模型用于mindspore模型finetune有什么方法?
2022-07-19 12:12:00 【小乐快乐】
正在迁移一个mindspore的网络, 已有torch版本, 且已经有torch预训练好的权重。 从头训练mindspore的网络太耗时, 希望可以直接加载torch的权重进行finetune。 请问有什么方法可以做到呢?
需要把torch 的checkpoint转化成MindSpore格式的checkpoint。然后用MindSpore加载checkpoint执行fune-tune任务。
转化checkpoint可参考如下代码:
from mindspore.train.serialization import save_checkpointfrom mindspore import Tensorimport torchdef pytorch2mindspore('res18_py.pth'):
par_dict = torch.load('res18_py.pth')['state_dict']
new_params_list = []
for name in par_dict:
param_dict = {}
parameter = par_dict[name]
print('========================py_name',name)
if name.endswith('normalize.bias'):
name = name[:name.rfind('normalize.bias')]
name = name + 'normalize.beta'
elif name.endswith('normalize.weight'):
name = name[:name.rfind('normalize.weight')]
name = name + 'normalize.gamma'
elif name.endswith('.running_mean'):
name = name[:name.rfind('.running_mean')]
name = name + '.moving_mean'
elif name.endswith('.running_var'):
name = name[:name.rfind('.running_var')]
name = name + '.moving_variance'
print('========================ms_name',name)
param_dict['name'] = name
param_dict['data'] = Tensor(parameter.numpy())
new_params_list.append(param_dict)
save_checkpoint(new_params_list, 'res18_ms.ckpt')
边栏推荐
- Flink SQL configures Kafka to chain a topic with multiple partitions, and there is no error. There is no problem with a single partition
- Graph theory review usaco2006jan gold separation path
- 面试官必问的 3 道 MQ 面试题,还有谁不会??
- Apicloud AVM framework creates digital scrolling components
- 高端纯电SUV顶级产品,AION LX Plus与蔚来ES6如何选?
- 请教一个问题:场景:flink sql 中使用累计窗口,窗口大小一天,每分钟统计一次当日累计
- 贪心【培训试题】活动选择
- [JSOI2007]重要的城市
- 康威定律——组织决定产品,领域驱动设计
- Analysis of e-commerce user behavior data
猜你喜欢
智源承认论文抄袭,相关责任人已离职!
unet和mask rcnn
【历史上的今天】7 月 4 日:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生
JS class 并不只是简单的语法糖!
(0711-0717) memorabilia of open source software security this week
Conway's Law -- organization decides products, and domain drives design
ICLR 2022 | GNNAsKernel: 能提升任意GNN表达能力的通用框架
EXCRT/扩展中国剩余定理学习笔记
梅科尔工作室-华为14天鸿蒙设备开发实战笔记五
Link: fatal error lnk1104: unable to open the file "ucrtd.lib" workaround Visual Studio
随机推荐
科技云报道:零信任和SASE有什么不一样?答案其实并不重要
Inequality learning notes
2022.7.10-----leetcode. seven hundred and forty-one
Mindspore1.1.1源码编译安装--核心编译阶段报错
推荐一款支持数据 + 代码生成的开发工具!yyds
省选专练之 [HAOI2009]毛毛虫
ACL 2022 | text to table: a new information extraction task
Devops has been practiced for many years. What is the most painful thing?
省选专练之GCD生成树
剑指offer题库总结(二)之字符串(C语言版本)
用APICloud如何开发出运行体验良好、高性能的 App
IDEA中如何安装插件和宝贝插件的推荐
【历史上的今天】7 月 2 日:BitTorrent 问世;商业系统 Linspire 被收购;索尼部署 PlayStation Now
小程序技术解决桌面应用敏捷迭代的一种思路
[pkusc2018] main fighting ground of provincial selection and special training
Is machine translation finished? Meta open source nllb translation model supports the translation of 200 languages
Rong Tui [jsoi2011] special products
target文件夹里的资源无法被程序加载
面试官必问的 3 道 MQ 面试题,还有谁不会??
Use of & & operators