Reputation: 71
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:
Upvotes: 6
Views: 44567
Reputation: 132
Just giving a smaller answer. To solve this, you could change the parameters of the function named load()
in the serialization.py
file. This is stored in: ./site-package/torch/serialization.py
Write:
def load(f, map_location='cpu', pickle_module=pickle, **pickle_load_args):
instead of:
def load(f, map_location=None, pickle_module=pickle, **pickle_load_args):
Hope it helps.
Upvotes: -1
Reputation: 121
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')
Upvotes: 12