如何将经过gpu训练的模型加载到cpu中? [英] how to load the gpu trained model into the cpu?

查看:1693
本文介绍了如何将经过gpu训练的模型加载到cpu中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PyTorch。我将在带有CPU的多个GPU上使用已经训练好的模型。该怎么做?

I am using PyTorch. I am going to use the already trained model on multiple GPUs with CPU. how to do this task?

我试过Anaconda 3和pytorch并使用cpu,但我没有GPU

I tried on Anaconda 3 and pytorch with cpu only i dont have gpu

model = models.get_pose_net(config, is_train=False)
gpus = [int(i) for i in config.GPUS.split(',')]
model = torch.nn.DataParallel(model, device_ids=gpus).cuda()

print('Created model...')
print(model)
checkpoint = torch.load(config.MODEL.RESUME)
model.load_state_dict(checkpoint)
model.eval()
print('Loaded pretrained weights...')

我得到的错误是

    AssertionError                            Traceback (most recent call  last)
<ipython-input-15-bbfcd201d332> in <module>()
      2 model = models.get_pose_net(config, is_train=False)
      3 gpus = [int(i) for i in config.GPUS.split(',')]
----> 4 model = torch.nn.DataParallel(model, device_ids=gpus).cuda()
      5 print('Created model...')
      6 print(model)

C:\Users\psl\Anaconda3\lib\site-packages\torch\nn\modules\module.py in cuda(self, device)
    258             Module: self
    259         """
--> 260         return self._apply(lambda t: t.cuda(device))
    261 
    262     def cpu(self):

C:\Users\psl\Anaconda3\lib\site-packages\torch\nn\modules\module.py in 

_apply(self,fn)
185 def _apply(self,fn):
186 for self.children()中的模块:
-> 187 module._apply(fn)
188
189参数self._parameters.values():

_apply(self, fn) 185 def _apply(self, fn): 186 for module in self.children(): --> 187 module._apply(fn) 188 189 for param in self._parameters.values():

C:\Users\psl\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _apply(self, fn)
    185     def _apply(self, fn):
    186         for module in self.children():
--> 187             module._apply(fn)
    188 
    189         for param in self._parameters.values():

C:\Users\psl\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _apply(self, fn)
    191                 # Tensors stored in modules are graph leaves, and we don't
    192                 # want to create copy nodes, so we have to unpack the data.
--> 193                 param.data = fn(param.data)
    194                 if param._grad is not None:
    195                     param._grad.data = fn(param._grad.data)

C:\Users\psl\Anaconda3\lib\site-packages\torch\nn\modules\module.py in <lambda>(t)
    258             Module: self
    259         """
--> 260         return self._apply(lambda t: t.cuda(device))
    261 
    262     def cpu(self):

 C:\Users\psl\Anaconda3\lib\site-packages\torch\cuda\__init__.py in _lazy_init()
    159         raise RuntimeError(
    160             "Cannot re-initialize CUDA in forked subprocess. " + msg)
--> 161     _check_driver()
    162     torch._C._cuda_init()
    163     _cudart = _load_cudart()

C:\Users\psl\Anaconda3\lib\site-packages\torch\cuda\__init__.py in _check_driver()
     80 Found no NVIDIA driver on your system. Please check that you
     81 have an NVIDIA GPU and installed a driver from
---> 82 http://www.nvidia.com/Download/index.aspx""")
     83         else:
     84             # TODO: directly link to the alternative bin that needs install

AssertionError: 
Found no NVIDIA driver on your system. Please check that you
have an NVIDIA GPU and installed a driver from
http://www.nvidia.com/Download/index.aspx


推荐答案

要强制将保存的模型加载到cpu上,使用以下命令。

To force load the saved model onto cpu, use the following command.

torch.load('/path/to/saved/model', map_location='cpu')

在您的情况下,将其更改为

In your case change it to

torch.load(config.MODEL.RESUME, map_location='cpu')

这篇关于如何将经过gpu训练的模型加载到cpu中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆