紧急通知:禁止一切关于政治&VPN翻墙等话题,发现相关帖子会立马删除封号
小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
将需要GPU计算的代码改为用CPU计算
-
最近需要将一个gpu上跑的小车的控制代码改为在cpu上跑,按如上标题搜索后进行了两处更改:将torch.device改为'cpu',在torch.device的参数中添加map_location='cpu',但仍显示如下报错。
原代码配置:Ubuntu18双系统+gpu
现在我的配置:虚拟机Ubuntu18+cpu
请问是pytorch的版本不匹配吗?(base) xxx@xxx-virtual-machine:~/catkin_ws/src$ python run.py torch:1.8.2 torchvision:0.9.2 device:cpu Process Process-2: Traceback (most recent call last): File "/home/xxx/anaconda3/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap self.run() File "/home/xxx/anaconda3/lib/python3.8/multiprocessing/process.py", line 108, in run self._target(*self._args, **self._kwargs) File "/home/xxx/catkin_ws/src/Node.py", line 263, in create_node importlib.import_module('.main', package=p[-3] + '.' + p[-2]) File "/home/xxx/anaconda3/lib/python3.8/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1014, in _gcd_import File "<frozen importlib._bootstrap>", line 991, in _find_and_load File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 671, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 783, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/home/xxx/catkin_ws/src/image/main.py", line 25, in <module> model.load_state_dict(torch.load('weight.pt'),map_location='cpu') File "/home/xxx/anaconda3/lib/python3.8/site-packages/torch/serialization.py", line 592, in load return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args) File "/home/xxx/anaconda3/lib/python3.8/site-packages/torch/serialization.py", line 851, in _load result = unpickler.load() File "/home/xxx/anaconda3/lib/python3.8/site-packages/torch/serialization.py", line 843, in persistent_load load_tensor(data_type, size, key, _maybe_decode_ascii(location)) File "/home/xxx/anaconda3/lib/python3.8/site-packages/torch/serialization.py", line 832, in load_tensor loaded_storages[key] = restore_location(storage, location) File "/home/xxx/anaconda3/lib/python3.8/site-packages/torch/serialization.py", line 175, in default_restore_location result = fn(storage, location) File "/home/xxx/anaconda3/lib/python3.8/site-packages/torch/serialization.py", line 151, in _cuda_deserialize device = validate_cuda_device(location) File "/home/xxx/anaconda3/lib/python3.8/site-packages/torch/serialization.py", line 135, 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=torch.device('cpu') to map your storages to the CPU.
-
@1687028512
自查了一下是代码位置添加错了
不是model.load_state_dict(torch.load('weight.pt'),map_location='cpu')
而是
model.load_state_dict(torch.load('weight.pt',map_location='cpu')
问题解决
-
-