This is my "open-box" version if I want to modify the parameters for some particular task, while the two above are hard-coded.
Follow the steps in "installation_commands.txt". Installation via Anaconda and creation of a virtual env to download the python libraries and pytorch/cuda.
organize_folder_structure.py: Organize the data in the folder structure (training,validation,testing) for the network.
Labels are resampled and resized to the corresponding image, to avoid array size conflicts. You can set here a new image resolution for the dataset.
init.py: List of options used to train the network.
check_loader_patches: Shows example of patches fed to the network during the training.
networks.py: The architecture available for segmentation is a nn-Unet.
train.py: Runs the training
predict_single_image.py: It launches the inference on a single input image chosen by the user.
Use first "organize_folder_structure.py" to create organize the data.
Modify the input parameters to select the two folders: images and labels folders with the dataset. Set the resolution of the images here before training.
.
├── Data_folder
| ├── CT
| | ├── 1.nii
| | ├── 2.nii
| | └── 3.nii
| ├── CT_labels
| | ├── 1.nii
| | ├── 2.nii
| | └── 3.nii
Data structure after running it:
.
├── Data_folder
| ├── CT
| ├── CT_labels
| ├── images
| | ├── train
| | | ├── image1.nii
| | | └── image2.nii
| | └── val
| | | ├── image3.nii
| | | └── image4.nii
| | └── test
| | | ├── image5.nii
| | | └── image6.nii
| ├── labels
| | ├── train
| | | ├── label1.nii
| | | └── label2.nii
| | └── val
| | | ├── label3.nii
| | | └── label4.nii
| | └── test
| | | ├── label5.nii
| | | └── label6.nii
Sample images: the following images show the segmentation of carotid artery from MRI sequence
Sample images: the following images show the multi-label segmentation of prostate transition zone and peripheral zone from MRI sequence
!
python predict_single_image.py --image './Data_folder/image.nii' --label './Data_folder/label.nii' --result './Data_folder/prova.nii' --weights './best_metric_model.pth'
The subfolder "multi_label_segmentation_example" include the modified code for multi_labels scenario.
The example segment the prostate (1 channel input) in the transition zone and peripheral zone (2 channels output).
The gif files with some example images are shown above.
Some note:
- You must add an additional channel for the background. Example: 0 background, 1 prostate, 2 prostate tumor = 3 out channels in total.
- Tensorboard can show you all segmented channels, but for now the metric is the Mean-Dice (of all channels). If you want to evaluate the Dice score for each channel you
have to modify a bit the plot_dice function. I will do it...one day...who knows...maybe not
- The loss is the DiceLoss + CrossEntropy. You can modify it if you want to try others (https://docs.monai.io/en/latest/losses.html#diceloss)
Check more examples at https://github.com/Project-MONAI/tutorials/blob/master/3d_segmentation/spleen_segmentation_3d.ipynb.