a b/trainers/CE.py
1
import random
2
from collections import defaultdict
3
from math import inf
4
5
from tensorflow.python.ops.losses.losses_impl import Reduction
6
7
from trainers import trainer_utils
8
from trainers.AEMODEL import AEMODEL, Phase, indicate_early_stopping, update_log_dicts
9
from trainers.DLMODEL import *
10
11
12
class CE(AEMODEL):
13
    class Config(AEMODEL.Config):
14
        def __init__(self):
15
            super().__init__('CE')
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.x_ce = tf.placeholder(tf.float32, [None, self.config.outputHeight, self.config.outputWidth, self.config.numChannels], name='input_ce')
21
        self.outputs = self.network(self.x_ce, dropout_rate=self.dropout_rate, dropout=self.dropout, config=self.config)
22
        self.reconstruction = self.outputs['x_hat']
23
24
        # Print Stats
25
        self.get_number_of_trainable_params()
26
        # Instantiate Saver
27
        self.saver = tf.train.Saver()
28
29
    def train(self, dataset):
30
        # Determine trainable variables
31
        self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
32
33
        # Build losses
34
        self.losses['L1'] = tf.losses.absolute_difference(self.x, self.reconstruction, reduction=Reduction.NONE)
35
        self.losses['loss'] = self.losses['reconstructionLoss'] = tf.reduce_mean(tf.reduce_sum(self.losses['L1'], axis=[1, 2, 3]))
36
37
        # Set the optimizer
38
        optim = self.create_optimizer(self.losses['loss'], var_list=self.variables, learningrate=self.config.learningrate,
39
                                      beta1=self.config.beta1, type=self.config.optimizer)
40
41
        # initialize all variables
42
        tf.global_variables_initializer().run(session=self.sess)
43
44
        best_cost = inf
45
        last_improvement = 0
46
        last_epoch = self.load_checkpoint()
47
48
        # Go go go!
49
        for epoch in range(last_epoch, self.config.numEpochs):
50
            ############
51
            # TRAINING #
52
            ############
53
            self.process(dataset, epoch, Phase.TRAIN, optim)
54
55
            # Increment last_epoch counter and save model
56
            last_epoch += 1
57
            self.save(self.checkpointDir, last_epoch)
58
59
            ##############
60
            # VALIDATION #
61
            ##############
62
            val_scalars = self.process(dataset, epoch, Phase.VAL)
63
64
            best_cost, last_improvement, stop = indicate_early_stopping(val_scalars['loss'], best_cost, last_improvement)
65
            if stop:
66
                print('Early stopping was triggered due to no improvement over the last 5 epochs')
67
                break
68
69
    def process(self, dataset, epoch, phase: Phase, optim=None):
70
        scalars = defaultdict(list)
71
        visuals = []
72
        num_batches = dataset.num_batches(self.config.batchsize, set=phase.value)
73
        for idx in range(0, num_batches):
74
            batch, _, brainmasks = dataset.next_batch(self.config.batchsize, return_brainmask=True, set=phase.value)
75
76
            masked_batch = retrieve_masked_batch(batch, brainmasks)
77
78
            fetches = {
79
                'reconstruction': self.reconstruction,
80
                **self.losses
81
            }
82
            if phase == Phase.TRAIN:
83
                fetches['optimizer'] = optim
84
85
            feed_dict = {
86
                self.x: batch,
87
                self.x_ce: masked_batch if phase == Phase.TRAIN else batch,
88
                self.dropout: phase == Phase.TRAIN,
89
                self.dropout_rate: self.config.dropout_rate
90
            }
91
92
            run = self.sess.run(fetches, feed_dict=feed_dict)
93
94
            # Print to console
95
            print(f'Epoch ({phase.value}): [{epoch:2d}] [{idx:4d}/{num_batches:4d}] loss: {run["loss"]:.8f}')
96
            update_log_dicts(*trainer_utils.get_summary_dict(batch, run), scalars, visuals)
97
98
        self.log_to_tensorboard(epoch, scalars, visuals, phase)
99
        return scalars
100
101
    def reconstruct(self, x, dropout=False):
102
        if x.ndim < 4:
103
            x = np.expand_dims(x, 0)
104
105
        fetches = {
106
            'reconstruction': self.reconstruction
107
        }
108
109
        feed_dict = {
110
            self.x: x,
111
            self.x_ce: x,
112
            self.dropout: dropout,
113
            self.dropout_rate: self.config.dropout_rate
114
        }
115
        results = self.sess.run(fetches, feed_dict=feed_dict)
116
117
        results['l1err'] = np.sum(np.abs(x - results['reconstruction']))
118
        results['l2err'] = np.sum(np.sqrt((x - results['reconstruction']) ** 2))
119
120
        return results
121
122
123
def retrieve_masked_batch(batch, brainmasks):
124
    def retrieve_brain_range(brainmask):
125
        pixels = np.argwhere(brainmask).T
126
        return (min(pixels[0]), max(pixels[0])), (min(pixels[1]), max(pixels[1]))
127
128
    brain_ranges = list(map(lambda brainmask: retrieve_brain_range(brainmask), brainmasks))
129
    # Masking out for Context Encoder
130
    m = np.ones(batch.shape)
131
    for (m, brain_range) in zip(m, brain_ranges):
132
        for _ in range(random.randint(1, 3)):
133
            size_w, size_h = 20, 20
134
            if brain_range[0][0] < brain_range[0][1] - size_w and brain_range[1][0] < brain_range[1][1] - size_h:
135
                x = random.randint(brain_range[0][0], brain_range[0][1] - size_w)
136
                y = random.randint(brain_range[1][0], brain_range[1][1] - size_h)
137
                m[x:x + size_w, y:y + size_h] = 0
138
    masked_batch = batch * m
139
    return masked_batch