2

我有一个export.pkl在 cuda 机器上训练过的模型。我想在 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

我有两个问题:

  • raise在我的export.pkl
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模型在 cpu 上工作?
  • 有没有办法export.pkl在 cuda 上制作另一个并在 cpu 上可用?

谢谢

4

1 回答 1

0

一种方法是通过使用空数据集指定模型并随后加载模型权重来加载学习器。对于 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)
于 2019-12-13T14:22:00.837 回答