# UnicodeDecodeError in load\_model

**URL:** <https://discuss.ai.google.dev/t/unicodedecodeerror-in-load-model/23889>\
**Category:** General Discussion\
**Tags:** keras, models\
**Created:** [May 3, 2023, 2:07am UTC](https://discuss.ai.google.dev/t/unicodedecodeerror-in-load-model/23889 "2023-05-03T02:07:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MURILO\_ARAUJO\_SOUZA](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/murilo_araujo_souza/32/18128_2.png) [@MURILO\_ARAUJO\_SOUZA](https://discuss.ai.google.dev/u/MURILO_ARAUJO_SOUZA)\
**Post date:** [May 3, 2023, 2:07am UTC](https://discuss.ai.google.dev/t/unicodedecodeerror-in-load-model/23889/1 "2023-05-03T02:07:16Z")

</div>

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

But when trying to load it:

```auto
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:

```auto
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).

---

<div class="post-metadata">

**Author:** ![chunduriv](https://avatars.discourse-cdn.com/v4/letter/c/bbe5ce/32.png) [@chunduriv](https://discuss.ai.google.dev/u/chunduriv)\
**Post date:** [May 3, 2023, 7:25am UTC](https://discuss.ai.google.dev/t/unicodedecodeerror-in-load-model/23889/3 "2023-05-03T07:25:37Z")

</div>

@MURILO_ARAUJO_SOUZA,

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?

```auto
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!

---

<div class="post-metadata">

**Author:** ![MURILO\_ARAUJO\_SOUZA](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/murilo_araujo_souza/32/18128_2.png) [@MURILO\_ARAUJO\_SOUZA](https://discuss.ai.google.dev/u/MURILO_ARAUJO_SOUZA)\
**Post date:** [May 3, 2023, 12:06pm UTC](https://discuss.ai.google.dev/t/unicodedecodeerror-in-load-model/23889/4 "2023-05-03T12:06:29Z")

</div>

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.

```auto
cp = ModelCheckpoint(filepath = dir_raiz + modelo + '_MLP_dM1_' + posto + '.hdf5',
monitor = 'val_loss',
verbose=1,
save_best_only=True,
mode=min)

```

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.

---

<div class="post-metadata">

**Author:** ![chunduriv](https://avatars.discourse-cdn.com/v4/letter/c/bbe5ce/32.png) [@chunduriv](https://discuss.ai.google.dev/u/chunduriv)\
**Post date:** [May 5, 2023, 6:13am UTC](https://discuss.ai.google.dev/t/unicodedecodeerror-in-load-model/23889/6 "2023-05-05T06:13:10Z")

</div>

@MURILO_ARAUJO_SOUZA,

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!

---

<div class="post-metadata">

**Author:** ![MURILO\_ARAUJO\_SOUZA](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/murilo_araujo_souza/32/18128_2.png) [@MURILO\_ARAUJO\_SOUZA](https://discuss.ai.google.dev/u/MURILO_ARAUJO_SOUZA)\
**Post date:** [May 8, 2023, 12:46pm UTC](https://discuss.ai.google.dev/t/unicodedecodeerror-in-load-model/23889/8 "2023-05-08T12:46:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hassan\_iamut\_said](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/hassan_iamut_said/32/17225_2.png) [@hassan\_iamut\_said](https://discuss.ai.google.dev/u/hassan_iamut_said)\
**Post date:** [October 11, 2023, 4:23am UTC](https://discuss.ai.google.dev/t/unicodedecodeerror-in-load-model/23889/10 "2023-10-11T04:23:26Z")

</div>

did you get it? I am fcing the same problem

---

<div class="post-metadata">

**Author:** ![MURILO\_ARAUJO\_SOUZA](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/murilo_araujo_souza/32/18128_2.png) [@MURILO\_ARAUJO\_SOUZA](https://discuss.ai.google.dev/u/MURILO_ARAUJO_SOUZA)\
**Post date:** [October 11, 2023, 10:41am UTC](https://discuss.ai.google.dev/t/unicodedecodeerror-in-load-model/23889/12 "2023-10-11T10:41:17Z")

</div>

Sorry, at this point i don’t remember exactly. But i guess i ended giving up and retrained my model.

---

<div class="post-metadata">

**Author:** ![Ti\_n\_Tr\_n\_H\_ng](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/ti_n_tr_n_h_ng/32/7198_2.png) [@Ti\_n\_Tr\_n\_H\_ng](https://discuss.ai.google.dev/u/Ti_n_Tr_n_H_ng)\
**Post date:** [December 13, 2023, 8:35am UTC](https://discuss.ai.google.dev/t/unicodedecodeerror-in-load-model/23889/14 "2023-12-13T08:35:17Z")

</div>

I have solved it. I changed the path of the folder. Maybe your path has special characters.  
For example:  
C:\Users\HP\Documents\AI\Weekly Report Slide\Group 2\CNN-Demo-main\CNN\CNN  
=\> C:\Users\HP\Documents\AI\Weekly\_Report\_Slide\Group 2\CNN-Demo-main\CNN\CNN
