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 (将#修改为@)