torchserver的fastaiv2到pytorch

torchserver的fastaiv2到pytorch,pytorch,fast-ai,Pytorch,Fast Ai,我通常使用fastai(v2或v1)进行快速原型设计。现在,我想将我的一个模型部署到torchserver,该模型经过fastai培训 假设我们有这样一个简单的模型: learn = cnn_learner(data, models.resnet34, metrics=[accuracy, error_rate, score]) # after the training torch.save(learn.model.state

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

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

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"
运行时错误回溯(最近一次调用)
在里面
---->1个型号火炬代表装载状态指令(状态);
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py处于加载状态(self、state、strict)
837如果len(错误)>0:
838 raise RUNTIMERROR({}:\n\t{}的加载状态中的错误)。格式(
-->839 self.\uuuuuuuuu类\uuuuuuuu名称\uuuuuuuuu,“\n\t”.join(错误\u msgs)))
840返回不兼容密钥(缺少密钥、意外密钥)
841
RuntimeError:ResNet的加载状态为“”时出错:
状态命令中缺少键:“conv1.weight”、“bn1.weight”、“bn1.bias”、“bn1.running\u mean”、“bn1.running\u var”、“layer1.0.conv1.weight”、“layer1.0.bn1.weight”
这在fastai 1.0.6或fastai 2.3.1+pytorch 1.8.1中发生