解决问题:RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available()
Posted super尚
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决问题: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()的主要内容,如果未能解决你的问题,请参考以下文章
“RuntimeError:生成器引发了 StopIteration”如何解决这个 python 问题?
如何解决“RuntimeError: CUDA error: invalid device ordinal”?
是否有解决方案绕过“无法在迭代期间将新密钥添加到哈希(RuntimeError)”?
解决RuntimeError: CUDA error: invalid device symbol问题(已解决)