Project-MONAI / tutorials

MONAI Tutorials
https://monai.io/started.html
Apache License 2.0
1.74k stars 662 forks source link

Auto3DSeg to test data for segmentation of the Lung #1724

Closed BhosaleSumedha closed 2 weeks ago

BhosaleSumedha commented 2 months ago

Hello everyone,

I am very new to MONAI and I would like to know how use auto3dseg function in MONAI for lung segmentation??

I have multiple test sets and I would like to use pre-trained models on these test sets but I am not able to find a proper model or any tutorial regarding the same in MONAI.

Any help would be grateful.

Regards, Sumedha

diazandr3s commented 2 months ago

Hi @BhosaleSumedha,

Have you tried the new Slicer MONAI Auto3DSeg module? https://discourse.slicer.org/t/new-extension-monai-auto3dseg-raise-the-bar-in-ai-medical-image-segmentation/35680

Just download the latest Slicer and install the extension via the Extension Manager.

You'll find a lung segmentation model ready to use.

Let us known how that goes.

BhosaleSumedha commented 2 months ago

Thank you for your reply @diazandr3s.

The module seems to be a very easy way to use pre-trained models. I can try this for the time being.

But, I was wondering if there is a script-based method to perform this task. I plan to use another model to improve the accuracy of my 3D model produced and then automate the process.

Regards, Sumedha

diazandr3s commented 2 months ago

Hi @BhosaleSumedha,

You can always use this model in a programmatic manner: https://github.com/lassoan/SlicerMONAIAuto3DSeg/issues/52#issuecomment-2075577774

Hope this helps,

abhishekpatil32 commented 2 weeks ago

Hii @diazandr3s, Continuing the above question, if I use the above linked code with a pretrained model like whole body segmentation and I would like to separate the segmentations (organs and other parts) instead of getting a single .nii segmentation file. How can I do that?

Thank you in advance

diazandr3s commented 2 weeks ago

Hi @abhishekpatil32,

Good question.

Each model has labels file where you can see the indices associated to each region/organ it segments. You could mask the output prediction to only get the regions you're interested in.

That should be done offline - meaning after getting the output prediction.

The indices associated to each organ/regions can be find in the labels file in each model here: https://github.com/lassoan/SlicerMONAIAuto3DSeg/releases/tag/Models

Hope this helps,

abhishekpatil32 commented 2 weeks ago

Hello @diazandr3s,

I am working on the whole body segmentation model (v1 and v2) using CT images. The model contains 104 ROIs which would be segmented in a single nii file.

I thought that the separation of a single nii file to 104 nii files would be done by modifying the auto3dseg_segresnet_inference.py especially after the logits prediction part.

If I do logits2pred part, the pred give a single image. This is my understanding

Can you please clarify if this is where i need to work on or is there something else I am missing?

diazandr3s commented 2 weeks ago

Hi @abhishekpatil32,

logits2pred converts the logits (immediate network output) into the indices associated to the regions (pred) - it is essentially a softmax function.

If the network was trained to segment 104 organs, the best pred will always be 104 indices. Unless you retrained the network, it'll always output 104 indices.

Here are the lines that save the prediction into a single nii/nrrd file: https://github.com/lassoan/SlicerMONAIAuto3DSeg/blob/ed3e56ab6d51214b922a3dfc076ae476cc5703ce/MONAIAuto3DSeg/Scripts/auto3dseg_segresnet_inference.py#L324-L327

You could save one nii file per different index

abhishekpatil32 commented 2 weeks ago

Hello @diazandr3s,

I understand your point about not retraining the network. I mentioned logits and pred variables from the inference file due to their sizes: logits: torch.Size([1, 105, 512, 347, 363]) pred: torch.Size([1, 1, 512, 347, 363])

You indicated that the separation of a single nii file into 104 nii files can be done offline, meaning after obtaining the output prediction. The final segmentation output you get after the inference is of size (512, 512, 218).

I am still not sure how to achieve this. Could you please help me out here?

diazandr3s commented 2 weeks ago

Hi @abhishekpatil32,

You indicated that the separation of a single nii file into 104 nii files can be done offline, meaning after obtaining the output prediction.

Yes, you can do this offline or you could do this before the final prediction is saved here: https://github.com/lassoan/SlicerMONAIAuto3DSeg/blob/ed3e56ab6d51214b922a3dfc076ae476cc5703ce/MONAIAuto3DSeg/Scripts/auto3dseg_segresnet_inference.py#L324-L327

The final segmentation output you get after the inference is of size (512, 512, 218).

You are right. The final prediction is a single 3D tensor. If you actually check the numbers in that tensor, they are numbers raning from 0 to 103. You could read that file, take the numbers/indices that you're interest and then re-save the file.

Say for for example you are interested in liver and it is represented by index 3:

You'll have a prediction with the same size but only containing the number 3. The rest should be zero.

Here we are discussing same idea: https://github.com/Project-MONAI/model-zoo/discussions/616#discussioncomment-10262474

Hope this makes sense.

abhishekpatil32 commented 2 weeks ago

Hello @diazandr3s,

I got it finally. So sorry for the confusion. Now I have done the extraction with ease.

Thank you so much.

diazandr3s commented 2 weeks ago

Thanks for letting us know. Closing issue for now.