-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtrain_v2v.py
More file actions
133 lines (102 loc) · 5.04 KB
/
train_v2v.py
File metadata and controls
133 lines (102 loc) · 5.04 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
import os
import numpy as np
import tensorflow as tf
from models import *
from losses import *
import matplotlib.image as mpim
from sys import stdout
# class weights
class_weights = np.load('class_weights.npy')
# Models
G = Generator()
D = Discriminator()
# Optimizers
generator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)
discriminator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)
@tf.function
def train_step(image, target, alpha):
with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
gen_output = G(image, training=True)
disc_real_output = D([image, target], training=True)
disc_fake_output = D([image, gen_output], training=True)
disc_loss = discriminator_loss(disc_real_output, disc_fake_output)
gen_loss, dice_loss, disc_loss_gen = generator_loss(target, gen_output, disc_fake_output, class_weights, alpha)
generator_gradients = gen_tape.gradient(gen_loss, G.trainable_variables)
discriminator_gradients = disc_tape.gradient(disc_loss, D.trainable_variables)
generator_optimizer.apply_gradients(zip(generator_gradients, G.trainable_variables))
discriminator_optimizer.apply_gradients(zip(discriminator_gradients, D.trainable_variables))
return gen_loss, dice_loss, disc_loss_gen
@tf.function
def test_step(image, target, alpha):
gen_output = G(image, training=False)
disc_real_output = D([image, target], training=False)
disc_fake_output = D([image, gen_output], training=False)
disc_loss = discriminator_loss(disc_real_output, disc_fake_output)
gen_loss, dice_loss, disc_loss_gen = generator_loss(target, gen_output, disc_fake_output, class_weights, alpha)
return gen_loss, dice_loss, disc_loss_gen
def fit(train_gen, valid_gen, alpha, epochs):
path = './RESULTS'
if os.path.exists(path)==False:
os.mkdir(path)
Nt = len(train_gen)
history = {'train': [], 'valid': []}
prev_loss = np.inf
epoch_v2v_loss = tf.keras.metrics.Mean()
epoch_dice_loss = tf.keras.metrics.Mean()
epoch_disc_loss = tf.keras.metrics.Mean()
epoch_v2v_loss_val = tf.keras.metrics.Mean()
epoch_dice_loss_val = tf.keras.metrics.Mean()
epoch_disc_loss_val = tf.keras.metrics.Mean()
for e in range(epochs):
print('Epoch {}/{}'.format(e+1,epochs))
b = 0
for Xb, yb in train_gen:
b += 1
losses = train_step(Xb, yb, alpha)
epoch_v2v_loss.update_state(losses[0])
epoch_dice_loss.update_state(losses[1])
epoch_disc_loss.update_state(losses[2])
stdout.write('\rBatch: {}/{} - loss: {:.4f} - dice_loss: {:.4f} - disc_loss: {:.4f}'
.format(b, Nt, epoch_v2v_loss.result(), epoch_dice_loss.result(), epoch_disc_loss.result()))
stdout.flush()
history['train'].append([epoch_v2v_loss.result(), epoch_dice_loss.result(), epoch_disc_loss.result()])
for Xb, yb in valid_gen:
losses_val = test_step(Xb, yb, alpha)
epoch_v2v_loss_val.update_state(losses_val[0])
epoch_dice_loss_val.update_state(losses_val[1])
epoch_disc_loss_val.update_state(losses_val[2])
stdout.write('\n loss_val: {:.4f} - dice_loss_val: {:.4f} - disc_loss_val: {:.4f}'
.format(epoch_v2v_loss_val.result(), epoch_dice_loss_val.result(), epoch_disc_loss_val.result()))
stdout.flush()
history['valid'].append([epoch_v2v_loss_val.result(), epoch_dice_loss_val.result(), epoch_disc_loss_val.result()])
# save pred image at epoch e
y_pred = G.predict(Xb)
y_true = np.argmax(yb, axis=-1)
y_pred = np.argmax(y_pred, axis=-1)
canvas = np.zeros((128, 128*3))
idx = np.random.randint(len(Xb))
x = Xb[idx,:,:,64,2]
canvas[0:128, 0:128] = (x - np.min(x))/(np.max(x)-np.min(x)+1e-6)
canvas[0:128, 128:2*128] = y_true[idx,:,:,64]/3
canvas[0:128, 2*128:3*128] = y_pred[idx,:,:,64]/3
fname = (path + '/pred@epoch_{:03d}.png').format(e+1)
mpim.imsave(fname, canvas, cmap='gray')
# save models
print(' ')
if epoch_v2v_loss_val.result() < prev_loss:
G.save_weights(path + '/Generator.h5')
D.save_weights(path + '/Discriminator.h5')
print("Validation loss decresaed from {:.4f} to {:.4f}. Models' weights are now saved.".format(prev_loss, epoch_v2v_loss_val.result()))
prev_loss = epoch_v2v_loss_val.result()
else:
print("Validation loss did not decrese from {:.4f}.".format(prev_loss))
print(' ')
# resets losses states
epoch_v2v_loss.reset_states()
epoch_dice_loss.reset_states()
epoch_disc_loss.reset_states()
epoch_v2v_loss_val.reset_states()
epoch_dice_loss_val.reset_states()
epoch_disc_loss_val.reset_states()
del Xb, yb, canvas, y_pred, y_true, idx
return history