-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.py
364 lines (281 loc) · 11.4 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
from pathlib import Path
from functools import partial
import numpy as np
import torch
from torch import nn
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms as T, utils
import torch.nn.functional as F
import t5
from torch.nn.utils.rnn import pad_sequence
from PIL import Image
import torchvision.transforms.functional as TF
import torchvision.transforms as T
from datasets.utils.file_utils import get_datasets_user_agent
import io
import urllib
from torch.utils.data.dataloader import default_collate
import nibabel as nib
USER_AGENT = get_datasets_user_agent()
# helpers functions
def exists(val):
return val is not None
def cycle(dl):
while True:
for data in dl:
yield data
def convert_image_to(img_type, image):
if image.mode != img_type:
return image.convert(img_type)
return image
# dataset, dataloader, collator
def my_collate(batch):
batch = list(filter(lambda x : x is not None, batch))
if batch == []:
return None
return default_collate(batch)
class supervisedIQT(Dataset):
def __init__(self, config, lr_files, hr_files, train=True):
self.config = config
self.lr_files = lr_files
self.hr_files = hr_files
self.mean_lr = self.config['Data']['mean']#202.68109075616067 #35.493949511348724
self.std_lr = self.config['Data']['std']#346.51374798642223 #37.11344433531084
if self.config['Train']['batch_sample']:
self.patch_size = self.config['Train']['patch_size_sub'] * self.config['Train']['batch_sample_factor']
else:
self.patch_size = self.config['Train']['patch_size_sub']
self.train = train
if self.train:
self.ratio = 0.2
else:
self.ratio = 0.8
self.files_lr = []
self.files_hr = []
for i in range(len(self.lr_files)):
self.files_lr.append(self.lr_files[i])
self.files_hr.append(self.hr_files[i])
def __len__(self):
return len(self.files_lr)
def normalize(self, img, mode='lr'): # transform 3D array to tensor
if self.config['Data']['norm'] == 'min-max':
return 2*(((img-img.min())/(img.max()-img.min()))-0.5)
return (img - self.mean_lr)/self.std_lr
def cube(self,data):
hyp_norm = data
if len(hyp_norm.shape)>3:
hyp_norm = hyp_norm[:,:, 2:258, 27:283]
else:
hyp_norm = hyp_norm[2:258, 27:283, :256]
return hyp_norm
def __getitem__(self, idx):
self.lr = self.files_lr[idx]
self.hr = self.lr.replace('lr_norm',self.config['Data']['groundtruth_fname'])#'T1w_acpc_dc_restore_brain') #'T1w_acpc_dc_restore_brain_sim036T_4x_groundtruth_norm')
self.lr = nib.load(self.lr)
self.lr_affine = self.lr.affine
self.lr = torch.tensor(self.lr.get_fdata().astype(np.float32))
self.img_shape = self.lr.shape
self.hr = nib.load(self.hr)
self.hr_affine = self.hr.affine
self.hr = torch.tensor(self.hr.get_fdata().astype(np.float32))
#Cube
low, high = 0, 256 #16, 240
assert self.lr.shape == (256,256,256), f'lr must be 256 256 256 but got {self.lr.shape}'
assert self.hr.shape == (256,256,256), f'hr must be 256 256 256 but got {self.hr.shape}'
self.lr = self.lr[low:high,low:high,low:high]
self.hr = self.hr[low:high,low:high,low:high]
random_idx = np.random.randint(low=0, high=(high-low)-self.patch_size, size=3)
self.lr = self.lr[random_idx[0]:random_idx[0]+self.patch_size, random_idx[1]:random_idx[1]+self.patch_size, random_idx[2]:random_idx[2]+self.patch_size]
self.hr = self.hr[random_idx[0]:random_idx[0]+self.patch_size, random_idx[1]:random_idx[1]+self.patch_size, random_idx[2]:random_idx[2]+self.patch_size]
non_zero = np.count_nonzero(self.lr)
self.total_voxel = self.patch_size * self.patch_size * self.patch_size
non_zero_proportion = (non_zero/self.total_voxel)
if (non_zero_proportion < self.ratio):
return self.__getitem__(idx)
self.lr = self.normalize(self.lr, mode='lr')
self.hr = self.normalize(self.hr, mode='hr')
sample_lr = torch.unsqueeze(self.lr, 0)
sample_hr = torch.unsqueeze(self.hr, 0)
if self.train:
return sample_hr, sample_lr
else:
return sample_hr, sample_lr
class supervisedIQT_INF(Dataset):
def __init__(self, config, lr_file):
self.lr_file = lr_file
self.config = config
self.mean_lr = self.config['Data']['mean']#202.68109075616067 #35.493949511348724
self.std_lr = self.config['Data']['std']#346.51374798642223 #37.11344433531084
if self.config['Train']['batch_sample']:
self.patch_size = self.config['Train']['patch_size_sub'] * self.config['Train']['batch_sample_factor']
else:
self.patch_size = self.config['Train']['patch_size_sub']
self.overlap = self.config['Eval']['overlap']
self.ratio = 0.05
self.total_voxel = self.patch_size * self.patch_size * self.patch_size
self.lr_idx = []
low, high = 0, 256
self.lr_data = nib.load(self.lr_file).get_fdata()[low:high,low:high,low:high]
for i in range(0,self.lr_data.shape[0]-self.patch_size+1,self.overlap):
for j in range(0,self.lr_data.shape[1]-self.patch_size+1,self.overlap):
for k in range(0,self.lr_data.shape[2]-self.patch_size+1,self.overlap):
self.lr_idx.append([i,j,k])
def __len__(self):
return len(self.lr_idx)
def normalize(self, img): # transform 3D array to tensor
image_torch = torch.FloatTensor(img)
image_torch = (image_torch - self.mean_lr)/self.std_lr
return image_torch
def cube(self,data):
hyp_norm = data
if len(hyp_norm.shape)>3:
hyp_norm = hyp_norm[:,:, 2:258, 27:283]
else:
hyp_norm = hyp_norm[2:258, 27:283]
return hyp_norm
def __getitem__(self, idx):
self.lr = self.lr_idx[idx]
self.lr = self.lr_data[self.lr[0]:self.lr[0]+self.patch_size, self.lr[1]:self.lr[1]+self.patch_size, self.lr[2]:self.lr[2]+self.patch_size]
self.lr = torch.tensor(self.lr.astype(np.float32))
self.img_shape = self.lr.shape
non_zero = np.count_nonzero(self.lr)
non_zero_proportion = (non_zero/self.total_voxel)
if (non_zero_proportion < self.ratio):
return None
self.lr = self.normalize(self.lr)
sample_lr = torch.unsqueeze(self.lr, 0)
return [sample_lr, torch.tensor(self.lr_idx[idx])]
class IQTDataset(Dataset):
def __init__(
self,
hr_files,
lr_files,
fake = False
):
self.hrfiles = hr_files
self.lrfiles = lr_files
self.fake = fake
assert len(self.hrfiles) == len(self.hrfiles), "Length should be same"
def transform(self, img, size=(256,256)):
return TF.resize(img, size)
def normalize(self, img):
img = (img-img.min())/(img.max()-img.min())
return img
def np2tensor(self, x, length, mode='2d'):
x = torch.tensor(x)
if mode == '2d':
if length == 2:
x = torch.unsqueeze(x,0)
elif length == 3:
x = torch.unsqueeze(x,0)
else:
if length == 3:
x = torch.unsqueeze(x,0)
elif length == 4:
x = torch.unsqueeze(x,0)
return x
def __len__(self):
return len(self.hrfiles)
def __getitem__(self, idx):
if not self.fake:
hrfile = self.hrfiles[idx]
lrfile = self.hrfiles[idx].replace('groundtruth_', 'lr_')
hrimg = np.load(hrfile).astype(np.float32)
hrimg = self.np2tensor(hrimg, len(hrimg.shape))
hrimg = self.transform(hrimg)
hrimg = self.normalize(hrimg)
lrimg = np.load(lrfile).astype(np.float32)
lrimg = self.np2tensor(lrimg, len(lrimg.shape))
lrimg = self.transform(lrimg)
lrimg = self.normalize(lrimg)
else:
hrimg = torch.randn(1,32,32,32)
lrimg = torch.randn(1,32,32,32)
return hrimg, lrimg
class Collator:
def __init__(self, image_size, url_label, text_label, image_label, name, channels):
self.url_label = url_label
self.text_label = text_label
self.image_label = image_label
self.download = url_label is not None
self.name = name
self.channels = channels
self.transform = T.Compose([
T.Resize(image_size),
T.CenterCrop(image_size),
T.ToTensor(),
])
def __call__(self, batch):
texts = []
images = []
for item in batch:
try:
if self.download:
image = self.fetch_single_image(item[self.url_label])
else:
image = item[self.image_label]
image = self.transform(image.convert(self.channels))
except:
continue
text = t5.t5_encode_text([item[self.text_label]], name=self.name)
texts.append(torch.squeeze(text))
images.append(image)
if len(texts) == 0:
return None
texts = pad_sequence(texts, True)
newbatch = []
for i in range(len(texts)):
newbatch.append((images[i], texts[i]))
return torch.utils.data.dataloader.default_collate(newbatch)
def fetch_single_image(self, image_url, timeout=1):
try:
request = urllib.request.Request(
image_url,
data=None,
headers={"user-agent": USER_AGENT},
)
with urllib.request.urlopen(request, timeout=timeout) as req:
image = Image.open(io.BytesIO(req.read())).convert('RGB')
except Exception:
image = None
return image
class Dataset(Dataset):
def __init__(
self,
folder,
image_size,
exts = ['jpg', 'jpeg', 'png', 'tiff'],
convert_image_to_type = None
):
super().__init__()
self.folder = folder
self.image_size = image_size
self.paths = [p for ext in exts for p in Path(f'{folder}').glob(f'**/*.{ext}')]
convert_fn = partial(convert_image_to, convert_image_to_type) if exists(convert_image_to_type) else nn.Identity()
self.transform = T.Compose([
T.Lambda(convert_fn),
T.Resize(image_size),
T.RandomHorizontalFlip(),
T.CenterCrop(image_size),
T.ToTensor()
])
def __len__(self):
return len(self.paths)
def __getitem__(self, index):
path = self.paths[index]
img = Image.open(path)
return self.transform(img)
def get_images_dataloader(
folder,
*,
batch_size,
image_size,
shuffle = True,
cycle_dl = False,
pin_memory = True
):
ds = Dataset(folder, image_size)
dl = DataLoader(ds, batch_size = batch_size, shuffle = shuffle, pin_memory = pin_memory)
if cycle_dl:
dl = cycle(dl)
return dl