-
Notifications
You must be signed in to change notification settings - Fork 38
/
segment_a_spleen.py
executable file
·79 lines (62 loc) · 2.92 KB
/
segment_a_spleen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2017 Division of Medical Image Computing, German Cancer Research Center (DKFZ)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
from medpy.io import save
from configs.Config_unet_spleen import get_config
from datasets.spleen.preprocessing import preprocess_single_file, postprocess_single_image
from experiments.UNetExperiment import UNetExperiment
def save_single_image(image, image_header, filename):
# medpy.io.save
save(image, filename, image_header)
print('> Resulting Image stored as {}'.format(filename))
if __name__ == "__main__":
c = get_config()
if len(sys.argv) == 1:
print("USAGE:\n\npython {} imagefilename [model_checkpoint [shapesize]]\n\n"
" imagefilename - a filename that stores a nii.gz formatted file.\n"
" model_checkpoint - a checkpoint filename to reload\n"
" shapesize - optional value that defines "
"the size of the shape, default is 64 (not yet used).".format(sys.argv[0]))
filename = "data/Task09_Spleen/imagesTs/spleen_15.nii.gz"
else:
filename = sys.argv[1]
print("Loading and processing file {}".format(filename))
if len(sys.argv) > 2:
c.checkpoint_dir = sys.argv[2]
c.do_load_checkpoint = True
print("Loading model from checkpoint {}".format(c.model_dir))
if len(c.model_dir) == 0 or not os.path.isdir(os.path.split(c.model_dir)[0]):
print("ERROR /!\\: No checkpoint dir is set, please provide in Config file.")
exit()
shapesize = 64
if len(sys.argv) > 3:
shapesize = int(sys.argv[3])
# Get the header in order to preserve voxel dimensions to store the segmented image later on
print('Preprocessing data.')
data, header = preprocess_single_file(filename, y_shape=shapesize, z_shape=shapesize)
print('Setting up model and start segmentation.')
exp = UNetExperiment(config=c, name=c.name, n_epochs=c.n_epochs,
seed=42, append_rnd_to_name=c.append_rnd_string, globs=globals()
)
result = exp.segment_single_image(data)
print('Postprocessing data.')
result = postprocess_single_image(result)
pathname, fname = os.path.split(filename)
destination_filename = pathname+"/segmented_"+fname
print('Saving file to disk: {}'.format(destination_filename))
save_single_image(result, header, destination_filename)