0

I have trained the ResNet152 on a custom dataset. When I try to load it this way:

trained_model = torch.nn.Module.load_state_dict(torch.load('/content/drive/My Drive/X-Ray-pneumonia-with-CV/X-ray-pytorch-model.pth'))
trained_model.eval()

i got an error: 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.

And when I add map_location:

trained_model = torch.nn.Module.load_state_dict(torch.load('/content/drive/My Drive/X-Ray-pneumonia-with-CV/X-ray-pytorch-model.pth',
                                                           map_location = torch.device('cpu')))
trained_model.eval()

I got another error: TypeError: load_state_dict() missing 1 required positional argument: 'state_dict'

So what did I do wrong? Please, help

1 Answers1

0

Instead of invoking torch.nn.Module.load_state_dict you should first instantiate an object of module class you want to load. Otherwise, argument self of load_state_dict is not bound to anything. This way, state dict that you load via torch.load is being passed as self instead of state_dict. Have a look at this answer to understand the difference.

Proko
  • 1,588
  • 1
  • 9
  • 20