Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using a modified predict.py for testing a pruned SqueezeNet Model

[phung@archlinux SqueezeNet-Pruning]$ python predict.py --image 3_100.jpg --model model_prunned --num_class 2
prediction in progress
Traceback (most recent call last):
File “predict.py”, line 66, in
prediction = predict_image(imagepath)
File “predict.py”, line 52, in predict_image
index = output.data.numpy().argmax()
TypeError: can’t convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
[phung@archlinux SqueezeNet-Pruning]$

I understand that numpy does not support GPU yet.

How shall I modify the code to get away from this error without invoking tensor copy data operation, tensor.cpu() ?

like image 640
kevin998x Avatar asked Dec 23 '18 03:12

kevin998x


3 Answers

Change

index = output.data.numpy().argmax()

to

index = output.cpu().data.numpy().argmax()

This means data is first moved to cpu and then converted to numpy array.

like image 51
Umang Gupta Avatar answered Nov 06 '22 17:11

Umang Gupta


I found out that I can just use

output.argmax()
like image 45
kevin998x Avatar answered Nov 06 '22 17:11

kevin998x


You can use torch.max function like follows:

value, index = torch.max(output,1)
like image 2
Kaushik Roy Avatar answered Nov 06 '22 18:11

Kaushik Roy