# How to start with monailabel for new models

**URL:** <https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063>\
**Category:** Support\
**Tags:** monailabel\
**Created:** [December 14, 2021, 10:55pm UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063 "2021-12-14T22:55:54Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [December 14, 2021, 10:55pm UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/1 "2021-12-14T22:55:54Z")

</div>

We would like to use monaiLabel to generate/refine existing segmentation of fetal mice. Given that there is no pre-existing model to do that, how does one go about this. We have about 20 or so already segmented scans.

At some point monaiLabel was very frequently updated. Due to the environment we work, frequent version changes and code updates is not feasible for us. Is it stable enough to use in a production environment on a regular basis?

---

<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:** [December 14, 2021, 11:44pm UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/2 "2021-12-14T23:44:51Z")

</div>

I haven’t tried it all myself, but here are recent materials about MONAILabel with Slicer for segmentation. We are arranging to hold a workshop on this material on January 12, 2022, 2-4pm EST as part of [project week](https://projectweek.na-mic.org/PW36_2022_Virtual/) where at least a few projects will try to apply it.

[https://catalog.us-east-1.prod.workshops.aws/v2/workshops/ff6964ec-b880-45d4-bc1e-468b0c7fa854/en-US/](https://catalog.us-east-1.prod.workshops.aws/v2/workshops/ff6964ec-b880-45d4-bc1e-468b0c7fa854/en-US/)

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [December 15, 2021, 7:06am UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/3 "2021-12-15T07:06:00Z")

</div>

@pieper Steve is that date fixed? Not one week later because PW is from Jan 17th ? 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:** [December 15, 2021, 1:43pm UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/4 "2021-12-15T13:43:15Z")

</div>

Yes, we scheduled it the week before Project Week on purpose so that we could block out 2 hours for it without cutting into other work. Also we wanted people to get up to speed in advance so that they could apply it to their own data during PW.

---

<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:** [December 15, 2021, 9:29pm UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/5 "2021-12-15T21:29:26Z")

</div>

> [@pieper](#):
>
> I haven’t tried it all myself, but here are recent materials about MONAILabel with Slicer for segmentation. We are arranging to hold a workshop on this material on January 12, 2022, 2-4pm EST as part of [project week](https://projectweek.na-mic.org/PW36_2022_Virtual/) where at least a few projects will try to apply it.

Thanks Steve. I managed to get the server running locally at some point, what I was actually for instructions on how to get started with a new model, as opposed to using a pretrained model (e.g., spleen segmentation). All the documentation I found so far refers to using existing models (e.g., [Quickstart — MONAI Label 0.8.1 Documentation](https://docs.monai.io/projects/label/en/latest/quickstart.html))

---

<div class="post-metadata">

**Author:** ![mangotee](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@mangotee](https://discourse.slicer.org/u/mangotee)\
**Post date:** [December 16, 2021, 12:22am UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/6 "2021-12-16T00:22:32Z")

</div>

Hi Murat! Good point, the existing documentation so far assumes a pretrained model. However, in my case this was not a problem - I used MONAILabel for inner ear structure segmentation in microCT (128^3 cubic volumes). I started with 0 annotated volumes, but nonetheless, I used the pre-trained left-atrium DeepEdit model (different anatomy AND modality), because 1) I wanted a quick start without much fiddling with the DeepEdit app code, and 2) I thought it might beneficial that the early encoder layers already have some “clue” about atomic 3D patch geometries. Not sure whether point 2) actually was beneficial (didn’t try the corollary), but to my delight, the DeepEdit’s UNet model quickly started snapping to the anatomy. Already after 2-3 manual annotations, I got surprisingly good segmentation guesses by the model. In your case, you have already 20 pre-annotated volumes. You can indicate this in the `datastore.json` file. Upon first start of DeepEdit, instead of annotating right-away, you could manually trigger the first pre-training. The resulting model might already be quite “valuable”. Let me know if I can help.

---

<div class="post-metadata">

**Author:** ![diazandr3s](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/diazandr3s/32/9973_2.png) [@diazandr3s](https://discourse.slicer.org/u/diazandr3s)\
**Post date:** [December 16, 2021, 2:33am UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/7 "2021-12-16T02:33:41Z")

</div>

Hi @muratmaga,

There are two ways of using a MONAI Label App (i.e. [DeepEdit](https://github.com/Project-MONAI/MONAILabel/tree/main/sample-apps/deepedit), [DeepGrow](https://github.com/Project-MONAI/MONAILabel/tree/main/sample-apps/deepgrow), [Segmentation](https://github.com/Project-MONAI/MONAILabel/tree/main/sample-apps/segmentation)) without downloading/using the available pretrained models.

The first approach is adding the flag --conf use\_pretrained\_model false when starting the server. This means you should write something like this:

> monailabel start\_server -a /PATH\_2\_APP/ -s /PATH\_2\_IMAGES/ --conf use\_pretrained\_model false

The second approach and the secure one is by changing the flag directly in the main file of your App. So, if you want to use a DeepEdit App, change [this line](https://github.com/Project-MONAI/MONAILabel/blob/main/sample-apps/deepedit/main.py#L99) to false.

With regards to the already annotated images, there are two things to consider. First, images and corresponding labels should have the same name, and secondly, already annotated labels should be placed inside the subfolder **/folder\_images/labels/final**.

Something like this

```auto
folder_images:
              - image_1
              - image_2
              - image_3
              labels:
                       final:
                             - image_1
                             - image_2

```

If this is not clear, in slide 9 of this [presentation](https://docs.google.com/presentation/d/19YWH7J-WBGl9MLfrd8eybJ_Bhwio7lXQjsM8EKcOOhk/edit?usp=sharing) I did an example of how to organise the images and already annotated labels before starting the server.

Just out of curiosity, are the scans 3D? Which MONAI Label App are you planning to use (i.e. DeepEdit, DeepGrow, Segmentation)? How many segments have the labels? Is this a single label or multilabel task?

As @mangotee nicely commented, please don’t hesitate to ask, we’re happy to help 🙂

---

<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:** [December 16, 2021, 3:37am UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/8 "2021-12-16T03:37:23Z")

</div>

Thanks @diazandr3s. Yes, images are 3D scans of fetal mice, and labels are multilabel (about ~30 labels). Volumes are about 200x400x400 voxels. Labels and volumes have identical prefix, but different formats. Volumes are in NRRD and labelmaps are in nii.gz (mostly because we want to keep the labelmaps compressed, but not necessarily the volumes). Is that an issue? Or do they have both same format?

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [December 16, 2021, 11:12am UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/9 "2021-12-16T11:12:51Z")

</div>

Hi @diazandr3s. Thank you for interesting work and your contribution to the Slicer community.

I have a question regarding MONAI-Label: Let’s say I have a few dozens of CTs of both feet, I have segment of the talus bone for each foot so there are two segments (labels) per CT, and I have a frame that defines the orientation and position (i.e. a linear transform) of each talus.

One way to imagine the usefulness of this transform information is that if you apply the inverse of this transform to the talus-segment you would have a zero-centered talus with a normalized orientation for comparing measurements between taluses of different CTs. And after this you can also mirror left-and-right for left-taluses and after that all the taluses (right ones and left-mirrored ones) would be oriented so that the lateral side of the bone is on the right.

Would it be possible to feed MONAI-Label the transform information, so when the model is trained per input CT it returns a label for each talus and the transform defining its position and orientation?

---

<div class="post-metadata">

**Author:** ![diazandr3s](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/diazandr3s/32/9973_2.png) [@diazandr3s](https://discourse.slicer.org/u/diazandr3s)\
**Post date:** [December 16, 2021, 11:50am UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/10 "2021-12-16T11:50:26Z")

</div>

This is a nice use-case for MONAI Label 🙂  
Do you mean 30 segments in each label? Just for me to understand, which are the segments in each 3D scan?  
If the task is multilabel like [this one](https://www.synapse.org/#!Synapse:syn3193805/wiki/217789), then I recommend you to use the [Multilabel DeepEdit App](https://github.com/Project-MONAI/MONAILabel/tree/main/sample-apps/deepedit_multilabel).  
My suggestion is to not mix image/label formats. Is it difficult to have both images and labels in the same format?

---

<div class="post-metadata">

**Author:** ![diazandr3s](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/diazandr3s/32/9973_2.png) [@diazandr3s](https://discourse.slicer.org/u/diazandr3s)\
**Post date:** [December 16, 2021, 11:57am UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/11 "2021-12-16T11:57:57Z")

</div>

Thanks, @mau_igna_06!  
Yes, you should be able to use MONAI Label for this task. You can easily define transforms that can be applied to the images for training or performing inference.  
I’m happy to show you how you can do this. For instance, I’m currently creating custom transforms for a multilabel task. In [here](https://github.com/Project-MONAI/MONAILabel/blob/multilabel_deepedit_preds/sample-apps/deepedit_multilabel_with_preds/lib/transforms.py#L29) I’m defining those transforms that I used for my App.

---

<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:** [December 16, 2021, 4:08pm UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/12 "2021-12-16T16:08:26Z")

</div>

> [@diazandr3s](#):
>
> Do you mean 30 segments in each label? J

Yes, it is a multi-label task exactly like the one you show, only in mice. We keep the labels and volumes together for our current workflow, so keeping them in separate formats with the same file name is a nice trick. It has gives us the benefit of compressing the labels (which does really well). But it is not a big deal, I can switch them to same format.

I will give a try, thanks for your pointers.I am excited to try.

---

<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:** [December 16, 2021, 8:59pm UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/13 "2021-12-16T20:59:59Z")

</div>

@diazandr3s  
actually I got stuck at the beginning currently. I am following these instructions  
[https://docs.monai.io/projects/label/en/latest/installation.html](https://docs.monai.io/projects/label/en/latest/installation.html)

and at the monailabel --help step I am getting

maga@magalab-ML:~/.local/bin$ ./monailabel  
Using PYTHONPATH=/home/maga:  
Python 2.7.18  
/usr/bin/python: No module named monailabel

It is not finding the python3. I tried setting PYTHONPATH=/usr/bin/python3 in the shell prior to executing monailabel, but that didn’t have any effect.

Any suggestions

---

<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:** [December 16, 2021, 9:08pm UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/14 "2021-12-16T21:08:56Z")

</div>

> [@muratmaga](#):
>
> PYTHONPATH=/usr/bin/python3

The other issue I am having, it is not clear from this instruction set that whether the installation needs to be done as a priviledged user or it is possible to run it as normal user. I hardcoded the python3 path and now I can use the monailabel file, but when I do  
monailabel apps --download --name deepedit --output apps

it tries to write in /usr/monailabel, which as a normal user I can’t. Is there a major config file where I can specify these? Or all of these instructions assume that installation is done as the root (or admin)?

---

<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:** [December 16, 2021, 9:48pm UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/15 "2021-12-16T21:48:52Z")

</div>

This is where I stand as a normal user install.

1. Hard coded the monailabel script to  
PYEXE=python3
2. After this, when I type:  
**~/.local/bin/monailabel apps --prefix ~/.local**  
this is what I get

```auto
Using PYTHONPATH=/home/maga:
Python 2.7.18
Available Sample Apps are: (/home/maga/.local/monailabel/sample-apps)
----------------------------------------------------
Deepedit based Apps
----------------------------------------------------
  deepedit : /home/maga/.local/monailabel/sample-apps/deepedit
  deepedit_multilabel : /home/maga/.local/monailabel/sample-apps/deepedit_multilabel

Deepgrow based Apps
----------------------------------------------------
  deepgrow : /home/maga/.local/monailabel/sample-apps/deepgrow

Standard Segmentation Apps
----------------------------------------------------
  segmentation : /home/maga/.local/monailabel/sample-apps/segmentation
  segmentation_left_atrium : /home/maga/.local/monailabel/sample-apps/segmentation_left_atrium
  segmentation_spleen : /home/maga/.local/monailabel/sample-apps/segmentation_spleen

```

1. Spleen dataset downloaded and installed fine:

```auto
~/.local/bin/monailabel datasets --download --name Task09_Spleen --output datasets
Using PYTHONPATH=/home/maga:
Python 2.7.18
Directory already exists: datasets/Task09_Spleen

```

1. Can’t get the deepedit to download, nor can start the server

```auto
~/.local/bin/monailabel apps --download --name deepedit --output apps
Using PYTHONPATH=/home/maga:
Python 2.7.18
Traceback (most recent call last):
  File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/maga/.local/lib/python3.8/site-packages/monailabel/main.py", line 333, in <module>
    Main().run()
  File "/home/maga/.local/lib/python3.8/site-packages/monailabel/main.py", line 108, in run
    self.action_apps(args)
  File "/home/maga/.local/lib/python3.8/site-packages/monailabel/main.py", line 161, in action_apps
    apps = os.listdir(apps_dir)
FileNotFoundError: [Errno 2] No such file or directory: '/usr/monailabel/sample-apps'

~/.local/bin/monailabel start_server --app apps/deepedit --studies datasets/Task09_Spleen/imagesTr
Using PYTHONPATH=/home/maga:
Python 2.7.18
APP Directory apps/deepedit NOT Found

```

---

<div class="post-metadata">

**Author:** ![diazandr3s](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/diazandr3s/32/9973_2.png) [@diazandr3s](https://discourse.slicer.org/u/diazandr3s)\
**Post date:** [December 16, 2021, 11:39pm UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/16 "2021-12-16T23:39:51Z")

</div>

Hi @muratmaga,

From what I can see, you’re working with Python 2.X (Python 2.7.18). As shown in the instructions, please use Python 3.X. My recommendation is you either create a virtual environment with a Python version 3.X or you work with the docker container.  
Once that is done, you could download the DeepEdit App as you’ve already tried:

> monailabel apps --download --name deepedit --output apps

or directly download it from the main repo.

---

<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:** [December 17, 2021, 2:47am UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/17 "2021-12-17T02:47:02Z")

</div>

It is a python3 environment. I even setup the update-alternatives to make default python python3. But anyways, I am getting issues witht the docker too: This inside output after running the docker command:

> root@magalab-ML:/opt/monai# monailabel apps --download --name deepedit --output apps  
> Using PYTHONPATH=/opt:  
> Directory already exists: /opt/monai/apps/deepedit  
> root@magalab-ML:/opt/monai# monailabel datasets --download --name Task09\_Spleen --output datasets  
> Using PYTHONPATH=/opt:  
> Directory already exists: datasets/Task09\_Spleen  
> root@magalab-ML:/opt/monai# monailabel start\_server --app apps\deepedit --studies datasets\Task09\_Spleen\imagesTr  
> Using PYTHONPATH=/opt:  
> APP Directory appsdeepedit NOT Found  
> root@magalab-ML:/opt/monai#

---

<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:** [December 17, 2021, 4:06am UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/18 "2021-12-17T04:06:04Z")

</div>

server seems to work, if the full correct path is provided in the command that invokes the server like this:

`monailabel start_server --app /opt/monai/apps/deepedit --studies /opt/monai/datasets/Task09_Spleen/imagesTr`

---

<div class="post-metadata">

**Author:** ![diazandr3s](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/diazandr3s/32/9973_2.png) [@diazandr3s](https://discourse.slicer.org/u/diazandr3s)\
**Post date:** [December 17, 2021, 11:26am UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/19 "2021-12-17T11:26:18Z")

</div>

Superb! Thanks for letting us know 🙂  
Please remember that if you want to work with multilabel DeepEdit, you should use [this App](https://github.com/Project-MONAI/MONAILabel/tree/main/sample-apps/deepedit_multilabel).  
We are in the process of merging both single and multilabel into a single App so it is easier for users.  
When using the multilabel DeepEdit App, please change the label names here: [MONAILabel/main.py at main · Project-MONAI/MONAILabel · GitHub](https://github.com/Project-MONAI/MONAILabel/blob/main/sample-apps/deepedit_multilabel/main.py#L43)

---

<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:** [December 18, 2021, 6:48pm UTC](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063/20 "2021-12-18T18:48:53Z")

</div>

> [@diazandr3s](#):
>
> Thanks for letting us know

I do have a comment about documentation.

It would be better if you specify the filepaths using the standard convention with slash `/`, as opposed to using windows convention of backslash `\`. Slash is more universal and most windows applications (like Python) will interpret the windows path with `C:/` still correctly, whereas in Unix `\` is interpreted as an escape character. It took me a while to figure out that the path errors are not real path errors, but simply interpreted incorrectly because examples are probably written on windows platform and same instructions apply to both OSes.

[Next page](https://discourse.slicer.org/t/how-to-start-with-monailabel-for-new-models/21063.md?page=2)
