Error when Load GPU-trained Model to CPU

username: t326wang
cooked: <p>Hi all,</p>
<p>Here are the piece of code when I try to load a gpu-trained model to cpu (and want to use CPU for evaluation):</p>
<pre><code class="lang-python">model_conv.load_state_dict(torch.load(model_file, map_location='cpu'))
model_conv = model_conv.cpu()
</code></pre>
<p>and the error message is:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "prediction.py", line 269, in &lt;module&gt;
    model_conv.load_state_dict(torch.load(resume_file, map_location='cpu'))
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/torch/serialization.py", line 229, in load
    return _load(f, map_location, pickle_module)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/torch/serialization.py", line 377, in _load
    result = unpickler.load()
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/torch/serialization.py", line 348, in persistent_load
    data_type(size), location)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/torch/serialization.py", line 246, in restore_location
    result = map_location(storage, location)
TypeError: 'str' object is not callable
</code></pre>
<p>My pytorch version is <code>0.1.12_1</code>. Any idea how to fix this? I have checked <a href="https://stackoverflow.com/questions/55511857/how-to-load-the-gpu-trained-model-into-the-cpu" rel="nofollow noopener">https://stackoverflow.com/questions/55511857/how-to-load-the-gpu-trained-model-into-the-cpu</a> but the solution seems not work in my case.</p>
<p>Without <code>map_location</code> argument, the error msg is</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "prediction.py", line 268, in &lt;module&gt;
    model_conv.load_state_dict(torch.load(resume_file))
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/torch/serialization.py", line 229, in load
    return _load(f, map_location, pickle_module)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/torch/serialization.py", line 377, in _load
    result = unpickler.load()
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/torch/serialization.py", line 348, in persistent_load
    data_type(size), location)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/torch/serialization.py", line 85, in default_restore_location
    result = fn(storage, location)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/torch/serialization.py", line 67, in _cuda_deserialize
    return obj.cuda(device_id)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/torch/_utils.py", line 57, in _cuda
    with torch.cuda.device(device):
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/torch/cuda/__init__.py", line 124, in __enter__
    _lazy_init()
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/torch/cuda/__init__.py", line 84, in _lazy_init
    _check_driver()
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/torch/cuda/__init__.py", line 58, in _check_driver
    http://www.nvidia.com/Download/index.aspx""")
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
</code></pre>
<p>Any idea how to fix this problem? Any suggestion is appreciated!</p>

username: ptrblck
cooked: <p>PyTorch <code>0.1.12_1</code> is one of the earliest releases, so I would strongly recommend to update to the latest stable release (<code>1.5.1</code>).</p>
<p>In <code>0.1.12</code>, you had to pass a function to <code>map_location</code> via:</p>
<pre><code class="lang-python">torch.load(..., map_location=lambda storage, location: 'cpu')
</code></pre>