Loading a deep learning model to a scripted module

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

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.

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

*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, I launched a python script which worked fine on my mac but when Slicer tried to run that script, it says

*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

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

Good luck,
Steve

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

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