motokimura / PyTorch_Gaussian_YOLOv3

PyTorch implementation of Gaussian YOLOv3 (including training code for COCO dataset)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

share a solution to the problem of using GPU in demo TypeError can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor

sisrfeng opened this issue · comments

change

            dw = width * np.power(sw, sigma_scale_wh) - width
            dh = height * np.power(sh, sigma_scale_wh) - height

to

            dw = width * np.power(sw.cpu(), sigma_scale_wh) - width
            dh = height * np.power(sh.cpu(), sigma_scale_wh) - height

ref:
Regardless of the problem in the code shown above, this issue happens when one tries to convert a tensor value stored in the GPU (cuda) to numpy, if the tensor variable is called x, then, x.numpy() would raise such an error. The solution is easy by bringing back the data to the cpu and then using numpy, as follows:
x.cpu().numpy()
If, however, one is interested in finding argmax, this could be done via output.argmax() as PyTorch does support it.