# Importing .h5 model using keras load\_model

**URL:** <https://discourse.slicer.org/t/importing-h5-model-using-keras-load-model/24103>\
**Category:** Development\
**Tags:** segmentation, python\
**Created:** [June 29, 2022, 12:53pm UTC](https://discourse.slicer.org/t/importing-h5-model-using-keras-load-model/24103 "2022-06-29T12:53:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zhenchen](https://avatars.discourse-cdn.com/v4/letter/z/ac8455/32.png) [@Zhenchen](https://discourse.slicer.org/u/Zhenchen)\
**Post date:** [June 29, 2022, 12:53pm UTC](https://discourse.slicer.org/t/importing-h5-model-using-keras-load-model/24103/1 "2022-06-29T12:53:23Z")

</div>

Hello everyone,

I am currently building a module for image segmentation using a Unet model that has already been trained. When I test the module, the execution blocks when calling the function **tensorflow.keras.load\_model()** and Slicer does not respond. Unfortunately, there is no error message.

When I try to load the model using the same function in pyCharm with the Python Interpreter being that of Slicer, everything goes well. I also checked the environment variables, it turns out that PYTHONHOME and PYTHONPATH are both the same when running the code inside or outside Slicer.

Has someone had the same problem? How can I solve it?

Thanks a lot  
Zhenchen

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [June 29, 2022, 1:27pm UTC](https://discourse.slicer.org/t/importing-h5-model-using-keras-load-model/24103/2 "2022-06-29T13:27:52Z")

</div>

Hard to say exactly, but if you’ve done anything that changes the environment variables or paths or tries to mix-and-match different python libs from different distributions that could cause this kind of behavior.

Tensorflow has always worked for me, including loading models, by using `pip_install("tensorflow")` in Slicer’s python. Probably start fresh with a new slicer install report what you did step-by-step.

---

<div class="post-metadata">

**Author:** ![Zhenchen](https://avatars.discourse-cdn.com/v4/letter/z/ac8455/32.png) [@Zhenchen](https://discourse.slicer.org/u/Zhenchen)\
**Post date:** [June 30, 2022, 9:24pm UTC](https://discourse.slicer.org/t/importing-h5-model-using-keras-load-model/24103/3 "2022-06-30T21:24:59Z")

</div>

Thank you very much for your response Steve.

In fact, I have tried what you suggested. Firstly, I deleted slicer and installed a new one. After that, I pasted my code about loading of the model into the script of a new Slicer module. Then, I installed all the necessary packages (tensorflow, cv2, tqdm, skimage, etc.) using the command ` pip_install("...")` in Slicer’s python interactor. Finally, I tested the new module but the problem still exists. The execution blocked at the step of loading of the model by calling the function ` tensorflow.keras.models.load_model()`.

Besides, I tried to debug with Slicer’s plug-in Debugging tools and PyCharm. In fact, the information “no frame is available” was shown when I tried to step into `model_config = f.attrs.get('model_config')` in the file _hdf5\_format.py_. I hope this information could help you…and thanks again for your help.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/b/1bebc05b6298300cfd7f8ff0b2f4b728d4acbf51.jpeg)

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [June 30, 2022, 10:36pm UTC](https://discourse.slicer.org/t/importing-h5-model-using-keras-load-model/24103/4 "2022-06-30T22:36:57Z")

</div>

My guess would be an incompatible hdf5 library being pulled in by one of the pip packages. You wouldn’t be able to debug that with pycharm but there are system tools ([such as sysinternals](https://docs.microsoft.com/en-us/sysinternals/) that can help you debug. If nothing works maybe you could share a reproducible example, e.g. a model file and the script the script that does the install and load that hangs, then maybe someone can trace what’s happening.

---

<div class="post-metadata">

**Author:** ![Zhenchen](https://avatars.discourse-cdn.com/v4/letter/z/ac8455/32.png) [@Zhenchen](https://discourse.slicer.org/u/Zhenchen)\
**Post date:** [July 4, 2022, 11:09am UTC](https://discourse.slicer.org/t/importing-h5-model-using-keras-load-model/24103/5 "2022-07-04T11:09:46Z")

</div>

Here is the link to my model and the script to load it. Hope someone could help.

> **[load\_model.zip](https://drive.google.com/file/d/1StkldO35vvPAq7JrsLKlzoulFepznkbO/view?usp=sharing)**
>
> Google Drive file.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 4, 2022, 12:14pm UTC](https://discourse.slicer.org/t/importing-h5-model-using-keras-load-model/24103/6 "2022-07-04T12:14:56Z")

</div>

HDF5 is a very troublesome library, there are problems with it all the time due to binary incompatibity issues. One solution is to [save your model number in the recommended SavedModel file saving format](https://www.tensorflow.org/guide/keras/save_and_serialize) instead of the problematic legacy HDF5 format (referred to as H5 in Keras documentation).

---

<div class="post-metadata">

**Author:** ![Zhenchen](https://avatars.discourse-cdn.com/v4/letter/z/ac8455/32.png) [@Zhenchen](https://discourse.slicer.org/u/Zhenchen)\
**Post date:** [July 5, 2022, 8:58am UTC](https://discourse.slicer.org/t/importing-h5-model-using-keras-load-model/24103/7 "2022-07-05T08:58:41Z")

</div>

Thank you very much, it works now.
