pytorch-deepdream
pytorch-deepdream copied to clipboard
Error loading Resnet50 pretrained on places_365 on a CPU
Hello! Firstly, let me just thank you for this great repo. Its the clearest practical explanation of Deep Dream I've found online!
The issue: When I try to run the following command (on a CPU-only machine)
python deepdream.py model_name RESNET50 --pretrained_weights PLACES_365 --layers_to_use layer3
I get the following error
Dreaming started!
Traceback (most recent call last):
File "C:/Users/Asus/Desktop/My stuff/DeepDream NFT/pytorch-deepdream/deepdream.py", line 235, in <module>
img = deep_dream_static_image(config, img=None) # img=None -> will be loaded inside of deep_dream_static_image
File "C:/Users/Asus/Desktop/My stuff/DeepDream NFT/pytorch-deepdream/deepdream.py", line 69, in deep_dream_static_image
model = utils.fetch_and_prepare_model(config['model_name'], config['pretrained_weights'], DEVICE)
File "C:\Users\Asus\Desktop\My stuff\DeepDream NFT\pytorch-deepdream\utils\utils.py", line 132, in fetch_and_prepare_model
model = ResNet50(pretrained_weights, requires_grad=False, show_progress=True).to(device)
File "C:\Users\Asus\Desktop\My stuff\DeepDream NFT\pytorch-deepdream\models\definitions\resnets.py", line 28, in __init__
state_dict = torch.load(resnet50_places365_binary_path)['state_dict']
File "C:\Users\Asus\Desktop\My stuff\DeepDream NFT\DeepDream_venv\lib\site-packages\torch\serialization.py", line 713, in load
return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
File "C:\Users\Asus\Desktop\My stuff\DeepDream NFT\DeepDream_venv\lib\site-packages\torch\serialization.py", line 930, in _legacy_load
result = unpickler.load()
File "C:\Users\Asus\Desktop\My stuff\DeepDream NFT\DeepDream_venv\lib\site-packages\torch\serialization.py", line 876, in persistent_load
wrap_storage=restore_location(obj, location),
File "C:\Users\Asus\Desktop\My stuff\DeepDream NFT\DeepDream_venv\lib\site-packages\torch\serialization.py", line 176, in default_restore_location
result = fn(storage, location)
File "C:\Users\Asus\Desktop\My stuff\DeepDream NFT\DeepDream_venv\lib\site-packages\torch\serialization.py", line 152, in _cuda_deserialize
device = validate_cuda_device(location)
File "C:\Users\Asus\Desktop\My stuff\DeepDream NFT\DeepDream_venv\lib\site-packages\torch\serialization.py", line 136, 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.
After looking into it, I think the issue may be that the saved model contains GPU tensors. This would make torch.load load those tensors to GPU by default, which causes the CUDA error.
Adding the argument map_location='cpu'
to torch.load
fixes the issue. I can create a pull request with this fix if you agree with it.