# Machine Learning Model Importing

**URL:** <https://discourse.slicer.org/t/machine-learning-model-importing/6298>\
**Category:** Development\
**Created:** [March 26, 2019, 8:07pm UTC](https://discourse.slicer.org/t/machine-learning-model-importing/6298 "2019-03-26T20:07:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolsocks](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/coolsocks/32/3299_2.png) [@coolsocks](https://discourse.slicer.org/u/coolsocks)\
**Post date:** [March 26, 2019, 8:07pm UTC](https://discourse.slicer.org/t/machine-learning-model-importing/6298/1 "2019-03-26T20:07:56Z")

</div>

Hi!

I am currently building a module which involves importing a deep learning model trained with keras to predict some segmentations. Initially, the question I had was how to go about running the keras/tensorflow packages in the module that I would like to create. I found an answe to that question here: [Loading a deep learning model to a scripted module](https://discourse.slicer.org/t/loading-a-deep-learning-model-to-a-scripted-module/6068)

What I’m wondering is that if this approach is feasible if I were to package these modules into an extension. If a user were to download this extension on a different computer, and use it there, would the packages still install in their version of slicer-python? If not, do you have any recommendations on how to approach this issue or any resources on how to do this?

Thanks!

---

<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 26, 2019, 9:08pm UTC](https://discourse.slicer.org/t/machine-learning-model-importing/6298/2 "2019-03-26T21:08:38Z")

</div>

It can be hard to distribute models given the constraints on computing environments. We think that when Slicer moves to python 3 (maybe as soon as next week) it will be easier, but there are still some potential problems. You might look at bundling through DeepInfer, which uses Docker for the computing.

[http://www.deepinfer.org/](http://www.deepinfer.org/)

---

<div class="post-metadata">

**Author:** ![mlp6](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mlp6/32/1614_2.png) [@mlp6](https://discourse.slicer.org/u/mlp6)\
**Post date:** [August 28, 2019, 6:01pm UTC](https://discourse.slicer.org/t/machine-learning-model-importing/6298/3 "2019-08-28T18:01:34Z")

</div>

Any updates on the “best practice” for developing a slicer module that uses tensorflow under the hood? We’re looking to implement [https://arxiv.org/abs/1908.05782](https://arxiv.org/abs/1908.05782) as a module, and would like to start with our best foot forward. The `DeepInfer` approach w/ a Docker backend seems attractive, but it would add some appreciable setup overhead on Windows client machines, which is our target.

---

<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:** [August 28, 2019, 6:26pm UTC](https://discourse.slicer.org/t/machine-learning-model-importing/6298/4 "2019-08-28T18:26:10Z")

</div>

Hi -

With the Slicer nightly you can use the `slicer.util.pip_install()` command to install tensorflow and many other standard packages directly. This opens the door for a lot of interesting things but the exact practices haven’t yet been explored. It would be great to see people try some experiments. I’d love to see how things go because getting some of these models directly in slicer has been on my wishlist for quite a while.

---

<div class="post-metadata">

**Author:** ![mlp6](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mlp6/32/1614_2.png) [@mlp6](https://discourse.slicer.org/u/mlp6)\
**Post date:** [August 28, 2019, 6:48pm UTC](https://discourse.slicer.org/t/machine-learning-model-importing/6298/5 "2019-08-28T18:48:02Z")

</div>

We might be those guinea pigs… our decision point hinges on the most robust way to support CPU and GPU hardware with the package install since the latter would need to play nicely w/ CUDA library installs, etc. We already have an existing pain point when doing that on a machine-to-machine basis, and Docker solves a lot of those problems. Some packages like (I think) `pytorch` bundle the necessary libraries with the package install, which would be another approach.

---

<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:** [August 28, 2019, 7:11pm UTC](https://discourse.slicer.org/t/machine-learning-model-importing/6298/6 "2019-08-28T19:11:24Z")

</div>

I believe it should work if the user has the right other dependencies installed (e.g. CUDA). I’ve tested with other python libraries pip installed in Slicer that access the GPU and it worked.

---

<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:** [August 28, 2019, 8:44pm UTC](https://discourse.slicer.org/t/machine-learning-model-importing/6298/7 "2019-08-28T20:44:18Z")

</div>

We use both tensorflow CPU and GPU in Slicer on Windows without docker. You may need to copy some Cuda DLLs manually to make GPU work, which should be no problem for developers, but may need some work to make it fully automatic and robust for non-technical users.

Overall, there are many deployment options you can choose from, with several examples you can follow (DeepInfer, TOMAAT, [NVidia Clara segment editor effect](https://github.com/NVIDIA/ai-assisted-annotation-client/), [AIGT](https://github.com/SlicerIGT/aigt)). The best option depends on your constraints - what hardware you need to support, how computationally demanding your application is, what are your time constraints, network connectivity, privacy concerns, how many users you need to serve, who are your users, etc.

---

<div class="post-metadata">

**Author:** ![JanWitowski](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/janwitowski/32/4512_2.png) [@JanWitowski](https://discourse.slicer.org/u/JanWitowski)\
**Post date:** [August 29, 2019, 12:04am UTC](https://discourse.slicer.org/t/machine-learning-model-importing/6298/8 "2019-08-29T00:04:39Z")

</div>

I have been doing some experiments with connecting a few automatic segmentation&object architectures (e.g. U-Net, RetinaNet) with TOMAAT and it seems like a simple tool for prototyping. Might post a short video of initial results next week.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [February 27, 2020, 12:12am UTC](https://discourse.slicer.org/t/machine-learning-model-importing/6298/9 "2020-02-27T00:12:52Z")

</div>

@Lassoan do you have specific instructions to get tensorflow with GPU enabled under Slicer?

---

<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:** [February 27, 2020, 1:52pm UTC](https://discourse.slicer.org/t/machine-learning-model-importing/6298/10 "2020-02-27T13:52:25Z")

</div>

If you pip install tensorflow 2 on Windows or Linux then you can use both CPU and GPU. No special instructions are needed (other than in general setting up Cuda on Linux can be tricky). See details in [release notes](https://github.com/tensorflow/tensorflow/releases/tag/v2.1.0).
