Closed agcala closed 11 months ago
The issue you are facing, UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y, commonly occurs when Python attempts to read a file using a character encoding (like 'cp1252' in your situation) that does not support specific bytes in the file. This problem usually arises when you try to open a binary file in text mode.
1. The problem in your code probably arises when attempting to load the saved model checkpoint using torch.load(filename). To resolve this problem, ensure that you open the file in binary mode when saving and loading the checkpoint, as shown below:
# When saving the checkpoint
torch.save({
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': loss
}, filename, _use_new_zipfile_serialization=False)
# When loading the checkpoint
chkpoint = torch.load(filename, map_location=torch.device('cpu'))
By using map_location=torch.device('cpu'), you guarantee that the model is loaded onto the CPU. This can be advantageous when saving the model on one machine or GPU and loading it on another, ultimately resolving any UnicodeDecodeError problems.
map_location=torch.device('cpu')
Made those changes and seems to be okay now thanks 👍
with open(chkpoint.name,'rb') as f:
chkpoint = torch.load(f, map_location=torch.device(device))
start_epoch = chkpoint['epoch']
model.load_state_dict(chkpoint['model_state_dict'])
optimizer.load_state_dict(chkpoint['optimizer_state_dict'])
🐛 Describe the bug
When trying to load back a previously saved model I get this error. I have searched previously similar errors but I cannot find a suitable answer.
Curiously when I load the model on the Spyder Ipython platform there is no such error. It only gives error when running the script. I use
in
save.model()
cause I saw somewhere that this may help, but it doesn'tVersions
These are the installed packages:
Windows 10 Pro Processor Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz Istalled RAM 16.0 Gb (15.9 Gb usable) 64-bit operating system, x64-based processor NVIDIA T400 4GB