1

我正在尝试为我的大学项目开发​​一个电话分类器模型。我已经训练了我的模型,当我尝试通过执行 python app/server.py serve 来部署模型时遇到了问题。我读了一篇文章(https://forums.fast.ai/t/unexpected-key-s-in-state-dict-model-opt/39745),我怀疑问题是由于不同的 fast.ai 版本之间运行我的 anaconda 和 Google Colab。

因此,我尝试使用 pip list fastai、conda list fastai 和 import fastai 来检查我电脑中 fastai 的版本;法泰。我的 Google colab 中的版本(我使用 Google Colab 开发我的模型)但结果是相同的(fastai 版本 = 1.0.59)。我什至尝试在 Google Colab 中更新我的 fastai 版本,但没有成功。这是异常代码:

Traceback (most recent call last):
  File "app/server.py", line 37, in <module>
    learn = loop.run_until_complete(asyncio.gather(*tasks))[0]
  File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 584, in run_until_complete
    return future.result()
  File "app/server.py", line 32, in setup_learner
    learn.load(model_file_name)
  File "C:\ProgramData\Anaconda3\lib\site-packages\fastai\basic_train.py", line 279, in load
    get_model(self.model).load_state_dict(state, strict=strict)
  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 845, in load_state_dict
    self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for Sequential:
        Missing key(s) in state_dict: "0.0.weight", "0.1.weight", "0.1.bias", "0.1.running_mean", "0.1.running_var", "0.4.0.conv1.weight", "0.4.0.bn1.weight", "0.4.0.bn1.bias", "0.4.0.bn1.running_mean", "0.4.0.bn1.running_var", "0.4.0.conv2.weight", "0.4.0.bn2.weight", "0.4.0.bn2.bias", "0.4.0.bn2.running_mean", "0.4.0.bn2.running_var", "0.4.1.conv1.weight", "0.4.1.bn1.weight", "0.4.1.bn1.bias", "0.4.1.bn1.running_mean", "0.4.1.bn1.running_var", "0.4.1.conv2.weight", "0.4.1.bn2.weight", "0.4.1.bn2.bias", "0.4.1.bn2.running_mean", "0.4.1.bn2.running_var", "0.4.2.conv1.weight", "0.4.2.bn1.weight", "0.4.2.bn1.bias", "0.4.2.bn1.running_mean", "0.4.2.bn1.running_var", "0.4.2.conv2.weight", "0.4.2.bn2.weight", "0.4.2.bn2.bias", "0.4.2.bn2.running_mean", "0.4.2.bn2.running_var", "0.5.0.conv1.weight", "0.5.0.bn1.weight", "0.5.0.bn1.bias", "0.5.0.bn1.running_mean", "0.5.0.bn1.running_var", "0.5.0.conv2.weight", "0.5.0.bn2.weight", "0.5.0.bn2.bias", "0.5.0.bn2.running_mean", "0.5.0.bn2.running_var", "0.5.0.downsample.0.weight", "0.5.0.downsample.1.weight", "0.5.0.downsample.1.bias", "0.5.0.downsample.1.running_mean", "0.5.0.downsample.1.running_var", "0.5.1.conv1.weight", "0.5.1.bn1.weight", "0.5.1.bn1.bias", "0.5.1.bn1.running_mean", "0.5.1.bn1.running_var", "0.5.1.conv2.weight", "0.5.1.bn2.weight", "0.5.1.bn2.bias", "0.5.1.bn2.running_mean", "0.5.1.bn2.running_var", "0.5.2.conv1.weight", "0.5.2.bn1.weight", "0.5.2.bn1.bias", "0.5.2.bn1.running_mean", "0.5.2.bn1.running_var", "0.5.2.conv2.weight", "0.5.2.bn2.weight", "0.5.2.bn2.bias", "0.5.2.bn2.running_mean", "0.5.2.bn2.running_var", "0.5.3.conv1.weight", "0.5.3.bn1.weight", "0.5.3.bn1.bias", "0.5.3.bn1.running_mean", "0.5.3.bn1.running_var", "0.5.3.conv2.weight", "0.5.3.bn2.weight", "0.5.3.bn2.bias", "0.5.3.bn2.running_mean", "0.5.3.bn2.running_var", "0.6.0.conv1.weight", "0.6.0.bn1.weight", "0.6.0.bn1.bias", "0.6.0.bn1.running_mean", "0.6.0.bn1.running_var", "0.6.0.conv2.weight", "0.6.0.bn2.weight", "0.6.0.bn2.bias", "0.6.0.bn2.running_mean", "0.6.0.bn2.running_var", "0.6.0.downsample.0.weight", "0.6.0.downsample.1.weight", "0.6.0.downsample.1.bias", "0.6.0.downsample.1.running_mean", "0.6.0.downsample.1.running_var", "0.6.1.conv1.weight", "0.6.1.bn1.weight", "0.6.1.bn1.bias", "0.6.1.bn1.running_mean", "0.6.1.bn1.running_var", "0.6.1.conv2.weight", "0.6.1.bn2.weight", "0.6.1.bn2.bias", "0.6.1.bn2.running_mean", "0.6.1.bn2.running_var", "0.6.2.conv1.weight", "0.6.2.bn1.weight", "0.6.2.bn1.bias", "0.6.2.bn1.running_mean", "0.6.2.bn1.running_var", "0.6.2.conv2.weight", "0.6.2.bn2.weight", "0.6.2.bn2.bias", "0.6.2.bn2.running_mean", "0.6.2.bn2.running_var", "0.6.3.conv1.weight", "0.6.3.bn1.weight", "0.6.3.bn1.bias", "0.6.3.bn1.running_mean", "0.6.3.bn1.running_var", "0.6.3.conv2.weight", "0.6.3.bn2.weight", "0.6.3.bn2.bias", "0.6.3.bn2.running_mean", "0.6.3.bn2.running_var", "0.6.4.conv1.weight", "0.6.4.bn1.weight", "0.6.4.bn1.bias", "0.6.4.bn1.running_mean", "0.6.4.bn1.running_var", "0.6.4.conv2.weight", "0.6.4.bn2.weight", "0.6.4.bn2.bias", "0.6.4.bn2.running_mean", "0.6.4.bn2.running_var", "0.6.5.conv1.weight", "0.6.5.bn1.weight", "0.6.5.bn1.bias", "0.6.5.bn1.running_mean", "0.6.5.bn1.running_var", "0.6.5.conv2.weight", "0.6.5.bn2.weight", "0.6.5.bn2.bias", "0.6.5.bn2.running_mean", "0.6.5.bn2.running_var", "0.7.0.conv1.weight", "0.7.0.bn1.weight", "0.7.0.bn1.bias", "0.7.0.bn1.running_mean", "0.7.0.bn1.running_var", "0.7.0.conv2.weight", "0.7.0.bn2.weight", "0.7.0.bn2.bias", "0.7.0.bn2.running_mean", "0.7.0.bn2.running_var", "0.7.0.downsample.0.weight", "0.7.0.downsample.1.weight", "0.7.0.downsample.1.bias", "0.7.0.downsample.1.running_mean", "0.7.0.downsample.1.running_var", "0.7.1.conv1.weight", "0.7.1.bn1.weight", "0.7.1.bn1.bias", "0.7.1.bn1.running_mean", "0.7.1.bn1.running_var", "0.7.1.conv2.weight", "0.7.1.bn2.weight", "0.7.1.bn2.bias", "0.7.1.bn2.running_mean", "0.7.1.bn2.running_var", "0.7.2.conv1.weight", "0.7.2.bn1.weight", "0.7.2.bn1.bias", "0.7.2.bn1.running_mean", "0.7.2.bn1.running_var", "0.7.2.conv2.weight", "0.7.2.bn2.weight", "0.7.2.bn2.bias", "0.7.2.bn2.running_mean", "0.7.2.bn2.running_var", "1.2.weight", "1.2.bias", "1.2.running_mean", "1.2.running_var", "1.4.weight", "1.4.bias", "1.6.weight", "1.6.bias", "1.6.running_mean", "1.6.running_var", "1.8.weight", "1.8.bias".
        Unexpected key(s) in state_dict: "opt_func", "loss_func", "metrics", "true_wd", "bn_wd", "wd", "train_bn", "model_dir", "callback_fns", "cb_state", "model", "data", "cls".

我的 fastai 版本与 Google colab 中的 fastai 相同,但我仍然遇到同样的问题。我希望我的模型能够部署在我的本地服务器上。

4

1 回答 1

1

感谢mrfabulous1,我理解了造成这种情况的原因,因此,我将他的回应分享给希望将来在本地部署他们的模型的人。

根据我在我的桌面上在 Google Colab 和 Anaconda 之间创建至少 20 个应用程序的经验,除非您的 Anaconda 环境与 Colab 环境 100% 匹配,否则该应用程序是不可能工作的。

在实践中,这意味着当您在训练模型的 Colab 上执行 !pip list 时。应用程序的 requirements.txt 文件中使用的所有文件库版本必须更改为与您在 Colab 上执行 !pip list 时收到的版本相匹配。

如果您尝试在 Anaconda 环境中的桌面上运行模型/应用程序,则应用程序将失败,除非您的 anaconda 环境中的所有文件版本号都与 Colab 版本匹配。

有时第 2 点甚至会失败,因为库可以进行安全修补或有尚未公布的微小更改并且没有与之关联的版本号。

因此,这意味着您每次在 Google Colab 上训练模型时都需要创建一个新的 anaconda 环境,并且任何库版本都与您当前的 Anaconda 环境不同。

如果您更改当前的 Anaconda 环境库版本,则使用该环境创建的所有现有应用程序可能/将会中断。

总之,每次 Colab 上的库/库与 Anaconda 上的库不同时,都会创建新环境。

干杯 mrfabulous1 ps。我现在使用 Docker 来解决这个问题。

于 2019-10-08T11:27:49.283 回答