Python 基于GPU的链式预测
我有一个训练有素的链式模型,我想用它来进行预测。默认情况下,我可以在CPU上预测图像,但我想使用GPU,但我不确定该怎么做。 下面是我的代码的样子:Python 基于GPU的链式预测,python,deep-learning,gpu,chainer,Python,Deep Learning,Gpu,Chainer,我有一个训练有素的链式模型,我想用它来进行预测。默认情况下,我可以在CPU上预测图像,但我想使用GPU,但我不确定该怎么做。 下面是我的代码的样子: model = MyModel() chainer.serializers.load_npz("snapshot", model) image = load_image(path) # returns a numpy array with chainer.no_brackprop_mode(), chainer.using_config("tra
model = MyModel()
chainer.serializers.load_npz("snapshot", model)
image = load_image(path) # returns a numpy array
with chainer.no_brackprop_mode(), chainer.using_config("train", False):
pred = model.__call__(image)
这在CPU上运行良好。我应该添加什么来预测GPU?
我试过:
model.to\u gpu(0)
chainer.cuda.get\u device\u from\u id(0)。use()
- 使用
image=CuPy.array(image)
ValueError: numpy and cupy must not be used together
type(W): <type 'cupy.core.core.ndarray'>, type(x): <type 'numpy.ndarray'>
ValueError:numpy和cupy不能一起使用
类型(W):,类型(x):
我做错了什么?如何在GPU上执行预测?
提前感谢您的帮助。我终于想出了一个让它工作的方法: 我没有使用
cupy.array(image)
,而是使用cuda.to\u gpu(image)
,然后使用cuda.to\u cpu(image)
。我不确定这两者之间的区别,但仍然有效