Closed Suchi97 closed 2 years ago
Hi @Suchi97,
Thanks for raising this issue. We addressed the multilabel task in this App (https://github.com/Project-MONAI/MONAILabel/tree/main/sample-apps/deepedit_multilabel). So please use the multilabel deepedit App instead.
If you'd like to work on segmenting the liver and spleen, please update this dictionary. Assign the segment numbers and names and then disable the use of pre-trained model (set to false) so you can train a model specifically for this task.
Hope this helps,
Please let us know
Thanks @diazandr3s for your reply. I will try the above mentioned suggestions and let u know the results.
Hi @diazandr3s
I tried the above steps,
Changed the dictionary values as below; self.label_names = { "spleen": 1, "liver": 2, "background": 0, }
class MyApp(MONAILabelApp):
def __init__(self, app_dir, studies, conf):
# Zero values are reserved to background. Non zero values are for the labels
self.label_names = {
"spleen": 1,
"liver": 2,
"background": 0,
}
network = conf.get("network", "dynunet")
use_pretrained_model = strtobool(conf.get("use_pretrained_model", "false"))
But when I ran the monailabel start_server --app apps/deepedit_multilabel --studies dataset/spleen
, I got the following error
Using PYTHONPATH=/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label:[2022-02-08 12:44:31,087] [26555] [MainThread] [INFO] (__main__:277) - USING:: app = /workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/apps_org[2022-02-08 12:44:31,088] [26555] [MainThread] [INFO] (__main__:277) - USING:: studies = /workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/dataset/spleen
[2022-02-08 12:44:31,088] [26555] [MainThread] [INFO] (__main__:277) - USING:: debug = False
[2022-02-08 12:44:31,088] [26555] [MainThread] [INFO] (__main__:277) - USING:: conf = None
[2022-02-08 12:44:31,088] [26555] [MainThread] [INFO] (__main__:277) - USING:: host = 0.0.0.0
[2022-02-08 12:44:31,088] [26555] [MainThread] [INFO] (__main__:277) - USING:: port = 8000
[2022-02-08 12:44:31,088] [26555] [MainThread] [INFO] (__main__:277) - USING:: log_config = None
[2022-02-08 12:44:31,088] [26555] [MainThread] [INFO] (__main__:277) - USING:: dryrun = False
[2022-02-08 12:44:31,088] [26555] [MainThread] [INFO] (__main__:277) - USING:: action = start_server
[2022-02-08 12:44:31,088] [26555] [MainThread] [INFO] (__main__:278) -
[2022-02-08 12:44:32,800] [26555] [MainThread] [INFO] (uvicorn.error:82) - Started server process [26555]
[2022-02-08 12:44:32,800] [26555] [MainThread] [INFO] (uvicorn.error:45) - Waiting for application startup.
[2022-02-08 12:44:32,800] [26555] [MainThread] [INFO] (monailabel.interfaces.utils.app:38) - Initializing App from: /workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/apps_org; studies: /workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/dataset/spleen; conf: {}
[2022-02-08 12:44:32,810] [26555] [MainThread] [ERROR] (uvicorn.error:119) - Traceback (most recent call last):
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/starlette/routing.py", line 621, in lifespan
async with self.lifespan_context(app):
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/starlette/routing.py", line 518, in __aenter__
await self._router.startup()
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/starlette/routing.py", line 598, in startup
await handler()
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/monailabel/app.py", line 99, in startup_event
instance = app_instance()
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/monailabel/interfaces/utils/app.py", line 44, in app_instance
c = get_class_of_subclass_from_file("main", main_py, "MONAILabelApp")
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/monailabel/utils/others/class_utils.py", line 53, in get_class_of_subclass_from_file
return get_class_of_subclass(module_from_file(module_name, file_path), class_c)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/monailabel/utils/others/class_utils.py", line 25, in module_from_file
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 843, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/apps_org/main.py", line 17, in <module>
from lib import DeepEdit, DeepEditSeg, MyStrategy, MyTrain
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/apps_org/lib/__init__.py", line 14, in <module>
from .train import MyTrain
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/apps_org/lib/train.py", line 34, in <module>
from monailabel.deepedit.multilabel.transforms import (
ImportError: cannot import name 'AddInitialSeedPointMissingLabelsd' from 'monailabel.deepedit.multilabel.transforms' (/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/monailabel/deepedit/multilabel/transforms.py)
[2022-02-08 12:44:32,810] [26555] [MainThread] [ERROR] (uvicorn.error:56) - Application startup failed. Exiting.
Also, there was two other import issues also. They are FindAllValidSlicesMissingLabelsd
, NormalizeLabelsInDatasetd
.
I solved these 3 import issues by changing AddInitialSeedPointMissingLabelsd
to AddInitialSeedPointCustomd
and FindAllValidSlicesMissingLabelsd
to FindAllValidSlicesCustomd
. But I couldn't find anything related to NormalizeLabelsInDatasetd
. So I commented the same.
If anything is wrong, Please correct me.
After changing the above packages, I could able to run the MONAI Label server using the command
monailabel start_server --app apps/deepedit_multilabel --studies dataset/spleen
Next I opened the Slicer application and selected Active Learning as MONAI Label. Then clicked on the Next Sample. But the images looks like as below:
Also, I couldn't able to label the liver and spleen using scribbles. Can you please help me to use the scribbles for multi labels.
Hi @Suchi97,
Thanks for the detailed report. Just yesterday I updated the multilabel deepedit (https://github.com/Project-MONAI/MONAILabel/pull/620) to support missing labels in the dataset.
Those new transforms are located here. If you're using the GitHub repository please do a pull, otherwise just copy those new transforms. Here is the NormalizeLabelsInDatasetd transform.
These are minor changes and you should still be able to train a model and make MONAI Label work as you already did.
The reason you're getting those results is that there is no a pre-trained model to segment only the liver and spleen. You should train a model first. For that, you can either create your own labels before training or use the BTCV dataset which already have those labels.
If you decided to use the BTCV dataset, just organize them and make sure images and labels have the same name as presented here
If you want to create your own labels using the Medical Segmentation Decathlon dataset (Spleen task), my suggestion is to use Grow From Seeds as presented here. Using scribbles for multilabel is still a work in progress as you can see in this issue
Hope this helps.
Please let us know
Hi @diazandr3s ,
First of all thanks for your suggestions. We were able to make some progress using MONAI Label as per your comments.
But we are facing some issue. We tried to use UNETR as a pretrained model and used spleen, liver and background as labels. And we are getting the following error
[2022-02-16 19:34:47,278] [18978] [MainThread] [INFO] (monailabel.endpoints.activelearning:58) - Next sample: {'id': 'spleen_9', 'ts': 1644941818, 'checksum': 'SHA256:d85db2ef501f9a27826102b01f0d9f466c7cd32693ef202e6d557d3edb875ae6', 'name': 'spleen_9.nii.gz', 'path': '/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/dataset/spleen/spleen_9.nii.gz'}
[2022-02-16 19:34:47,522] [21162] [MainThread] [INFO] (__main__:38) - Initializing App from: /workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/UNETR_liver_trial/MONAILabel/sample-apps/deepedit_multilabel; studies: /workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/dataset/spleen; conf: {}
[2022-02-16 19:34:49,925] [21162] [MainThread] [INFO] (monailabel.utils.others.class_utils:35) - Subclass for MONAILabelApp Found: <class 'main.MyApp'>
[2022-02-16 19:34:50,949] [18978] [MainThread] [INFO] (monailabel.endpoints.infer:159) - Infer Request: {'model': 'deepedit_seg', 'image': 'spleen_9', 'client_id': 'user-xyz'}
[2022-02-16 19:34:50,949] [18978] [MainThread] [INFO] (monailabel.interfaces.app:230) - Image => /workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/dataset/spleen/spleen_9.nii.gz
[2022-02-16 19:34:50,950] [18978] [MainThread] [INFO] (monailabel.interfaces.tasks.infer:221) - Infer Request (final): {'model': 'deepedit_seg', 'image': '/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/dataset/spleen/spleen_9.nii.gz', 'client_id': 'user-xyz'}
[2022-02-16 19:34:50,951] [18978] [MainThread] [INFO] (monailabel.interfaces.utils.transform:61) - PRE - Run Transform
[2022-02-16 19:34:50,951] [18978] [MainThread] [INFO] (monailabel.interfaces.utils.transform:62) - PRE - Input Keys: dict_keys(['model', 'image', 'client_id', 'image_path'])
[2022-02-16 19:34:51,309] [18978] [MainThread] [INFO] (monailabel.interfaces.utils.transform:98) - PRE - Transform (LoadImaged): Time: 0.3569; image: (512, 512, 41)(float32)
[2022-02-16 19:34:51,309] [18978] [MainThread] [INFO] (monailabel.interfaces.utils.transform:98) - PRE - Transform (AddChanneld): Time: 0.0000; image: (1, 512, 512, 41)(float32)
[2022-02-16 19:34:51,378] [18978] [MainThread] [INFO] (monailabel.interfaces.utils.transform:98) - PRE - Transform (Orientationd): Time: 0.0677; image: (1, 512, 512, 41)(float32)
[2022-02-16 19:34:51,425] [18978] [MainThread] [INFO] (monailabel.interfaces.utils.transform:98) - PRE - Transform (ScaleIntensityRanged): Time: 0.0473; image: (1, 512, 512, 41)(float32)
[2022-02-16 19:34:51,522] [18978] [MainThread] [INFO] (monailabel.interfaces.utils.transform:98) - PRE - Transform (Resized): Time: 0.0958; image: (1, 128, 128, 128)(float32)
[2022-02-16 19:34:51,537] [18978] [MainThread] [INFO] (monailabel.interfaces.utils.transform:98) - PRE - Transform (DiscardAddGuidanced): Time: 0.0149; image: (4, 128, 128, 128)(float32)
[2022-02-16 19:34:51,537] [18978] [MainThread] [INFO] (monailabel.interfaces.utils.transform:98) - PRE - Transform (ToTensord): Time: 0.0001; image: torch.Size([4, 128, 128, 128])(torch.float32)
[2022-02-16 19:34:51,537] [18978] [MainThread] [INFO] (monailabel.interfaces.tasks.infer:355) - Running Inferer:: SimpleInferer
[2022-02-16 19:34:51,537] [18978] [MainThread] [INFO] (monailabel.interfaces.tasks.infer:297) - Infer model path: /workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/UNETR_liver_trial/MONAILabel/sample-apps/deepedit_multilabel/model/pretrained_unetr.pt
[2022-02-16 19:34:52,390] [18978] [MainThread] [ERROR] (uvicorn.error:380) - Exception in ASGI application
Traceback (most recent call last):
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py", line 377, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/fastapi/applications.py", line 212, in __call__
await super().__call__(scope, receive, send)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/starlette/applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
raise exc
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/starlette/middleware/cors.py", line 84, in __call__
await self.app(scope, receive, send)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
raise exc
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/starlette/routing.py", line 656, in __call__
await route.handle(scope, receive, send)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/starlette/routing.py", line 259, in handle
await self.app(scope, receive, send)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/starlette/routing.py", line 61, in app
response = await func(request)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/fastapi/routing.py", line 226, in app
raw_response = await run_endpoint_function(
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/fastapi/routing.py", line 159, in run_endpoint_function
return await dependant.call(**values)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/UNETR_liver_trial/MONAILabel/monailabel/endpoints/infer.py", line 177, in api_run_inference
return run_inference(background_tasks, model, image, session_id, params, file, label, output)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/UNETR_liver_trial/MONAILabel/monailabel/endpoints/infer.py", line 160, in run_inference
result = instance.infer(request)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/UNETR_liver_trial/MONAILabel/monailabel/interfaces/app.py", line 231, in infer
result_file_name, result_json = task(request)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/UNETR_liver_trial/MONAILabel/monailabel/interfaces/tasks/infer.py", line 233, in __call__
data = self.run_inferer(data, device=device)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/UNETR_liver_trial/MONAILabel/monailabel/interfaces/tasks/infer.py", line 360, in run_inferer
network = self._get_network(device)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/UNETR_liver_trial/MONAILabel/monailabel/interfaces/tasks/infer.py", line 327, in _get_network
network.load_state_dict(model_state_dict, strict=self.load_strict)
File "/workspace/Hands_On_Lab_Ideation_HCLS_20thJan/Lab_Sessions/Lab_0_MONAI_Label/venv_label/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1406, in load_state_dict
raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for UNETR:
size mismatch for vit.patch_embedding.patch_embeddings.weight: copying a param with shape torch.Size([1536, 9, 16,16, 16]) from checkpoint, the shape in current model is torch.Size([1536, 4, 16, 16, 16]).
size mismatch for encoder1.layer.conv1.conv.weight: copying a param with shape torch.Size([64, 9, 3, 3, 3]) from checkpoint, the shape in current model is torch.Size([64, 4, 3, 3, 3]).
size mismatch for encoder1.layer.conv3.conv.weight: copying a param with shape torch.Size([64, 9, 1, 1, 1]) from checkpoint, the shape in current model is torch.Size([64, 4, 1, 1, 1]).
size mismatch for out.conv.conv.weight: copying a param with shape torch.Size([8, 64, 1, 1, 1]) from checkpoint, the shape in current model is torch.Size([3, 64, 1, 1, 1]).
size mismatch for out.conv.conv.bias: copying a param with shape torch.Size([8]) from checkpoint, the shape in current model is torch.Size([3]).
I believe that this issue is due to the change in the input channels and the labels. The original model has 8 labels and one image. For our model 3 labels and one image which can be see from the image above
So before changing the labels, the input channels ("in_channels": len(self.label_names) + 1,
) will be 9 according to the code.
After changing the labels to "spleen", "liver" and background", the input channels become 4.
So how we should change the input channels inside the model similar to having custom channels for pretrained network. And similarly, the output channels are should also be adjusted. Looking in the above error, the model is not using the custom input and output channels.
Looking forward to your reply.
Thank you for help in advance
Thanks for your message, @Suchi97
As you said, the problem is that the number of input channels is different. The current pre-trained model has 9 channels as input. The one you're trying to use has 4 channels (spleen, liver, background and image).
If you want a model for segmenting those organs, you either use the pre-trained model and then remove the other labels after doing inference, or you retrain a model specifically for those organs using the BTCV dataset. The good thing about MONAI Label is that it is rather easy to get a pre-trained model without much change. In this case, the only change is the dictionary before triggering the training.
I hope this helps. Please let us know how you proceed :)
Thanks @diazandr3s
for your reply
As per your suggestion, we retrained the model specifically for those labels (liver and spleen) using the BTCV dataset. Then used that trained model to label the liver and spleen, We could able to see some progress while doing the above steps. Thanks again for your help.
We are using GPU having 32 GB memory. So, when executing the MONAI label, the whole GPU memory is utilized. The result of the nvidia-smi is attached given below
Do MONAI label needs 32 GB for doing the process? When we submit the label and then its starts the training process. The following error is obtained
RuntimeError: CUDA out of memory. Tried to allocate 256.00 MiB (GPU 0; 31.75 GiB total capacity; 510.83 MiB already allocated; 150.38 MiB free; 772.00 MiB reserved in total by PyTorch)
Can you please help me to solve this. Thanks in advance.
if you are using multilabel.. and 512x512xN kind of higher spatial size.. with 32GB GPU.. this won't be enough.. you can switch to small spatial size.. please remember.. this is more of resource related choices and not related to application logic.
Thanks, @Suchi97. Please confirm the image size you're using for this. I trained a model using a GPU of 24GB on images 128x128x128
Thanks @SachidanandAlle and @diazandr3s for your reply. We trained the model using GPU of 32GB on images having the spatial size 128x128x128. Its working now.
Glad to hear this. Thanks for letting us know.
Closing issue by now.
Describe the bug We are trying to do multi label annotation of liver and spleen. We used the spleen segmentation example and we followed this link to modify our multilabel annotation code. We changed the code as explained in the To Reproduce section,
To Reproduce Steps to reproduce the behavior:
Go to apps/deepedit/train.py
monailabel start_server --app apps/deepedit --studies dataset/spleen
After running the above command, we selected an image by clicking the Next Sample button in Slicer App. The app pops up a dialog box showing Failed to run inference in MONAI Label server. In the terminal, we are getting the following error
Environment
MONAI Label version : 0.3.1
Any suggestions is appreciated. Thanks in advance