NicMSlesions
Easy multiple sclerosis white matter lesion segmentation using convolutional deep neural networks.
Install / Use
/learn @sergivalverde/NicMSlesionsREADME
NicMSlesions

Easy multiple sclerosis white matter lesion segmentation using convolutional deep neural networks.
About:
NicMSlesions makes it easy to accurately segment white matter (WM) lesions on Magnetic Resonance Images (MRI) using supervised deep learning. With nicMSlesions, training and/or inference of a complex neural network model is performed with just a few mouse clicks. More interestingly, nicMSlesions can be trained with a very small set of annotated images, which can be very interesting to facilitate the use of supervised deep learning on the fairly amount of yet unlabeled data (see original publication for more information.)
NicMSlesions graphical user interface (GUI) is inspired on the classical FSL software, a widely well-known neuro-imaging toolbox. As some of the most common tools in FSL, nicMSlesions can be run both under a GUI or via scripting.
NicMSlesions has been tested only on GNU/Linux systems but should work everywhere where Tensorflow can be installed. Although is possible to run it on CPU, it is based on a set of 3D convolutional layers, so using a GPU with cuda capabilities is highnly recommended.
Requirements:
The current version is based on Keras, so the following softwares are necessary to run the program:
-
A decent machine running GNU/Linux or Windows. Deep learning methods are computationally expensive, so at least 8GB of RAM is necessary to train a new model. Although this limitation can be addressed using data generators, at this particular version of nicMSlesions we have avoided this option in order to speed up training, so 16GB is definitely highly recommended. For performing only inference, RAM requirements are more modest.
-
A running Python environment. We highly recommend using the Anaconda distribution.
-
Git.
-
Tensorflow. For GPU computing, please be sure that your computer has the appropriate CUDA libraries installed.
Installation:
First install Tensorflow using pip:
# clone the repo if necessary
git clone https://github.com/sergivalverde/nicMSlesions.git
cd nicMSlesions
# install tensorflow (if you have GPU)
pip install tensorflow-gpu==1.6.0
# if you don't have GPU (DO NOT INSTALL BOTH!!)
pip install tensorflow==1.6.0
The rest of python libraries may be
easily installed also using pip:
pip install -r requirements.txt
Optionally, you may want to add nicMSlesions to your system path for future use.
Data and image specifications:
At this moment, nicMSlesions only accepts nifti images. For each subject, FLAIR and T1-w images are mandatory to infer segmentation, while an additional binary lesion mask delineated on the FLAIR image sequence is also mandatory to train a new model. Future versions will incorporate DICOM images.
After converting images to nifti, raw images can be used as input. By default, the T1-w image is registered internally (affine) to the FLAIR space and skull stripping is performed, so there is no need to register or pre-process the input modalities before running nicMSlesions.
How to run it:
Training:
The simplest way to train a new model is via the GUI. On a terminal, just run nicMSlesions_linux on GNU/Linux or nicMSlesions_win.bat on Windows. Click on the training tab.
-
Select training folder. Browse the folder containing a set of training subjects.

So far, nicMSlesions expects to find training subjects in separated folders. For instance:

-
Select at least T1-w and FLAIR identification tags. Generally, image databases tend to have a unique identifier for each of the MRI sequences, so here the user has only to provide a valid tag for each of the input modalities, and nicMSlesions will do the rest. For instance, if FLAIR images follows
patient-id_flair.nii.gznomenclature, just setting the FLAIR tag toflairwill be enough to ensure that all FLAIR images will be loaded for each of the training subjects inside thetraining_folder. So far, nicMSlesions supports up to 4 input modalities:
-
Set up a name for the training model and/or select a pre-trained model from the library.

If the
use pretrainedtoggle button is pressed, a pre-trained model can be selected from either a predetermined modelbaselineor from a previous training session. Using a pretrained model, a new training session can be run with just a couple of new training images. This may be very interesting for instance to train a model on a new image domain where just a few annotations are available or also to update an already trained model with new training samples. However, the more the training available subjects, the better the accuracy on the target image domain. -
Configure other options. In most of the cases, the previous three steps are sufficient to train a new model. However, additional options are available to tune on the
other optionsmenu.
The following number of parameters can be tuned:
-
Data options:
-
Register modalities: by default, the T1-w sequence is registered to the FLAIR space. Disable this option if images are already registered accordingly.
-
Skull-stripping: remove skull before processing the data. By default, all images are skull-stripped. Disable this option if images are already skull-stripped.
-
Denoising: denoise the images before processing. The number of iterations can be controlled by the parameter
denoise_iter. -
Save tmp files: save intermediate images used during processing (registration matrices, registered and skull-stripped masks, etc...)
-
Show debug messages: show some interesting messages when running that can be useful to debug possible errors.
-
-
Model options:
-
Max epochs: maximum number of epochs to train the network.
-
Validation %: percentage of the training samples that are used to validation. 25% of the samples are used by default.
-
Test batch size: number of samples inferred at the same time. This parameter is important and it is directly related with your RAM memory. Reduce this around 5000 on machines with less than 8GB of RAM.
-
GPU number: select physical GPU unit in case that more than one are available to use. Ssee
nvidia-smifor more info about the GPU number. Change this value only if a GPU is used. -
Verbosity: set the output verbosity of the network model. Setting this parameter to 1 prints the training procedure for each of the epochs. This can be interesting when training a new method. If not necessary keep it to
0to reduce messages.
-
-
Training:
-
Balanced dataset: Perform balanced training or not. If set to
True, the same number of positive (lesion) and negative (healthy) samples are used. -
Fraction negative / positive samples: In case that an unbalanced training wants to be perfromed, values higher than 1 sets the fraction of sampled negative samples with respect to lesion samples.
-
-
Post-processing:
-
Out probability th: set the minimum lesion output probability. Selecting lower values
th < 0.5for this parameter increases the sensitivity of the method, of course at the cost of increasing also the number of false positives. Setting this parameter to values between0.5and0.8is recommended in most of the configurations. -
Min out region size: set the minimum lesion output size. This parameter is related to the image resolution. Setting this parameter to
10is recommended in most the cases. -
Minimum volume error: Minimum error that the program can handle (in ml.). Segmentations with a resulting error < than the selected minimum error will be discarded.
-
-
-
Finally, click the
start trainingbutton to start a training session.
Inference:
Very similarly click on the inference tab to start the program.
-
Select the inference folder. Browse folder containing a set of inference subjects. The same folder structure is expected for inference.

-
Similarly to training, select T1-w and FLAIR identification tags.
-
Select an available training model from the list.
-
Click the
start inferencebutton to perform WM lesion segmentation. -
Validate results. A new folder should appear with the net model name when finished. Both probabilistic and final hard segmentation will be there.
Batch processing:
NicMSlesions can be also run without the GUI application. For doing so, simply use the provided nic_train_network_batch.py and nic_infer_segmentation_batch.py. Both scripts can be run after setting the configuration file located at config/configuration.cfg. Basically, the same parameters shown above can be configured using this file. For instance:
[database]
train_folder = /path/to/the/training/folder
inference_folder = /path/to/the/inference/folder
flair_tags = FLAIR_TAG
t1_tags = T1_TAG
mod3_tags = None
mod4_tags = None
roi_tags = MASK_TAG
register_modalities = True
skull_stripping = True
save_tmp = True
debug = False
[train]
fu
Related Skills
node-connect
341.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
84.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
341.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
84.4kCommit, push, and open a PR
