使用MindSpore Hub加载预训练模型并迁移

1、下载安装MindSpore HUB,因为我是MindSpore1.9.0,所以安装1.9.0的MindSpore Hub

pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/1.9.0/Hub/any/mindspore_hub-1.9.0-py3-none-any.whl --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple

2、使用现有模型进行迁移学习

先附上官方教程

从Hub加载模型 — MindSpore master documentation

一看每个模型都是不同的人写的...写法等全都不一样,用起来也让你头大。先说几个我试了的。

GoogLeNet imagenet2012预训,必须写num_classes,必须等于1000....给这个参数纯属闲的。

model = "mindspore/1.9/googlenet_imagenet2012" #可用 num_classes=1000
network = mshub.load(model,num_classes=1000)
network.set_train(False)

Resnet50,不能用,它会自己去加载参数,但是对应不上。

model = "mindspore/1.9/resnet50_imagenet2012"
network = mshub.load(model)
network.set_train(False)
For 'load_param_into_net', end_point.weight in the argument 'net' should have the same shape as end_point.weight in the argument 'parameter_dict'. But got its shape (10, 2048) in the argument 'net' and shape (1001, 2048) in the argument 'parameter_dict'.May you need to check whether the checkpoint you loaded is correct or the batch size and so on in the 'net' and 'parameter_dict' are same.

 VGG16,不能用...这个感觉写的时候就没有写对,反正你不用给任何参数,就给你出错。

model = "mindspore/1.9/vgg16_bn_imagenet2012"
network = mshub.load(model)
network.set_train(False)
    nn.Dense(512 * (self.image_h // 32) * (self.image_w // 32), 4096),
TypeError: unsupported operand type(s) for //: 'str' and 'int'

好的,于是,我就使用GoogleNet想做一些迁移工作,于是换了最后一层的线性层,代码如下。

# 加载预训练的GoogleNet模型
model = "mindspore/1.9/googlenet_imagenet2012" #可用 num_classes=1000
network = mshub.load(model, num_classes=1000, include_top=False)# include_top=False 去掉了最后的线性层
# network = mshub.load(model, num_classes=1000)
network.set_train(False)
# 增加一个新的分类层
last_channel = 1024
num_classes = args.num_classes
classification_layer = nn.Dense(last_channel, num_classes)
classification_layer.set_train(True)
train_network = nn.SequentialCell([network, classification_layer])

但是。。。真的太麻烦了,并且还报错,维度的错误,但由于模型文件不在本地,也根本无法调试。我决定放弃了,看来必须自己找到网络的模型,然后尝试加载预训练的参数会好很多....

总结一下,总之,很难用。文档写的也都不对。按照文档写的进行操作也会报错。每个模型应该是包给了不同的人做的,代码风格不一,以至于导致了最后每个模型的使用和体验都很差。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值