-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataloader.py
More file actions
221 lines (155 loc) · 8.72 KB
/
dataloader.py
File metadata and controls
221 lines (155 loc) · 8.72 KB
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
import torch
from torch.utils.data import Dataset
from PIL import Image
import numpy as np
import os
from datetime import datetime
class PairedDataset(Dataset):
def __init__(self, root_dir, phase="train", transform=None, weights=None):
super().__init__()
self.root_dir = root_dir
self.phase = phase
self.folder_path_PET = self.root_dir + "/preprocess_PET/{}".format(self.phase)
self.folder_path_MRI = self.root_dir + "/preprocess_MRI/{}".format(self.phase) ##### CHANGE BACK HERE
self.records_PET = list(np.unique([i.rsplit("_",1)[0] for i in os.listdir(self.folder_path_PET)]))
self.records_MRI = list(np.unique([i.rsplit("_",1)[0] for i in os.listdir(self.folder_path_MRI)]))
self.transform = transform
def __len__(self):
return len(self.records_MRI)
def __getitem__(self, index):
#MRI Item
tuple_data = []
try :
slice_list_MRI = [j for j in os.listdir(self.folder_path_MRI) if j.startswith(self.records_MRI[index])]
slice_list_MRI.sort()
ref_MRI = np.load(os.path.join(self.folder_path_MRI, slice_list_MRI[0]), allow_pickle=True).item()
dims_MRI = (len(slice_list_MRI),) + (3, 224, 224) #Because the VGG19 do a center crop of the images at 224
array_volume_MRI = np.zeros(dims_MRI)
for s in slice_list_MRI:
array = np.load(os.path.join(self.folder_path_MRI, s), allow_pickle=True).item()["data"]
array_slice = np.tile(array[:, :], [1, 1, 3])
array_slice = Image.fromarray((array_slice*255).astype(np.uint8))
if self.transform:
array_slice = self.transform(array_slice)
array_volume_MRI[slice_list_MRI.index(s), :, :, :] = array_slice
array_volume_MRI = torch.FloatTensor(array_volume_MRI)
#Label
label_str = ref_MRI["label"]
label = 0 if label_str == "CN" else 1
label = torch.FloatTensor([label])
#Modality
modality_MRI = torch.LongTensor([1])
tuple_data.append([array_volume_MRI, modality_MRI])
except Exception as e :
print("No MRI")
#PET item
try :
slice_list_PET = [j for j in os.listdir(self.folder_path_PET) if j.startswith(self.records_PET[index])]
slice_list_PET.sort()
ref_PET = np.load(os.path.join(self.folder_path_PET, slice_list_PET[0]), allow_pickle=True).item()
#ref_data = ref_PET["data"]
dims_PET = (len(slice_list_PET),) + (3, 224, 224) #Because the VGG19 do a center crop of the images at 224
array_volume_PET = np.zeros(dims_PET)
for s in slice_list_PET:
array = np.load(os.path.join(self.folder_path_PET, s), allow_pickle=True).item()["data"]
array_slice = np.tile(array[:, :], [1, 1, 3])
array_slice = Image.fromarray((array_slice*255).astype(np.uint8))
if self.transform:
array_slice = self.transform(array_slice)
array_volume_PET[slice_list_PET.index(s), :, :, :] = array_slice
array_volume_PET = torch.FloatTensor(array_volume_PET)
#Labels
label_str = ref_PET["label"]
label = 0 if label_str == "CN" else 1
label = torch.FloatTensor([label])
#Modality
modality_PET = torch.LongTensor([0])
tuple_data.append([array_volume_PET, modality_PET])
except Exception as e:
print("No PET")
tuple_data.append(label)
return tuple_data
class IncompletePairedDataset(Dataset):
def __init__(self, root_dir, phase="train", transform=None, missing_pattern = None, missing_list = None):
super().__init__()
self.root_dir = root_dir
self.phase = phase
self.missing_pattern = missing_pattern
self.missing_list = missing_list
self.folder_path_PET = self.root_dir + "/preprocess_PET/{}".format(self.phase)
self.folder_path_MRI = self.root_dir + "/preprocess_MRI/{}".format(self.phase)
if not self.missing_pattern :
self.records_PET = list(np.unique([i.rsplit("_",1)[0] for i in os.listdir(self.folder_path_PET)]))
self.records_MRI = list(np.unique([i.rsplit("_",1)[0] for i in os.listdir(self.folder_path_MRI)]))
else :
if self.missing_pattern == "MRI":
MRIs = list(np.unique([i.rsplit("_",1)[0] for i in os.listdir(self.folder_path_MRI)]))
self.records_MRI = [i for i in MRIs if i[:10] not in self.missing_list]
self.records_PET = list(np.unique([i.rsplit("_",1)[0] for i in os.listdir(self.folder_path_PET)]))
elif self.missing_pattern == "PET":
PETs = list(np.unique([i.rsplit("_",1)[0] for i in os.listdir(self.folder_path_PET)]))
self.records_PET = [i for i in PETs if i[:10] not in self.missing_list]
self.records_MRI = list(np.unique([i.rsplit("_",1)[0] for i in os.listdir(self.folder_path_MRI)]))
elif self.missing_pattern == "both":
MRIs = list(np.unique([i.rsplit("_",1)[0] for i in os.listdir(self.folder_path_MRI)]))
PETs = list(np.unique([i.rsplit("_",1)[0] for i in os.listdir(self.folder_path_PET)]))
self.records_MRI = [i for i in MRIs if i[:10] not in self.missing_list[0]]
self.records_PET = [i for i in PETs if i[:10] not in self.missing_list[1]]
self.transform = transform
def __len__(self):
return max(len(self.records_MRI), len(self.records_PET))
def __getitem__(self, index):
#MRI Item
tuple_data = []
try :
slice_list_MRI = [j for j in os.listdir(self.folder_path_MRI) if j.startswith(self.records_MRI[index])]
slice_list_MRI.sort()
ref_MRI = np.load(os.path.join(self.folder_path_MRI, slice_list_MRI[0]), allow_pickle=True).item()
dims_MRI = (len(slice_list_MRI),) + (3, 224, 224) #Because the VGG19 do a center crop of the images at 224
array_volume_MRI = np.zeros(dims_MRI)
for s in slice_list_MRI:
array = np.load(os.path.join(self.folder_path_MRI, s), allow_pickle=True).item()["data"]
array_slice = np.tile(array[:, :], [1, 1, 3])
array_slice = Image.fromarray((array_slice*255).astype(np.uint8))
if self.transform:
array_slice = self.transform(array_slice)
array_volume_MRI[slice_list_MRI.index(s), :, :, :] = array_slice
array_volume_MRI = torch.FloatTensor(array_volume_MRI)
#Label
label_str = ref_MRI["label"]
label = 0 if label_str == "CN" else 1
label = torch.FloatTensor([label])
#Modality
modality_MRI = torch.LongTensor([1])
tuple_data.append([array_volume_MRI, modality_MRI])
except Exception:
pass
#print("No MRI")
#PET item
try :
slice_list_PET = [j for j in os.listdir(self.folder_path_PET) if j.startswith(self.records_PET[index])]
slice_list_PET.sort()
ref_PET = np.load(os.path.join(self.folder_path_PET, slice_list_PET[0]), allow_pickle=True).item()
#ref_data = ref_PET["data"]
dims_PET = (len(slice_list_PET),) + (3, 224, 224) #Because the VGG19 do a center crop of the images at 224
array_volume_PET = np.zeros(dims_PET)
for s in slice_list_PET:
array = np.load(os.path.join(self.folder_path_PET, s), allow_pickle=True).item()["data"]
array_slice = np.tile(array[:, :], [1, 1, 3])
array_slice = Image.fromarray((array_slice*255).astype(np.uint8))
if self.transform:
array_slice = self.transform(array_slice)
array_volume_PET[slice_list_PET.index(s), :, :, :] = array_slice
array_volume_PET = torch.FloatTensor(array_volume_PET)
#Labels
label_str = ref_PET["label"]
label = 0 if label_str == "CN" else 1
label = torch.FloatTensor([label])
#Modality
modality_PET = torch.LongTensor([0])
tuple_data.append([array_volume_PET, modality_PET])
except Exception:
pass
#print("No PET")
tuple_data.append(label)
return tuple_data