Switch to unified view

a/README.md b/README.md
1
1
2
2
3
4
[<img src="https://img.shields.io/badge/chat-slack%20channel-75BBC4.svg">](https://join.slack.com/t/mdtoolkit/shared_invite/enQtNTQ3MjY2MzE0MDg2LWNjY2I2Njc5MTY0NmM0ZWIxNmQwZDRhYzk2MDdhM2QxYjliYTcwYzhkNTAxYmRkMDA0MjcyNDMyYjllNTZhY2M)
5
<p align="center"><img src="assets/mdt_logo_2.png"  width=450></p><br>
6
7
Copyright © German Cancer Research Center (DKFZ), <a href="https://www.dkfz.de/en/mic/index.php">Division of Medical Image Computing (MIC)</a>. Please make sure that your usage of this code is in compliance with the code <a href="https://github.com/pfjaeger/medicaldetectiontoolkit/blob/master/LICENSE">license</a>.  
3
Copyright © German Cancer Research Center (DKFZ), <a href="https://www.dkfz.de/en/mic/index.php">Division of Medical Image Computing (MIC)</a>. Please make sure that your usage of this code is in compliance with the code <a href="https://github.com/pfjaeger/medicaldetectiontoolkit/blob/master/LICENSE">license</a>.  
8
4
9
## Release Notes
5
## Release Notes
10
**v0.1.0**: 
6
**v0.1.0**: 
11
- Updates to python 3.7, torch 1.4.0, torchvision 0.5.0, entailing a change in custom extensions NMS and RoIAlign 
7
- Updates to python 3.7, torch 1.4.0, torchvision 0.5.0, entailing a change in custom extensions NMS and RoIAlign 
12
        (now in C++ and CUDA).
8
        (now in C++ and CUDA).
13
- Scalar monitoring is changed to torch-included tensorboard. 
9
- Scalar monitoring is changed to torch-included tensorboard. 
14
- Added qualitative example plots for validation and testing. 
10
- Added qualitative example plots for validation and testing. 
15
- Default optimizer is changed to AdamW instead of Adam to account for fix in weight-decay handling, 
11
- Default optimizer is changed to AdamW instead of Adam to account for fix in weight-decay handling, 
16
norms and biases can optionally be excluded from weight decay. 
12
norms and biases can optionally be excluded from weight decay. 
17
- Introduced optional dynamic learning-rate scheduling. 
13
- Introduced optional dynamic learning-rate scheduling. 
18
- A specific CUDA device can now be selected via script argument.
14
- A specific CUDA device can now be selected via script argument.
19
- Inside the models, GT class labels identification is changed from `'roi_labels'` to `'class_target'` to streamline naming scheme.
15
- Inside the models, GT class labels identification is changed from `'roi_labels'` to `'class_target'` to streamline naming scheme.
20
- Added dataset [tutorial](experiments/tutorial.md).
16
- Added dataset [tutorial](experiments/tutorial.md).
21
17
22
**v0.0.2**: Small fixes mainly regarding server-env settings (cluster deployment).\
18
**v0.0.2**: Small fixes mainly regarding server-env settings (cluster deployment).\
23
**v0.0.1**: Original framework as used for the corresponding paper, with Python 3.6 and torch 0.4.1 dependencies, 
19
**v0.0.1**: Original framework as used for the corresponding paper, with Python 3.6 and torch 0.4.1 dependencies, 
24
        custom extensions NMS and RoIAlign in C and CUDA, scalar monitoring via plot files.
20
        custom extensions NMS and RoIAlign in C and CUDA, scalar monitoring via plot files.
25
        
21
        
26
## Overview
22
## Overview
27
This is a comprehensive framework for object detection featuring:
23
This is a comprehensive framework for object detection featuring:
28
- 2D + 3D implementations of prevalent object detectors: e.g. Mask R-CNN [1], Retina Net [2], Retina U-Net [3]. 
24
- 2D + 3D implementations of prevalent object detectors: e.g. Mask R-CNN [1], Retina Net [2], Retina U-Net [3]. 
29
- Modular and light-weight structure ensuring sharing of all processing steps (incl. backbone architecture) for comparability of models.
25
- Modular and light-weight structure ensuring sharing of all processing steps (incl. backbone architecture) for comparability of models.
30
- training with bounding box and/or pixel-wise annotations.
26
- training with bounding box and/or pixel-wise annotations.
31
- dynamic patching and tiling of 2D + 3D images (for training and inference).
27
- dynamic patching and tiling of 2D + 3D images (for training and inference).
32
- weighted consolidation of box predictions across patch-overlaps, ensembles, and dimensions [3].
28
- weighted consolidation of box predictions across patch-overlaps, ensembles, and dimensions [3].
33
- monitoring + evaluation simultaneously on object and patient level. 
29
- monitoring + evaluation simultaneously on object and patient level. 
34
- 2D + 3D output visualizations.
30
- 2D + 3D output visualizations.
35
- integration of COCO mean average precision metric [5]. 
31
- integration of COCO mean average precision metric [5]. 
36
- integration of MIC-DKFZ batch generators for extensive data augmentation [6].
32
- integration of MIC-DKFZ batch generators for extensive data augmentation [6].
37
- easy modification to evaluation of instance segmentation and/or semantic segmentation.
33
- easy modification to evaluation of instance segmentation and/or semantic segmentation.
38
<br/>
34
<br/>
39
[1] He, Kaiming, et al.  <a href="https://arxiv.org/abs/1703.06870">"Mask R-CNN"</a> ICCV, 2017<br>
35
[1] He, Kaiming, et al.  <a href="https://arxiv.org/abs/1703.06870">"Mask R-CNN"</a> ICCV, 2017<br>
40
[2] Lin, Tsung-Yi, et al.  <a href="https://arxiv.org/abs/1708.02002">"Focal Loss for Dense Object Detection"</a> TPAMI, 2018.<br>
36
[2] Lin, Tsung-Yi, et al.  <a href="https://arxiv.org/abs/1708.02002">"Focal Loss for Dense Object Detection"</a> TPAMI, 2018.<br>
41
[3] Jaeger, Paul et al. <a href="http://arxiv.org/abs/1811.08661"> "Retina U-Net: Embarrassingly Simple Exploitation
37
[3] Jaeger, Paul et al. <a href="http://arxiv.org/abs/1811.08661"> "Retina U-Net: Embarrassingly Simple Exploitation
42
of Segmentation Supervision for Medical Object Detection" </a>, 2018
38
of Segmentation Supervision for Medical Object Detection" </a>, 2018
43
39
44
[5] https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/cocoeval.py<br/>
40
[5] https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/cocoeval.py<br/>
45
[6] https://github.com/MIC-DKFZ/batchgenerators<br/><br>
41
[6] https://github.com/MIC-DKFZ/batchgenerators<br/><br>
46
42
47
A tutorial on how to add your own data set can be found under [`experiments/tutorial.md`](experiments/tutorial.md).
43
A tutorial on how to add your own data set can be found under [`experiments/tutorial.md`](experiments/tutorial.md).
48
44
49
## How to cite this code
45
## How to cite this code
50
Please cite the original publication [3].
46
Please cite the original publication [3].
51
47
52
## Installation
48
## Installation
53
Setup package in virtual environment
49
Setup package in virtual environment
54
```
50
```
55
git clone https://github.com/MIC-DKFZ/medicaldetectiontoolkit.git.
51
git clone https://github.com/MIC-DKFZ/medicaldetectiontoolkit.git.
56
cd medicaldetectiontoolkit
52
cd medicaldetectiontoolkit
57
virtualenv -p python3.7 mdt
53
virtualenv -p python3.7 mdt
58
source mdt/bin/activate
54
source mdt/bin/activate
59
python setup.py install
55
python setup.py install
60
```
56
```
61
##### Custom Extensions
57
##### Custom Extensions
62
This framework uses two custom mixed C++/CUDA extensions: Non-maximum suppression (NMS) and RoIAlign. Both are adapted from the original pytorch extensions (under torchvision.ops.boxes and ops.roialign).
58
This framework uses two custom mixed C++/CUDA extensions: Non-maximum suppression (NMS) and RoIAlign. Both are adapted from the original pytorch extensions (under torchvision.ops.boxes and ops.roialign).
63
The extensions are automatically compiled from the provided source files under medicaldetectiontoolkit/custom_extensions with above setup.py. 
59
The extensions are automatically compiled from the provided source files under medicaldetectiontoolkit/custom_extensions with above setup.py. 
64
However, the extensions need to be compiled specifically for certain GPU architectures. Hence, please ensure that the architectures you need are included in your shell's
60
However, the extensions need to be compiled specifically for certain GPU architectures. Hence, please ensure that the architectures you need are included in your shell's
65
environment variable ```TORCH_CUDA_ARCH_LIST``` before compilation. 
61
environment variable ```TORCH_CUDA_ARCH_LIST``` before compilation. 
66
62
67
Example: You want to use the modules with the new TITAN RTX GPU, which has 
63
Example: You want to use the modules with the new TITAN RTX GPU, which has 
68
Compute Capability 7.5 (Turing Architecture), but sometimes you also want to use it with a TITAN Xp (6.1, Pascal). Before installation you need to
64
Compute Capability 7.5 (Turing Architecture), but sometimes you also want to use it with a TITAN Xp (6.1, Pascal). Before installation you need to
69
```export TORCH_CUDA_ARCH_LIST="6.1;7.5"```. A link list of GPU model names to Compute Capability can be found here: https://developer.nvidia.com/cuda-gpus. 
65
```export TORCH_CUDA_ARCH_LIST="6.1;7.5"```. A link list of GPU model names to Compute Capability can be found here: https://developer.nvidia.com/cuda-gpus. 
70
Note: If you'd like to import the raw extensions (not the wrapper modules), be sure to import torch first.
66
Note: If you'd like to import the raw extensions (not the wrapper modules), be sure to import torch first.
71
67
72
68
73
## Prepare the Data
69
## Prepare the Data
74
This framework is meant for you to be able to train models on your own data sets. 
70
This framework is meant for you to be able to train models on your own data sets. 
75
Two example data loaders are provided in medicaldetectiontoolkit/experiments including thorough documentation to ensure a quick start for your own project. The way I load Data is to have a preprocessing script, which after preprocessing saves the Data of whatever data type into numpy arrays (this is just run once). During training / testing, the data loader then loads these numpy arrays dynamically. (Please note the Data Input side is meant to be customized by you according to your own needs and the provided Data loaders are merely examples: LIDC has a powerful Dataloader that handles 2D/3D inputs and is optimized for patch-based training and inference. Toy-Experiments have a lightweight Dataloader, only handling 2D without patching. The latter makes sense if you want to get familiar with the framework.).
71
Two example data loaders are provided in medicaldetectiontoolkit/experiments including thorough documentation to ensure a quick start for your own project. The way I load Data is to have a preprocessing script, which after preprocessing saves the Data of whatever data type into numpy arrays (this is just run once). During training / testing, the data loader then loads these numpy arrays dynamically. (Please note the Data Input side is meant to be customized by you according to your own needs and the provided Data loaders are merely examples: LIDC has a powerful Dataloader that handles 2D/3D inputs and is optimized for patch-based training and inference. Toy-Experiments have a lightweight Dataloader, only handling 2D without patching. The latter makes sense if you want to get familiar with the framework.).
76
72
77
## Execute
73
## Execute
78
1. Set I/O paths, model and training specifics in the configs file: medicaldetectiontoolkit/experiments/your_experiment/configs.py
74
1. Set I/O paths, model and training specifics in the configs file: medicaldetectiontoolkit/experiments/your_experiment/configs.py
79
2. Train the model: 
75
2. Train the model: 
80
76
81
    ```
77
    ```
82
    python exec.py --mode train --exp_source experiments/my_experiment --exp_dir path/to/experiment/directory       
78
    python exec.py --mode train --exp_source experiments/my_experiment --exp_dir path/to/experiment/directory       
83
    ``` 
79
    ``` 
84
    This copies snapshots of configs and model to the specified exp_dir, where all outputs will be saved. By default, the data is split into 60% training and 20% validation and 20% testing data to perform a 5-fold cross validation (can be changed to hold-out test set in configs) and all folds will be trained iteratively. In order to train a single fold, specify it using the folds arg: 
80
    This copies snapshots of configs and model to the specified exp_dir, where all outputs will be saved. By default, the data is split into 60% training and 20% validation and 20% testing data to perform a 5-fold cross validation (can be changed to hold-out test set in configs) and all folds will be trained iteratively. In order to train a single fold, specify it using the folds arg: 
85
    ```
81
    ```
86
    python exec.py --folds 0 1 2 .... # specify any combination of folds [0-4]
82
    python exec.py --folds 0 1 2 .... # specify any combination of folds [0-4]
87
    ```
83
    ```
88
3. Run inference:
84
3. Run inference:
89
    ```
85
    ```
90
    python exec.py --mode test --exp_dir path/to/experiment/directory 
86
    python exec.py --mode test --exp_dir path/to/experiment/directory 
91
    ```
87
    ```
92
    This runs the prediction pipeline and saves all results to exp_dir.
88
    This runs the prediction pipeline and saves all results to exp_dir.
93
    
89
    
94
    
90
    
95
## Models
91
## Models
96
92
97
This framework features all models explored in [3] (implemented in 2D + 3D): The proposed Retina U-Net, a simple but effective Architecture fusing state-of-the-art semantic segmentation with object detection,<br><br>
93
This framework features all models explored in [3] (implemented in 2D + 3D): The proposed Retina U-Net, a simple but effective Architecture fusing state-of-the-art semantic segmentation with object detection,<br><br>
98
<p align="center"><img src="assets/retu_figure.png"  width=50%></p><br>
94
<p align="center">
99
also implementations of prevalent object detectors, such as Mask R-CNN, Faster R-CNN+ (Faster R-CNN w\ RoIAlign), Retina Net, U-Faster R-CNN+ (the two stage counterpart of Retina U-Net: Faster R-CNN with auxiliary semantic segmentation), DetU-Net (a U-Net like segmentation architecture with heuristics for object detection.)<br><br><br>
95
also implementations of prevalent object detectors, such as Mask R-CNN, Faster R-CNN+ (Faster R-CNN w\ RoIAlign), Retina Net, U-Faster R-CNN+ (the two stage counterpart of Retina U-Net: Faster R-CNN with auxiliary semantic segmentation), DetU-Net (a U-Net like segmentation architecture with heuristics for object detection.)<br><br><br>
100
<p align="center"><img src="assets/baseline_figure.png"  width=85%></p><br>
96
<p align="center"></p><br>
101
97
102
## Training annotations
98
## Training annotations
103
This framework features training with pixelwise and/or bounding box annotations. To overcome the issue of box coordinates in 
99
This framework features training with pixelwise and/or bounding box annotations. To overcome the issue of box coordinates in 
104
data augmentation, we feed the annotation masks through data augmentation (create a pseudo mask, if only bounding box annotations provided) and draw the boxes afterwards.<br><br>
100
data augmentation, we feed the annotation masks through data augmentation (create a pseudo mask, if only bounding box annotations provided) and draw the boxes afterwards.<br><br>
105
<p align="center"><img src="assets/annotations.png"  width=85%></p><br>
101
<p align="center"></p><br>
106
102
107
103
108
The framework further handles two types of pixel-wise annotations: 
104
The framework further handles two types of pixel-wise annotations: 
109
105
110
1. A label map with individual ROIs identified by increasing label values, accompanied by a vector containing in each position the class target for the lesion with the corresponding label (for this mode set get_rois_from_seg_flag = False when calling ConvertSegToBoundingBoxCoordinates in your Data Loader).
106
1. A label map with individual ROIs identified by increasing label values, accompanied by a vector containing in each position the class target for the lesion with the corresponding label (for this mode set get_rois_from_seg_flag = False when calling ConvertSegToBoundingBoxCoordinates in your Data Loader).
111
2. A binary label map. There is only one foreground class and single lesions are not identified. All lesions have the same class target (foreground). In this case the Dataloader runs a Connected Component Labelling algorithm to create processable lesion - class target pairs on the fly (for this mode set get_rois_from_seg_flag = True when calling ConvertSegToBoundingBoxCoordinates in your Data Loader). 
107
2. A binary label map. There is only one foreground class and single lesions are not identified. All lesions have the same class target (foreground). In this case the Dataloader runs a Connected Component Labelling algorithm to create processable lesion - class target pairs on the fly (for this mode set get_rois_from_seg_flag = True when calling ConvertSegToBoundingBoxCoordinates in your Data Loader). 
112
108
113
## Prediction pipeline
109
## Prediction pipeline
114
This framework provides an inference module, which automatically handles patching of inputs, and tiling, ensembling, and weighted consolidation of output predictions:<br><br><br>
110
This framework provides an inference module, which automatically handles patching of inputs, and tiling, ensembling, and weighted consolidation of output predictions:<br><br><br>
115
<img src="assets/prediction_pipeline.png" ><br><br>
111
<br><br>
116
112
117
113
118
## Consolidation of predictions (Weighted Box Clustering)
114
## Consolidation of predictions (Weighted Box Clustering)
119
Multiple predictions of the same image (from  test time augmentations, tested epochs and overlapping patches), result in a high amount of boxes (or cubes), which need to be consolidated. In semantic segmentation, the final output would typically be obtained by averaging every pixel over all predictions. As described in [3], **weighted box clustering** (WBC) does this for box predictions:<br>
115
Multiple predictions of the same image (from  test time augmentations, tested epochs and overlapping patches), result in a high amount of boxes (or cubes), which need to be consolidated. In semantic segmentation, the final output would typically be obtained by averaging every pixel over all predictions. As described in [3], **weighted box clustering** (WBC) does this for box predictions:<br>
120
<p align="center"><img src="assets/wcs_text.png"  width=650><br><br></p>
116
121
<p align="center"><img src="assets/wcs_readme.png"  width=800><br><br></p>
117
122
118
123
124
125
## Visualization / Monitoring
126
By default, loss functions and performance metrics are monitored:<br><br><br>
127
<img src="assets/loss_monitoring.png"  width=700><br>
128
<hr>
129
Histograms of matched output predictions for training/validation/testing are plotted per foreground class:<br><br><br>
130
<img src="assets/hist_example.png"  width=550>
131
<hr>
132
Input images + ground truth annotations + output predictions of a sampled validation abtch are plotted after each epoch (here 2D sampled slice with +-3 neighbouring context slices in channels):<br><br><br>
133
<img src="assets/output_monitoring_1.png"  width=750>
134
<hr>
135
Zoomed into the last two lines of the plot:<br><br><br>
136
<img src="assets/output_monitoring_2.png"  width=700>
137
138
139
## License
119
## License
140
This framework is published under the [Apache License Version 2.0](LICENSE).
120
This framework is published under the [Apache License Version 2.0](LICENSE).
141
121
142
122
143
123
144
124
145
125