在尝试在计算机的CPU而不是GPU中运行代码时遇到RunTimeError
。该代码最初来自此GitHub项目-IBD: Interpretable Basis Decomposition for Visual Explanation。这是一个研究项目。我尝试将CUDA设置为false
,并在此网站上查看了其他解决方案。
GPU = False # running on GPU is highly suggested
CLEAN = False # set to "True" if you want to clean the temporary large files after generating result
APP = "classification" # Do not change! mode choide: "classification", "imagecap", "vqa". Currently "imagecap" and "vqa" are not supported.
CATAGORIES = ["object", "part"] # Do not change! concept categories that are chosen to detect: "object", "part", "scene", "material", "texture", "color"
CAM_THRESHOLD = 0.5 # the threshold used for CAM visualization
FONT_PATH = "components/font.ttc" # font file path
FONT_SIZE = 26 # font size
SEG_RESOLUTION = 7 # the resolution of cam map
BASIS_NUM = 7 # In decomposition, this is to decide how many concepts are used to interpret the weight vector of a class.
这是错误:
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.
答案 0 :(得分:9)
如果您没有GPU,请对加载模型使用 map_location = torch.device('cpu')
my_model = net.load_state_dict(torch.load('classifier.pt', map_location=torch.device('cpu')))
答案 1 :(得分:5)
“如果您在仅使用 CPU 的机器上运行,请使用带有 map_location=torch.device('cpu') 的 torch.load 将您的存储映射到 CPU。”
model = torch.load('model/pytorch_resnet50.pth',map_location ='cpu')
答案 2 :(得分:4)
给出一个较小的答案。为了解决这个问题,您可以在load()
文件中更改名为serialization.py
的函数的参数。它存储在:./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 :(得分:1)
我遇到了同样的问题,而不是修改昨天运行良好的现有代码,首先我检查了我的GPU是否空闲
nvidia-smi
我可以看到,它的利用不足,因此作为传统解决方案,我关闭了笔记本电脑,然后重新启动,它就可以正常工作了。
(我要记住的一点是,它较早就可以工作,并且我没有更改代码中的任何内容,因此一旦重新启动它就可以正常工作,并且我能够使用GPU)
答案 4 :(得分:0)
在陈述问题时,提示您正在尝试在非cuda机器上使用cuda-model。请注意错误消息的详细信息-please use torch.load with map_location='cpu' to map your storages to the CPU
。当我尝试在仅cpu的计算机上加载(从检查点)预训练的模型时,我遇到了类似的问题。该模型是在cuda机器上训练的,因此无法正确加载。将map_location='cpu'
参数添加到load
方法后,一切正常。
答案 5 :(得分:0)
您可以在加载时使用torch.load的map_location参数重新映射张量位置。
在以下存储库中,在文件“ test.py”中,model = loadmodel()调用model_loader.py文件以使用torch.load()加载模型。
这只会映射来自GPU0的存储,请添加map_location:
str
在model_loader.py文件中,在调用torch.load()函数的任何地方添加map_location = {'cuda:0':'cpu'}。
答案 6 :(得分:0)
出于某种原因,即使您的机器有 GPU,portainer 也会发生这种情况。一个粗略的解决方案是重新启动它。如果您在部署后更改容器的状态(例如,您在容器运行时更改重新启动策略)通常会发生这种情况,这让我认为这是一些更重要的问题。