File size: 14,802 Bytes
9bd9a8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import os, cv2
import numpy as np
from network_configure import conf_unet
from network import *
from utils.predict_utils import get_coord, PercentileNormalizer, PadAndCropResizer
from utils.train_utils import augment_patch
from utils import train_utils

# UNet implementation inherited from GVTNets: https://github.com/zhengyang-wang/GVTNets
training_config = {'base_learning_rate': 0.0004,
                                     'lr_decay_steps':5e3, 
                                     'lr_decay_rate':0.5, 
                                     'lr_staircase':True}

class Noise2Same(object):

    def __init__(self, base_dir, name, 
                 dim=2, in_channels=1, lmbd=None, 
                 masking='gaussian', mask_perc=0.5,
                 opt_config=training_config, **kwargs):

        self.base_dir = base_dir # model direction
        self.name = name # model name
        self.dim = dim # image dimension
        self.in_channels = in_channels # image channels
        self.lmbd = lmbd # lambda in loss fn
        self.masking = masking
        self.mask_perc = mask_perc
        
        self.opt_config = opt_config
        conf_unet['dimension'] = '%dD'%dim
        self.net = UNet(conf_unet)
        
    def _model_fn(self, features, labels, mode):
        conv_op = convolution_2D if self.dim==2 else convolution_3D
        axis = {3:[1,2,3,4], 2:[1,2,3]}[self.dim]
        
        def image_summary(img):
            return tf.reduce_max(img, axis=1) if self.dim == 3 else img
        
        # Local average excluding the center pixel (donut)
        def mask_kernel(features):
            kernel = (np.array([[0.5, 1.0, 0.5], [1.0, 0.0, 1.0], [0.5, 1.0, 0.5]]) 
                      if self.dim == 2 else 
                      np.array([[[0, 0.5, 0], [0.5, 1.0, 0.5], [0, 0.5, 0]],
                                [[0.5, 1.0, 0.5], [1.0, 0.0, 1.0], [0.5, 1.0, 0.5]],
                                [[0, 0.5, 0], [0.5, 1.0, 0.5], [0, 0.5, 0]]]))
            kernel = (kernel/kernel.sum())
            kernels = np.empty([3, 3, self.in_channels, self.in_channels])
            for i in range(self.in_channels):
                kernels[:,:,i,i] = kernel
            nn_conv_op = tf.nn.conv2d if self.dim == 2 else tf.nn.conv3d
            return nn_conv_op(features, tf.constant(kernels.astype('float32')), 
                              [1]*self.dim+[1,1], padding='SAME')
        
        if not mode == tf.estimator.ModeKeys.PREDICT:
            noise, mask = tf.split(labels, [self.in_channels, self.in_channels], -1)
            
            if self.masking == 'gaussian':
                masked_features = (1 - mask) * features + mask * noise
            elif self.masking == 'donut':
                masked_features = (1 - mask) * features + mask * mask_kernel(features)
            else:
                raise NotImplementedError
            
            # Prediction from masked input
            with tf.variable_scope('main_unet', reuse=tf.compat.v1.AUTO_REUSE):
                out = self.net(masked_features, mode == tf.estimator.ModeKeys.TRAIN)
                out = batch_norm(out, mode == tf.estimator.ModeKeys.TRAIN, 'unet_out')
                out = relu(out)
                preds = conv_op(out, self.in_channels, 1, 1, False, name = 'out_conv')
                
            # Prediction from full input
            with tf.variable_scope('main_unet', reuse=tf.compat.v1.AUTO_REUSE):
                rawout = self.net(features, mode == tf.estimator.ModeKeys.TRAIN)
                rawout = batch_norm(rawout, mode == tf.estimator.ModeKeys.TRAIN, 'unet_out')
                rawout = relu(rawout)
                rawpreds = conv_op(rawout, self.in_channels, 1, 1, False, name = 'out_conv')
            
            # Loss components
            rec_mse = tf.reduce_mean(tf.square(rawpreds - features), axis=None)
            inv_mse = tf.reduce_sum(tf.square(rawpreds - preds) * mask) / tf.reduce_sum(mask)
            bsp_mse = tf.reduce_sum(tf.square(features - preds) * mask) / tf.reduce_sum(mask)

            # Tensorboard display
            tf.summary.image('1_inputs', image_summary(features), max_outputs=3)
            tf.summary.image('2_raw_predictions', image_summary(rawpreds), max_outputs=3)
            tf.summary.image('3_mask', image_summary(mask), max_outputs=3)
            tf.summary.image('4_masked_predictions', image_summary(preds), max_outputs=3)
            tf.summary.image('5_difference', image_summary(rawpreds-preds), max_outputs=3)
            tf.summary.image('6_rec_error', image_summary(preds-features), max_outputs=3)
            tf.summary.scalar('reconstruction', rec_mse, family='loss_metric') 
            tf.summary.scalar('invariance', inv_mse, family='loss_metric') 
            tf.summary.scalar('blind_spot', bsp_mse, family='loss_metric')
                
        else:
            with tf.variable_scope('main_unet'):
                out = self.net(features, mode == tf.estimator.ModeKeys.TRAIN)
                out = batch_norm(out, mode == tf.estimator.ModeKeys.TRAIN, 'unet_out')
                out = relu(out)
                preds = conv_op(out, self.in_channels, 1, 1, False, name = 'out_conv')
            return tf.estimator.EstimatorSpec(mode=mode, predictions=preds)
        
        lmbd = 2 if self.lmbd is None else self.lmbd
        loss = rec_mse + lmbd*tf.sqrt(inv_mse)

        if mode == tf.estimator.ModeKeys.TRAIN:
            global_step = tf.train.get_or_create_global_step()
            learning_rate = tf.train.exponential_decay(self.opt_config['base_learning_rate'], 
                                                       global_step, 
                                                       self.opt_config['lr_decay_steps'], 
                                                       self.opt_config['lr_decay_rate'], 
                                                       self.opt_config['lr_staircase'])
            optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)

            update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope='main_unet')
            with tf.control_dependencies(update_ops):
                train_op = optimizer.minimize(loss, global_step)
        else:
            train_op = None
        
        metrics = {'loss_metric/invariance':tf.metrics.mean(inv_mse),
                              'loss_metric/blind_spot':tf.metrics.mean(bsp_mse), 
                              'loss_metric/reconstruction':tf.metrics.mean(rec_mse)}
        
        return tf.estimator.EstimatorSpec(mode=mode, predictions=preds, loss=loss, train_op=train_op, 
                                          eval_metric_ops=metrics)


    def _input_fn(self, sources, patch_size, batch_size, is_train=True):
        # Stratified sampling inherited from Noise2Void: https://github.com/juglab/n2v
        get_stratified_coords = getattr(train_utils, 'get_stratified_coords%dD'%self.dim)
        rand_float_coords = getattr(train_utils, 'rand_float_coords%dD'%self.dim)
        
        def generator():
            while(True):
                source = sources[np.random.randint(len(sources))]
                valid_shape = source.shape[:-1] - np.array(patch_size)
                if any([s<=0 for s in valid_shape]):
                    source_patch = augment_patch(source)
                else:
                    coords = [np.random.randint(0, shape_i+1) for shape_i in valid_shape]
                    s = tuple([slice(coord, coord+size) for coord, size in zip(coords, patch_size)])
                    source_patch = augment_patch(source[s])
                
                mask = np.zeros_like(source_patch)
                for c in range(self.in_channels):
                    boxsize = np.round(np.sqrt(100/self.mask_perc)).astype(np.int)
                    maskcoords = get_stratified_coords(rand_float_coords(boxsize), 
                                                       box_size=boxsize, shape=tuple(patch_size))
                    indexing = maskcoords + (c,)
                    mask[indexing] = 1.0

                noise_patch = np.concatenate([np.random.normal(0, 0.2, source_patch.shape), mask], axis=-1)
                yield source_patch, noise_patch
                
        def generator_val():
            for idx in range(len(sources)):
                source_patch = sources[idx]
                patch_size = source_patch.shape[:-1]
                boxsize = np.round(np.sqrt(100/self.mask_perc)).astype(np.int)
                maskcoords = get_stratified_coords(rand_float_coords(boxsize), 
                                                   box_size=boxsize, shape=tuple(patch_size))
                indexing = maskcoords + (0,)
                mask = np.zeros_like(source_patch)
                mask[indexing] = 1.0
                noise_patch = np.concatenate([np.random.normal(0, 0.2, source_patch.shape), mask], axis=-1)
                yield source_patch, noise_patch

        output_types = (tf.float32, tf.float32)
        output_shapes = (tf.TensorShape(list(patch_size) + [self.in_channels]), 
                                             tf.TensorShape(list(patch_size) + [self.in_channels*2]))
        gen = generator if is_train else generator_val
        dataset = tf.data.Dataset.from_generator(gen, output_types=output_types, output_shapes=output_shapes)
        dataset = dataset.batch(batch_size).prefetch(tf.data.experimental.AUTOTUNE)

        return dataset


    def train(self, source_lst, patch_size, validation=None, batch_size=64, save_steps=1000, log_steps=200, steps=50000):
        assert len(patch_size)==self.dim
        assert len(source_lst[0].shape)==self.dim + 1
        assert source_lst[0].shape[-1]==self.in_channels

        ses_config = tf.ConfigProto()
        ses_config.gpu_options.allow_growth = True

        run_config = tf.estimator.RunConfig(model_dir=self.base_dir+'/'+self.name, 
                                            save_checkpoints_steps=save_steps,
                                            session_config=ses_config, 
                                            log_step_count_steps=log_steps,
                                            save_summary_steps=log_steps,
                                            keep_checkpoint_max=2)

        estimator = tf.estimator.Estimator(model_fn=self._model_fn, 
                                             model_dir=self.base_dir+'/'+self.name, 
                                             config=run_config)
        
        input_fn = lambda: self._input_fn(source_lst, patch_size, batch_size=batch_size)
        
        if validation is not None:
            train_spec = tf.estimator.TrainSpec(input_fn=input_fn, max_steps=steps)
            val_input_fn = lambda: self._input_fn(validation.astype('float32'), 
                                                  validation.shape[1:-1], 
                                                  batch_size=4, 
                                                  is_train=False)
            eval_spec = tf.estimator.EvalSpec(input_fn=val_input_fn, throttle_secs=120)
            tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
        else:
            estimator.train(input_fn=input_fn, steps=steps)
            

    # Used for single image prediction
    def predict(self, image, resizer=PadAndCropResizer(), checkpoint_path=None,
               im_mean=None, im_std=None):

        tf.logging.set_verbosity(tf.logging.ERROR)
        estimator = tf.estimator.Estimator(model_fn=self._model_fn, 
                                            model_dir=self.base_dir+'/'+self.name)
        
        im_mean, im_std = ((image.mean(), image.std()) if im_mean is None or im_std is None else (im_mean, im_std)) 
        image = (image - im_mean)/im_std
        if self.in_channels == 1:
            image = resizer.before(image, 2 ** (self.net.depth), exclude=None)
            input_fn = tf.estimator.inputs.numpy_input_fn(x=image[None, ..., None], batch_size=1, num_epochs=1, shuffle=False)
            image = list(estimator.predict(input_fn=input_fn, checkpoint_path=checkpoint_path))[0][..., 0]
            image = resizer.after(image, exclude=None)
        else:
            image = resizer.before(image, 2 ** (self.net.depth), exclude=-1)
            input_fn = tf.estimator.inputs.numpy_input_fn(x=image[None], batch_size=1, num_epochs=1, shuffle=False)
            image = list(estimator.predict(input_fn=input_fn, checkpoint_path=checkpoint_path))[0]
            image = resizer.after(image, exclude=-1)
        image = image*im_std + im_mean

        return image
    
    # Used for batch images prediction
    def batch_predict(self, images, resizer=PadAndCropResizer(), checkpoint_path=None,
               im_mean=None, im_std=None, batch_size=32):

        tf.logging.set_verbosity(tf.logging.ERROR)
        estimator = tf.estimator.Estimator(model_fn=self._model_fn, 
                                            model_dir=self.base_dir+'/'+self.name)
        
        im_mean, im_std = ((images.mean(), images.std()) if im_mean is None or im_std is None else (im_mean, im_std)) 
        
        images = (images - im_mean)/im_std
        images = resizer.before(images, 2 ** (self.net.depth), exclude=0)
        input_fn = tf.estimator.inputs.numpy_input_fn(x=images[ ..., None], batch_size=batch_size, num_epochs=1, shuffle=False)
        images = np.stack(list(estimator.predict(input_fn=input_fn, checkpoint_path=checkpoint_path)))[..., 0]
        images = resizer.after(images, exclude=0)
        images = images*im_std + im_mean

        return images

    # Used for extremely large input images
    def crop_predict(self, image, size, margin, resizer=PadAndCropResizer(), checkpoint_path=None,
               im_mean=None, im_std=None):

        tf.logging.set_verbosity(tf.logging.ERROR)
        estimator = tf.estimator.Estimator(model_fn=self._model_fn, 
                                            model_dir=self.base_dir+'/'+self.name)
        
        im_mean, im_std = ((image.mean(), image.std()) if im_mean is None or im_std is None else (im_mean, im_std)) 
        image = (image - im_mean)/im_std
        out_image = np.empty(image.shape, dtype='float32')
        for src_s, trg_s, mrg_s in get_coord(image.shape, size, margin):
            patch = resizer.before(image[src_s], 2 ** (self.net.depth), exclude=None)
            input_fn = tf.estimator.inputs.numpy_input_fn(x=patch[None, ..., None], batch_size=1, num_epochs=1, shuffle=False)
            patch = list(estimator.predict(input_fn=input_fn, checkpoint_path=checkpoint_path))[0][..., 0]
            patch = resizer.after(patch, exclude=None)
            out_image[trg_s] = patch[mrg_s]
            
        image = out_image*im_std + im_mean

        return image