ky2k commited on
Commit
de947e8
1 Parent(s): 0316f24

First demo commit, v2

Browse files
README.md CHANGED
@@ -7,6 +7,7 @@ sdk: gradio
7
  sdk_version: 3.1.3
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
7
  sdk_version: 3.1.3
8
  app_file: app.py
9
  pinned: false
10
+ python_version: 3.7
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from models import Noise2Same
3
+ import gradio as gr
4
+
5
+ os.system("mkdir trained_models/denoising_ImageNet")
6
+ os.system("cd trained_models/denoising_ImageNet; gdown https://drive.google.com/uc?id=1asrwULW1lDFasystBc3UfShh5EeTHpkW; gdown https://drive.google.com/uc?id=1Re1ER7KtujBunN0-74QmYrrOx77WpVXK; gdown https://drive.google.com/uc?id=1QdlyUPUKyyGtqD0zBrj5F7qQZtmUELSu; gdown https://drive.google.com/uc?id=1LQsYR26ldHebcdQtP2zt4Mh-ZH9vXQ2S; gdown https://drive.google.com/uc?id=1AxTDD4dS0DtzmBywjGyeJYgDrw-XjYbc; gdown https://drive.google.com/uc?id=1w4UdNAbOjvWSL0Jgbq8_hCniaxqsbLaQ; cd ../..")
7
+ os.system("wget -O arch.png https://i.imgur.com/NruRABn.png")
8
+ os.system("wget -O parrot.png https://i.imgur.com/zdji3xv.png")
9
+ os.system("wget -O lion.png https://i.imgur.com/qNT0lJJ.png")
10
+
11
+ model = Noise2Same('trained_models/', 'denoising_ImageNet', dim=2, in_channels=3)
12
+
13
+ def norm(x):
14
+ x = (x-x.min())/(x.max()-x.min())
15
+ return x
16
+
17
+ def predict(img):
18
+ pred = model.predict(img.astype('float32'))
19
+ return norm(pred)
20
+
21
+ img = gr.inputs.Image()
22
+
23
+ title = "Noise2Same: Optimizing A Self-Supervised Bound for Image Denoising"
24
+ description = "Interactive demo of Noise2Same, an image denoising method developed by Yaochen Xie"
25
+
26
+ gr.Interface(predict, "image", "image", examples=[["lion.png"], ["arch.png"], ["parrot.png"]], title=title, description=description).launch()
basic_ops.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging, os
2
+ logging.disable(logging.WARNING)
3
+ os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
4
+
5
+ import tensorflow as tf
6
+ from network_configure import conf_basic_ops
7
+
8
+
9
+ """This script defines basic operaters.
10
+ """
11
+
12
+
13
+ def convolution_2D(inputs, filters, kernel_size, strides, use_bias, name=None):
14
+ """Performs 2D convolution without activation function.
15
+ If followed by batch normalization, set use_bias=False.
16
+ """
17
+ return tf.layers.conv2d(
18
+ inputs=inputs,
19
+ filters=filters,
20
+ kernel_size=kernel_size,
21
+ strides=strides,
22
+ padding='same',
23
+ use_bias=use_bias,
24
+ kernel_initializer=conf_basic_ops['kernel_initializer'],
25
+ name=name,
26
+ )
27
+
28
+ def convolution_3D(inputs, filters, kernel_size, strides, use_bias, name=None):
29
+ """Performs 3D convolution without activation function.
30
+ If followed by batch normalization, set use_bias=False.
31
+ """
32
+ return tf.layers.conv3d(
33
+ inputs=inputs,
34
+ filters=filters,
35
+ kernel_size=kernel_size,
36
+ strides=strides,
37
+ padding='same',
38
+ use_bias=use_bias,
39
+ kernel_initializer=conf_basic_ops['kernel_initializer'],
40
+ name=name,
41
+ )
42
+
43
+ def transposed_convolution_2D(inputs, filters, kernel_size, strides, use_bias, name=None):
44
+ """Performs 2D transposed convolution without activation function.
45
+ If followed by batch normalization, set use_bias=False.
46
+ """
47
+ return tf.layers.conv2d_transpose(
48
+ inputs=inputs,
49
+ filters=filters,
50
+ kernel_size=kernel_size,
51
+ strides=strides,
52
+ padding='same',
53
+ use_bias=use_bias,
54
+ kernel_initializer=conf_basic_ops['kernel_initializer'],
55
+ name=name,
56
+ )
57
+
58
+ def transposed_convolution_3D(inputs, filters, kernel_size, strides, use_bias, name=None):
59
+ """Performs 3D transposed convolution without activation function.
60
+ If followed by batch normalization, set use_bias=False.
61
+ """
62
+ return tf.layers.conv3d_transpose(
63
+ inputs=inputs,
64
+ filters=filters,
65
+ kernel_size=kernel_size,
66
+ strides=strides,
67
+ padding='same',
68
+ use_bias=use_bias,
69
+ kernel_initializer=conf_basic_ops['kernel_initializer'],
70
+ name=name,
71
+ )
72
+
73
+ def batch_norm(inputs, training, name=None):
74
+ """Performs a batch normalization.
75
+ We set fused=True for a significant performance boost.
76
+ See https://www.tensorflow.org/performance/performance_guide#common_fused_ops
77
+ """
78
+ return tf.layers.batch_normalization(
79
+ inputs=inputs,
80
+ momentum=conf_basic_ops['momentum'],
81
+ epsilon=conf_basic_ops['epsilon'],
82
+ center=True,
83
+ scale=True,
84
+ training=training,
85
+ fused=True,
86
+ name=name,
87
+ )
88
+
89
+ def relu(inputs, name=None):
90
+ return tf.nn.relu(inputs, name=name) if conf_basic_ops['relu_type'] == 'relu' \
91
+ else tf.nn.relu6(inputs, name=name)
models.py ADDED
@@ -0,0 +1,276 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, cv2
2
+ import numpy as np
3
+ from network_configure import conf_unet
4
+ from network import *
5
+ from utils.predict_utils import get_coord, PercentileNormalizer, PadAndCropResizer
6
+ from utils.train_utils import augment_patch
7
+ from utils import train_utils
8
+
9
+ # UNet implementation inherited from GVTNets: https://github.com/zhengyang-wang/GVTNets
10
+ training_config = {'base_learning_rate': 0.0004,
11
+ 'lr_decay_steps':5e3,
12
+ 'lr_decay_rate':0.5,
13
+ 'lr_staircase':True}
14
+
15
+ class Noise2Same(object):
16
+
17
+ def __init__(self, base_dir, name,
18
+ dim=2, in_channels=1, lmbd=None,
19
+ masking='gaussian', mask_perc=0.5,
20
+ opt_config=training_config, **kwargs):
21
+
22
+ self.base_dir = base_dir # model direction
23
+ self.name = name # model name
24
+ self.dim = dim # image dimension
25
+ self.in_channels = in_channels # image channels
26
+ self.lmbd = lmbd # lambda in loss fn
27
+ self.masking = masking
28
+ self.mask_perc = mask_perc
29
+
30
+ self.opt_config = opt_config
31
+ conf_unet['dimension'] = '%dD'%dim
32
+ self.net = UNet(conf_unet)
33
+
34
+ def _model_fn(self, features, labels, mode):
35
+ conv_op = convolution_2D if self.dim==2 else convolution_3D
36
+ axis = {3:[1,2,3,4], 2:[1,2,3]}[self.dim]
37
+
38
+ def image_summary(img):
39
+ return tf.reduce_max(img, axis=1) if self.dim == 3 else img
40
+
41
+ # Local average excluding the center pixel (donut)
42
+ def mask_kernel(features):
43
+ kernel = (np.array([[0.5, 1.0, 0.5], [1.0, 0.0, 1.0], [0.5, 1.0, 0.5]])
44
+ if self.dim == 2 else
45
+ np.array([[[0, 0.5, 0], [0.5, 1.0, 0.5], [0, 0.5, 0]],
46
+ [[0.5, 1.0, 0.5], [1.0, 0.0, 1.0], [0.5, 1.0, 0.5]],
47
+ [[0, 0.5, 0], [0.5, 1.0, 0.5], [0, 0.5, 0]]]))
48
+ kernel = (kernel/kernel.sum())
49
+ kernels = np.empty([3, 3, self.in_channels, self.in_channels])
50
+ for i in range(self.in_channels):
51
+ kernels[:,:,i,i] = kernel
52
+ nn_conv_op = tf.nn.conv2d if self.dim == 2 else tf.nn.conv3d
53
+ return nn_conv_op(features, tf.constant(kernels.astype('float32')),
54
+ [1]*self.dim+[1,1], padding='SAME')
55
+
56
+ if not mode == tf.estimator.ModeKeys.PREDICT:
57
+ noise, mask = tf.split(labels, [self.in_channels, self.in_channels], -1)
58
+
59
+ if self.masking == 'gaussian':
60
+ masked_features = (1 - mask) * features + mask * noise
61
+ elif self.masking == 'donut':
62
+ masked_features = (1 - mask) * features + mask * mask_kernel(features)
63
+ else:
64
+ raise NotImplementedError
65
+
66
+ # Prediction from masked input
67
+ with tf.variable_scope('main_unet', reuse=tf.compat.v1.AUTO_REUSE):
68
+ out = self.net(masked_features, mode == tf.estimator.ModeKeys.TRAIN)
69
+ out = batch_norm(out, mode == tf.estimator.ModeKeys.TRAIN, 'unet_out')
70
+ out = relu(out)
71
+ preds = conv_op(out, self.in_channels, 1, 1, False, name = 'out_conv')
72
+
73
+ # Prediction from full input
74
+ with tf.variable_scope('main_unet', reuse=tf.compat.v1.AUTO_REUSE):
75
+ rawout = self.net(features, mode == tf.estimator.ModeKeys.TRAIN)
76
+ rawout = batch_norm(rawout, mode == tf.estimator.ModeKeys.TRAIN, 'unet_out')
77
+ rawout = relu(rawout)
78
+ rawpreds = conv_op(rawout, self.in_channels, 1, 1, False, name = 'out_conv')
79
+
80
+ # Loss components
81
+ rec_mse = tf.reduce_mean(tf.square(rawpreds - features), axis=None)
82
+ inv_mse = tf.reduce_sum(tf.square(rawpreds - preds) * mask) / tf.reduce_sum(mask)
83
+ bsp_mse = tf.reduce_sum(tf.square(features - preds) * mask) / tf.reduce_sum(mask)
84
+
85
+ # Tensorboard display
86
+ tf.summary.image('1_inputs', image_summary(features), max_outputs=3)
87
+ tf.summary.image('2_raw_predictions', image_summary(rawpreds), max_outputs=3)
88
+ tf.summary.image('3_mask', image_summary(mask), max_outputs=3)
89
+ tf.summary.image('4_masked_predictions', image_summary(preds), max_outputs=3)
90
+ tf.summary.image('5_difference', image_summary(rawpreds-preds), max_outputs=3)
91
+ tf.summary.image('6_rec_error', image_summary(preds-features), max_outputs=3)
92
+ tf.summary.scalar('reconstruction', rec_mse, family='loss_metric')
93
+ tf.summary.scalar('invariance', inv_mse, family='loss_metric')
94
+ tf.summary.scalar('blind_spot', bsp_mse, family='loss_metric')
95
+
96
+ else:
97
+ with tf.variable_scope('main_unet'):
98
+ out = self.net(features, mode == tf.estimator.ModeKeys.TRAIN)
99
+ out = batch_norm(out, mode == tf.estimator.ModeKeys.TRAIN, 'unet_out')
100
+ out = relu(out)
101
+ preds = conv_op(out, self.in_channels, 1, 1, False, name = 'out_conv')
102
+ return tf.estimator.EstimatorSpec(mode=mode, predictions=preds)
103
+
104
+ lmbd = 2 if self.lmbd is None else self.lmbd
105
+ loss = rec_mse + lmbd*tf.sqrt(inv_mse)
106
+
107
+ if mode == tf.estimator.ModeKeys.TRAIN:
108
+ global_step = tf.train.get_or_create_global_step()
109
+ learning_rate = tf.train.exponential_decay(self.opt_config['base_learning_rate'],
110
+ global_step,
111
+ self.opt_config['lr_decay_steps'],
112
+ self.opt_config['lr_decay_rate'],
113
+ self.opt_config['lr_staircase'])
114
+ optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
115
+
116
+ update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope='main_unet')
117
+ with tf.control_dependencies(update_ops):
118
+ train_op = optimizer.minimize(loss, global_step)
119
+ else:
120
+ train_op = None
121
+
122
+ metrics = {'loss_metric/invariance':tf.metrics.mean(inv_mse),
123
+ 'loss_metric/blind_spot':tf.metrics.mean(bsp_mse),
124
+ 'loss_metric/reconstruction':tf.metrics.mean(rec_mse)}
125
+
126
+ return tf.estimator.EstimatorSpec(mode=mode, predictions=preds, loss=loss, train_op=train_op,
127
+ eval_metric_ops=metrics)
128
+
129
+
130
+ def _input_fn(self, sources, patch_size, batch_size, is_train=True):
131
+ # Stratified sampling inherited from Noise2Void: https://github.com/juglab/n2v
132
+ get_stratified_coords = getattr(train_utils, 'get_stratified_coords%dD'%self.dim)
133
+ rand_float_coords = getattr(train_utils, 'rand_float_coords%dD'%self.dim)
134
+
135
+ def generator():
136
+ while(True):
137
+ source = sources[np.random.randint(len(sources))]
138
+ valid_shape = source.shape[:-1] - np.array(patch_size)
139
+ if any([s<=0 for s in valid_shape]):
140
+ source_patch = augment_patch(source)
141
+ else:
142
+ coords = [np.random.randint(0, shape_i+1) for shape_i in valid_shape]
143
+ s = tuple([slice(coord, coord+size) for coord, size in zip(coords, patch_size)])
144
+ source_patch = augment_patch(source[s])
145
+
146
+ mask = np.zeros_like(source_patch)
147
+ for c in range(self.in_channels):
148
+ boxsize = np.round(np.sqrt(100/self.mask_perc)).astype(np.int)
149
+ maskcoords = get_stratified_coords(rand_float_coords(boxsize),
150
+ box_size=boxsize, shape=tuple(patch_size))
151
+ indexing = maskcoords + (c,)
152
+ mask[indexing] = 1.0
153
+
154
+ noise_patch = np.concatenate([np.random.normal(0, 0.2, source_patch.shape), mask], axis=-1)
155
+ yield source_patch, noise_patch
156
+
157
+ def generator_val():
158
+ for idx in range(len(sources)):
159
+ source_patch = sources[idx]
160
+ patch_size = source_patch.shape[:-1]
161
+ boxsize = np.round(np.sqrt(100/self.mask_perc)).astype(np.int)
162
+ maskcoords = get_stratified_coords(rand_float_coords(boxsize),
163
+ box_size=boxsize, shape=tuple(patch_size))
164
+ indexing = maskcoords + (0,)
165
+ mask = np.zeros_like(source_patch)
166
+ mask[indexing] = 1.0
167
+ noise_patch = np.concatenate([np.random.normal(0, 0.2, source_patch.shape), mask], axis=-1)
168
+ yield source_patch, noise_patch
169
+
170
+ output_types = (tf.float32, tf.float32)
171
+ output_shapes = (tf.TensorShape(list(patch_size) + [self.in_channels]),
172
+ tf.TensorShape(list(patch_size) + [self.in_channels*2]))
173
+ gen = generator if is_train else generator_val
174
+ dataset = tf.data.Dataset.from_generator(gen, output_types=output_types, output_shapes=output_shapes)
175
+ dataset = dataset.batch(batch_size).prefetch(tf.data.experimental.AUTOTUNE)
176
+
177
+ return dataset
178
+
179
+
180
+ def train(self, source_lst, patch_size, validation=None, batch_size=64, save_steps=1000, log_steps=200, steps=50000):
181
+ assert len(patch_size)==self.dim
182
+ assert len(source_lst[0].shape)==self.dim + 1
183
+ assert source_lst[0].shape[-1]==self.in_channels
184
+
185
+ ses_config = tf.ConfigProto()
186
+ ses_config.gpu_options.allow_growth = True
187
+
188
+ run_config = tf.estimator.RunConfig(model_dir=self.base_dir+'/'+self.name,
189
+ save_checkpoints_steps=save_steps,
190
+ session_config=ses_config,
191
+ log_step_count_steps=log_steps,
192
+ save_summary_steps=log_steps,
193
+ keep_checkpoint_max=2)
194
+
195
+ estimator = tf.estimator.Estimator(model_fn=self._model_fn,
196
+ model_dir=self.base_dir+'/'+self.name,
197
+ config=run_config)
198
+
199
+ input_fn = lambda: self._input_fn(source_lst, patch_size, batch_size=batch_size)
200
+
201
+ if validation is not None:
202
+ train_spec = tf.estimator.TrainSpec(input_fn=input_fn, max_steps=steps)
203
+ val_input_fn = lambda: self._input_fn(validation.astype('float32'),
204
+ validation.shape[1:-1],
205
+ batch_size=4,
206
+ is_train=False)
207
+ eval_spec = tf.estimator.EvalSpec(input_fn=val_input_fn, throttle_secs=120)
208
+ tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
209
+ else:
210
+ estimator.train(input_fn=input_fn, steps=steps)
211
+
212
+
213
+ # Used for single image prediction
214
+ def predict(self, image, resizer=PadAndCropResizer(), checkpoint_path=None,
215
+ im_mean=None, im_std=None):
216
+
217
+ tf.logging.set_verbosity(tf.logging.ERROR)
218
+ estimator = tf.estimator.Estimator(model_fn=self._model_fn,
219
+ model_dir=self.base_dir+'/'+self.name)
220
+
221
+ im_mean, im_std = ((image.mean(), image.std()) if im_mean is None or im_std is None else (im_mean, im_std))
222
+ image = (image - im_mean)/im_std
223
+ if self.in_channels == 1:
224
+ image = resizer.before(image, 2 ** (self.net.depth), exclude=None)
225
+ input_fn = tf.estimator.inputs.numpy_input_fn(x=image[None, ..., None], batch_size=1, num_epochs=1, shuffle=False)
226
+ image = list(estimator.predict(input_fn=input_fn, checkpoint_path=checkpoint_path))[0][..., 0]
227
+ image = resizer.after(image, exclude=None)
228
+ else:
229
+ image = resizer.before(image, 2 ** (self.net.depth), exclude=-1)
230
+ input_fn = tf.estimator.inputs.numpy_input_fn(x=image[None], batch_size=1, num_epochs=1, shuffle=False)
231
+ image = list(estimator.predict(input_fn=input_fn, checkpoint_path=checkpoint_path))[0]
232
+ image = resizer.after(image, exclude=-1)
233
+ image = image*im_std + im_mean
234
+
235
+ return image
236
+
237
+ # Used for batch images prediction
238
+ def batch_predict(self, images, resizer=PadAndCropResizer(), checkpoint_path=None,
239
+ im_mean=None, im_std=None, batch_size=32):
240
+
241
+ tf.logging.set_verbosity(tf.logging.ERROR)
242
+ estimator = tf.estimator.Estimator(model_fn=self._model_fn,
243
+ model_dir=self.base_dir+'/'+self.name)
244
+
245
+ im_mean, im_std = ((images.mean(), images.std()) if im_mean is None or im_std is None else (im_mean, im_std))
246
+
247
+ images = (images - im_mean)/im_std
248
+ images = resizer.before(images, 2 ** (self.net.depth), exclude=0)
249
+ input_fn = tf.estimator.inputs.numpy_input_fn(x=images[ ..., None], batch_size=batch_size, num_epochs=1, shuffle=False)
250
+ images = np.stack(list(estimator.predict(input_fn=input_fn, checkpoint_path=checkpoint_path)))[..., 0]
251
+ images = resizer.after(images, exclude=0)
252
+ images = images*im_std + im_mean
253
+
254
+ return images
255
+
256
+ # Used for extremely large input images
257
+ def crop_predict(self, image, size, margin, resizer=PadAndCropResizer(), checkpoint_path=None,
258
+ im_mean=None, im_std=None):
259
+
260
+ tf.logging.set_verbosity(tf.logging.ERROR)
261
+ estimator = tf.estimator.Estimator(model_fn=self._model_fn,
262
+ model_dir=self.base_dir+'/'+self.name)
263
+
264
+ im_mean, im_std = ((image.mean(), image.std()) if im_mean is None or im_std is None else (im_mean, im_std))
265
+ image = (image - im_mean)/im_std
266
+ out_image = np.empty(image.shape, dtype='float32')
267
+ for src_s, trg_s, mrg_s in get_coord(image.shape, size, margin):
268
+ patch = resizer.before(image[src_s], 2 ** (self.net.depth), exclude=None)
269
+ input_fn = tf.estimator.inputs.numpy_input_fn(x=patch[None, ..., None], batch_size=1, num_epochs=1, shuffle=False)
270
+ patch = list(estimator.predict(input_fn=input_fn, checkpoint_path=checkpoint_path))[0][..., 0]
271
+ patch = resizer.after(patch, exclude=None)
272
+ out_image[trg_s] = patch[mrg_s]
273
+
274
+ image = out_image*im_std + im_mean
275
+
276
+ return image
network.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging, os
2
+ logging.disable(logging.WARNING)
3
+ os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
4
+
5
+ import tensorflow as tf
6
+ from basic_ops import *
7
+ from resnet_module import *
8
+
9
+
10
+ """This script generates the U-Net architecture according to conf_unet.
11
+ """
12
+
13
+ class UNet(object):
14
+ def __init__(self, conf_unet):
15
+ self.depth = conf_unet['depth']
16
+ self.dimension = conf_unet['dimension']
17
+ self.first_output_filters = conf_unet['first_output_filters']
18
+ self.encoding_block_sizes = conf_unet['encoding_block_sizes']
19
+ self.downsampling = conf_unet['downsampling']
20
+ self.decoding_block_sizes = conf_unet['decoding_block_sizes']
21
+ self.skip_method = conf_unet['skip_method']
22
+
23
+ def __call__(self, inputs, training):
24
+ """Add operations to classify a batch of input images.
25
+
26
+ Args:
27
+ inputs: A Tensor representing a batch of input images.
28
+ training: A boolean. Set to True to add operations required only when
29
+ training the classifier.
30
+
31
+ Returns:
32
+ A logits Tensor with shape [<batch_size>, self.num_classes].
33
+ """
34
+
35
+ return self._build_network(inputs, training)
36
+
37
+
38
+ ################################################################################
39
+ # Composite blocks building the network
40
+ ################################################################################
41
+ def _build_network(self, inputs, training):
42
+ # first_convolution
43
+ if self.dimension == '2D':
44
+ convolution = convolution_2D
45
+ elif self.dimension == '3D':
46
+ convolution = convolution_3D
47
+ inputs = convolution(inputs, self.first_output_filters, 3, 1, False, 'first_convolution')
48
+
49
+ # encoding_block_1
50
+ with tf.variable_scope('encoding_block_1'):
51
+ for block_index in range(0, self.encoding_block_sizes[0]):
52
+ inputs = res_block(inputs, self.first_output_filters, training, self.dimension,
53
+ 'res_%d' % block_index)
54
+
55
+ # encoding_block_i (down) = downsampling + zero or more res_block, i = 2, 3, ..., depth
56
+ skip_inputs = [] # for identity skip connections
57
+ for i in range(2, self.depth+1):
58
+ skip_inputs.append(inputs)
59
+ with tf.variable_scope('encoding_block_%d' % i):
60
+ output_filters = self.first_output_filters * (2**(i-1))
61
+
62
+ # downsampling
63
+ downsampling_func = self._get_downsampling_function(self.downsampling[i-2])
64
+ inputs = downsampling_func(inputs, output_filters, training, self.dimension,
65
+ 'downsampling')
66
+
67
+ for block_index in range(0, self.encoding_block_sizes[i-1]):
68
+ inputs = res_block(inputs, output_filters, training, self.dimension,
69
+ 'res_%d' % block_index)
70
+
71
+ # bottom_block = a combination of same_gto and res_block
72
+ with tf.variable_scope('bottom_block'):
73
+ output_filters = self.first_output_filters * (2**(self.depth-1))
74
+ for block_index in range(0, 1):
75
+ current_func = res_block
76
+ inputs = current_func(inputs, output_filters, training, self.dimension,
77
+ 'block_%d' % block_index)
78
+
79
+ """
80
+ Note: Identity skip connections are between the output of encoding_block_i and
81
+ the output of upsampling in decoding_block_i, i = 1, 2, ..., depth-1.
82
+ skip_inputs[i] is the output of encoding_block_i now.
83
+ len(skip_inputs) == depth - 1
84
+ skip_inputs[depth-2] should be combined during decoding_block_depth-1
85
+ skip_inputs[0] should be combined during decoding_block_1
86
+ """
87
+
88
+ # decoding_block_j (up) = upsampling + zero or more res_block, j = depth-1, depth-2, ..., 1
89
+ for j in range(self.depth-1, 0, -1):
90
+ with tf.variable_scope('decoding_block_%d' % j):
91
+ output_filters = self.first_output_filters * (2**(j-1))
92
+
93
+ # upsampling
94
+ upsampling_func = up_transposed_convolution
95
+ inputs = upsampling_func(inputs, output_filters, training, self.dimension,
96
+ 'upsampling')
97
+
98
+ # combine with skip connections
99
+ if self.skip_method == 'add':
100
+ inputs = tf.add(inputs, skip_inputs[j-1])
101
+ elif self.skip_method == 'concat':
102
+ inputs = tf.concat([inputs, skip_inputs[j-1]], axis=-1)
103
+
104
+ for block_index in range(0, self.decoding_block_sizes[self.depth-1-j]):
105
+ inputs = res_block(inputs, output_filters, training, self.dimension,
106
+ 'res_%d' % block_index)
107
+
108
+ return inputs
109
+
110
+
111
+ def _get_downsampling_function(self, name):
112
+ if name == 'down_res_block':
113
+ return down_res_block
114
+ elif name == 'convolution':
115
+ return down_convolution
116
+ else:
117
+ raise ValueError("Unsupported function: %s." % (name))
network_configure.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging, os
2
+ logging.disable(logging.WARNING)
3
+ os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
4
+
5
+ import tensorflow as tf
6
+
7
+
8
+ """This is the configuration file.
9
+ """
10
+
11
+
12
+ ################################################################################
13
+ # Settings for Basic Operaters
14
+ ################################################################################
15
+
16
+ conf_basic_ops = dict()
17
+
18
+ # kernel_initializer for convolutions and transposed convolutions
19
+ # If None, the default initializer is the Glorot (Xavier) normal initializer.
20
+ conf_basic_ops['kernel_initializer'] = tf.glorot_uniform_initializer()
21
+
22
+ # momentum for batch normalization
23
+ conf_basic_ops['momentum'] = 0.997
24
+
25
+ # epsilon for batch normalization
26
+ conf_basic_ops['epsilon'] = 1e-5
27
+
28
+ # String options: 'relu', 'relu6'
29
+ conf_basic_ops['relu_type'] = 'relu'
30
+
31
+ ################################################################################
32
+ # Settings for Attention Modules
33
+ ################################################################################
34
+
35
+ # Set the attention in same_gto
36
+ conf_attn_same = dict()
37
+
38
+ # Define the relationship between total_key_filters and output_filters.
39
+ # total_key_filters = output_filters // key_ratio
40
+ conf_attn_same['key_ratio'] = 1
41
+
42
+ # Define the relationship between total_value_filters and output_filters.
43
+ # total_key_filters = output_filters // value_ratio
44
+ conf_attn_same['value_ratio'] = 1
45
+
46
+ # number of heads
47
+ conf_attn_same['num_heads'] = 2
48
+
49
+ # dropout rate, 0.0 means no dropout
50
+ conf_attn_same['dropout_rate'] = 0.0
51
+
52
+ # whether to use softmax on attention_weights
53
+ conf_attn_same['use_softmax'] = False
54
+
55
+ # whether to use bias terms in input/output transformations
56
+ conf_attn_same['use_bias'] = True
57
+
58
+ # Set the attention in up_gto
59
+ conf_attn_up = dict()
60
+
61
+ conf_attn_up['key_ratio'] = 1
62
+ conf_attn_up['value_ratio'] = 1
63
+ conf_attn_up['num_heads'] = 2
64
+ conf_attn_up['dropout_rate'] = 0
65
+ conf_attn_up['use_softmax'] = False
66
+ conf_attn_up['use_bias'] = True
67
+
68
+ # Set the attention in down_gto
69
+ conf_attn_down = dict()
70
+
71
+ conf_attn_down['key_ratio'] = 1
72
+ conf_attn_down['value_ratio'] = 1
73
+ conf_attn_down['num_heads'] = 2
74
+ conf_attn_down['dropout_rate'] = 0.0
75
+ conf_attn_down['use_softmax'] = False
76
+ conf_attn_down['use_bias'] = True
77
+
78
+ ################################################################################
79
+ # Describing the U-net
80
+ ################################################################################
81
+
82
+ conf_unet = dict()
83
+
84
+ """
85
+ Describe your U-Net under the following framework:
86
+
87
+ ********************************************************************************************
88
+ layers | output_filters
89
+ |
90
+ first_convolution + encoding_block_1 (same) | first_output_filters
91
+ + encoding_block_i, i = 2, 3, ..., depth. (down) | first_output_filters*(2**(i-1))
92
+ + bottom_block | first_output_filters*(2**(depth-1))
93
+ + decoding_block_j, j = depth-1, depth-2, ..., 1 (up) | first_output_filters*(2**(j-1))
94
+ + output_layer
95
+ ********************************************************************************************
96
+
97
+ Specifically,
98
+ encoding_block_1 (same) = one or more res_block
99
+ encoding_block_i (down) = downsampling + zero or more res_block, i = 2, 3, ..., depth-1
100
+ encoding_block_depth (down) = downsampling
101
+ bottom_block = a combination of same_gto and res_block
102
+ decoding_block_j (up) = upsampling + zero or more res_block, j = depth-1, depth-2, ..., 1
103
+
104
+ Identity skip connections are between the output of encoding_block_i and
105
+ the output of upsampling in decoding_block_i, i = 1, 2, ..., depth-1.
106
+ The combination method could be 'add' or 'concat'.
107
+ """
108
+
109
+ # Set U-Net depth.
110
+ conf_unet['depth'] = 3
111
+
112
+ # Set the output_filters for first_convolution and encoding_block_1 (same).
113
+ conf_unet['first_output_filters'] = 96
114
+
115
+ # Set the encoding block sizes, i.e., number of res_block in encoding_block_i, i = 1, 2, ..., depth.
116
+ # It is an integer list whose length equals to depth.
117
+ # The first entry should be positive since encoding_block_1 = one or more res_block.
118
+ # The last entry should be zero since encoding_block_depth (down) = downsampling.
119
+ conf_unet['encoding_block_sizes'] = [1, 1, 0]
120
+
121
+ # Set the decoding block sizes, i.e., number of res_block in decoding_block_j, j = depth-1, depth-2, ..., 1.
122
+ # It is an integer list whose length equals to depth-1.
123
+ conf_unet['decoding_block_sizes'] = [1, 1]
124
+
125
+ # Set the downsampling methods for each encoding_block_i, i = 2, 3, ..., depth.
126
+ # It is an string list whose length equals to depth-1.
127
+ # String options: 'down_gto_v1', 'down_gto_v2', 'down_res_block', 'convolution'
128
+ conf_unet['downsampling'] = ['convolution', 'convolution']
129
+
130
+ # Set the combination method for identity skip connections
131
+ # String options: 'add', 'concat'
132
+ conf_unet['skip_method'] = 'concat'
133
+
134
+ # Set the output layer
135
+
136
+
137
+ # Check
138
+ assert conf_unet['depth'] == len(conf_unet['encoding_block_sizes'])
139
+ assert conf_unet['encoding_block_sizes'][0] > 0
140
+ assert conf_unet['encoding_block_sizes'][-1] == 0
141
+ assert conf_unet['depth'] == len(conf_unet['decoding_block_sizes']) + 1
142
+ assert conf_unet['depth'] == len(conf_unet['downsampling']) + 1
143
+ assert conf_unet['skip_method'] in ['add', 'concat']
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ tensorflow==1.15
2
+ scipy
3
+ scikit-image
4
+ tifffile
5
+ gdown
6
+ opencv-python
7
+ numpy
resnet_module.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging, os
2
+ logging.disable(logging.WARNING)
3
+ os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
4
+
5
+ import tensorflow as tf
6
+ from basic_ops import *
7
+
8
+
9
+ """This script defines non-attention same-, up-, down- modules.
10
+ Note that pre-activation is used for residual-like blocks.
11
+ Note that the residual block could be used for downsampling.
12
+ """
13
+
14
+
15
+ def res_block(inputs, output_filters, training, dimension, name):
16
+ """Standard residual block with pre-activation.
17
+
18
+ Args:
19
+ inputs: a Tensor with shape [batch, (d,) h, w, channels]
20
+ output_filters: an integer
21
+ training: a boolean for batch normalization and dropout
22
+ dimension: a string, dimension of inputs/outputs -- 2D, 3D
23
+ name: a string
24
+
25
+ Returns:
26
+ A Tensor of shape [batch, (_d,) _h, _w, output_filters]
27
+ """
28
+ if dimension == '2D':
29
+ convolution = convolution_2D
30
+ kernel_size = 3
31
+ elif dimension == '3D':
32
+ convolution = convolution_3D
33
+ kernel_size = 3
34
+ else:
35
+ raise ValueError("Dimension (%s) must be 2D or 3D." % (dimension))
36
+
37
+ with tf.variable_scope(name):
38
+ if inputs.shape[-1] == output_filters:
39
+ shortcut = inputs
40
+ inputs = batch_norm(inputs, training, 'batch_norm_1')
41
+ inputs = relu(inputs, 'relu_1')
42
+ else:
43
+ inputs = batch_norm(inputs, training, 'batch_norm_1')
44
+ inputs = relu(inputs, 'relu_1')
45
+ shortcut = convolution(inputs, output_filters, 1, 1, False, 'projection_shortcut')
46
+ inputs = convolution(inputs, output_filters, kernel_size, 1, False, 'convolution_1')
47
+ inputs = batch_norm(inputs, training, 'batch_norm_2')
48
+ inputs = relu(inputs, 'relu_2')
49
+ inputs = convolution(inputs, output_filters, kernel_size, 1, False, 'convolution_2')
50
+ return tf.add(shortcut, inputs)
51
+
52
+
53
+ def down_res_block(inputs, output_filters, training, dimension, name):
54
+ """Standard residual block with pre-activation for downsampling."""
55
+ if dimension == '2D':
56
+ convolution = convolution_2D
57
+ projection_shortcut = convolution_2D
58
+ elif dimension == '3D':
59
+ convolution = convolution_3D
60
+ projection_shortcut = convolution_3D
61
+ else:
62
+ raise ValueError("Dimension (%s) must be 2D or 3D." % (dimension))
63
+
64
+ with tf.variable_scope(name):
65
+ # The projection_shortcut should come after the first batch norm and ReLU.
66
+ inputs = batch_norm(inputs, training, 'batch_norm_1')
67
+ inputs = relu(inputs, 'relu_1')
68
+ shortcut = projection_shortcut(inputs, output_filters, 1, 2, False, 'projection_shortcut')
69
+ inputs = convolution(inputs, output_filters, 2, 2, False, 'convolution_1')
70
+ inputs = batch_norm(inputs, training, 'batch_norm_2')
71
+ inputs = relu(inputs, 'relu_2')
72
+ inputs = convolution(inputs, output_filters, 3, 1, False, 'convolution_2')
73
+ return tf.add(shortcut, inputs)
74
+
75
+ def down_convolution(inputs, output_filters, training, dimension, name):
76
+ """Use a single stride 2 convolution for downsampling."""
77
+ if dimension == '2D':
78
+ convolution = convolution_2D
79
+ pool = tf.layers.max_pooling2d
80
+ elif dimension == '3D':
81
+ convolution = convolution_3D
82
+ pool = tf.layers.max_pooling3d
83
+ else:
84
+ raise ValueError("Dimension (%s) must be 2D or 3D." % (dimension))
85
+
86
+ with tf.variable_scope(name):
87
+ inputs = convolution(inputs, output_filters, 2, 2, True, 'convolution')
88
+ return inputs
89
+
90
+ def up_transposed_convolution(inputs, output_filters, training, dimension, name):
91
+ """Use a single stride 2 transposed convolution for upsampling."""
92
+ if dimension == '2D':
93
+ transposed_convolution = transposed_convolution_2D
94
+ elif dimension == '3D':
95
+ transposed_convolution = transposed_convolution_3D
96
+ else:
97
+ raise ValueError("Dimension (%s) must be 2D or 3D." % (dimension))
98
+
99
+ with tf.variable_scope(name):
100
+ inputs = transposed_convolution(inputs, output_filters, 2, 2, True, 'transposed_convolution')
101
+ return inputs
trained_models/README.md ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Checkpoints for the trained model
2
+ -----
3
+ Due to the limitation of repo size, we upload the model files to Google Drive. You can manually download them [here](https://drive.google.com/drive/folders/1VYMo1OoaGxoOLNx6-qIt2Wg03lsZw_kA?usp=sharing).
utils/evaluation_utils.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from scipy.misc import ascent
3
+ from skimage.measure import compare_psnr, compare_mse, compare_ssim
4
+ from .predict_utils import normalize_mi_ma
5
+
6
+ def normalize(x, pmin=2, pmax=99.8, axis=None, clip=False, eps=1e-20, dtype=np.float32):
7
+ """Percentile-based image normalization."""
8
+
9
+ mi = np.percentile(x,pmin,axis=axis,keepdims=True)
10
+ ma = np.percentile(x,pmax,axis=axis,keepdims=True)
11
+ return normalize_mi_ma(x, mi, ma, clip=clip, eps=eps, dtype=dtype)
12
+
13
+ def norm_minmse(gt, x, normalize_gt=True):
14
+ """
15
+ normalizes and affinely scales an image pair such that the MSE is minimized
16
+
17
+ Parameters
18
+ ----------
19
+ gt: ndarray
20
+ the ground truth image
21
+ x: ndarray
22
+ the image that will be affinely scaled
23
+ normalize_gt: bool
24
+ set to True of gt image should be normalized (default)
25
+ Returns
26
+ -------
27
+ gt_scaled, x_scaled
28
+ """
29
+ if normalize_gt:
30
+ gt = normalize(gt, 0.1, 99.9, clip=False).astype(np.float32, copy = False)
31
+ x = x.astype(np.float32, copy=False) - np.mean(x)
32
+ gt = gt.astype(np.float32, copy=False) - np.mean(gt)
33
+ scale = np.cov(x.flatten(), gt.flatten())[0, 1] / np.var(x.flatten())
34
+ return gt, scale * x
35
+
36
+
37
+ def get_scores(gt, x, multichan=False):
38
+
39
+ gt_, x_ = norm_minmse(gt, x)
40
+
41
+ mse = compare_mse(gt_, x_)
42
+ psnr = compare_psnr(gt_, x_, data_range = 1.)
43
+ ssim = compare_ssim(gt_, x_, data_range = 1., multichannel=multichan)
44
+
45
+ return np.sqrt(mse), psnr, ssim
46
+
47
+ if __name__ == '__main__':
48
+
49
+ # ground truth image
50
+ y = ascent().astype(np.float32)
51
+ # input image to compare to
52
+ x1 = y + 30*np.random.normal(0,1,y.shape)
53
+ # a scaled and shifted version of x1
54
+ x2 = 2*x1+100
55
+
56
+ # calulate mse, psnr, and ssim of the normalized/scaled images
57
+ mse1 = compare_mse(*norm_minmse(y, x1))
58
+ mse2 = compare_mse(*norm_minmse(y, x2))
59
+ # should be the same
60
+ print("MSE1 = %.6f\nMSE2 = %.6f"%(mse1, mse2))
61
+
62
+ psnr1 = compare_psnr(*norm_minmse(y, x1), data_range = 1.)
63
+ psnr2 = compare_psnr(*norm_minmse(y, x2), data_range = 1.)
64
+ # should be the same
65
+ print("PSNR1 = %.6f\nPSNR2 = %.6f"%(psnr1,psnr2))
66
+
67
+ ssim1 = compare_ssim(*norm_minmse(y, x1), data_range = 1.)
68
+ ssim2 = compare_ssim(*norm_minmse(y, x2), data_range = 1.)
69
+ # should be the same
70
+ print("SSIM1 = %.6f\nSSIM2 = %.6f"%(ssim1,ssim2))
utils/predict_utils.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import print_function, unicode_literals, absolute_import, division
2
+ from six.moves import range, zip, map, reduce, filter
3
+
4
+ import collections
5
+ import warnings
6
+ import numpy as np
7
+
8
+
9
+ def get_coord(shape, size, margin):
10
+ n_tiles_i = int(np.ceil((shape[2]-size)/float(size-2*margin)))
11
+ n_tiles_j = int(np.ceil((shape[1]-size)/float(size-2*margin)))
12
+ for i in range(n_tiles_i+1):
13
+ src_start_i = i*(size-2*margin) if i<n_tiles_i else (shape[2]-size)
14
+ src_end_i = src_start_i+size
15
+ left_i = margin if i>0 else 0
16
+ right_i = margin if i<n_tiles_i else 0
17
+ for j in range(n_tiles_j+1):
18
+ src_start_j = j*(size-2*margin) if j<n_tiles_j else (shape[1]-size)
19
+ src_end_j = src_start_j+size
20
+ left_j = margin if j>0 else 0
21
+ right_j = margin if j<n_tiles_j else 0
22
+ src_s = (slice(None, None),
23
+ slice(src_start_j, src_end_j),
24
+ slice(src_start_i, src_end_i))
25
+
26
+ trg_s = (slice(None, None),
27
+ slice(src_start_j+left_j, src_end_j-right_j),
28
+ slice(src_start_i+left_i, src_end_i-right_i))
29
+
30
+ mrg_s = (slice(None, None),
31
+ slice(left_j, -right_j if right_j else None),
32
+ slice(left_i, -right_i if right_i else None))
33
+
34
+ yield src_s, trg_s, mrg_s
35
+
36
+
37
+ # Below implementation of prediction utils inherited from CARE: https://github.com/CSBDeep/CSBDeep
38
+ # Content-Aware Image Restoration: Pushing the Limits of Fluorescence Microscopy. Martin Weigert, Uwe Schmidt, Tobias Boothe, Andreas Müller, Alexandr Dibrov, Akanksha Jain, Benjamin Wilhelm, Deborah Schmidt, Coleman Broaddus, Siân Culley, Mauricio Rocha-Martins, Fabián Segovia-Miranda, Caren Norden, Ricardo Henriques, Marino Zerial, Michele Solimena, Jochen Rink, Pavel Tomancak, Loic Royer, Florian Jug, and Eugene W. Myers. Nature Methods 15.12 (2018): 1090–1097.
39
+
40
+ def _raise(e):
41
+ raise e
42
+
43
+ def consume(iterator):
44
+ collections.deque(iterator, maxlen=0)
45
+
46
+ def axes_check_and_normalize(axes,length=None,disallowed=None,return_allowed=False):
47
+ """
48
+ S(ample), T(ime), C(hannel), Z, Y, X
49
+ """
50
+ allowed = 'STCZYX'
51
+ axes is not None or _raise(ValueError('axis cannot be None.'))
52
+ axes = str(axes).upper()
53
+ consume(a in allowed or _raise(ValueError("invalid axis '%s', must be one of %s."%(a,list(allowed)))) for a in axes)
54
+ disallowed is None or consume(a not in disallowed or _raise(ValueError("disallowed axis '%s'."%a)) for a in axes)
55
+ consume(axes.count(a)==1 or _raise(ValueError("axis '%s' occurs more than once."%a)) for a in axes)
56
+ length is None or len(axes)==length or _raise(ValueError('axes (%s) must be of length %d.' % (axes,length)))
57
+ return (axes,allowed) if return_allowed else axes
58
+
59
+
60
+ def axes_dict(axes):
61
+ """
62
+ from axes string to dict
63
+ """
64
+ axes, allowed = axes_check_and_normalize(axes,return_allowed=True)
65
+ return { a: None if axes.find(a) == -1 else axes.find(a) for a in allowed }
66
+
67
+
68
+ def normalize_mi_ma(x, mi, ma, clip=False, eps=1e-20, dtype=np.float32):
69
+ if dtype is not None:
70
+ x = x.astype(dtype,copy=False)
71
+ mi = dtype(mi) if np.isscalar(mi) else mi.astype(dtype,copy=False)
72
+ ma = dtype(ma) if np.isscalar(ma) else ma.astype(dtype,copy=False)
73
+ eps = dtype(eps)
74
+ try:
75
+ import numexpr
76
+ x = numexpr.evaluate("(x - mi) / ( ma - mi + eps )")
77
+ except ImportError:
78
+ x = (x - mi) / ( ma - mi + eps )
79
+ if clip:
80
+ x = np.clip(x,0,1)
81
+ return x
82
+
83
+ class PercentileNormalizer(object):
84
+
85
+ def __init__(self, pmin=2, pmax=99.8, do_after=True, dtype=np.float32, **kwargs):
86
+
87
+ (np.isscalar(pmin) and np.isscalar(pmax) and 0 <= pmin < pmax <= 100) or _raise(ValueError())
88
+ self.pmin = pmin
89
+ self.pmax = pmax
90
+ self._do_after = do_after
91
+ self.dtype = dtype
92
+ self.kwargs = kwargs
93
+
94
+ def before(self, img, axes):
95
+
96
+ len(axes) == img.ndim or _raise(ValueError())
97
+ channel = axes_dict(axes)['C']
98
+ axes = None if channel is None else tuple((d for d in range(img.ndim) if d != channel))
99
+ self.mi = np.percentile(img,self.pmin,axis=axes,keepdims=True).astype(self.dtype,copy=False)
100
+ self.ma = np.percentile(img,self.pmax,axis=axes,keepdims=True).astype(self.dtype,copy=False)
101
+ return normalize_mi_ma(img, self.mi, self.ma, dtype=self.dtype, **self.kwargs)
102
+
103
+ def after(self, img):
104
+
105
+ self.do_after or _raise(ValueError())
106
+ alpha = self.ma - self.mi
107
+ beta = self.mi
108
+ return ( alpha*img+beta ).astype(self.dtype,copy=False)
109
+
110
+ def do_after(self):
111
+
112
+ return self._do_after
113
+
114
+
115
+ class PadAndCropResizer(object):
116
+
117
+ def __init__(self, mode='reflect', **kwargs):
118
+
119
+ self.mode = mode
120
+ self.kwargs = kwargs
121
+
122
+ def _normalize_exclude(self, exclude, n_dim):
123
+ """Return normalized list of excluded axes."""
124
+ if exclude is None:
125
+ return []
126
+ exclude_list = [exclude] if np.isscalar(exclude) else list(exclude)
127
+ exclude_list = [d%n_dim for d in exclude_list]
128
+ len(exclude_list) == len(np.unique(exclude_list)) or _raise(ValueError())
129
+ all(( isinstance(d,int) and 0<=d<n_dim for d in exclude_list )) or _raise(ValueError())
130
+ return exclude_list
131
+
132
+ def before(self, x, div_n, exclude):
133
+
134
+ def _split(v):
135
+ a = v // 2
136
+ return a, v-a
137
+ exclude = self._normalize_exclude(exclude, x.ndim)
138
+ self.pad = [_split((div_n-s%div_n)%div_n) if (i not in exclude) else (0,0) for i,s in enumerate(x.shape)]
139
+ x_pad = np.pad(x, self.pad, mode=self.mode, **self.kwargs)
140
+ for i in exclude:
141
+ del self.pad[i]
142
+ return x_pad
143
+
144
+ def after(self, x, exclude):
145
+
146
+ pads = self.pad[:len(x.shape)]
147
+ crop = [slice(p[0], -p[1] if p[1]>0 else None) for p in self.pad]
148
+ for i in self._normalize_exclude(exclude, x.ndim):
149
+ crop.insert(i,slice(None))
150
+ len(crop) == x.ndim or _raise(ValueError())
151
+ return x[tuple(crop)]
152
+
utils/train_utils.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from tqdm import tqdm
3
+
4
+
5
+ def augment_patch(patch):
6
+ if len(patch.shape[:-1]) == 2:
7
+ patch = np.rot90(patch, k=np.random.randint(4), axes=(0, 1))
8
+ elif len(patch.shape[:-1]) == 3:
9
+ patch = np.rot90(patch, k=np.random.randint(4), axes=(1, 2))
10
+
11
+ patch = np.flip(patch, axis=-2) if np.random.randint(2) else patch
12
+ return patch
13
+
14
+
15
+ # Below implementation of stratified sampling inherited from Noise2Void: https://github.com/juglab/n2v
16
+ # Noise2void: learning denoising from single noisy images. Krull, Alexander, Tim-Oliver Buchholz, and Florian Jug. Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition. 2019.
17
+
18
+ def get_stratified_coords2D(coord_gen, box_size, shape):
19
+ box_count_y = int(np.ceil(shape[0] / box_size))
20
+ box_count_x = int(np.ceil(shape[1] / box_size))
21
+ x_coords = []
22
+ y_coords = []
23
+ for i in range(box_count_y):
24
+ for j in range(box_count_x):
25
+ y, x = next(coord_gen)
26
+ y = int(i * box_size + y)
27
+ x = int(j * box_size + x)
28
+ if (y < shape[0] and x < shape[1]):
29
+ y_coords.append(y)
30
+ x_coords.append(x)
31
+ return (y_coords, x_coords)
32
+
33
+
34
+ def get_stratified_coords3D(coord_gen, box_size, shape):
35
+ box_count_z = int(np.ceil(shape[0] / box_size))
36
+ box_count_y = int(np.ceil(shape[1] / box_size))
37
+ box_count_x = int(np.ceil(shape[2] / box_size))
38
+ x_coords = []
39
+ y_coords = []
40
+ z_coords = []
41
+ for i in range(box_count_z):
42
+ for j in range(box_count_y):
43
+ for k in range(box_count_x):
44
+ z, y, x = next(coord_gen)
45
+ z = int(i * box_size + z)
46
+ y = int(j * box_size + y)
47
+ x = int(k * box_size + x)
48
+ if (z < shape[0] and y < shape[1] and x < shape[2]):
49
+ z_coords.append(z)
50
+ y_coords.append(y)
51
+ x_coords.append(x)
52
+ return (z_coords, y_coords, x_coords)
53
+
54
+ def rand_float_coords2D(boxsize):
55
+ while True:
56
+ yield (np.random.rand() * boxsize, np.random.rand() * boxsize)
57
+
58
+ def rand_float_coords3D(boxsize):
59
+ while True:
60
+ yield (np.random.rand() * boxsize, np.random.rand() * boxsize, np.random.rand() * boxsize)