elham esmaili
elham esmaili

Reputation: 71

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

Upvotes: 6

Views: 44567

Answers (2)

Muhammad Shifa
Muhammad Shifa

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

viggi lucifer
viggi lucifer

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

Related Questions