Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

please use torch.load with map_location=torch.device('cpu')

I am running Python program, but I do not have a GPU, what can I do to make Python use CPU instead of GPU?

$ python extract_feature.py --data mnist --net checkpoint_4.pth.tar --features pretrained

It gives me the following warning:

=> RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.

The photo is the Structure of my Python project:

1

like image 686
elham esmaili Avatar asked Jul 22 '26 02:07

elham esmaili


1 Answers

I got into a similar error. Then by trying the following workaround issue is solved. (If your model is .pth or .h5 format.)

MODEL_PATH = 'Somemodelname.pth'
model.load_state_dict(torch.load(MODEL_PATH, 
map_location=torch.device('cpu')))

If you want certain GPU to be used in your machine. Then,

map_location = torch.device('cuda:device_id')

like image 130
viggi lucifer Avatar answered Jul 24 '26 15:07

viggi lucifer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!