forked from shreyaspadhy/UNet-Zoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
371 lines (288 loc) · 12 KB
/
data.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import re
import torch
from torch.utils.data.dataset import Dataset
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import os
import scipy.io as sio
from dataParser import getImg, get_truth_file
from itertools import groupby
import torchvision.transforms as tr
from itertools import islice
# load prediction dataset
class UnetPred(Dataset):
__file = []
__im = []
im_ht = 0
im_wd = 0
dataset_size = 0
def __init__(self, predict_folder, keywords=["P1", "1", "flair"], im_size=[128, 128], transform=None):
self.__file = []
self.__im = []
self.im_ht = im_size[0]
self.im_wd = im_size[1]
self.transform = transform
folder = predict_folder
file_list = os.listdir(folder)
for file in file_list:
if file.endswith(".png"):
filename = os.path.splitext(file)[0]
filename_fragments = filename.split("_")
samekeywords = list(set(filename_fragments) & set(keywords))
if len(samekeywords) == len(keywords):
# 1. read file name
self.__file.append(file)
# 2. read raw image
# TODO: I think we should open image only in getitem,
# otherwise memory explodes
# rawImage = getImg(folder + file)
self.__im.append(os.path.join(folder, file))
# print(self.__im[-1])
# 3. read mask image
# mask_file = getMaskFileName(file)
# mask_file = get_truth_file(file, keywords[0])
# print(mask_file)
# maskImage = getImg(folder + mask_file)
# self.__mask.append(folder + mask_file)
# self.dataset_size = len(self.__file)
# print("lengths : ", len(self.__im), len(self.__mask))
self.dataset_size = len(self.__file)
sio.savemat('filelist2.mat', {'data': self.__im})
def __getitem__(self, index):
img = getImg(self.__im[index])
# mask = getImg(self.__mask[index])
img = img.resize((self.im_ht, self.im_wd))
# mask = mask.resize((self.im_ht, self.im_wd))
# mask.show()
if self.transform is not None:
# TODO: Not sure why not take full image
img_tr = self.transform(img)
# mask_tr = self.transform(mask)
# img_tr = self.transform(img[None, :, :])
# mask_tr = self.transform(mask[None, :, :])
return img_tr
# return img.float(), mask.float()
def __len__(self):
return len(self.__im)
def get_file(self):
return self.__file
class BraTSDatasetUnet(Dataset):
__file = []
__im = []
__mask = []
im_ht = 0
im_wd = 0
dataset_size = 0
def __init__(self, dataset_folder, train=True, keywords=["P1", "1", "flair"], im_size=[128, 128], transform=None):
self.__file = []
self.__im = []
self.__mask = []
self.im_ht = im_size[0]
self.im_wd = im_size[1]
self.transform = transform
folder = dataset_folder
# # Open and load text file including the whole training data
if train:
folder = "train/"
else:
folder = "test/"
folder = os.path.join(dataset_folder, folder)
file_list = os.listdir(folder)
for file in file_list:
if file.endswith(".png"):
filename = os.path.splitext(file)[0]
filename_fragments = filename.split("_")
samekeywords = list(set(filename_fragments) & set(keywords))
if len(samekeywords) == len(keywords):
# 1. read file name
self.__file.append(filename)
# 2. read raw image
# TODO: I think we should open image only in getitem,
# otherwise memory explodes
# rawImage = getImg(folder + file)
self.__im.append(os.path.join(folder, file))
# print(self.__im[-1])
# 3. read mask image
# mask_file = getMaskFileName(file)
mask_file = get_truth_file(file, keywords[0])
# print(mask_file)
# maskImage = getImg(folder + mask_file)
self.__mask.append(folder + mask_file)
# self.dataset_size = len(self.__file)
# print("lengths : ", len(self.__im), len(self.__mask))
self.dataset_size = len(self.__file)
if not train:
sio.savemat('filelist2.mat', {'data': self.__im})
def __getitem__(self, index):
img = getImg(self.__im[index])
mask = getImg(self.__mask[index])
img = img.resize((self.im_ht, self.im_wd))
mask = mask.resize((self.im_ht, self.im_wd))
# mask.show()
if self.transform is not None:
# TODO: Not sure why not take full image
img_tr = self.transform(img)
mask_tr = self.transform(mask)
# img_tr = self.transform(img[None, :, :])
# mask_tr = self.transform(mask[None, :, :])
return img_tr, mask_tr
# return img.float(), mask.float()
def __len__(self):
return len(self.__im)
class BraTSDatasetLSTM(Dataset):
def __init__(self, dataset_folder, train=True, keywords=["P1", "1", "flair"], im_size=[128, 128], transform=None):
self.__im = []
self.__mask = []
self.__im1 = []
self.__im3 = []
self.dataset_size = 0
self.__file = []
self.im_ht = im_size[0]
self.im_wd = im_size[1]
self.transform = transform
self.train = False
folder = dataset_folder
# # Open and load text file including the whole training data
self.train = train
if train:
folder = os.path.join(dataset_folder, "train")
else:
folder = os.path.join(dataset_folder, "test")
png_list = []
for file in os.listdir(folder):
if file.endswith('.png'):
png_list.append(file)
png_list.sort()
# this is a weird lambda function, because of weird naming convention
def groupby_lambda(x):
# if 'valid' in x:
# return x.split('_')[0] + x.split('_')[1] + x.split('_')[2]
# else:
# return x.split('_')[0] + x.split('_')[1]
return x.split('.')[0].split('_')[:-2]
unique_list = [list(g) for _, g in groupby(png_list, lambda x: groupby_lambda(x))]
# print(len(unique_list))
def numeric_sort_lambda(x):
x = x.split('.')[0]
return int(x.rpartition('_')[-1])
ready_file_list = []
for unique in unique_list:
unique = [unique[:int(len(unique)/2)], unique[int(len(unique)/2):]]
for u in unique:
u.sort(key=numeric_sort_lambda)
if len(unique) != 2:
print(unique)
ready_file_list.append(unique)
for r in ready_file_list:
# print(r)
for idx, file in islice(enumerate(r[0]),1,len(r[0])-1):
# if not self.train:
# print('\n')
# print(r[0][idx-1], r[0][idx], r[0][idx+1])
self.__im.append(os.path.join(folder, file))
# file_frags = list(file.split('.')[0].rpartition('_'))
# file_frags[-1] = r[0][idx-1].split('.')[0].rpartition('_')[-1]
# file_frags.append('.png')
# file1 = ''.join(file_frags)
file1 = r[0][idx-1]
self.__im1.append(os.path.join(folder, file1))
# print(file1)
# print(file)
# file_frags = list(file.split('.')[0].rpartition('_'))
# file_frags[-1] = r[0][idx+1].split('.')[0].rpartition('_')[-1]
# file_frags.append('.png')
# file3 = ''.join(file_frags)
file3 = r[0][idx+1]
self.__im3.append(os.path.join(folder, file3))
# print(file3)
mask_file = get_truth_file(file, keywords[0])
self.__mask.append(os.path.join(folder, mask_file))
# print(mask_file)
self.dataset_size = len(self.__file)
def __getitem__(self, index):
# if not self.train:
# print(len(self.__im1), len(self.__im), len(self.__im3))
# print('\n', self.__im1[index].split('/')[-1], self.__im[index].split('/')[-1], self.__im3[index].split('/')[-1], self.__mask[index].split('/')[-1])
img1 = getImg(self.__im1[index])
img = getImg(self.__im[index])
img3 = getImg(self.__im3[index])
mask = getImg(self.__mask[index])
# img.show()
# mask.show()
if self.transform is not None:
# TODO: Not sure why not take full image
img_tr1 = self.transform(img1)
img_tr = self.transform(img)
img_tr3 = self.transform(img3)
mask_tr = self.transform(mask)
# img_tr = self.transform(img[None, :, :])
# mask_tr = self.transform(mask[None, :, :])
return img_tr1, img_tr, img_tr3, mask_tr
# return img.float(), mask.float()
def __len__(self):
return len(self.__im)
class LstmPred(Dataset):
def __init__(self, predict_folder, keywords=["P1", "1", "flair"], im_size=[128, 128], transform=None):
self.__im = []
self.__im1 = []
self.__im3 = []
self.dataset_size = 0
self.__file = []
self.im_ht = im_size[0]
self.im_wd = im_size[1]
self.transform = transform
folder = predict_folder
png_list = []
for file in os.listdir(folder):
if file.endswith('.png'):
png_list.append(file)
# sort by alphabetical before grouping
png_list.sort()
def groupby_lambda(x):
# return x.split('_')[0] + x.split('_')[1]
return x.split('.')[0].split('_')[:-2]
unique_list = [list(g) for _, g in groupby(png_list, lambda x: groupby_lambda(x))]
# print(unique_list)
# print(len(unique_list))
def numeric_sort_lambda(x):
x = x.split('.')[0]
return int(x.split('_')[2])
sorted_file_list = []
for unique in unique_list:
unique.sort(key=numeric_sort_lambda)
sorted_file_list.append(unique)
# print(sorted_file_list)
for r in sorted_file_list:
# print(r)
for idx, file in islice(enumerate(r),1,len(r)-1):
self.__im.append(os.path.join(folder, file))
self.__file.append(file)
# file1 = ''
# file_frags = file.split('.')[0].split('_')
# file1 = file_frags[0] + '_' + file_frags[1] + '_' + str(int(r[idx-1].split('.')[0].split('_')[2])) + '.png'
# print(file1)
file1 = r[idx - 1]
self.__im1.append(os.path.join(folder, file1))
# file3 = ''
# file3 = file_frags[0] + '_' + file_frags[1] + '_' + str(int(r[idx+1].split('.')[0].split('_')[2])) + '.png'
# print(file3)
file3 = r[idx + 1]
self.__im3.append(os.path.join(folder, file3))
# print('\n')
# print(file1, file, file3)
self.dataset_size = len(self.__file)
def __getitem__(self, index):
# print('\n', self.__im1[index].split('/')[-1], self.__im[index].split('/')[-1], self.__im3[index].split('/')[-1])
img1 = getImg(self.__im1[index])
img = getImg(self.__im[index])
img3 = getImg(self.__im3[index])
if self.transform is not None:
img_tr1 = self.transform(img1)
img_tr = self.transform(img)
img_tr3 = self.transform(img3)
return img_tr1, img_tr, img_tr3
def __len__(self):
return len(self.__im)
def get_file(self):
return self.__file