如何使用fastai保存的模型?

问题描述

我在 google colab 中训练了我的模型,并在我的电脑中下载了 .pkl 文件。现在,我该如何使用它?如何加载 .pkl 文件,我是否需要安装 fastai 才能使其工作?

解决方法

如何加载 .pkl 文件

假设您已使用 learner.save 保存模型,您可以使用补充 learner.load 方法。

我需要安装 fastai 才能工作吗

是的,如果您以这种方式保存,则需要 fastai。您还可以通过以下方式保存包含在 learner 中的 PyTorch 模型本身:

torch.save(learner.model,"/path/to/model.pt") # or save it's state_dict,better option
model = torch.load("/path/to/model.pt")

无论哪种方式,您都需要这些库作为 pickle 存储数据,但必须以代码方式提供 class 定义和创建。