Commit
·
faf3b5a
0
Parent(s):
initial commit
Browse files- LICENSE +21 -0
- README.md +33 -0
- figures/Segmentation_CustusX.PNG +0 -0
- livermask/livermask.py +140 -0
- requirements.txt +40 -0
- setup.py +37 -0
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2020 André Pedersen
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Automatic liver segmentation in CT using deep learning
|
2 |
+
|
3 |
+
#### NOTE: Trained 2D model on the LITS dataset is automatically downloaded when running the inference script and can be used as you wish, but please, give credit. ENJOY! :)
|
4 |
+
|
5 |
+
|
6 |
+

|
7 |
+
|
8 |
+
The figure shows a predicted liver with the corresponding patient CT in 3DSlicer. It is the Volume-10 from the LITS17 dataset.
|
9 |
+
|
10 |
+
First of all:
|
11 |
+
The LITS dataset can be accessible from here (https://competitions.codalab.org), and the corresponding paper for the challenge (Bilic. P et al.. (2019). The Liver Tumor Segmentation Benchmark (LiTS). https://arxiv.org/abs/1901.04056). If trained model is used please cite this paper.
|
12 |
+
|
13 |
+
Usage:
|
14 |
+
> git clone https://github.com/andreped/livermask.git \
|
15 |
+
> cd livermask \
|
16 |
+
> python3 -m venv venv \
|
17 |
+
> python -m pip install -r /path/to/requirements.txt . \ <- might want to run > python setup.py bdist_wheel < before
|
18 |
+
> cd livermask \
|
19 |
+
> python livermask.py "path_to_ct_nifti.nii" "output_name.nii"
|
20 |
+
|
21 |
+
If you lack any modules after, try installing them through setup.py (could be done instead of using requirements.txt):
|
22 |
+
> pip install wheel \
|
23 |
+
> python setup.py bdist_wheel
|
24 |
+
|
25 |
+
NOTE: Currently, model only works for the nifti format, and outputs a binary volume in the same format (*.nii). But this format can be imported in CustusX. I wouldn't recommend mixing DICOM and .nii prediction file in CustusX, as there seem to be some orientation issues between these (bug to be fixed in the future). But simply convert DICOM -> NIFTI using the command-line tool dcm2niix (https://github.com/rordenlab/dcm2niix).
|
26 |
+
|
27 |
+
Convert DICOM -> NIFTI doing this:
|
28 |
+
> dcm2niix -s y -m y -d 1 "path_to_CT_folder" "output_name"
|
29 |
+
|
30 |
+
Note that "-d 1" assumed that "path_to_CT_folder" is the folder just before the set of DICOM scans you want to import and convert. This can be removed if you want to convert multiple ones at the same time. It is possible to set "." for "output_name", which in theory should output a file with the same name as the DICOM folder, but that doesn't seem to happen...
|
31 |
+
|
32 |
+
A few final notes:
|
33 |
+
1) If you get SSLError during downloading the model, disable VPN, e.g. cisco. For those on the sintef network, try changing network to Eduroam or similar, as it might be a most-famous evry-issue...
|
figures/Segmentation_CustusX.PNG
ADDED
|
livermask/livermask.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import os, sys
|
3 |
+
import h5py
|
4 |
+
from tqdm import tqdm
|
5 |
+
import nibabel as nib
|
6 |
+
from nibabel.processing import resample_to_output, resample_from_to
|
7 |
+
from scipy.ndimage import zoom
|
8 |
+
from tensorflow.python.keras.models import load_model
|
9 |
+
import gdown
|
10 |
+
from skimage.morphology import remove_small_holes, binary_dilation, binary_erosion, ball
|
11 |
+
from skimage.measure import label, regionprops
|
12 |
+
import warnings
|
13 |
+
warnings.filterwarnings('ignore', '.*output shape of zoom.*')
|
14 |
+
|
15 |
+
def intensity_normalization(volume, intensity_clipping_range):
|
16 |
+
result = np.copy(volume)
|
17 |
+
|
18 |
+
result[volume < intensity_clipping_range[0]] = intensity_clipping_range[0]
|
19 |
+
result[volume > intensity_clipping_range[1]] = intensity_clipping_range[1]
|
20 |
+
|
21 |
+
min_val = np.amin(result)
|
22 |
+
max_val = np.amax(result)
|
23 |
+
if (max_val - min_val) != 0:
|
24 |
+
result = (result - min_val) / (max_val - min_val)
|
25 |
+
|
26 |
+
return result
|
27 |
+
|
28 |
+
def post_process(pred):
|
29 |
+
return pred
|
30 |
+
|
31 |
+
def get_model():
|
32 |
+
url = "https://drive.google.com/uc?id=181VE-FiqZ2z7xY30LK9GIvLeEEJW0YF-"
|
33 |
+
output = "model.h5"
|
34 |
+
md5 = "ef5a6dfb794b39bea03f5496a9a49d4d"
|
35 |
+
gdown.cached_download(url, output, md5=md5) #, postprocess=gdown.extractall)
|
36 |
+
|
37 |
+
def func(path, output):
|
38 |
+
|
39 |
+
cwd = "/".join(os.path.realpath(__file__).replace("\\", "/").split("/")[:-1]) + "/"
|
40 |
+
|
41 |
+
#print(cwd)
|
42 |
+
#print(" :) ")
|
43 |
+
|
44 |
+
name = cwd + "model.h5"
|
45 |
+
#name = "\.model.h5"
|
46 |
+
|
47 |
+
# get model
|
48 |
+
get_model()
|
49 |
+
|
50 |
+
# load model
|
51 |
+
model = load_model(name, compile=False)
|
52 |
+
|
53 |
+
print("preprocessing...")
|
54 |
+
nib_volume = nib.load(path)
|
55 |
+
new_spacing = [1., 1., 1.]
|
56 |
+
resampled_volume = resample_to_output(nib_volume, new_spacing, order=1)
|
57 |
+
data = resampled_volume.get_data().astype('float32')
|
58 |
+
|
59 |
+
curr_shape = data.shape
|
60 |
+
|
61 |
+
# resize to get (512, 512) output images
|
62 |
+
img_size = 512
|
63 |
+
data = zoom(data, [img_size / data.shape[0], img_size / data.shape[1], 1.0], order=1)
|
64 |
+
|
65 |
+
# intensity normalization
|
66 |
+
intensity_clipping_range = [-150, 250] # HU clipping limits (Pravdaray's configs)
|
67 |
+
data = intensity_normalization(volume=data, intensity_clipping_range=intensity_clipping_range)
|
68 |
+
|
69 |
+
# fix orientation
|
70 |
+
data = np.rot90(data, k=1, axes=(0, 1))
|
71 |
+
data = np.flip(data, axis=0)
|
72 |
+
|
73 |
+
print("predicting...")
|
74 |
+
# predict on data
|
75 |
+
pred = np.zeros_like(data).astype(np.float32)
|
76 |
+
for i in tqdm(range(data.shape[-1]), "pred: "):
|
77 |
+
pred[..., i] = model.predict(np.expand_dims(np.expand_dims(np.expand_dims(data[..., i], axis=0), axis=-1), axis=0))[0, ..., 1]
|
78 |
+
del data
|
79 |
+
|
80 |
+
# threshold
|
81 |
+
pred = (pred >= 0.4).astype(int)
|
82 |
+
|
83 |
+
# fix orientation back
|
84 |
+
pred = np.flip(pred, axis=0)
|
85 |
+
pred = np.rot90(pred, k=-1, axes=(0, 1))
|
86 |
+
|
87 |
+
print("resize back...")
|
88 |
+
# resize back from 512x512
|
89 |
+
pred = zoom(pred, [curr_shape[0] / img_size, curr_shape[1] / img_size, 1.0], order=1)
|
90 |
+
pred = (pred >= 0.5).astype(np.float32)
|
91 |
+
|
92 |
+
print("morphological post-processing...")
|
93 |
+
# morpological post-processing
|
94 |
+
# 1) first erode
|
95 |
+
pred = binary_erosion(pred.astype(bool), ball(3)).astype(np.float32)
|
96 |
+
|
97 |
+
# 2) keep only largest connected component
|
98 |
+
labels = label(pred)
|
99 |
+
regions = regionprops(labels)
|
100 |
+
area_sizes = []
|
101 |
+
for region in regions:
|
102 |
+
area_sizes.append([region.label, region.area])
|
103 |
+
area_sizes = np.array(area_sizes)
|
104 |
+
tmp = np.zeros_like(pred)
|
105 |
+
tmp[labels == area_sizes[np.argmax(area_sizes[:, 1]), 0]] = 1
|
106 |
+
pred = tmp.copy()
|
107 |
+
del tmp, labels, regions, area_sizes
|
108 |
+
|
109 |
+
# 3) dilate
|
110 |
+
pred = binary_dilation(pred.astype(bool), ball(3))
|
111 |
+
|
112 |
+
# 4) remove small holes
|
113 |
+
pred = remove_small_holes(pred.astype(bool), area_threshold=0.001*np.prod(pred.shape)).astype(np.float32)
|
114 |
+
|
115 |
+
print("saving...")
|
116 |
+
pred = pred.astype(np.uint8)
|
117 |
+
img = nib.Nifti1Image(pred, affine=resampled_volume.affine)
|
118 |
+
resampled_lab = resample_from_to(img, nib_volume, order=0)
|
119 |
+
nib.save(resampled_lab, output)
|
120 |
+
|
121 |
+
|
122 |
+
def main():
|
123 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
124 |
+
|
125 |
+
#__os.path
|
126 |
+
|
127 |
+
path = sys.argv[1]
|
128 |
+
output = sys.argv[2]
|
129 |
+
#output = sys.argv[3]
|
130 |
+
|
131 |
+
func(path, output)
|
132 |
+
|
133 |
+
|
134 |
+
if __name__ == "__main__":
|
135 |
+
main()
|
136 |
+
|
137 |
+
|
138 |
+
|
139 |
+
|
140 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
absl-py==0.9.0
|
2 |
+
astor==0.8.1
|
3 |
+
certifi==2019.11.28
|
4 |
+
chardet==3.0.4
|
5 |
+
cycler==0.10.0
|
6 |
+
decorator==4.4.1
|
7 |
+
filelock==3.0.12
|
8 |
+
gast==0.3.3
|
9 |
+
gdown==3.10.1
|
10 |
+
grpcio==1.26.0
|
11 |
+
h5py==2.10.0
|
12 |
+
idna==2.8
|
13 |
+
imageio==2.6.1
|
14 |
+
Keras-Applications==1.0.8
|
15 |
+
Keras-Preprocessing==1.1.0
|
16 |
+
kiwisolver==1.1.0
|
17 |
+
livermask==0.1
|
18 |
+
Markdown==3.2
|
19 |
+
matplotlib==3.1.3
|
20 |
+
mock==4.0.1
|
21 |
+
networkx==2.4
|
22 |
+
nibabel==3.0.1
|
23 |
+
numpy==1.18.1
|
24 |
+
Pillow==7.0.0
|
25 |
+
protobuf==3.11.3
|
26 |
+
pyparsing==2.4.6
|
27 |
+
PySocks==1.7.1
|
28 |
+
python-dateutil==2.8.1
|
29 |
+
PyWavelets==1.1.1
|
30 |
+
requests==2.22.0
|
31 |
+
scikit-image==0.16.2
|
32 |
+
scipy==1.4.1
|
33 |
+
six==1.14.0
|
34 |
+
tensorboard==1.13.1
|
35 |
+
tensorflow==1.13.1
|
36 |
+
tensorflow-estimator==1.13.0
|
37 |
+
termcolor==1.1.0
|
38 |
+
tqdm==4.42.1
|
39 |
+
urllib3==1.25.8
|
40 |
+
Werkzeug==1.0.0
|
setup.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import setuptools
|
2 |
+
|
3 |
+
with open("README.md", "r") as fh:
|
4 |
+
long_description = fh.read()
|
5 |
+
|
6 |
+
setuptools.setup(
|
7 |
+
name='livermask',
|
8 |
+
version='0.1',
|
9 |
+
author="Andre Pedersen",
|
10 |
+
author_email="[email protected]",
|
11 |
+
description="A package for automatic segmentation of liver from CT data",
|
12 |
+
long_description=long_description,
|
13 |
+
long_description_content_type="text/markdown",
|
14 |
+
url="https://github.com/andreped/livermask",
|
15 |
+
packages=setuptools.find_packages(),
|
16 |
+
entry_points={
|
17 |
+
'console_scripts': [
|
18 |
+
'livermask = livermask.livermask:main'
|
19 |
+
]
|
20 |
+
},
|
21 |
+
install_requires=[
|
22 |
+
'tensorflow==1.13.1',
|
23 |
+
'numpy',
|
24 |
+
'scipy',
|
25 |
+
'tqdm',
|
26 |
+
'nibabel',
|
27 |
+
'h5py',
|
28 |
+
'gdown',
|
29 |
+
'scikit-image'
|
30 |
+
],
|
31 |
+
classifiers=[
|
32 |
+
"Programming Language :: Python :: 3",
|
33 |
+
"License :: OSI Approved :: MIT License",
|
34 |
+
"Operating System :: OS Independent",
|
35 |
+
],
|
36 |
+
python_requires='>=3.6',
|
37 |
+
)
|