Error when Load GPU-trained Model to CPU

Hi all,

Here are the piece of code when I try to load a gpu-trained model to cpu (and want to use CPU for evaluation):

model_conv.load_state_dict(torch.load(model_file, map_location='cpu'))
model_conv = model_conv.cpu()

and the error message is:

Traceback (most recent call last):
  File "prediction.py", line 269, in <module>
    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

My pytorch version is 0.1.12_1. Any idea how to fix this? I have checked https://stackoverflow.com/questions/55511857/how-to-load-the-gpu-trained-model-into-the-cpu but the solution seems not work in my case.

Without map_location argument, the error msg is

Traceback (most recent call last):
  File "prediction.py", line 268, in <module>
    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

Any idea how to fix this problem? Any suggestion is appreciated!

PyTorch 0.1.12_1 is one of the earliest releases, so I would strongly recommend to update to the latest stable release (1.5.1).

In 0.1.12, you had to pass a function to map_location via:

torch.load(..., map_location=lambda storage, location: 'cpu')