-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_disc_test.py
275 lines (260 loc) · 7.53 KB
/
prepare_disc_test.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
"""
Prepare the training and run it on the cluster or a local machine (automatically detected)
"""
import logging
import os
from pathlib import Path
from typing import List, Tuple
import pandas as pd
# logger has to be set before tensorflow is imported
tf_logger = logging.getLogger("tensorflow")
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import tensorflow as tf
from gan_normalization import train_gan_normalization
from SegClassRegBasis.utils import get_gpu
# set tf thread mode
os.environ["TF_GPU_THREAD_MODE"] = "gpu_private"
### normalization method ###
preprocessing_parameters: List[Tuple[str, dict]] = [
(
"",
{
"depth": 3,
"filter_base": 16,
"min_max": False,
"smoothing_sigma": 1,
"latent_weight": 1,
"image_weight": 1,
"skip_edges": True,
"latent": False,
"train_on_gen": False,
"n_epochs": 50,
"batch_size": 64,
},
),
(
"_f16",
{
"depth": 3,
"filter_base": 16,
"min_max": False,
"smoothing_sigma": 1,
"latent_weight": 1,
"image_weight": 1,
"disc_filter_base": 16,
"skip_edges": True,
"latent": False,
"train_on_gen": False,
"n_epochs": 50,
"batch_size": 64,
},
),
(
"_f8",
{
"depth": 3,
"filter_base": 16,
"min_max": False,
"smoothing_sigma": 1,
"latent_weight": 1,
"image_weight": 1,
"disc_filter_base": 16,
"skip_edges": True,
"latent": False,
"train_on_gen": False,
"n_epochs": 50,
"batch_size": 64,
},
),
(
"_BetterConv",
{
"depth": 3,
"filter_base": 64,
"min_max": False,
"smoothing_sigma": 0.5,
"latent_weight": 1,
"image_weight": 1,
"image_gen_weight": 0.5,
"skip_edges": True,
"latent": False,
"train_on_gen": False,
"disc_type": "BetterConv",
"n_epochs": 50,
"batch_size": 32,
},
),
(
"_BetterConv_lr_em2",
{
"depth": 3,
"filter_base": 64,
"min_max": False,
"smoothing_sigma": 0.5,
"latent_weight": 1,
"image_weight": 1,
"image_gen_weight": 0.5,
"skip_edges": True,
"latent": False,
"train_on_gen": False,
"disc_type": "BetterConv",
"n_epochs": 50,
"batch_size": 32,
"disc_start_lr": 1e-2,
"disc_end_lr": 1e-2,
},
),
(
"_BetterConv_lr_em3",
{
"depth": 3,
"filter_base": 64,
"min_max": False,
"smoothing_sigma": 0.5,
"latent_weight": 1,
"image_weight": 1,
"image_gen_weight": 0.5,
"skip_edges": True,
"latent": False,
"train_on_gen": False,
"disc_type": "BetterConv",
"n_epochs": 50,
"batch_size": 32,
"disc_start_lr": 1e-3,
"disc_end_lr": 1e-3,
},
),
(
"_BetterConv_lr_em4",
{
"depth": 3,
"filter_base": 64,
"min_max": False,
"smoothing_sigma": 0.5,
"latent_weight": 1,
"image_weight": 1,
"image_gen_weight": 0.5,
"skip_edges": True,
"latent": False,
"train_on_gen": False,
"disc_type": "BetterConv",
"n_epochs": 50,
"batch_size": 32,
"disc_start_lr": 1e-4,
"disc_end_lr": 1e-4,
},
),
(
"_BetterConv_lr_em5",
{
"depth": 3,
"filter_base": 64,
"min_max": False,
"smoothing_sigma": 0.5,
"latent_weight": 1,
"image_weight": 1,
"image_gen_weight": 0.5,
"skip_edges": True,
"latent": False,
"train_on_gen": False,
"disc_type": "BetterConv",
"n_epochs": 50,
"batch_size": 32,
"disc_start_lr": 1e-5,
"disc_end_lr": 1e-5,
},
),
(
"_BetterConv_lr_em6",
{
"depth": 3,
"filter_base": 64,
"min_max": False,
"smoothing_sigma": 0.5,
"latent_weight": 1,
"image_weight": 1,
"image_gen_weight": 0.5,
"skip_edges": True,
"latent": False,
"train_on_gen": False,
"disc_type": "BetterConv",
"n_epochs": 50,
"batch_size": 32,
"disc_start_lr": 1e-6,
"disc_end_lr": 1e-6,
},
),
(
"_seg",
{
"depth": 3,
"filter_base": 64,
"min_max": False,
"smoothing_sigma": 0.5,
"latent_weight": 1,
"image_weight": 1,
"image_gen_weight": 0.5,
"skip_edges": True,
"latent": False,
"train_on_gen": False,
"disc_type": "BetterConv",
"n_epochs": 50,
"batch_size": 32,
"disc_start_lr": 1e-5,
"disc_end_lr": 1e-5,
"train_on_segmentation": True,
"unet_parameters": {
"regularize": (True, "L2", 1e-5),
"drop_out": (True, 0.01),
"activation": "elu",
"cross_hair": False,
"clip_value": 1,
"res_connect": True,
"n_filters": (
64,
128,
256,
512,
1024,
),
"do_bias": True,
"do_batch_normalization": False,
"ratio": 2,
},
},
),
]
if __name__ == "__main__":
data_dir = Path(os.environ["data_dir"])
experiment_dir = Path(os.environ["experiment_dir"])
GROUP_BASE_NAME = "Discriminator_Test"
exp_group_base_dir = experiment_dir / GROUP_BASE_NAME
if not exp_group_base_dir.exists():
exp_group_base_dir.mkdir(parents=True)
gpu = tf.device(get_gpu(memory_limit=2000))
# load data
timepoints = pd.read_csv(data_dir / "timepoints.csv", sep=";", index_col=0)
N_CHANNELS = 3
MODALITIES = ("T2 axial", "Diff axial b800", "ADC axial original")
# set config
PREPROCESSED_DIR = Path(GROUP_BASE_NAME)
# only use before therapy images that are segmented
timepoints_train = timepoints.query(
"treatment_status=='before therapy' & segmented & 'Frankfurt' in location"
).index
timepoints_train_norm = timepoints.query("'Frankfurt' in location").index
group_dir_rel = Path(GROUP_BASE_NAME)
for mod_num in range(N_CHANNELS):
for gan_suffix, norm_params in preprocessing_parameters:
with gpu:
train_gan_normalization(
timepoints_train=timepoints_train_norm,
mod_num=mod_num,
preprocessed_dir=PREPROCESSED_DIR,
experiment_group=group_dir_rel,
modality=MODALITIES[mod_num],
gan_suffix=gan_suffix,
identity=True,
**norm_params,
)