python - 运行时错误 : Attempting to deserialize object on a CUDA device

标签 python python-3.x python-2.7 machine-learning computer-vision

我在尝试在机器的 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.

最佳答案

如果你没有 gpu 然后使用 ma​​p_location=torch.device('cpu') 加载 model.load()

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

关于python - 运行时错误 : Attempting to deserialize object on a CUDA device,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56369030/

相关文章:

python - 递归调用复杂度

python - 如何将两个等长列表转换为表示有序对的嵌套列表列表?

python-3.x - Pytest 创建临时 CSV 文件以供读取

python - Scrapy提取错误的IMG SRC

javascript - 收到静态文件但在 django 中不起作用

python - 关于二进制文件的一般问题

python - 使用 groupby 创建具有最大值的新列

python - python 实现任意树

python - 如何为Python虚拟环境配置Ansible?

python - 如何在Python中获取除某些值之外的排序数组的索引?