|
a |
|
b/trainers/AE.py |
|
|
1 |
from collections import defaultdict |
|
|
2 |
from math import inf |
|
|
3 |
|
|
|
4 |
from tensorflow.python.ops.losses.losses_impl import Reduction |
|
|
5 |
|
|
|
6 |
from trainers import trainer_utils |
|
|
7 |
from trainers.AEMODEL import AEMODEL, Phase, update_log_dicts, indicate_early_stopping |
|
|
8 |
from trainers.DLMODEL import * |
|
|
9 |
|
|
|
10 |
|
|
|
11 |
class AE(AEMODEL): |
|
|
12 |
def __init__(self, sess, config, network=None): |
|
|
13 |
super().__init__(sess, config, network) |
|
|
14 |
self.x = tf.placeholder(tf.float32, [None, self.config.outputHeight, self.config.outputWidth, self.config.numChannels], name='x') |
|
|
15 |
self.outputs = self.network(self.x, dropout_rate=self.dropout_rate, dropout=self.dropout, config=self.config) |
|
|
16 |
self.reconstruction = self.outputs['x_hat'] |
|
|
17 |
|
|
|
18 |
# Print Stats |
|
|
19 |
self.get_number_of_trainable_params() |
|
|
20 |
# Instantiate Saver |
|
|
21 |
self.saver = tf.train.Saver() |
|
|
22 |
|
|
|
23 |
def train(self, dataset): |
|
|
24 |
# Determine trainable variables |
|
|
25 |
self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES) |
|
|
26 |
|
|
|
27 |
# Build losses |
|
|
28 |
self.losses['L1'] = tf.losses.absolute_difference(self.x, self.reconstruction, reduction=Reduction.NONE) |
|
|
29 |
self.losses['reconstructionLoss'] = self.losses['loss'] = tf.reduce_mean(tf.reduce_sum(self.losses['L1'], axis=[1, 2, 3])) |
|
|
30 |
|
|
|
31 |
# Set the optimizer |
|
|
32 |
optim = self.create_optimizer(self.losses['loss'], var_list=self.variables, learningrate=self.config.learningrate, |
|
|
33 |
beta1=self.config.beta1, type=self.config.optimizer) |
|
|
34 |
|
|
|
35 |
# initialize all variables |
|
|
36 |
tf.global_variables_initializer().run(session=self.sess) |
|
|
37 |
|
|
|
38 |
best_cost = inf |
|
|
39 |
last_improvement = 0 |
|
|
40 |
last_epoch = self.load_checkpoint() |
|
|
41 |
|
|
|
42 |
# Go go go! |
|
|
43 |
for epoch in range(last_epoch, self.config.numEpochs): |
|
|
44 |
############ |
|
|
45 |
# TRAINING # |
|
|
46 |
############ |
|
|
47 |
self.process(dataset, epoch, Phase.TRAIN, optim) |
|
|
48 |
|
|
|
49 |
# Increment last_epoch counter and save model |
|
|
50 |
last_epoch += 1 |
|
|
51 |
self.save(self.checkpointDir, last_epoch) |
|
|
52 |
|
|
|
53 |
############## |
|
|
54 |
# VALIDATION # |
|
|
55 |
############## |
|
|
56 |
val_scalars = self.process(dataset, epoch, Phase.VAL) |
|
|
57 |
|
|
|
58 |
best_cost, last_improvement, stop = indicate_early_stopping(val_scalars['loss'], best_cost, last_improvement) |
|
|
59 |
if stop: |
|
|
60 |
print('Early stopping was triggered due to no improvement over the last 5 epochs') |
|
|
61 |
break |
|
|
62 |
|
|
|
63 |
def process(self, dataset, epoch, phase: Phase, optim=None): |
|
|
64 |
scalars = defaultdict(list) |
|
|
65 |
visuals = [] |
|
|
66 |
num_batches = dataset.num_batches(self.config.batchsize, set=phase.value) |
|
|
67 |
for idx in range(0, num_batches): |
|
|
68 |
batch, _, _ = dataset.next_batch(self.config.batchsize, set=phase.value) |
|
|
69 |
|
|
|
70 |
fetches = { |
|
|
71 |
'reconstruction': self.reconstruction, |
|
|
72 |
**self.losses |
|
|
73 |
} |
|
|
74 |
if phase == Phase.TRAIN: |
|
|
75 |
fetches['optimizer'] = optim |
|
|
76 |
|
|
|
77 |
feed_dict = { |
|
|
78 |
self.x: batch, |
|
|
79 |
self.dropout: phase == Phase.TRAIN, |
|
|
80 |
self.dropout_rate: self.config.dropout_rate |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
run = self.sess.run(fetches, feed_dict=feed_dict) |
|
|
84 |
|
|
|
85 |
# Print to console |
|
|
86 |
print(f'Epoch ({phase.value}): [{epoch:2d}] [{idx:4d}/{num_batches:4d}] loss: {run["loss"]:.8f}') |
|
|
87 |
update_log_dicts(*trainer_utils.get_summary_dict(batch, run), scalars, visuals) |
|
|
88 |
|
|
|
89 |
self.log_to_tensorboard(epoch, scalars, visuals, phase) |
|
|
90 |
return scalars |
|
|
91 |
|
|
|
92 |
def reconstruct(self, x, dropout=False): |
|
|
93 |
if x.ndim < 4: |
|
|
94 |
x = np.expand_dims(x, 0) |
|
|
95 |
|
|
|
96 |
fetches = { |
|
|
97 |
'reconstruction': self.reconstruction |
|
|
98 |
} |
|
|
99 |
|
|
|
100 |
feed_dict = { |
|
|
101 |
self.x: x, |
|
|
102 |
self.dropout: dropout, # apply only during MC sampling. |
|
|
103 |
self.dropout_rate: self.config.dropout_rate |
|
|
104 |
} |
|
|
105 |
results = self.sess.run(fetches, feed_dict=feed_dict) |
|
|
106 |
|
|
|
107 |
results['l1err'] = np.sum(np.abs(x - results['reconstruction'])) |
|
|
108 |
results['l2err'] = np.sum(np.sqrt((x - results['reconstruction']) ** 2)) |
|
|
109 |
|
|
|
110 |
return results |