pytorch / pytorch

Tensors and Dynamic neural networks in Python with strong GPU acceleration
https://pytorch.org
Other
84.71k stars 22.81k forks source link

torch.load() -- UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y when loading a saved model #116355

Closed agcala closed 11 months ago

agcala commented 11 months ago

🐛 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.

import torch
import torch.nn as nn
import torch.optim as optim
.  .  .  .  .
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=1e-5)
device=torch.cuda.current_device()
for epoch in range(start_epoch,start_epoch+total_epochs): 
.  .  .  .  .  
    print('Saving model to: '+filename)
    torch.save({
        'epoch': epoch,
        'model_state_dict': model.state_dict(),
        'optimizer_state_dict': optimizer.state_dict(),
        'loss': loss
        },
        filename,
        _use_new_zipfile_serialization=False)
 .  .  .  .  
chkpoint = torch.load(filename)

(base) o:\horses>python program.py batch=16 model=Pytorch-5-2023-12-23-09-15.pth
Traceback (most recent call last):
File "program.py", line 85, in <module>
chkpoint = torch.load(chkpoint)
File "k:\anaconda\lib\site-packages\torch\serialization.py", line 987, in load
if _is_zipfile(opened_file):
File "k:\anaconda\lib\site-packages\torch\serialization.py", line 121, in _is_zipfile
byte = f.read(1)
File "k:\anaconda\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 1564: character maps to <undefined>

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

_use_new_zipfile_serialization=False

in save.model() cause I saw somewhere that this may help, but it doesn't

Versions

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

_ipyw_jlab_nb_ext_conf    0.1.0            py39haa95532_0  
absl-py                   2.0.0                    pypi_0    pypi  
aiohttp                   3.9.1                    pypi_0    pypi
aiosignal                 1.3.1                    pypi_0    pypi
alabaster                 0.7.12             pyhd3eb1b0_0  
anaconda                  2022.10                  py39_0  
anaconda-client           1.11.0           py39haa95532_0  
anaconda-navigator        2.3.1            py39haa95532_0  
anaconda-project          0.11.1           py39haa95532_0  
annotated-types           0.6.0                    pypi_0    pypi
antlr4-python3-runtime    4.9.3                    pypi_0    pypi
anyio                     3.5.0            py39haa95532_0  
appdirs                   1.4.4              pyhd3eb1b0_0  
argon2-cffi               21.3.0             pyhd3eb1b0_0  
argon2-cffi-bindings      21.2.0           py39h2bbff1b_0  
arrow                     1.2.2              pyhd3eb1b0_0  
asgiref                   3.7.2                    pypi_0    pypi
astroid                   2.11.7           py39haa95532_0  
astropy                   5.1              py39h080aedc_0  
astunparse                1.6.3                    pypi_0    pypi
async-timeout             4.0.3                    pypi_0    pypi
atomicwrites              1.4.0                      py_0  
attrs                     21.4.0             pyhd3eb1b0_0  
automat                   20.2.0                     py_0  
autopep8                  1.6.0              pyhd3eb1b0_1  
babel                     2.9.1              pyhd3eb1b0_0  
backcall                  0.2.0              pyhd3eb1b0_0  
backports                 1.1                pyhd3eb1b0_0  
backports.functools_lru_cache 1.6.4              pyhd3eb1b0_0  
backports.tempfile        1.0                pyhd3eb1b0_1  
backports.weakref         1.0.post1                  py_1  
bcrypt                    3.2.0            py39h2bbff1b_1  
beautifulsoup4            4.11.1           py39haa95532_0  
bentoml                   1.1.10                   pypi_0    pypi
vbinaryornot               0.4.4              pyhd3eb1b0_1  
bitarray                  2.5.1            py39h2bbff1b_0  
bkcharts                  0.2              py39haa95532_1  
black                     22.6.0           py39haa95532_0  
blas                      1.0                         mkl  
bleach                    4.1.0              pyhd3eb1b0_0  
blis                      0.7.11                   pypi_0    pypi
blosc                     1.21.0               h19a0ad4_1  
bokeh                     2.4.3            py39haa95532_0  
boto3                     1.24.28          py39haa95532_0  
botocore                  1.27.28          py39haa95532_0  
bottleneck                1.3.5            py39h080aedc_0  
brotli                    1.0.9                h2bbff1b_7  
brotli-bin                1.0.9                h2bbff1b_7  
brotlipy                  0.7.0           py39h2bbff1b_1003  
build                     1.0.3                    pypi_0    pypi
bzip2                     1.0.8                he774522_0  
ca-certificates           2022.07.19           haa95532_0  
cachetools                5.3.2                    pypi_0    pypi
catalogue                 2.0.10                   pypi_0    pypi
category-encoders         2.6.3                    pypi_0    pypi
cattrs                    23.1.2                   pypi_0    pypi
certifi                   2022.9.14        py39haa95532_0  
cffi                      1.15.1           py39h2bbff1b_0  
cfitsio                   3.470                h2bbff1b_7  
chardet                   4.0.0           py39haa95532_1003  
charls                    2.2.0                h6c2663c_0  
charset-normalizer        2.0.4              pyhd3eb1b0_0  
circus                    0.18.0                   pypi_0    pypi
click                     8.0.4            py39haa95532_0  
click-option-group        0.5.6                    pypi_0    pypi
cloudpathlib              0.16.0                   pypi_0    pypi
cloudpickle               2.0.0              pyhd3eb1b0_0  
clyent                    1.2.2            py39haa95532_1  
colorama                  0.4.6                    pypi_0    pypi
colorcet                  3.0.0            py39haa95532_0  
colorconsole              0.8.0                    pypi_0    pypi
comtypes                  1.1.10          py39haa95532_1002  
conda                     22.9.0           py39haa95532_0  
conda-build               3.22.0           py39haa95532_0  
conda-content-trust       0.1.3            py39haa95532_0  
conda-env                 2.6.0                haa95532_1  
conda-pack                0.6.0              pyhd3eb1b0_0  
conda-package-handling    1.9.0            py39h8cc25b3_0  
conda-repo-cli            1.0.20           py39haa95532_0  
conda-token               0.4.0              pyhd3eb1b0_0  
conda-verify              3.4.2                      py_1  
confection                0.1.4                    pypi_0    pypi
console_shortcut          0.1.1                         4  
constantly                15.1.0             pyh2b92418_0  
contextlib2               21.6.0                   pypi_0    pypi
cookiecutter              1.7.3              pyhd3eb1b0_0  
cryptography              37.0.1           py39h21b164f_0  
cssselect                 1.1.0              pyhd3eb1b0_0  
curl                      7.84.0               h2bbff1b_0  
cycler                    0.11.0             pyhd3eb1b0_0  
cymem                     2.0.8                    pypi_0    pypi
cython                    0.29.32          py39hd77b12b_0  
cytoolz                   0.11.0           py39h2bbff1b_0  
daal4py                   2021.6.0         py39h757b272_1  
dal                       2021.6.0           h59b6b97_874  
dask                      2022.7.0         py39haa95532_0  
dask-core                 2022.7.0         py39haa95532_0  
dataclasses               0.8                pyh6d0b6a4_7  
datashader                0.14.1           py39haa95532_0  
datashape                 0.5.4            py39haa95532_1  
debugpy                   1.5.1            py39hd77b12b_0  
decorator                 5.1.1              pyhd3eb1b0_0  
deepmerge                 1.1.0                    pypi_0    pypi
defusedxml                0.7.1              pyhd3eb1b0_0  
deprecated                1.2.14                   pypi_0    pypi
diff-match-patch          20200713           pyhd3eb1b0_0  
dill                      0.3.4              pyhd3eb1b0_0  
distributed               2022.7.0         py39haa95532_0  
docutils                  0.18.1           py39haa95532_3  
einops                    0.6.1                    pypi_0    pypi
entrypoints               0.4              py39haa95532_0  
et_xmlfile                1.1.0            py39haa95532_0  
exceptiongroup            1.2.0                    pypi_0    pypi
fastai                    2.7.13                   pypi_0    pypi
fastcore                  1.5.29                   pypi_0    pypi
fastdownload              0.0.7                    pypi_0    pypi
fastprogress              1.0.3                    pypi_0    pypi
fftw                      3.3.9                h2bbff1b_1  
filelock                  3.6.0              pyhd3eb1b0_0  
flake8                    4.0.1              pyhd3eb1b0_1  
flask                     1.1.2              pyhd3eb1b0_0  
flatbuffers               23.5.26                  pypi_0    pypi
fonttools                 4.25.0             pyhd3eb1b0_0  
freetype                  2.10.4               hd328e21_0  
frozenlist                1.4.1                    pypi_0    pypi
fs                        2.4.16                   pypi_0    pypi
fsspec                    2022.7.1         py39haa95532_0  
future                    0.18.2           py39haa95532_1  
gast                      0.4.0                    pypi_0    pypi
gensim                    4.1.2            py39hd77b12b_0  
giflib                    5.2.1                h62dcd97_0  
glob2                     0.7                pyhd3eb1b0_0  
google-auth               2.25.2                   pypi_0    pypi
google-auth-oauthlib      1.2.0                    pypi_0    pypi
google-pasta              0.2.0                    pypi_0    pypi
greenlet                  1.1.1            py39hd77b12b_0  
grpcio                    1.60.0                   pypi_0    pypi
h11                       0.14.0                   pypi_0    pypi
h5py                      3.7.0            py39h3de5c98_0  
hdf5                      1.10.6               h1756f20_1  
heapdict                  1.0.1              pyhd3eb1b0_0  
holoviews                 1.15.0           py39haa95532_0  
httpcore                  1.0.2                    pypi_0    pypi
httpx                     0.25.2                   pypi_0    pypi
hvplot                    0.8.0            py39haa95532_0  
hyperlink                 21.0.0             pyhd3eb1b0_0  
icc_rt                    2022.1.0             h6049295_2  
icu                       58.2                 ha925a31_3  
idna                      3.3                pyhd3eb1b0_0  
imagecodecs               2021.8.26        py39hc0a7faf_1  
imageio                   2.19.3           py39haa95532_0  
imagesize                 1.4.1            py39haa95532_0  
importlib-metadata        6.11.0                   pypi_0    pypi
importlib_metadata        4.11.3               hd3eb1b0_0  
incremental               21.3.0             pyhd3eb1b0_0  
inflection                0.5.1            py39haa95532_0  
iniconfig                 1.1.1              pyhd3eb1b0_0  
intake                    0.6.5              pyhd3eb1b0_0  
intel-openmp              2021.4.0          haa95532_3556  
intervaltree              3.1.0              pyhd3eb1b0_0  
ipykernel                 6.15.2           py39haa95532_0  
ipython                   7.31.1           py39haa95532_1  
ipython_genutils          0.2.0              pyhd3eb1b0_1  
ipywidgets                7.6.5              pyhd3eb1b0_1  
isort                     5.9.3              pyhd3eb1b0_0  
itemadapter               0.3.0              pyhd3eb1b0_0  
itemloaders               1.0.4              pyhd3eb1b0_1  
itsdangerous              2.0.1              pyhd3eb1b0_0  
jax                       0.4.23                   pypi_0    pypi
jaxlib                    0.4.23                   pypi_0    pypi
jdcal                     1.4.1              pyhd3eb1b0_0  
jedi                      0.18.1           py39haa95532_1  
jellyfish                 0.9.0            py39h2bbff1b_0  
jinja2                    3.1.2                    pypi_0    pypi
jinja2-time               0.2.0              pyhd3eb1b0_3  
jmespath                  0.10.0             pyhd3eb1b0_0  
joblib                    1.1.0              pyhd3eb1b0_0  
jpeg                      9e                   h2bbff1b_0  
jq                        1.6                  haa95532_1  
json5                     0.9.6              pyhd3eb1b0_0  
jsonschema                4.16.0           py39haa95532_0  
jupyter                   1.0.0            py39haa95532_8  
jupyter_client            7.3.4            py39haa95532_0  
jupyter_console           6.4.3              pyhd3eb1b0_0  
jupyter_core              4.11.1           py39haa95532_0  
jupyter_server            1.18.1           py39haa95532_0  
jupyterlab                3.4.4            py39haa95532_0  
jupyterlab_pygments       0.1.2                      py_0  
jupyterlab_server         2.10.3             pyhd3eb1b0_1  
jupyterlab_widgets        1.0.0              pyhd3eb1b0_1  
keras                     2.10.0                   pypi_0    pypi
keras-preprocessing       1.1.2                    pypi_0    pypi
keyring                   23.4.0           py39haa95532_0  
kiwisolver                1.4.2            py39hd77b12b_0  
langcodes                 3.3.0                    pypi_0    pypi
lazy-object-proxy         1.6.0            py39h2bbff1b_0  
lcms2                     2.12                 h83e58a3_0  
lerc                      3.0                  hd77b12b_0  
libaec                    1.0.4                h33f27b4_1  
libarchive                3.6.1                hebabd0d_0  
libbrotlicommon           1.0.9                h2bbff1b_7  
libbrotlidec              1.0.9                h2bbff1b_7  
libbrotlienc              1.0.9                h2bbff1b_7  
libclang                  16.0.6                   pypi_0    pypi
libcurl                   7.84.0               h86230a5_0  
libdeflate                1.8                  h2bbff1b_5  
libiconv                  1.16                 h2bbff1b_2  
liblief                   0.11.5               hd77b12b_1  
libpng                    1.6.37               h2a8f88b_0  
libsodium                 1.0.18               h62dcd97_0  
libspatialindex           1.9.3                h6c2663c_0  
libssh2                   1.10.0               hcd4344a_0  
libtiff                   4.4.0                h8a3f274_0  
libwebp                   1.2.2                h2bbff1b_0  
libxml2                   2.9.14               h0ad7f3c_0  
libxslt                   1.1.35               h2bbff1b_0  
libzopfli                 1.0.3                ha925a31_0  
lightning                 2.1.2                    pypi_0    pypi
lightning-utilities       0.10.0                   pypi_0    pypi
llvmlite                  0.38.0           py39h23ce68f_0  
locket                    1.0.0            py39haa95532_0  
lxml                      4.9.1            py39h1985fb9_0  
lz4                       3.1.3            py39h2bbff1b_0  
lz4-c                     1.9.3                h2bbff1b_1  
lzo                       2.10                 he774522_2  
m2-msys2-runtime          2.5.0.17080.65c939c               3  
m2-patch                  2.7.5                         2  
m2w64-libwinpthread-git   5.0.0.4634.697f757               2  
markdown                  3.3.4            py39haa95532_0  
markdown-it-py            3.0.0                    pypi_0    pypi
markupsafe                2.0.1            py39h2bbff1b_0  
matplotlib                3.5.2            py39haa95532_0  
matplotlib-base           3.5.2            py39hd77b12b_0  
matplotlib-inline         0.1.6            py39haa95532_0  
mccabe                    0.6.1            py39haa95532_2  
mdurl                     0.1.2                    pypi_0    pypi
menuinst                  1.4.19           py39h59b6b97_0  
mistune                   0.8.4           py39h2bbff1b_1000  
mkl                       2021.4.0           haa95532_640  
mkl-service               2.4.0            py39h2bbff1b_0  
mkl_fft                   1.3.1            py39h277e83a_0  
mkl_random                1.2.2            py39hf11a4ad_0  
ml-dtypes                 0.3.1                    pypi_0    pypi
mock                      4.0.3              pyhd3eb1b0_0  
mpmath                    1.2.1            py39haa95532_0  
msgpack-python            1.0.3            py39h59b6b97_0  
msys2-conda-epoch         20160418                      1  
multidict                 6.0.4                    pypi_0    pypi
multipledispatch          0.6.0            py39haa95532_0  
munkres                   1.1.4                      py_0  
murmurhash                1.0.10                   pypi_0    pypi
mypy_extensions           0.4.3            py39haa95532_1  
navigator-updater         0.3.0            py39haa95532_0  
nbclassic                 0.3.5              pyhd3eb1b0_0  
nbclient                  0.5.13           py39haa95532_0  
nbconvert                 6.4.4            py39haa95532_0  
nbformat                  5.5.0            py39haa95532_0  
nest-asyncio              1.5.5            py39haa95532_0  
networkx                  2.8.4            py39haa95532_0  
nltk                      3.7                pyhd3eb1b0_0  
nose                      1.3.7           pyhd3eb1b0_1008  
notebook                  6.4.12           py39haa95532_0  
numba                     0.55.1           py39hf11a4ad_0  
numexpr                   2.8.3            py39hb80d3ca_0  
numpy                     1.24.4                   pypi_0    pypi
numpydoc                  1.4.0            py39haa95532_0  
nvidia-ml-py              11.525.150               pypi_0    pypi
oauthlib                  3.2.2                    pypi_0    pypi
olefile                   0.46               pyhd3eb1b0_0  
omegaconf                 2.3.0                    pypi_0    pypi
openjpeg                  2.4.0                h4fc8c34_0  
openpyxl                  3.0.10           py39h2bbff1b_0  
openssl                   1.1.1q               h2bbff1b_0  
opentelemetry-api         1.20.0                   pypi_0    pypi
opentelemetry-instrumentation 0.41b0                   pypi_0    pypi
opentelemetry-instrumentation-aiohttp-client 0.41b0                   pypi_0    pypi
opentelemetry-instrumentation-asgi 0.41b0                   pypi_0    pypi
opentelemetry-sdk         1.20.0                   pypi_0    pypi
opentelemetry-semantic-conventions 0.41b0                   pypi_0    pypi
opentelemetry-util-http   0.41b0                   pypi_0    pypi
opt-einsum                3.3.0                    pypi_0    pypi
outcome                   1.3.0.post0              pypi_0    pypi
packaging                 23.2                     pypi_0    pypi
pandas                    1.4.4            py39hd77b12b_0  
pandocfilters             1.5.0              pyhd3eb1b0_0  
panel                     0.13.1           py39haa95532_0  
param                     1.12.0             pyhd3eb1b0_0  
paramiko                  2.8.1              pyhd3eb1b0_0  
parsel                    1.6.0            py39haa95532_0  
parso                     0.8.3              pyhd3eb1b0_0  
partd                     1.2.0              pyhd3eb1b0_1  
pathlib                   1.0.1              pyhd3eb1b0_1  
pathspec                  0.9.0            py39haa95532_0  
patsy                     0.5.2            py39haa95532_1  
pep8                      1.7.1            py39haa95532_1  
pexpect                   4.8.0              pyhd3eb1b0_3  
pickleshare               0.7.5           pyhd3eb1b0_1003  
pillow                    9.2.0            py39hdc2b20a_1  
pip                       23.3.2                   pypi_0    pypi
pip-requirements-parser   32.0.1                   pypi_0    pypi
pip-tools                 7.3.0                    pypi_0    pypi
pkginfo                   1.8.2              pyhd3eb1b0_0  
platformdirs              2.5.2            py39haa95532_0  
plotly                    5.9.0            py39haa95532_0  
pluggy                    1.0.0            py39haa95532_1  
powershell_shortcut       0.0.1                         3  
poyo                      0.5.0              pyhd3eb1b0_0  
preshed                   3.0.9                    pypi_0    pypi
prometheus_client         0.14.1           py39haa95532_0  
prompt-toolkit            3.0.20             pyhd3eb1b0_0  
prompt_toolkit            3.0.20               hd3eb1b0_0  
protego                   0.1.16                     py_0  
protobuf                  3.19.6                   pypi_0    pypi
psutil                    5.9.0            py39h2bbff1b_0  
ptyprocess                0.7.0              pyhd3eb1b0_2  
py                        1.11.0             pyhd3eb1b0_0  
py-lief                   0.11.5           py39hd77b12b_1  
pyasn1                    0.4.8              pyhd3eb1b0_0  
pyasn1-modules            0.2.8                      py_0  
pycodestyle               2.8.0              pyhd3eb1b0_0  
pycosat                   0.6.3            py39h2bbff1b_0  
pycparser                 2.21               pyhd3eb1b0_0  
pyct                      0.4.8            py39haa95532_1  
pycurl                    7.45.1           py39hcd4344a_0  
pydantic                  2.5.2                    pypi_0    pypi
pydantic-core             2.14.5                   pypi_0    pypi
pydispatcher              2.0.5            py39haa95532_2  
pydocstyle                6.1.1              pyhd3eb1b0_0  
pyerfa                    2.0.0            py39h2bbff1b_0  
pyflakes                  2.4.0              pyhd3eb1b0_0  
pygments                  2.17.2                   pypi_0    pypi
pyhamcrest                2.0.2              pyhd3eb1b0_2  
pyjwt                     2.4.0            py39haa95532_0  
pylint                    2.14.5           py39haa95532_0  
pyls-spyder               0.4.0              pyhd3eb1b0_0  
pynacl                    1.5.0            py39h8cc25b3_0  
pyodbc                    4.0.34           py39hd77b12b_0  
pyopenssl                 22.0.0             pyhd3eb1b0_0  
pyparsing                 3.0.9            py39haa95532_0  
pyproject-hooks           1.0.0                    pypi_0    pypi
pyqt                      5.9.2            py39hd77b12b_6  
pyqt5                     5.15.10                  pypi_0    pypi
pyqt5-qt5                 5.15.2                   pypi_0    pypi
pyqt5-sip                 12.13.0                  pypi_0    pypi
pyqtwebengine             5.15.6                   pypi_0    pypi
pyqtwebengine-qt5         5.15.2                   pypi_0    pypi
pyro-api                  0.1.2                    pypi_0    pypi
pyro-ppl                  1.8.6                    pypi_0    pypi
pyrsistent                0.18.0           py39h196d8e1_0  
pysocks                   1.7.1            py39haa95532_0  
pytables                  3.6.1            py39h56d22b6_1  
pytest                    7.1.2            py39haa95532_0  
python                    3.9.13               h6244533_1  
python-dateutil           2.8.2              pyhd3eb1b0_0  
python-fastjsonschema     2.16.2           py39haa95532_0  
python-json-logger        2.0.7                    pypi_0    pypi
python-libarchive-c       2.9                pyhd3eb1b0_1  
python-lsp-black          1.3.0                    pypi_0    pypi
python-lsp-jsonrpc        1.0.0              pyhd3eb1b0_0  
python-lsp-server         1.5.0                    pypi_0    pypi
python-multipart          0.0.6                    pypi_0    pypi
python-slugify            5.0.2              pyhd3eb1b0_0  
python-snappy             0.6.0            py39hd77b12b_3  
pytorch-lightning         1.9.5                    pypi_0    pypi
pytorch-tabnet            4.0                      pypi_0    pypi
pytz                      2022.1           py39haa95532_0  
pyviz_comms               2.0.2              pyhd3eb1b0_0  
pywavelets                1.3.0            py39h2bbff1b_0  
pywin32                   302              py39h2bbff1b_2  
pywin32-ctypes            0.2.0           py39haa95532_1000  
pywinpty                  2.0.2            py39h5da7b33_0  
pyyaml                    6.0              py39h2bbff1b_1  
pyzmq                     23.2.0           py39hd77b12b_0  
qdarkstyle                3.0.2              pyhd3eb1b0_0  
qstylizer                 0.1.10             pyhd3eb1b0_0  
qt                        5.9.7            vc14h73c81de_0  
qtawesome                 1.0.3              pyhd3eb1b0_0  
qtconsole                 5.3.2                    pypi_0    pypi
qtpy                      2.2.0            py39haa95532_0  
queuelib                  1.5.0            py39haa95532_0  
regex                     2022.7.9         py39h2bbff1b_0  
requests                  2.28.1           py39haa95532_0  
requests-file             1.5.1              pyhd3eb1b0_0  
requests-oauthlib         1.3.1                    pypi_0    pypi
rich                      13.7.0                   pypi_0    pypi
rope                      0.22.0             pyhd3eb1b0_0  
rsa                       4.9                      pypi_0    pypi
rtree                     0.9.7            py39h2eaa2aa_1  
ruamel_yaml               0.15.100         py39h2bbff1b_0  
s3transfer                0.6.0            py39haa95532_0  
schema                    0.7.5                    pypi_0    pypi
scikit-image              0.19.2           py39hf11a4ad_0  
scikit-learn              1.0.2            py39hf11a4ad_1  
scikit-learn-intelex      2021.6.0         py39haa95532_0  
scipy                     1.9.1            py39he11b74f_0  
scrapy                    2.8.0                    pypi_0    pypi
seaborn                   0.11.2             pyhd3eb1b0_0  
selenium                  4.16.0                   pypi_0    pypi
send2trash                1.8.0              pyhd3eb1b0_1  
service_identity          18.1.0             pyhd3eb1b0_1  
setuptools                63.4.1           py39haa95532_0  
simple-di                 0.1.5                    pypi_0    pypi
sip                       4.19.13          py39hd77b12b_0  
six                       1.16.0             pyhd3eb1b0_1  
smart_open                5.2.1            py39haa95532_0  
snappy                    1.1.9                h6c2663c_0  
sniffio                   1.3.0                    pypi_0    pypi
snowballstemmer           2.2.0              pyhd3eb1b0_0  
sortedcollections         2.1.0              pyhd3eb1b0_0  
sortedcontainers          2.4.0              pyhd3eb1b0_0  
soupsieve                 2.3.1              pyhd3eb1b0_0  
spacy                     3.7.2                    pypi_0    pypi
spacy-legacy              3.0.12                   pypi_0    pypi
spacy-loggers             1.0.5                    pypi_0    pypi
sphinx                    5.0.2            py39haa95532_0  
sphinxcontrib-applehelp   1.0.2              pyhd3eb1b0_0  
sphinxcontrib-devhelp     1.0.2              pyhd3eb1b0_0  
sphinxcontrib-htmlhelp    2.0.0              pyhd3eb1b0_0  
sphinxcontrib-jsmath      1.0.1              pyhd3eb1b0_0  
sphinxcontrib-qthelp      1.0.3              pyhd3eb1b0_0  
sphinxcontrib-serializinghtml 1.1.5              pyhd3eb1b0_0  
spyder                    5.3.3                    pypi_0    pypi
spyder-kernels            2.3.3                    pypi_0    pypi
sqlalchemy                1.4.39           py39h2bbff1b_0  
sqlite                    3.39.3               h2bbff1b_0  
srsly                     2.4.8                    pypi_0    pypi
starlette                 0.34.0                   pypi_0    pypi
statsmodels               0.13.2           py39h2bbff1b_0  
sympy                     1.10.1           py39haa95532_0  
tabulate                  0.8.10           py39haa95532_0  
tbb                       2021.6.0             h59b6b97_0  
tbb4py                    2021.6.0         py39h59b6b97_0  
tblib                     1.7.0              pyhd3eb1b0_0  
tenacity                  8.0.1            py39haa95532_1  
tensorboard               2.15.1                   pypi_0    pypi
tensorboard-data-server   0.7.2                    pypi_0    pypi
tensorboard-plugin-wit    1.8.1                    pypi_0    pypi
tensorflow                2.10.0                   pypi_0    pypi
tensorflow-estimator      2.10.0                   pypi_0    pypi
tensorflow-io-gcs-filesystem 0.31.0                   pypi_0    pypi
termcolor                 2.4.0                    pypi_0    pypi
terminado                 0.13.1           py39haa95532_0  
testpath                  0.6.0            py39haa95532_0  
text-unidecode            1.3                pyhd3eb1b0_0  
textdistance              4.2.1              pyhd3eb1b0_0  
tflearn                   0.5.0                    pypi_0    pypi
thinc                     8.2.1                    pypi_0    pypi
threadpoolctl             2.2.0              pyh0d69192_0  
three-merge               0.1.1              pyhd3eb1b0_0  
tifffile                  2021.7.2           pyhd3eb1b0_2  
tinycss                   0.4             pyhd3eb1b0_1002  
tk                        8.6.12               h2bbff1b_0  
tldextract                3.2.0              pyhd3eb1b0_0  
toml                      0.10.2             pyhd3eb1b0_0  
tomli                     2.0.1            py39haa95532_0  
tomlkit                   0.11.1           py39haa95532_0  
toolz                     0.11.2             pyhd3eb1b0_0  
torch                     2.1.2+cu118              pypi_0    pypi
torchaudio                2.1.2+cu118              pypi_0    pypi
torchmetrics              0.11.4                   pypi_0    pypi
torchvision               0.16.2+cu118             pypi_0    pypi
tornado                   6.1              py39h2bbff1b_0  
tqdm                      4.64.1           py39haa95532_0  
traitlets                 5.1.1              pyhd3eb1b0_0  
trio                      0.23.2                   pypi_0    pypi
trio-websocket            0.11.1                   pypi_0    pypi
twisted                   22.2.0           py39h2bbff1b_1  
twisted-iocpsupport       1.0.2            py39h2bbff1b_0  
typer                     0.9.0                    pypi_0    pypi
typing-extensions         4.9.0                    pypi_0    pypi
tzdata                    2022c                h04d1e81_0  
ujson                     5.4.0            py39hd77b12b_0  
unidecode                 1.2.0              pyhd3eb1b0_0  
urllib3                   1.26.11          py39haa95532_0  
uvicorn                   0.24.0.post1             pypi_0    pypi
vc                        14.2                 h21ff451_1  
vs2015_runtime            14.27.29016          h5e58377_2  
w3lib                     1.21.0             pyhd3eb1b0_0  
wasabi                    1.1.2                    pypi_0    pypi
watchdog                  2.1.6            py39haa95532_0  
watchfiles                0.21.0                   pypi_0    pypi
wcwidth                   0.2.5              pyhd3eb1b0_0  
weasel                    0.3.4                    pypi_0    pypi
webencodings              0.5.1            py39haa95532_1  
websocket-client          0.58.0           py39haa95532_4  
werkzeug                  2.0.3              pyhd3eb1b0_0  
whatthepatch              1.0.5                    pypi_0    pypi
wheel                     0.37.1             pyhd3eb1b0_0  
widgetsnbextension        3.5.2            py39haa95532_0  
win_inet_pton             1.1.0            py39haa95532_0  
win_unicode_console       0.5              py39haa95532_0  
wincertstore              0.2              py39haa95532_2  
windows-curses            2.3.2                    pypi_0    pypi
winpty                    0.4.3                         4  
wrapt                     1.14.1           py39h2bbff1b_0  
wsproto                   1.2.0                    pypi_0    pypi
xarray                    0.20.1             pyhd3eb1b0_1  
xlrd                      2.0.1              pyhd3eb1b0_0  
xlsxwriter                3.0.3              pyhd3eb1b0_0  
xlwings                   0.27.15          py39haa95532_0  
xz                        5.2.6                h8cc25b3_0  
yaml                      0.2.5                he774522_0  
yapf                      0.31.0             pyhd3eb1b0_0  
yarl                      1.9.4                    pypi_0    pypi
zeromq                    4.3.4                hd77b12b_0  
zfp                       0.5.5                hd77b12b_6  
zict                      2.1.0            py39haa95532_0  
zipp                      3.8.0            py39haa95532_0  
zlib                      1.2.12               h8cc25b3_3  
zope                      1.0              py39haa95532_1  
zope.interface            5.4.0            py39h2bbff1b_0  
zstd                      1.5.2                h19a0ad4_0  
Lilneo786 commented 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.

agcala commented 11 months ago

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'])