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相关的知识,希望对你有一定的参考价值。

今天在跑yolov7的时候遇见,模型加载问题,因为我是使用CPU来加载pt模型的,但是出现了错误;

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.

模型是使用CUDA跑的,但是加载是使用CPU加载的,报错的意思就是需要是需要反序列化加载,就是在加载模型的后面,加一个参数map_location,这个当时还以为是OS出现问题了(当时看见网上说是
1、CUDA版本不匹配
2、pytorch版本不对
3、OS模块出问题了),但是这个其实也都有可能,每个运行环境都不一样

原始加载方式:


报错:
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.

解决方法:

在加载模型的时候加上map_location=‘cpu’

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
weigths = torch.load('yolov.pt',map_location='cpu')
model = weigths['model']
model = model.half().to(device)
_ = model.eval()


希望这个对你有用!

谢谢点赞评论!

以上是关于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 问题解决