Diff of /trainers/ConstrainedAE.py [000000] .. [978658]

Switch to unified view

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