-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_loader.py
178 lines (152 loc) · 6.43 KB
/
data_loader.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import os.path as osp
import numpy as np
from data_layer import DataLoader
from utils.Liver_Kits import get_mhd_list, get_mhd_list_with_liver, mhd_reader
class MedImageLoader2D(DataLoader):
""" 2D medical image data loader.
Params
------
`rootdir`: data root directory
`datadir`: dataset directories, split with `+`. For example: "trainset1+trainset2"
`batch_size`: batch size of image loader
`wwidth`: medical image window width
`wlevel`: medical image window level
`img_channel`: image channel, default is 1 (i.e. gray image)
`once`: just loop once or not
`random`: if random flag is set, then the dataset is shuffled(if set) according to
system time, else only with numpy random seed.
`shuffle`: a boolean, shuffle dataset or not. If shuffle flag is not set, then `random`
will be disabled.
Note: For experiemnts repeatable, please set fixed cfg.RNG_SEED and random=False.
"""
def __init__(self, rootdir, datadir, batch_size,
wwidth=None,
wlevel=None,
img_channel=1,
once=False,
random=False,
shuffle=False):
datadirs = datadir.split("+")
self._db_path = []
for d in datadirs:
self._db_path.append(osp.join(rootdir, d))
self._images = get_mhd_list_with_liver(osp.join(self._db_path[0], "mask"))
for path in self._db_path[1:]:
self._images.extend(get_mhd_list_with_liver(osp.join(path, "mask")))
self._batch_size = batch_size
self._wwidth = wwidth
self._wlevel = wlevel
self._img_channel = img_channel
self._height = 512
self._width = 512
super(MedImageLoader2D, self).__init__(once, random, shuffle)
@property
def height(self):
return self._height
@property
def width(self):
return self._width
@property
def channel(self):
return self._img_channel
def next_minibatch(self, db_inds):
assert len(db_inds) == self._batch_size
images = np.zeros((self._batch_size, self.height, self.width, self.channel),
dtype=np.float32)
masks = np.zeros_like(images, dtype=np.int32)
image_names = []
for i, ind in enumerate(db_inds):
mask_file = self.images[ind]
image_file = mask_file.replace("mask", "liver").replace("_m_", "_o_")
_, mask = mhd_reader(mask_file)
_, image = mhd_reader(image_file)
mask = np.reshape(mask.copy(), (self.height, self.width, self.channel))
image = np.reshape(image, (self.height, self.width, self.channel))
thresh = -1000 if mask[0, 0, 0] < -1000 else 0
mask[mask > thresh] = 1
mask[mask < thresh] = 0
masks[i,...] = mask.astype(np.int32)
images[i,...] = image
name = osp.basename(mask_file).replace("_m_", "_p_")
image_names.append(name)
# set window width and level
widd2 = self._wwidth / 2
images = (np.clip(images, self._wlevel - widd2, self._wlevel + widd2) -
(self._wlevel - widd2)) / 2**16 * self._wwidth
blob = {"images": images, "labels": masks, "names": image_names}
return blob
class MedImageLoader3D(DataLoader):
""" 3D medical image data loader.
Params
------
`rootdir`: data root directory
`datadir`: dataset directories, split with `+`. For example: "trainset1+trainset2"
`batch_size`: batch size of image loader, only support 1
`wwidth`: medical image window width
`wlevel`: medical image window level
`img_channel`: image channel, default is 1 (i.e. gray image)
`once`: just loop once or not
`random`: if random flag is set, then the dataset is shuffled(if set) according to
system time, else only with numpy random seed.
`shuffle`: a boolean, shuffle dataset or not. If shuffle flag is not set, then `random`
will be disabled.
Note: For experiemnts repeatable, please set fixed cfg.RNG_SEED and random=False.
"""
def __init__(self, rootdir, datadir, batch_size,
wwidth=None,
wlevel=None,
img_channel=1,
once=False,
random=False,
shuffle=False):
datadirs = datadir.split('+')
self._db_path = []
for d in datadirs:
self._db_path.append(osp.join(rootdir, d))
self._images = get_mhd_list(osp.join(self._db_path[0], "mask"))
for path in self._db_path[1:]:
self._images.extend(get_mhd_list(osp.join(path, "mask")))
self._batch_size = batch_size
self._wwidth = wwidth
self._wlevel = wlevel
self._volume_channels = img_channel
self._height = 512
self._width = 512
super(MedImageLoader3D, self).__init__(once, random, shuffle)
@property
def height(self):
return self._height
@property
def width(self):
return self._width
@property
def channels(self):
return self._volume_channels
def next_minibatch(self, db_inds):
assert len(db_inds) == self.batch_size
images = []
masks = []
image_names = []
meta_datas = []
for i, ind in enumerate(db_inds):
mask_file = self.images[ind]
image_file = mask_file.replace("mask", "liver").replace("_m", "")
_, mask = mhd_reader(mask_file)
meta_data, image = mhd_reader(image_file)
mask = np.reshape(mask.copy(), (-1, self.height, self.width, self.channels)) # depth is finally determined
thresh = -1000 if mask[0, 0, 0] < -1000 else 0
mask[mask > thresh] = 1
mask[mask < thresh] = 0
mask = mask.astype(np.int32)
image = np.reshape(image, (-1, self.height, self.width, self.channels))
# set window width and level
widd2 = self._wwidth / 2
image = (np.clip(image, self._wlevel - widd2, self._wlevel + widd2) -
(self._wlevel - widd2)) / 2**16 * self._wwidth
images.append(image)
masks.append(mask)
name = osp.basename(mask_file).replace("_m", "_p")
image_names.append(name)
meta_datas.append(meta_data)
blob = {"images": images, "labels": masks, "names": image_names, "meta": meta_datas}
return blob