pytorch-openpose icon indicating copy to clipboard operation
pytorch-openpose copied to clipboard

TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

Open JeremyKeusters opened this issue 5 years ago • 4 comments

First of all: thanks for your amazing work! I would like to use OpenPose, but can't run Caffe in my current setup/environment. This repo is a life-saver.

Now to the point: I got the following error when running the demo.py: TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

I fixed this by changing the line in python/hand.py: output = self.model(data).numpy()

to: output = self.model(data).cpu().numpy()

As numpy can't work on stuff that is still on the GPU.. I'm not sure if this needs to be changed in the repo and if this is the best solution for this error? Thanks.

My environment specs are the following: OS: CentOS Linux release 7.5.1804 CUDA version: 8.0 cuDNN version: 7.0.5 Torch version: 0.4.1 Python version: Python 3.6.6 GPU's: 4x GeForce RTX 2070

The full error stack:

Traceback (most recent call last):
  File "demo.py", line 31, in <module>
    peaks = hand_estimation(oriImg[y:y+w, x:x+w, :])
  File "python/hand.py", line 46, in __call__
    output = self.model(data).numpy()
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

JeremyKeusters avatar Jul 12 '19 19:07 JeremyKeusters

output = self.model(data).cpu().numpy()

Hzzone avatar Jul 12 '19 23:07 Hzzone

Yup, that’s the same solution as I posted above. So you agree that this is the right solution?

JeremyKeusters avatar Jul 12 '19 23:07 JeremyKeusters

@JeremyKeusters your solution has helped me. Thanks

12345k avatar Oct 13 '19 12:10 12345k

output = self.model(data).cpu().numpy();this is right!

swithmn1 avatar May 20 '20 05:05 swithmn1