-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_data_backend.py
239 lines (206 loc) · 8.28 KB
/
test_data_backend.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
from self_supervised_3d_tasks.data.kaggle_retina_data import get_kaggle_generator, get_kaggle_cross_validation
from self_supervised_3d_tasks.data.make_data_generator import get_data_generators
from self_supervised_3d_tasks.data.numpy_2d_loader import Numpy2DLoader
from self_supervised_3d_tasks.data.segmentation_task_loader import SegmentationGenerator3D, PatchSegmentationGenerator3D
import numpy as np
def get_dataset_regular_train(
batch_size,
f_train,
f_val,
train_split,
data_generator,
data_dir_train,
val_split=0.1,
train_data_generator_args={},
val_data_generator_args={},
**kwargs,
):
train_split = train_split * (1 - val_split) # normalize train split
train_data_generator, val_data_generator, _ = get_data_generators(
data_generator=data_generator,
data_path=data_dir_train,
train_split=train_split,
val_split=val_split, # we are eventually not using the full dataset here
train_data_generator_args={
**{"batch_size": batch_size, "pre_proc_func": f_train},
**train_data_generator_args,
},
val_data_generator_args={
**{"batch_size": batch_size, "pre_proc_func": f_val},
**val_data_generator_args,
},
**kwargs,
)
return train_data_generator, val_data_generator
def get_dataset_regular_test(
batch_size,
f_test,
data_generator,
data_dir_test,
train_data_generator_args={},
test_data_generator_args={},
**kwargs,
):
if "val_split" in kwargs:
del kwargs["val_split"]
return get_data_generators(
data_generator=data_generator,
data_path=data_dir_test,
train_data_generator_args={
**{"batch_size": batch_size, "pre_proc_func": f_test},
**test_data_generator_args,
},
**kwargs,
)
def get_dataset_kaggle_train_original(
batch_size,
f_train,
f_val,
train_split,
csv_file_train,
data_dir,
val_split=0.1,
train_data_generator_args={},
val_data_generator_args={},
**kwargs,
):
train_split = train_split * (1 - val_split) # normalize train split
train_data_generator, val_data_generator, _ = get_kaggle_generator(
data_path=data_dir,
csv_file=csv_file_train,
train_split=train_split,
val_split=val_split, # we are eventually not using the full dataset here
train_data_generator_args={
**{"batch_size": batch_size, "pre_proc_func": f_train},
**train_data_generator_args,
},
val_data_generator_args={
**{"batch_size": batch_size, "pre_proc_func": f_val},
**val_data_generator_args,
},
**kwargs,
)
return train_data_generator, val_data_generator
def get_dataset_kaggle_test(
batch_size,
f_test,
csv_file_test,
data_dir,
train_data_generator_args={}, # DO NOT remove
test_data_generator_args={},
**kwargs,
):
if "val_split" in kwargs:
del kwargs["val_split"]
return get_kaggle_generator(
data_path=data_dir,
csv_file=csv_file_test,
train_data_generator_args={
**{"batch_size": batch_size, "pre_proc_func": f_test},
**test_data_generator_args,
},
**kwargs,
)
def get_data_from_gen(gen):
print("Loading Test data")
data = None
labels = None
max_iter = len(gen)
i = 0
for d, l in gen:
if data is None:
data = d
labels = l
else:
data = np.concatenate((data, d), axis=0)
labels = np.concatenate((labels, l), axis=0)
print(f"\r{(i * 100.0) / max_iter:.2f}%", end="")
i += 1
if i == max_iter:
break
print("")
return data, labels
def get_dataset_train(dataset_name, batch_size, f_train, f_val, train_split, kwargs):
if dataset_name == "kaggle_retina":
return get_dataset_kaggle_train_original(
batch_size, f_train, f_val, train_split, **kwargs
)
elif dataset_name == "pancreas3d":
return get_dataset_regular_train(
batch_size, f_train, f_val, train_split, data_generator=SegmentationGenerator3D, **kwargs,
)
elif dataset_name == 'brats' or dataset_name == 'ukb3d':
return get_dataset_regular_train(
batch_size, f_train, f_val, train_split, data_generator=PatchSegmentationGenerator3D, **kwargs,
)
elif dataset_name == "pancreas2d":
return get_dataset_regular_train(
batch_size, f_train, f_val, train_split, data_generator=Numpy2DLoader, **kwargs,
)
else:
raise ValueError("not implemented")
def get_dataset_test(dataset_name, batch_size, f_test, kwargs):
if dataset_name == "kaggle_retina":
gen_test = get_dataset_kaggle_test(batch_size, f_test, **kwargs)
elif dataset_name == "pancreas3d":
gen_test = get_dataset_regular_test(
batch_size, f_test, data_generator=SegmentationGenerator3D, **kwargs
)
elif dataset_name == 'brats' or dataset_name == 'ukb3d':
gen_test = get_dataset_regular_test(
batch_size, f_test, data_generator=PatchSegmentationGenerator3D, **kwargs
)
elif dataset_name == "pancreas2d":
gen_test = get_dataset_regular_test(
batch_size, f_test, data_generator=Numpy2DLoader, **kwargs,
)
else:
raise ValueError("not implemented")
return get_data_from_gen(gen_test)
class StandardDataLoader:
def __init__(self, dataset_name, batch_size, algorithm_def,
**kwargs):
self.algorithm_def = algorithm_def
self.batch_size = batch_size
self.dataset_name = dataset_name
self.kwargs = kwargs
def get_dataset(self, repetition, train_split):
f_train, f_val = self.algorithm_def.get_finetuning_preprocessing()
gen_train, gen_val = get_dataset_train(
self.dataset_name, self.batch_size, f_train, f_val, train_split, self.kwargs
)
x_test, y_test = get_dataset_test(self.dataset_name, self.batch_size, f_val, self.kwargs)
return gen_train, gen_val, x_test, y_test
class CvDataKaggle:
def __init__(self, dataset_name, batch_size, algorithm_def,
n_repetitions,
csv_file,
data_dir,
val_split=0.1,
test_data_generator_args={},
val_data_generator_args={},
train_data_generator_args={},
**kwargs):
assert dataset_name == "kaggle_retina", "CV only implemented for kaggle so far"
f_train, f_val = algorithm_def.get_finetuning_preprocessing()
self.cv = get_kaggle_cross_validation(data_path=data_dir, csv_file=csv_file,
k_fold=n_repetitions,
train_data_generator_args={
**{"batch_size": batch_size, "pre_proc_func": f_train},
**train_data_generator_args,
},
val_data_generator_args={
**{"batch_size": batch_size, "pre_proc_func": f_val},
**val_data_generator_args,
},
test_data_generator_args={
**{"batch_size": batch_size, "pre_proc_func": f_val},
**test_data_generator_args,
}, **kwargs)
self.val_split = val_split
def get_dataset(self, repetition, train_split):
train_split = train_split * (1 - self.val_split) # normalize train split
gen_train, gen_val, gen_test = self.cv.make_generators(test_chunk=repetition, train_split=train_split,
val_split=self.val_split)
x_test, y_test = get_data_from_gen(gen_test)
return gen_train, gen_val, x_test, y_test