|
a |
|
b/fetal/experiments/train_semi.py |
|
|
1 |
import glob |
|
|
2 |
import os |
|
|
3 |
|
|
|
4 |
import keras.backend as K |
|
|
5 |
import numpy as np |
|
|
6 |
from keras import Input, Model |
|
|
7 |
from keras.engine.network import Network |
|
|
8 |
from keras.layers import Activation, Concatenate |
|
|
9 |
from keras.optimizers import Adam |
|
|
10 |
from tqdm import tqdm |
|
|
11 |
|
|
|
12 |
import fetal_net |
|
|
13 |
import fetal_net.metrics |
|
|
14 |
import fetal_net.preprocess |
|
|
15 |
from fetal.config_utils import get_config |
|
|
16 |
from fetal.utils import get_last_model_path, create_data_file, set_gpu_mem_growth |
|
|
17 |
from fetal_net.data import open_data_file |
|
|
18 |
from fetal_net.generator import get_training_and_validation_generators |
|
|
19 |
from fetal_net.model.fetal_net import fetal_envelope_model |
|
|
20 |
|
|
|
21 |
set_gpu_mem_growth() |
|
|
22 |
|
|
|
23 |
config = get_config() |
|
|
24 |
if not "dis_model_name" in config: |
|
|
25 |
config["dis_model_name"] = "discriminator_image" |
|
|
26 |
if not "dis_loss" in config: |
|
|
27 |
config["dis_loss"] = "binary_crossentropy_loss" |
|
|
28 |
if not "gen_steps" in config: |
|
|
29 |
config["gen_steps"] = 1 |
|
|
30 |
if not "dis_steps" in config: |
|
|
31 |
config["dis_steps"] = 1 |
|
|
32 |
if not "gd_loss_ratio" in config: |
|
|
33 |
config["gd_loss_ratio"] = 10 |
|
|
34 |
|
|
|
35 |
|
|
|
36 |
class Scheduler: |
|
|
37 |
def __init__(self, n_itrs_per_epoch_d, n_itrs_per_epoch_g, init_lr, lr_decay, lr_patience): |
|
|
38 |
self.init_dsteps = n_itrs_per_epoch_d |
|
|
39 |
self.init_gsteps = n_itrs_per_epoch_g |
|
|
40 |
self.init_lr = init_lr |
|
|
41 |
self.lr_decay = lr_decay |
|
|
42 |
self.lr_patience = lr_patience |
|
|
43 |
|
|
|
44 |
self.dsteps = self.init_dsteps |
|
|
45 |
self.gsteps = self.init_gsteps |
|
|
46 |
self.lr = self.init_lr |
|
|
47 |
self.steps_stuck = 0 |
|
|
48 |
self.best_loss = np.inf |
|
|
49 |
|
|
|
50 |
def get_dsteps(self): |
|
|
51 |
return self.dsteps |
|
|
52 |
|
|
|
53 |
def get_gsteps(self): |
|
|
54 |
return self.gsteps |
|
|
55 |
|
|
|
56 |
def get_lr(self): |
|
|
57 |
return self.lr |
|
|
58 |
|
|
|
59 |
def update_steps(self, n_round, loss): |
|
|
60 |
if loss < self.best_loss: |
|
|
61 |
self.steps_stuck = 0 |
|
|
62 |
self.best_loss = loss |
|
|
63 |
else: |
|
|
64 |
self.steps_stuck += 1 |
|
|
65 |
|
|
|
66 |
if self.steps_stuck > self.lr_patience: |
|
|
67 |
self.lr *= self.lr_decay |
|
|
68 |
self.steps_stuck = 0 |
|
|
69 |
print('Reducing LR to {}'.format(self.lr)) |
|
|
70 |
|
|
|
71 |
# if key in self.schedules['step_decay']: |
|
|
72 |
# self.dsteps = max(int(self.init_dsteps * self.schedules['step_decay'][key]), 1) |
|
|
73 |
# self.gsteps = max(int(self.init_gsteps * self.schedules['step_decay'][key]), 1) |
|
|
74 |
|
|
|
75 |
|
|
|
76 |
def build_dsc(out_labels, outs): |
|
|
77 |
s = '' |
|
|
78 |
for l, o in zip(out_labels, outs): |
|
|
79 |
s = s + '{}={:.3f}, '.format(l, o) |
|
|
80 |
return s[:-2] + '|' |
|
|
81 |
|
|
|
82 |
|
|
|
83 |
def add_noise_to_segs(segs): |
|
|
84 |
if np.random.choice([True, False]): |
|
|
85 |
segs = segs.astype(np.float32) |
|
|
86 |
segs += np.random.normal(0, 0.025, segs.shape) |
|
|
87 |
segs *= np.random.normal(1, 0.025, segs.shape) |
|
|
88 |
segs = np.clip(segs, a_min=0, a_max=1) |
|
|
89 |
return segs |
|
|
90 |
|
|
|
91 |
|
|
|
92 |
def mul_merge_maps(r, s): |
|
|
93 |
r_plus = r * s |
|
|
94 |
r_minus = r * (1 - s) |
|
|
95 |
return np.concatenate((r_plus, r_minus), axis=1) |
|
|
96 |
|
|
|
97 |
|
|
|
98 |
def input2discriminator(real_patches, real_segs, semi_patches, semi_segs, d_out_shape, mul_merge=True): |
|
|
99 |
if mul_merge: |
|
|
100 |
real = mul_merge_maps(real_patches, add_noise_to_segs(real_segs)) |
|
|
101 |
fake = mul_merge_maps(semi_patches, semi_segs) |
|
|
102 |
else: |
|
|
103 |
real = np.concatenate((real_patches, add_noise_to_segs(real_segs)), axis=1) |
|
|
104 |
fake = np.concatenate((semi_patches, semi_segs), axis=1) |
|
|
105 |
|
|
|
106 |
d_x_batch = np.concatenate((real, fake), axis=0) |
|
|
107 |
|
|
|
108 |
# real : 1, fake : 0 |
|
|
109 |
d_y_batch = np.clip(np.random.uniform(0.9, 1.0, size=[d_x_batch.shape[0]] + list(d_out_shape)[1:]), |
|
|
110 |
a_min=0, a_max=1) |
|
|
111 |
d_y_batch[real.shape[0]:, ...] = 1 - d_y_batch[real.shape[0]:, ...] |
|
|
112 |
|
|
|
113 |
return d_x_batch, d_y_batch |
|
|
114 |
|
|
|
115 |
|
|
|
116 |
def input2gan(real_patches, real_segs, semi_patches, d_out_shape): |
|
|
117 |
g_x_batch = [real_patches, semi_patches] |
|
|
118 |
# set 1 to all labels (real : 1, fake : 0) |
|
|
119 |
g_y_batch = [ |
|
|
120 |
real_segs, |
|
|
121 |
np.clip(np.random.uniform(0.9, 1.0, size=[real_patches.shape[0]] + list(d_out_shape)[1:]), a_min=0, a_max=1) |
|
|
122 |
] |
|
|
123 |
return g_x_batch, g_y_batch |
|
|
124 |
|
|
|
125 |
|
|
|
126 |
def main(overwrite=False): |
|
|
127 |
# convert input images into an hdf5 file |
|
|
128 |
if overwrite or not os.path.exists(config["data_file"]): |
|
|
129 |
create_data_file(config) |
|
|
130 |
|
|
|
131 |
data_file_opened = open_data_file(config["data_file"]) |
|
|
132 |
|
|
|
133 |
seg_loss_func = getattr(fetal_net.metrics, config['loss']) |
|
|
134 |
dis_loss_func = getattr(fetal_net.metrics, config['dis_loss']) |
|
|
135 |
|
|
|
136 |
# instantiate new model |
|
|
137 |
seg_model_func = getattr(fetal_net.model, config['model_name']) |
|
|
138 |
gen_model = seg_model_func(input_shape=config["input_shape"], |
|
|
139 |
initial_learning_rate=config["initial_learning_rate"], |
|
|
140 |
**{'dropout_rate': config['dropout_rate'], |
|
|
141 |
'loss_function': seg_loss_func, |
|
|
142 |
'mask_shape': None if config["weight_mask"] is None else config[ |
|
|
143 |
"input_shape"], |
|
|
144 |
'old_model_path': config['old_model']}) |
|
|
145 |
|
|
|
146 |
dis_model_func = getattr(fetal_net.model, config['dis_model_name']) |
|
|
147 |
dis_model = dis_model_func( |
|
|
148 |
input_shape=[config["input_shape"][0] + config["n_labels"]] + config["input_shape"][1:], |
|
|
149 |
initial_learning_rate=config["initial_learning_rate"], |
|
|
150 |
**{'dropout_rate': config['dropout_rate'], |
|
|
151 |
'loss_function': dis_loss_func}) |
|
|
152 |
|
|
|
153 |
if not overwrite \ |
|
|
154 |
and len(glob.glob(config["model_file"] + 'g_*.h5')) > 0: |
|
|
155 |
# dis_model_path = get_last_model_path(config["model_file"] + 'dis_') |
|
|
156 |
gen_model_path = get_last_model_path(config["model_file"] + 'g_') |
|
|
157 |
# print('Loading dis model from: {}'.format(dis_model_path)) |
|
|
158 |
print('Loading gen model from: {}'.format(gen_model_path)) |
|
|
159 |
# dis_model = load_old_model(dis_model_path) |
|
|
160 |
# gen_model = load_old_model(gen_model_path) |
|
|
161 |
# dis_model.load_weights(dis_model_path) |
|
|
162 |
gen_model.load_weights(gen_model_path) |
|
|
163 |
|
|
|
164 |
gen_model.summary() |
|
|
165 |
dis_model.summary() |
|
|
166 |
|
|
|
167 |
# Build "frozen discriminator" |
|
|
168 |
frozen_dis_model = Network( |
|
|
169 |
dis_model.inputs, |
|
|
170 |
dis_model.outputs, |
|
|
171 |
name='frozen_discriminator' |
|
|
172 |
) |
|
|
173 |
frozen_dis_model.trainable = False |
|
|
174 |
|
|
|
175 |
inputs_real = Input(shape=config["input_shape"]) |
|
|
176 |
inputs_fake = Input(shape=config["input_shape"]) |
|
|
177 |
segs_real = Activation(None, name='seg_real')(gen_model(inputs_real)) |
|
|
178 |
segs_fake = Activation(None, name='seg_fake')(gen_model(inputs_fake)) |
|
|
179 |
valid = Activation(None, name='dis')(frozen_dis_model(Concatenate(axis=1)([segs_fake, inputs_fake]))) |
|
|
180 |
combined_model = Model(inputs=[inputs_real, inputs_fake], |
|
|
181 |
outputs=[segs_real, valid]) |
|
|
182 |
combined_model.compile(loss=[seg_loss_func, 'binary_crossentropy'], |
|
|
183 |
loss_weights=[1, config["gd_loss_ratio"]], |
|
|
184 |
optimizer=Adam(config["initial_learning_rate"])) |
|
|
185 |
combined_model.summary() |
|
|
186 |
|
|
|
187 |
# get training and testing generators |
|
|
188 |
train_generator, validation_generator, n_train_steps, n_validation_steps = get_training_and_validation_generators( |
|
|
189 |
data_file_opened, |
|
|
190 |
batch_size=config["batch_size"], |
|
|
191 |
data_split=config["validation_split"], |
|
|
192 |
overwrite=overwrite, |
|
|
193 |
validation_keys_file=config["validation_file"], |
|
|
194 |
training_keys_file=config["training_file"], |
|
|
195 |
test_keys_file=config["test_file"], |
|
|
196 |
n_labels=config["n_labels"], |
|
|
197 |
labels=config["labels"], |
|
|
198 |
patch_shape=(*config["patch_shape"], config["patch_depth"]), |
|
|
199 |
validation_batch_size=config["validation_batch_size"], |
|
|
200 |
augment=config["augment"], |
|
|
201 |
skip_blank_train=config["skip_blank_train"], |
|
|
202 |
skip_blank_val=config["skip_blank_val"], |
|
|
203 |
truth_index=config["truth_index"], |
|
|
204 |
truth_size=config["truth_size"], |
|
|
205 |
prev_truth_index=config["prev_truth_index"], |
|
|
206 |
prev_truth_size=config["prev_truth_size"], |
|
|
207 |
truth_downsample=config["truth_downsample"], |
|
|
208 |
truth_crop=config["truth_crop"], |
|
|
209 |
patches_per_epoch=config["patches_per_epoch"], |
|
|
210 |
categorical=config["categorical"], is3d=config["3D"], |
|
|
211 |
drop_easy_patches_train=config["drop_easy_patches_train"], |
|
|
212 |
drop_easy_patches_val=config["drop_easy_patches_val"]) |
|
|
213 |
|
|
|
214 |
# get training and testing generators |
|
|
215 |
_, semi_generator, _, _ = get_training_and_validation_generators( |
|
|
216 |
data_file_opened, |
|
|
217 |
batch_size=config["batch_size"], |
|
|
218 |
data_split=config["validation_split"], |
|
|
219 |
overwrite=overwrite, |
|
|
220 |
validation_keys_file=config["validation_file"], |
|
|
221 |
training_keys_file=config["training_file"], |
|
|
222 |
test_keys_file=config["test_file"], |
|
|
223 |
n_labels=config["n_labels"], |
|
|
224 |
labels=config["labels"], |
|
|
225 |
patch_shape=(*config["patch_shape"], config["patch_depth"]), |
|
|
226 |
validation_batch_size=config["validation_batch_size"], |
|
|
227 |
val_augment=config["augment"], |
|
|
228 |
skip_blank_train=config["skip_blank_train"], |
|
|
229 |
skip_blank_val=config["skip_blank_val"], |
|
|
230 |
truth_index=config["truth_index"], |
|
|
231 |
truth_size=config["truth_size"], |
|
|
232 |
prev_truth_index=config["prev_truth_index"], |
|
|
233 |
prev_truth_size=config["prev_truth_size"], |
|
|
234 |
truth_downsample=config["truth_downsample"], |
|
|
235 |
truth_crop=config["truth_crop"], |
|
|
236 |
patches_per_epoch=config["patches_per_epoch"], |
|
|
237 |
categorical=config["categorical"], is3d=config["3D"], |
|
|
238 |
drop_easy_patches_train=config["drop_easy_patches_train"], |
|
|
239 |
drop_easy_patches_val=config["drop_easy_patches_val"]) |
|
|
240 |
|
|
|
241 |
# start training |
|
|
242 |
scheduler = Scheduler(config["dis_steps"], config["gen_steps"], |
|
|
243 |
init_lr=config["initial_learning_rate"], |
|
|
244 |
lr_patience=config["patience"], |
|
|
245 |
lr_decay=config["learning_rate_drop"]) |
|
|
246 |
|
|
|
247 |
best_loss = np.inf |
|
|
248 |
for epoch in range(config["n_epochs"]): |
|
|
249 |
postfix = {'g': None, 'd': None} # , 'val_g': None, 'val_d': None} |
|
|
250 |
with tqdm(range(n_train_steps // config["gen_steps"]), dynamic_ncols=True, |
|
|
251 |
postfix={'gen': None, 'dis': None, 'val_gen': None, 'val_dis': None, None: None}) as pbar: |
|
|
252 |
for n_round in pbar: |
|
|
253 |
# train D |
|
|
254 |
outputs = np.zeros(dis_model.metrics_names.__len__()) |
|
|
255 |
for i in range(scheduler.get_dsteps()): |
|
|
256 |
real_patches, real_segs = next(train_generator) |
|
|
257 |
semi_patches, _ = next(semi_generator) |
|
|
258 |
d_x_batch, d_y_batch = input2discriminator(real_patches, real_segs, |
|
|
259 |
semi_patches, |
|
|
260 |
gen_model.predict(semi_patches, |
|
|
261 |
batch_size=config["batch_size"]), |
|
|
262 |
dis_model.output_shape) |
|
|
263 |
outputs += dis_model.train_on_batch(d_x_batch, d_y_batch) |
|
|
264 |
if scheduler.get_dsteps(): |
|
|
265 |
outputs /= scheduler.get_dsteps() |
|
|
266 |
postfix['d'] = build_dsc(dis_model.metrics_names, outputs) |
|
|
267 |
pbar.set_postfix(**postfix) |
|
|
268 |
|
|
|
269 |
# train G (freeze discriminator) |
|
|
270 |
outputs = np.zeros(combined_model.metrics_names.__len__()) |
|
|
271 |
for i in range(scheduler.get_gsteps()): |
|
|
272 |
real_patches, real_segs = next(train_generator) |
|
|
273 |
semi_patches, _ = next(validation_generator) |
|
|
274 |
g_x_batch, g_y_batch = input2gan(real_patches, real_segs, semi_patches, dis_model.output_shape) |
|
|
275 |
outputs += combined_model.train_on_batch(g_x_batch, g_y_batch) |
|
|
276 |
outputs /= scheduler.get_gsteps() |
|
|
277 |
|
|
|
278 |
postfix['g'] = build_dsc(combined_model.metrics_names, outputs) |
|
|
279 |
pbar.set_postfix(**postfix) |
|
|
280 |
|
|
|
281 |
# evaluate on validation set |
|
|
282 |
dis_metrics = np.zeros(dis_model.metrics_names.__len__(), dtype=float) |
|
|
283 |
gen_metrics = np.zeros(gen_model.metrics_names.__len__(), dtype=float) |
|
|
284 |
evaluation_rounds = n_validation_steps |
|
|
285 |
for n_round in range(evaluation_rounds): # rounds_for_evaluation: |
|
|
286 |
val_patches, val_segs = next(validation_generator) |
|
|
287 |
|
|
|
288 |
# D |
|
|
289 |
if scheduler.get_dsteps() > 0: |
|
|
290 |
d_x_test, d_y_test = input2discriminator(val_patches, val_segs, |
|
|
291 |
val_patches, |
|
|
292 |
gen_model.predict(val_patches, |
|
|
293 |
batch_size=config[ |
|
|
294 |
"validation_batch_size"]), |
|
|
295 |
dis_model.output_shape) |
|
|
296 |
dis_metrics += dis_model.evaluate(d_x_test, d_y_test, batch_size=config["validation_batch_size"], |
|
|
297 |
verbose=0) |
|
|
298 |
|
|
|
299 |
# G |
|
|
300 |
# gen_x_test, gen_y_test = input2gan(val_patches, val_segs, dis_model.output_shape) |
|
|
301 |
gen_metrics += gen_model.evaluate(val_patches, val_segs, |
|
|
302 |
batch_size=config["validation_batch_size"], |
|
|
303 |
verbose=0) |
|
|
304 |
|
|
|
305 |
dis_metrics /= float(evaluation_rounds) |
|
|
306 |
gen_metrics /= float(evaluation_rounds) |
|
|
307 |
# save the model and weights with the best validation loss |
|
|
308 |
if gen_metrics[0] < best_loss: |
|
|
309 |
best_loss = gen_metrics[0] |
|
|
310 |
print('Saving Model...') |
|
|
311 |
with open(os.path.join(config["base_dir"], "g_{}_{:.3f}.json".format(epoch, gen_metrics[0])), |
|
|
312 |
'w') as f: |
|
|
313 |
f.write(gen_model.to_json()) |
|
|
314 |
gen_model.save_weights( |
|
|
315 |
os.path.join(config["base_dir"], "g_{}_{:.3f}.h5".format(epoch, gen_metrics[0]))) |
|
|
316 |
|
|
|
317 |
postfix['val_d'] = build_dsc(dis_model.metrics_names, dis_metrics) |
|
|
318 |
postfix['val_g'] = build_dsc(gen_model.metrics_names, gen_metrics) |
|
|
319 |
# pbar.set_postfix(**postfix) |
|
|
320 |
print('val_d: ' + postfix['val_d'], end=' | ') |
|
|
321 |
print('val_g: ' + postfix['val_g']) |
|
|
322 |
# pbar.refresh() |
|
|
323 |
|
|
|
324 |
# update step sizes, learning rates |
|
|
325 |
scheduler.update_steps(epoch, gen_metrics[0]) |
|
|
326 |
K.set_value(dis_model.optimizer.lr, scheduler.get_lr()) |
|
|
327 |
K.set_value(combined_model.optimizer.lr, scheduler.get_lr()) |
|
|
328 |
|
|
|
329 |
data_file_opened.close() |
|
|
330 |
|
|
|
331 |
|
|
|
332 |
if __name__ == "__main__": |
|
|
333 |
main(overwrite=config["overwrite"]) |