UnicodeDecodeError in load_model

I have trained a model just fine and saved it into a .hdf5 file.

But when trying to load it:

dir_raiz = 'C:/Ronaldo/Scripts/Vazões/Vazões/TOCANTINS/TUCURUI/MLP/'
posto = 'TUCURUI' 
modelo = 'Mod01'
model = load_model(dir_raiz + modelo + '_MLP_dM1_' + posto + '.hdf5')

I get the error:

Traceback (most recent call last):

  File C:\ProgramData\anaconda3\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\ronaldo\scripts\vazões\vazões\tocantins\tucurui\mlp\dm1\carrega_elia_mod01_dm1.py:61
    model = load_model(dir_raiz + modelo + 'MLP_dM1' + posto + '.hdf5')

  File ~\AppData\Roaming\Python\Python310\site-packages\keras\saving\saving_api.py:212 in load_model
    return legacy_sm_saving_lib.load_model(

  File ~\AppData\Roaming\Python\Python310\site-packages\keras\utils\traceback_utils.py:70 in error_handler
    raise e.with_traceback(filtered_tb) from None

  File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\lib\io\file_io.py:703 in is_directory_v2
    return _pywrap_file_io.IsDirectory(compat.path_to_bytes(path))

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf5 in position 26: invalid start byte

Any ideas in how to fix it?

I used the same script in 7 different computers, and only in 2 of them this is happening (all of them creates and model, and 2 of them fail to load it).


Welcome to the Tensorflow Forum!

The file path contains non-ascii characters that cannot be decoded using the utf-8 code.

Can you try to fix this issue is to use the os module to join the directory and file name instead of concatenating them with the + operator?

import os
from tensorflow.keras.models import load_model

dir_raiz = 'C:/Ronaldo/Scripts/Vazões/Vazões/TOCANTINS/TUCURUI/MLP/'
posto = 'TUCURUI'
modelo = 'Mod01'
filename = modelo + '_MLP_dM1_' + posto + '.hdf5'
filepath = os.path.join(dir_raiz, filename)
model = load_model(filepath)

Thank you!

Hello @chunduriv

That didn’t work out. The same error happened. To save the model, i have been using ModelCheckpoint and it worked fine, even with the non-ascii characters in the file path.

cp = ModelCheckpoint(filepath = dir_raiz + modelo + '_MLP_dM1_' + posto + '.hdf5',
monitor = 'val_loss',

And like i said, i can load the model in some computers and in others i can’t. All of them have non-ascii characters in the file path.


When reading the file using load_model, can you specify the encoding explicitly by adding the encoding parameter as shown below

model = load_model(dir_raiz + modelo + 'MLP_dM1' + posto + '.hdf5', encoding='latin1')

i can load the model in some computers and in others i can’t.

if you are able to load the model successfully on some computers but not on others, it’s possible that there may be differences in the environment or dependencies that are causing the issue.

Thank you!

Hello again @chunduriv

I am getting the error:

TypeError: load_model() got an unexpected keyword argument 'encoding'.

What could be the reason that i can train and save the model in a computer, but can’t load at the same computer? It is the same environment and dependecies.