Discussions>RuntimeError: Attempting to deserialize object on a CUDA device>

RuntimeError: Attempting to deserialize object on a CUDA device

Why I'm getting this issue? I try to load a pre-trained PyTorch model on my laptop, but I get this error.

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
 
2 votesJO266.00
1 Answers
JW297.00
2

It seems you trained your model on a CUDA device, saved the weights which were stored on it, but trying to load a CPU device. You need to pass the device specifically during the weights loading.

import torch

device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
model = Net()
model.load_state_dict(torch.load(weights_paths, map_location=device))
# Or you can move the loaded model into the specific device
model.to(device)
Reply
Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?