MONAILabel module should allow user to select different volumes in current scene

this selector bar is gray.

1 Like

This is probably intentional. You can submit a feature request to GitHub - Project-MONAI/MONAILabel: MONAI Label is an intelligent open source image labeling and learning tool. to discuss this with the developers of the extension.

1 Like

This seems to be normal behavior, if you start your MONAILabel server and press “Next Sample” in the MONAILabel extension, the “Source Volume” will be automatically set, but remain greyed out.

The MONAILabel extension accepts that you open a different volume in 3D Slicer, run inference (“Autosegmentation → Run”), optimize the labels via the integrated “Segment editor”, then “Submit Label” to the MONAILabel server.

for example, I am training the model ,I fond the accuracy is very good ,now I want to use a new volume to test the model ,then how to do?

One way is to finish training all the data of datasets. I will continue clicking next sample until the last one.

In fact ,the source volume means the current volume that I want to process .if I want to use the pretrained model to segment my data anytime,how to do ?

In my experience what you do is close the scene, load a new volume, and then it becomes the selected target for running the inference.

Yes ,right! But this is not convenient,and I can only load a volume into the scene, I want to load different volumes into the scene.

for example, I have many volumes ,I want to label them one by one and submit the volume with label . I can chose different volumes in source volume bar. The button next sample can just help load data from datasets.

1 Like

You probably will need to do some data preparation and bring the files exactly into the data file structure that MONAILabell uses, then connect MONAILabel server to your own dataset.
Call your volumes by “Next sample”, label 4-5 volumes this way, “Submit label” each time, then train.
Depending on your purpose, the “segmentation” model is probably better than “deepedit”.

1 Like

@slicer365 Do you feel that the model works well already and so instead of training you just want to use it conveniently for segmenting your images?

@pieper @rbumm What is the recommended method for deploying models trained by MONAILabel? Creating a MONAI bundle? Should we create a Segment Editor effect that can run MONAI bundles locally? Is there an inference server for MONAI bundles?

I have some brain hemorrhage CT data, I want to segment the hematoma, then I used the pre-training model of spleen, I just use 4 sets of data to modify and submit the label, and then train it, the result is amazing, it performs very well.

1 Like

The segment editor effect is an interesting idea! I am not aware of a public inference server, but maybe bundles are already part of the MONAI Model Zoo, @diazandr3s could you comment?

Here is how inference can be made with a running MONAILabel Server, specifying a trained model (tested code):

logic = slicer.util.getModuleLogic('MONAILabel')
# connect to server
try:
    #check if Monailabel is connected correctly
    server_add = "http://127.0.0.1:8000"
    logic.setServer(server_url=server_add)
    MONAILabelClient = logic.info()
except Exception as e:
    slicer.util.errorDisplay("Unable to connect to MONAILabel server on http://127.0.0.1:8000"+str(e))
    import traceback
    traceback.print_exc()
else:
    # save the volume and get the path
    # inputVolume is a previously generated scalar volume
    tempVolDir, image_id, in_file = self.saveVolTemp(inputVolume)
    model = "segmentation"
    params = {'largest_cc': True}
    # make inference
    result_file, params = logic.infer(model, in_file, params)
    # load the segmentation file in Slicer
    tempResultSegmentation = slicer.util.loadSegmentation(result_file)

The model is a *.pt file that can be found in the MONAILabel app directory (Windows example):

1 Like

Here is what ChatGPT suggests to make inferences with a trained MONAILabel model and using Pytorch (untested code):

import torch
import monai

# Load the saved model from MONAILabel
model_path = "path/to/saved/model"
model = torch.load(model_path)

# Set the model to evaluation mode
model.eval()

# Load the image data you want to make predictions on
image_path = "path/to/image"
image = monai.utils.load_nifti(image_path)[0]

# Convert the image to a PyTorch tensor
image_tensor = torch.tensor(image)

# Make predictions using the model
with torch.no_grad():
    predictions = model(image_tensor)

# Process the predictions as needed
# ...

# Save the output to a file
output_path = "path/to/output"
monai.utils.save_nifti(output_path, predictions)

1 Like

This is amazing, @rbumm

Hi @slicer365,

Thanks for posting these questions/comments.

Just to clarify, you could fetch the Next Sample even if training is happening. No need to stop the training process. You could also run inference and training if you have enough GPU memory to do it.

If you want to run batch inference on more than one volume, you could run MONAI Label without starting it as a server. For this, just update the folder here (MONAILabel/main.py at main · Project-MONAI/MONAILabel · GitHub), model name here (MONAILabel/main.py at main · Project-MONAI/MONAILabel · GitHub - it should be “segmentation” or “deepedit” depending on the model you’re using) and then run the main file within the docker or virtual env like a standard python script: python main.py

This will run inference on all the unlabelled volumes.

For other users, here is a similar discussion: MONAILabel module should allow user to select different volumes in current scene · Project-MONAI/MONAILabel · Discussion #1354 · GitHub

Hope this helps

The above, ChatGPT suggested code seems to be based on a previous version of MONAI and is not working.

@diazandr3s how would you suggest loading a trained MONAILabel model (*.pt) in Python directly if you just want to create inference on a given scalar volume and not start a server? I see it is party answered in your previous post, but is there an easy elegant solution?

If we succeed in establishing such a mechanism in a 3D Slicers Segment Editor effect we could probably make MONAILabel models or other MONAI models from the impressively growing Zoo much easier to use and popular.

1 Like

This is a very good question, @rbumm.

Each MONAI Label App has a main.py file. You could run the main.py file of each app as a standard python script and without starting the MONAI Label server. Just provide the arguments needed such as the folder that contains the images you want to run inference on, and the model name.

Here are the main.py files:

MONAI Label Radiology app: https://github.com/Project-MONAI/MONAILabel/blob/main/sample-apps/radiology/main.py

MONAI Label Bundle app (for the model zoo): https://github.com/Project-MONAI/MONAILabel/blob/main/sample-apps/monaibundle/main.py

Let’s say I have my segmentation model in MONAI Label trained and want to run inference on one CT image in a folder called “/home/user/CT-test/”

I can run the main.py file of the radiology app in the monai label virtual env like this:

python main.py --studies “/home/user/CT-test/” --model “segmentation” --test “infer”

This command will run inference on the CT images you have in folder “/home/user/CT-test/”

Happy to clarify this in a call if needed :slight_smile:

2 Likes

I have tested for many hours and combinations (from powershell, from Slicer Python Console etc) but do not get this yet working @diazandr3s .
Running your above script line in a otherwise working monailabel server environment throws:

File "C:\Users\Rudolf\apps\radiology\lib\activelearning\last.py", line 14, in <module>
    from monailabel.interfaces.datastore import Datastore
ModuleNotFoundError: No module named 'monailabel'
1 Like

Hi @rbumm,

Are you running the command on the python virtual env that has monai label installed? It seems it is not the case as it doesn’t find the monailabel module.

Please let us know,

I am running this in a powershell now following our instructions from last year

monailabel start_server --app apps/radiology --studies datasets/Task06_Lung/imagesTr --conf models segmentation`

works as expected.

How should I install the module?

1 Like

It is strange. If this command runs as expected, this means the monailabel module is installed:

monailabel start_server --app apps/radiology --studies datasets/Task06_Lung/imagesTr --conf models segmentation`

Can you please show what you see after running?

monailabel -h

Can you also share what you see when entering the python command and then running import monailabel?

Something like this:

python

then

import monailabel