Fastai Ulmfit 模型在 Cuda Machin 上为 CPU 训练



我有一个在cuda机器上训练过的export.pkl模型。我想在Macbook上使用它:

from fastai.text import load_learner
from utils import get_corpus
learner = load_learner('./models')
corpus = get_corpus()
res = [ str(learner.predict(c)[0]) for c in corpus ]

我收到以下错误:

...
File "/Users/gautiergilabert/Envs/cc/lib/python3.7/site-packages/torch/nn/parallel/data_parallel.py", line 146, in forward
"them on device: {}".format(self.src_device_obj, t.device))
RuntimeError: module must have its parameters and buffers on device cuda:0 (device_ids[0]) but found one of them on device: cpu

我有两个问题:

  • 我在export.pkl中找到了raise
for t in chain(self.module.parameters(), self.module.buffers()):
if t.device != self.src_device_obj:
raise RuntimeError("module must have its parameters and buffers "
"on device {} (device_ids[0]) but found one of "
"them on device: {}".format(self.src_device_obj, t.device))

关于文档字符串中的模块:module to be parallelized。我真的不明白它是什么。我的苹果电脑?

除了我的Macbook,我想在CPU上运行该模型

  • 有没有办法使这个export.pkl模型在 中央处理器 ?
  • 有没有办法在 cuda 上制作另一个export.pkl并使其在 CPU 上可用?

谢谢

一种方法是通过使用空数据集指定模型并在之后加载模型权重来加载学习器。对于resnet图像分类器,这样的东西应该可以工作:

from fastai.vision import *
# path where the model is saved under path/models/model-name
path = "model_path"
tfms = get_transforms()
data = ImageDataBunch.single_from_classes(".", classes=["class1", "class2"], ds_tfms=tfms)
learner = cnn_learner(data, models.resnet34, metrics=accuracy)
# loads model from model_path/models/model_name.pth
learner.load("model_name")
image = open_image("test.jpg")
pred_class, pred_idx, outputs = learner.predict(image)

最新更新