RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False
时间: 2023-11-04 08:01:02 浏览: 115
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False 这个错误通常发生在使用PyTorch加载保存在GPU上的模型时,但是当前环境没有可用的CUDA设备。要解决这个问题,你可以使用torch.load函数,并指定map_location参数为torch.device('cpu')或'map_location='cpu'',将模型加载到CPU上。
相关问题
untimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False.
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. 如果你在一个仅有CPU的机器上运行,请使用torch.load并设置map_location=torch.device('cpu')将存储映射到CPU上解决这个问题。你可以在加载模型时添加map_location='cpu'参数,然后将模型放到CPU上进行运算。例如:
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
weights = torch.load('yolov.pt', map_location='cpu')
model = weights['model']
model = model.half().to(device)
_ = model.eval()
Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False.
回答: 该错误提示表示尝试在CUDA设备上反序列化对象,但torch.cuda.is_available()返回False,即没有可用的CUDA设备。要解决这个问题,可以使用torch.load函数时添加参数map_location=torch.device('cpu'),将存储映射到CPU上。这样可以确保在没有CUDA设备的CPU-only机器上正常加载对象。<em>1</em><em>2</em><em>3</em>
#### 引用[.reference_title]
- *1* [pytorch出错: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False](https://blog.csdn.net/qq_27149279/article/details/106037681)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item]
- *2* *3* [RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is Fal](https://blog.csdn.net/qq_44936246/article/details/126375749)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item]
[ .reference_list ]