pytorch 如何从gpu切换到cpu?

trnvg8h3  于 2023-04-12  发布在  其他
关注(0)|答案(3)|浏览(204)

嗨,我想知道如何在我的CPU而不是GPU上运行机器学习代码?
我试过在设置文件中使GPU错误,但它无法修复它。

全局设置

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"
map_location='cpu'

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
Traceback (most recent call last):
  File "test.py", line 22, in <module>
    model = loadmodel()
  File "/home/joshuayun/Desktop/IBD/loader/model_loader.py", line 44, 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.
lbsnaicq

lbsnaicq1#

如果我没有错,你会在代码model = loadmodel()处得到上面的错误。我不知道你在loadmodel()中做了什么,但你可以尝试以下几点:

  • defaults.device设置为cpu
  • torch.load(model_weights)更改为torch.load(model_weights, map_location=torch.device('cpu'))
kyvafyod

kyvafyod2#

如果您使用的是从nn.Module扩展的模型,您可以将整个模型移动到CPU或GPU,这样做:

device = torch.device("cuda")
model.to(device)
# or
device = torch.device("cpu")
model.to(device)

如果你只想移动一个Tensor:

x = torch.Tensor(10).cuda()
# or
x = torch.Tensor(10).cpu()
wljmcqd8

wljmcqd83#

os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

相关问题