微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

fastaiv2 到 pytorch for torchserver

如何解决fastaiv2 到 pytorch for torchserver

我通常使用 fastai(v2 或 v1)进行快速原型设计。现在我想将我用 fastai 训练的模型之一部署到 torchserver。

假设我们有一个像这样的简单模型:

learn = cnn_learner(data,models.resnet34,metrics=[accuracy,error_rate,score])
# after the training 
torch.save(learn.model.state_dict(),"./test1.pth")
state = torch.load("./test1.pth")
model_torch_rep = models.resnet34()
model_torch_rep.load_state_dict(state)

我尝试了很多不同的东西,结果都一样

RuntimeError                              Traceback (most recent call last)
<ipython-input-284-e4dbdce23d43> in <module>
----> 1 model_torch_rep.load_state_dict(state);

/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in load_state_dict(self,state_dict,strict)
    837         if len(error_msgs) > 0:
    838             raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
--> 839                                self.__class__.__name__,"\n\t".join(error_msgs)))
    840         return _IncompatibleKeys(missing_keys,unexpected_keys)
    841 

RuntimeError: Error(s) in loading state_dict for resnet:
    Missing key(s) in state_dict: "conv1.weight","bn1.weight","bn1.bias","bn1.running_mean","bn1.running_var","layer1.0.conv1.weight","layer1.0.bn1.weight"

这发生在 fastai 1.0.6 或 fastai 2.3.1 + pytorch 1.8.1 ...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。