Search code examples
pythondeep-learninggpuchainer

Chainer - predict using GPU


I have a trained Chainer model that I want to use to perform predictions. I can predict images on CPU by default, but I want to use a GPU, and I'm not sure how I should do it. Here is what my code looks like:

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)

This works fine on CPU. What should I add to it to predict on GPU ? I tried:

  • model.to_gpu(0)
  • chainer.cuda.get_device_from_id(0).use()
  • convert image to a CuPy array with image = cupy.array(image)

With all of these options, I get an error:

ValueError: numpy and cupy must not be used together
type(W): <type 'cupy.core.core.ndarray'>, type(x): <type 'numpy.ndarray'>

What am I doing wrong here ? How do I perform predictions on GPU ? Thanks in advance for help.


Solution

  • I finally figured out a way to have it to work: Instead of using cupy.array(image), I used cuda.to_gpu(image) and then cuda.to_cpu(image). I'm not sure of the difference between these but still, it works.