Skip to content

Commit 6194324

Browse files
mmarcinkiewicznv-kkudrynski
authored andcommitted
[UNET3D/TF] Maintenance release 21.10
1 parent 0fadac2 commit 6194324

22 files changed

+1001
-552
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
ARG FROM_IMAGE_NAME=nvcr.io/nvidia/tensorflow:20.06-tf1-py3
1+
ARG FROM_IMAGE_NAME=nvcr.io/nvidia/tensorflow:21.10-tf1-py3
22
FROM ${FROM_IMAGE_NAME}
33

44
ADD . /workspace/unet3d
55
WORKDIR /workspace/unet3d
66

7-
RUN pip install git+https://github.com/NVIDIA/dllogger
7+
RUN pip install nvidia-pyindex
8+
RUN pip install nvidia-dllogger==0.1.0
89
RUN pip install --disable-pip-version-check -r requirements.txt
10+
11+
ENV TF_GPU_HOST_MEM_LIMIT_IN_MB=120000
12+
ENV XLA_FLAGS="--xla_multiheap_size_constraint_per_heap=2600000000"
13+
ENV OMPI_MCA_coll_hcoll_enable=0

TensorFlow/Segmentation/UNet_3D_Medical/README.md

+90-72
Large diffs are not rendered by default.

TensorFlow/Segmentation/UNet_3D_Medical/dataset/data_loader.py

+150-106
Large diffs are not rendered by default.

TensorFlow/Segmentation/UNet_3D_Medical/dataset/preprocess_data.py

+55-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
1+
# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -12,6 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
""" Preprocess dataset and prepare it for training
16+
17+
Example usage:
18+
$ python preprocess_data.py --input_dir ./src --output_dir ./dst
19+
--vol_per_file 2
20+
21+
All arguments are listed under `python preprocess_data.py -h`.
22+
23+
"""
1524
import os
1625
import argparse
1726
from random import shuffle
@@ -20,7 +29,6 @@
2029
import nibabel as nib
2130
import tensorflow as tf
2231

23-
2432
PARSER = argparse.ArgumentParser()
2533

2634
PARSER.add_argument('--input_dir', '-i',
@@ -38,10 +46,15 @@
3846

3947

4048
def load_features(path):
49+
""" Load features from Nifti
50+
51+
:param path: Path to dataset
52+
:return: Loaded data
53+
"""
4154
data = np.zeros((240, 240, 155, 4), dtype=np.uint8)
4255
name = os.path.basename(path)
4356
for i, modality in enumerate(["_t1.nii.gz", "_t1ce.nii.gz", "_t2.nii.gz", "_flair.nii.gz"]):
44-
vol = load_single_nifti(os.path.join(path, name+modality)).astype(np.float32)
57+
vol = load_single_nifti(os.path.join(path, name + modality)).astype(np.float32)
4558
vol[vol > 0.85 * vol.max()] = 0.85 * vol.max()
4659
vol = 255 * vol / vol.max()
4760
data[..., i] = vol.astype(np.uint8)
@@ -50,16 +63,37 @@ def load_features(path):
5063

5164

5265
def load_segmentation(path):
66+
""" Load segmentations from Nifti
67+
68+
:param path: Path to dataset
69+
:return: Loaded data
70+
"""
5371
path = os.path.join(path, os.path.basename(path)) + "_seg.nii.gz"
5472
return load_single_nifti(path).astype(np.uint8)
5573

5674

5775
def load_single_nifti(path):
76+
""" Load Nifti file as numpy
77+
78+
:param path: Path to file
79+
:return: Loaded data
80+
"""
5881
data = nib.load(path).get_fdata().astype(np.int16)
5982
return np.transpose(data, (1, 0, 2))
6083

6184

62-
def write_to_file(features_list, labels_list, foreground_mean_list, foreground_std_list, output_dir, count):
85+
def write_to_file(features_list, labels_list, foreground_mean_list, foreground_std_list, output_dir, # pylint: disable=R0913
86+
count):
87+
""" Dump numpy array to tfrecord
88+
89+
:param features_list: List of features
90+
:param labels_list: List of labels
91+
:param foreground_mean_list: List of means for each volume
92+
:param foreground_std_list: List of std for each volume
93+
:param output_dir: Directory where to write
94+
:param count: Index of the record
95+
:return:
96+
"""
6397
output_filename = os.path.join(output_dir, "volume-{}.tfrecord".format(count))
6498
filelist = list(zip(np.array(features_list),
6599
np.array(labels_list),
@@ -69,17 +103,22 @@ def write_to_file(features_list, labels_list, foreground_mean_list, foreground_s
69103

70104

71105
def np_to_tfrecords(filelist, output_filename):
106+
""" Convert numpy array to tfrecord
107+
108+
:param filelist: List of files
109+
:param output_filename: Destination directory
110+
"""
72111
writer = tf.io.TFRecordWriter(output_filename)
73112

74-
for idx in range(len(filelist)):
75-
X = filelist[idx][0].flatten().tostring()
76-
Y = filelist[idx][1].flatten().tostring()
77-
mean = filelist[idx][2].astype(np.float32).flatten()
78-
stdev = filelist[idx][3].astype(np.float32).flatten()
113+
for file_item in filelist:
114+
sample = file_item[0].flatten().tostring()
115+
label = file_item[1].flatten().tostring()
116+
mean = file_item[2].astype(np.float32).flatten()
117+
stdev = file_item[3].astype(np.float32).flatten()
79118

80119
d_feature = {}
81-
d_feature['X'] = tf.train.Feature(bytes_list=tf.train.BytesList(value=[X]))
82-
d_feature['Y'] = tf.train.Feature(bytes_list=tf.train.BytesList(value=[Y]))
120+
d_feature['X'] = tf.train.Feature(bytes_list=tf.train.BytesList(value=[sample]))
121+
d_feature['Y'] = tf.train.Feature(bytes_list=tf.train.BytesList(value=[label]))
83122
d_feature['mean'] = tf.train.Feature(float_list=tf.train.FloatList(value=mean))
84123
d_feature['stdev'] = tf.train.Feature(float_list=tf.train.FloatList(value=stdev))
85124

@@ -90,8 +129,9 @@ def np_to_tfrecords(filelist, output_filename):
90129
writer.close()
91130

92131

93-
def main():
94-
# parse arguments
132+
def main(): # pylint: disable=R0914
133+
""" Starting point of the application"""
134+
95135
params = PARSER.parse_args()
96136
input_dir = params.input_dir
97137
output_dir = params.output_dir
@@ -101,7 +141,7 @@ def main():
101141
if params.single_data_dir:
102142
patient_list.extend([os.path.join(input_dir, folder) for folder in os.listdir(input_dir)])
103143
else:
104-
assert "HGG" in os.listdir(input_dir) and "LGG" in os.listdir(input_dir),\
144+
assert "HGG" in os.listdir(input_dir) and "LGG" in os.listdir(input_dir), \
105145
"Data directory has to contain folders named HGG and LGG. " \
106146
"If you have a single folder with patient's data please set --single_data_dir flag"
107147
path_hgg = os.path.join(input_dir, "HGG")
@@ -135,7 +175,7 @@ def main():
135175
foreground_mean_list.append(fg_mean)
136176
foreground_std_list.append(fg_std)
137177

138-
if (i+1) % params.vol_per_file == 0:
178+
if (i + 1) % params.vol_per_file == 0:
139179
write_to_file(features_list, labels_list, foreground_mean_list, foreground_std_list, output_dir, count)
140180

141181
# Clear lists
@@ -158,4 +198,3 @@ def main():
158198

159199
if __name__ == '__main__':
160200
main()
161-

0 commit comments

Comments
 (0)