# Loading a deep learning model to a scripted module

**URL:** <https://discourse.slicer.org/t/loading-a-deep-learning-model-to-a-scripted-module/6068>\
**Category:** Development\
**Tags:** module\
**Created:** [March 8, 2019, 8:02am UTC](https://discourse.slicer.org/t/loading-a-deep-learning-model-to-a-scripted-module/6068 "2019-03-08T08:02:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZiyunLiang](https://avatars.discourse-cdn.com/v4/letter/z/5daacb/32.png) [@ZiyunLiang](https://discourse.slicer.org/u/ZiyunLiang)\
**Post date:** [March 8, 2019, 8:02am UTC](https://discourse.slicer.org/t/loading-a-deep-learning-model-to-a-scripted-module/6068/1 "2019-03-08T08:02:13Z")

</div>

Hi, I’m working on a project about pelvic floor MRI image recognition. In this project I am trying to automatic recognize feature points and measure distance on MRI images which will help with pelvic floor disease detection. Right now I have already trained a neural network with Keras and stored the model in a .h5 file. Since Slicer is the perfect open-source platform to implement this idea, I have been working hard to learn, but still a beginner to Slicer. I have finished building the GUI of a scripted module.Now I am trying to put my model into the scripted module I created. And I’m seeking advices and ideas about how to do this.

I’m now using  
_from pip.\_internal import main as \_main_  
_\_main([‘install’, ‘pandas’])_  
to load packages to Slicer and  
_model = load\_model(’./models/’ + model\_name + ‘.h5’,compile=False)_  
to load my module.  
I’m wondering if my idea is ok and is there a better way to do it?

Does anyone have any suggestions?  
Thank you!  
Andrea

---

<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:** [March 8, 2019, 10:08pm UTC](https://discourse.slicer.org/t/loading-a-deep-learning-model-to-a-scripted-module/6068/2 "2019-03-08T22:08:40Z")

</div>

Your approach sounds reasonable to me. You may see compatibility issues between Slicer’s python and some generally available python packages. There’s more info [in this thread](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984).

---

<div class="post-metadata">

**Author:** ![ZiyunLiang](https://avatars.discourse-cdn.com/v4/letter/z/5daacb/32.png) [@ZiyunLiang](https://discourse.slicer.org/u/ZiyunLiang)\
**Post date:** [March 11, 2019, 2:35am UTC](https://discourse.slicer.org/t/loading-a-deep-learning-model-to-a-scripted-module/6068/3 "2019-03-11T02:35:31Z")

</div>

Hi, Steve! Thank you for the advice!

I succeeded in installing the packages, but I’m having a trouble loading the .h5 file. I’m pretty sure it is in the correct directory but when I tried to load the file, it says

```auto
*IOError: Unable to open file (unable to open file: name = './models/best_model_RMSE_RES50.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)*

```

Also, I tried multithreading and used the python on my mac as described [here](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#How_to_run_Python_script_using_a_non-Slicer_Python_environment), I launched a python script which worked fine on my mac but when Slicer tried to run that script, it says

```auto
*subprocess.CalledProcessError: Command '['/usr/bin/python', '/Users/andrealiang/Desktop/Pelvicfloor-2019-02-25/lib/slicer-4.11/qt-scripted-modules/nonSlicer_python.py']' returned non-zero exit status 1*

```

I’m really confused here. I’m wondering if it is because Slicer has some problem when reading .h5 file.  
Is there any solution on how to solve this problem?  
Please help!

Thank you!  
Andrea

---

<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:** [March 12, 2019, 1:08pm UTC](https://discourse.slicer.org/t/loading-a-deep-learning-model-to-a-scripted-module/6068/4 "2019-03-12T13:08:12Z")

</div>

Hi Andrea -

For the first issue, you might try specifying the full path to your .h5 file. The message is saying that it can’t find the file at the path you specified. Depending on how you started Slicer it might have a different working directory.

The second one could be the same issue. The script exits with an error, which could mean it didn’t find the file or could mean something else. Have a look at the documentation and examples in slicer.

[https://docs.python.org/2/library/subprocess.html](https://docs.python.org/2/library/subprocess.html)

> <https://github.com/Slicer/Slicer/blob/4c0b0b15276aedc0e12d77e2227536426979d773/Applications/SlicerApp/Testing/Python/SlicerAppTesting.py#L50-L67>

Good luck,  
Steve

---

<div class="post-metadata">

**Author:** ![ZiyunLiang](https://avatars.discourse-cdn.com/v4/letter/z/5daacb/32.png) [@ZiyunLiang](https://discourse.slicer.org/u/ZiyunLiang)\
**Post date:** [March 13, 2019, 11:24am UTC](https://discourse.slicer.org/t/loading-a-deep-learning-model-to-a-scripted-module/6068/5 "2019-03-13T11:24:06Z")

</div>

Hi Steve,  
Thanks for the answer. You are right about the working directory and I solved the problem by specifying the full path.

Thanks!  
Andrea

---

<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, 8:31am UTC](https://discourse.slicer.org/t/loading-a-deep-learning-model-to-a-scripted-module/6068/6 "2022-06-29T08:31:56Z")

</div>

Hello Andrea,

I am trying to build a module for segmentation using a pre-trained Unet model. I also used the load\_model() function to load my .h5 model file. But when I run it in Slicer, the execution blocks with no error information and the Slicer does not respond. And I have to force quit Slicer. Maybe you have an idea of the reason behind the blockage.

Thanks a lot.  
Zhenchen
