|
1 | | -# Referenced from: https://github.com/microsoft/denoised-smoothing/blob/master/code/archs/dncnn.py |
2 | | -# Original: https://github.com/cszn/DnCNN/blob/master/TrainingCodes/dncnn_keras/main_train.py |
3 | | - |
| 1 | +""" |
| 2 | +# Referenced from: |
| 3 | +https://github.com/microsoft/denoised-smoothing/blob/master/code/archs/dncnn.py |
| 4 | +# Original: |
| 5 | +https://github.com/cszn/DnCNN/blob/master/TrainingCodes/dncnn_keras/main_train.py |
| 6 | +""" |
4 | 7 | from tensorflow.keras import layers |
5 | 8 | import tensorflow as tf |
6 | 9 |
|
7 | 10 | WEIGHT_DECAY = 1e-4 |
8 | 11 |
|
9 | | -def conv_block(x, channels=64, ksize=3, padding="same", bn=False): |
10 | | - x = layers.Conv2D( |
11 | | - channels, |
12 | | - kernel_size=ksize, |
13 | | - padding=padding, |
14 | | - use_bias=True if bn else False, |
15 | | - kernel_initializer="orthogonal" |
16 | | - )(x) |
17 | | - if bn: |
18 | | - x = layers.BatchNormalization(momentum=0.0, epsilon=0.0001)(x) |
19 | | - x = layers.Activation("relu")(x) |
20 | | - return x |
21 | | - |
22 | | - |
23 | | -def run_dncnn(x, image_chnls=3, depth=17, n_channels=64): |
24 | | - x = conv_block(x, channels=n_channels) |
25 | | - for _ in range(depth - 2): |
26 | | - x = conv_block(x, channels=n_channels, bn=True) |
27 | | - |
28 | | - outputs = layers.Conv2D( |
29 | | - image_chnls, |
30 | | - kernel_size=3, |
31 | | - padding="same", |
32 | | - use_bias=False, |
33 | | - kernel_initializer="orthogonal", |
34 | | - )(x) |
35 | | - return outputs |
36 | | - |
37 | | - |
38 | | -def get_dncnn(image_size=32, image_chnls=3, |
39 | | - depth=17, n_channels=64): |
40 | | - inputs = layers.Input((image_size, image_size, image_chnls)) |
41 | | - outputs = run_dncnn(inputs, depth=depth, n_channels=n_channels) |
42 | | - outputs = layers.Subtract()([inputs, outputs]) |
43 | | - return tf.keras.Model(inputs, outputs) |
| 12 | +def conv_block(x: tf.Tensor, channels: int = 64, ksize: int = 3, |
| 13 | + padding: str = "same", bn: bool = False) -> tf.Tensor: |
| 14 | + """Constructs a convolution block. |
| 15 | +
|
| 16 | + :param x: inputs [batch_size, h, w, nb_channels] |
| 17 | + :param channels: number of channels (int) |
| 18 | + :param ksize: kernel size (int or a tuple of ints) |
| 19 | + :param padding: padding type (str) |
| 20 | + :param bn: whether to use Batch Normalization |
| 21 | + :return: output of the convolution block |
| 22 | + """ |
| 23 | + x = layers.Conv2D( |
| 24 | + channels, |
| 25 | + kernel_size=ksize, |
| 26 | + padding=padding, |
| 27 | + use_bias=True if bn else False, |
| 28 | + kernel_initializer="orthogonal" |
| 29 | + )(x) |
| 30 | + if bn: |
| 31 | + x = layers.BatchNormalization(momentum=0.0, epsilon=0.0001)(x) |
| 32 | + x = layers.Activation("relu")(x) |
| 33 | + return x |
| 34 | + |
| 35 | + |
| 36 | +def run_dncnn(x: tf.Tensor, image_chnls: int = 3, depth: int = 17, |
| 37 | + n_channels: int = 64) -> tf.Tensor: |
| 38 | + """Runs a DNCNN block. |
| 39 | +
|
| 40 | + :param x: inputs [batch_size, h, w, nb_channels] |
| 41 | + :param image_chnls: number of channels in the output images (int) |
| 42 | + :param depth: depth of the network (int) |
| 43 | + :param n_channels: number of channels in the convolutional layers (int) |
| 44 | + :return: batch of images |
| 45 | + """ |
| 46 | + x = conv_block(x, channels=n_channels) |
| 47 | + for _ in range(depth - 2): |
| 48 | + x = conv_block(x, channels=n_channels, bn=True) |
| 49 | + |
| 50 | + outputs = layers.Conv2D( |
| 51 | + image_chnls, |
| 52 | + kernel_size=3, |
| 53 | + padding="same", |
| 54 | + use_bias=False, |
| 55 | + kernel_initializer="orthogonal" |
| 56 | + )(x) |
| 57 | + return outputs |
| 58 | + |
| 59 | + |
| 60 | +def get_dncnn(image_size: int = 32, image_chnls: int = 3, |
| 61 | + depth: int = 17, n_channels: int = 64) -> tf.keras.Model: |
| 62 | + """Constructs a DNCNN model. |
| 63 | +
|
| 64 | + :param image_size: size of the images (int) |
| 65 | + :param image_chnls: number of channels in the inputs images (int) |
| 66 | + :param depth: depth of the network (int) |
| 67 | + :param n_channels: number of channels in the convolutional layers (int) |
| 68 | + :return: DNCNN model |
| 69 | + """ |
| 70 | + inputs = layers.Input((image_size, image_size, image_chnls)) |
| 71 | + outputs = run_dncnn(inputs, depth=depth, n_channels=n_channels) |
| 72 | + outputs = layers.Subtract()([inputs, outputs]) |
| 73 | + return tf.keras.Model(inputs, outputs) |
0 commit comments