1) Make sure you have set up the PROMISE12 data set. If not, download it from https://promise12.grand-challenge.org/ (registration required) and run data/PROMISE12/setup.py
2) Make sure you are in NiftyNet root, setting niftynet_path correctly to the path with the niftynet folder in it
In [ ]:
import os,sys
niftynet_path=r'path/to/NiftyNet'
os.chdir(niftynet_path)
3) Make sure you have all the dependencies installed (replacing gpu with cpu for cpu-only mode):
In [ ]:
import pip
#pip.main(['install','-r','requirements-gpu.txt'])
pip.main(['install','-r','requirements-cpu.txt'])
pip.main(['install', 'SimpleITK>=1.0.0'])
The simplest way to use NiftyNet is via the commandline net_segment.py script. Normally, this is done on the command line with a command like this from the NiftyNet root directory:
python net_segment.py train --conf demos/PROMISE12/promise12_demo_train_config.ini --max_iter 10
Notice that we use configuration file that is specific to this experiment. This file contains default settings. Also note that we can override these settings on the command line.
To execute NiftyNet from within the notebook, you can run the following python code:
In [ ]:
import os
import sys
import niftynet
sys.argv=['','train','-a','net_segment','--conf',os.path.join('demos','PROMISE12','promise12_demo_train_config.ini'),'--max_iter','10']
niftynet.main()
Now you have trained (a few iterations of) a deep learning network for medical image segmentation. If you have some time on your hands, you can finish training the network (by leaving off the max_iter argument) and try it out, by running the following command
python net_segment.py inference --conf demos/PROMISE12/promise12_demo_inference_config.ini
or the following python code in the Notebook
In [ ]:
import os
import sys
import niftynet
sys.argv=['', 'inference','-a','net_segment','--conf',os.path.join('demos','PROMISE12','promise12_demo_inference_config.ini')]
niftynet.main()
Otherwise, you can load up some pre-trained weights for the network:
python net_segment.py inference --conf demo/PROMISE12/promise12_demo_config.ini --model_dir demo/PROMISE12/pretrained
or the following python code in the Notebook
In [ ]:
import os
import sys
import niftynet
sys.argv=['', 'inference','-a','net_segment','--conf',os.path.join('demos','PROMISE12','promise12_demo_inference_config.ini'), '--model_dir', os.path.join('demos','PROMISE12','pretrained')]
niftynet.main()
You can find your segmented images in output/promise12_demo
NiftyNet has taken care of a lot of details behind the scenes:
All of this was controlled by the configuration file.
Let's take a closer look at the configuration file. Further details about the configuration settings are available in config/readme.md
These lines define how NiftyNet organizes your data. In this case, in the ./data/PROMISE12 folder there is one T2-weighted MR image named 'Case??_T2.nii.gz' and one reference segmentation named 'Case??_segmentation.nii.gz' per patient. The images for each patient are automatically grouped because they share the same prefix 'Case??'. For training, we exclude patients Case20-Case26, and for inference, we only include patients Case20-Case26, so that our training and inference data are mutually exclusive.
These lines are setting up some system parameters: which GPUs to use (in this case whatever is available), where to save the trained network parameters, and how many threads to use for queuing them up.
The following lines specify network properties.
In this demo