RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is Fal

Posted 小艾衰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is Fal相关的知识,希望对你有一定的参考价值。

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.

gpu训练后的模型在无gpu机器上面载入参数的时候
需要设置torch.load(map_loction=torch.device(\'cpu\'))

解决问题:RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available()

问题:

Traceback (most recent call last):
  File "test.py", line 22, in <module>
    model = loadmodel()
  File "/home/joshuayun/Desktop/IBD/loader/model_loader.py", line 48, in loadmodel
    checkpoint = torch.load(settings.MODEL_FILE)
  File "/home/joshuayun/.local/lib/python3.6/site-packages/torch/serialization.py", line 387, in load
    return _load(f, map_location, pickle_module, **pickle_load_args)
  File "/home/joshuayun/.local/lib/python3.6/site-packages/torch/serialization.py", line 574, in _load
    result = unpickler.load()
  File "/home/joshuayun/.local/lib/python3.6/site-packages/torch/serialization.py", line 537, in persistent_load
    deserialized_objects[root_key] = restore_location(obj, location)
  File "/home/joshuayun/.local/lib/python3.6/site-packages/torch/serialization.py", line 119, in default_restore_location
    result = fn(storage, location)
  File "/home/joshuayun/.local/lib/python3.6/site-packages/torch/serialization.py", line 95, in _cuda_deserialize
    device = validate_cuda_device(location)
  File "/home/joshuayun/.local/lib/python3.6/site-packages/torch/serialization.py", line 79, in validate_cuda_device
    raise RuntimeError('Attempting to deserialize object on a CUDA '
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='cpu' to map your storages to the CPU.

解决方法1:

加载的时候使用cpu

model = torch.load('model/pytorch_resnet50.pth',map_location ='cpu')

解决方法2:

在./site-package/torch/serialization.py文件中更改

def load(f, map_location='cpu', pickle_module=pickle, **pickle_load_args):

为:

def load(f, map_location=None, pickle_module=pickle, **pickle_load_args):

解决方法3:

非要使用GPU的话,那环境出问题了,可能的地方有很多,查查吧。

以上是关于RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is Fal的主要内容,如果未能解决你的问题,请参考以下文章

RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is Fal

解决问题:RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available()

解决问题:RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available()

RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is Fal

RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count() is 1.(

Spark bind on port 0. Attempting port 1 问题解决