RuntimeError:尝试反序列化 CUDA 设备上的对象

新手上路,请多包涵

我遇到了 RunTimeError 当我试图在我机器的 CPU 而不是 GPU 中运行代码时。代码最初来自这个 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.

原文由 Joshua 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 585
1 个回答

如果你没有 gpu 然后使用 map_location=torch.device(‘cpu’) 加载 model.load()

 my_model = net.load_state_dict(torch.load('classifier.pt', map_location=torch.device('cpu')))

原文由 Biplob Das 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题