Skip to content

Commit 8326f5b

Browse files
committed
pylinting
1 parent 211e728 commit 8326f5b

File tree

8 files changed

+449
-395
lines changed

8 files changed

+449
-395
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
This file was deleted.

research/denoised_smoothing/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ Minimal implementation of [Denoised Smoothing: A Provable Defense for Pretrained
1313

1414
Randomized Smoothing is a well-tested method to provably defend against _l2_ adversarial attacks under a specific radii. But it assumes that a classifier performs well under Gaussian noisy perturbations which may not always be the case.
1515

16-
**Note**: I utilized many scripts from the [official repository](https://github.com/microsoft/denoised-smoothing) of Denoised Smoothing to develop this repository. My aim with this repository is to provide a template for researchers to conduct certification tests with Keras/TensorFlow models. I encourage the readers to check out the original repository, it's really well-developed.
16+
**Note**: Many scripts have been utilized from the [official repository](https://github.com/microsoft/denoised-smoothing) of Denoised Smoothing to develop this.
1717

1818
## Further notes
1919

2020
* The Denoised Smoothing process is demonstrated on the CIFAR-10 dataset.
21-
* You can train a classifier quickly with the [`Train_Classifier.ipynb`](https://colab.research.google.com/github/sayakpaul/Denoised-Smoothing-TF/blob/main/Train_Classifier.ipynb) notebook.
22-
* Training the denoiser is demonstrated in the [`Train_Denoiser.ipynb`](https://colab.research.google.com/github/sayakpaul/Denoised-Smoothing-TF/blob/main/Train_Denoiser.ipynb) notebook.
23-
* Certification tests are in [`Certification_Test.ipynb`](https://colab.research.google.com/github/sayakpaul/Denoised-Smoothing-TF/blob/main/Certification_Test.ipynb) notebook.
21+
* You can train a classifier quickly with the [`Train_Classifier.ipynb`](https://colab.research.google.com/github/sayakpaul/neural-structured-learning/blob/master/research/denoised_smoothing/notebooks/Train_Classifier.ipynb) notebook.
22+
* Training of the denoiser is demonstrated in the [`Train_Denoiser.ipynb`](https://colab.research.google.com/github/sayakpaul/neural-structured-learning/blob/master/research/denoised_smoothing/notebooks/Train_Denoiser.ipynb) notebook.
23+
* Certification tests are in [`Certification_Test.ipynb`](https://colab.research.google.com/github/sayakpaul/neural-structured-learning/blob/master/research/denoised_smoothing/notebooks/Certification_Test.ipynb) notebook.
2424

2525
All the notebooks can be executed on Colab! You also have the option to train using the free TPUs.
2626

@@ -34,7 +34,7 @@ As we can see prepending a pre-trained denoiser is extremely helpful for our pur
3434

3535
## Models
3636

37-
The models are available inside [`models.tar.gz`](https://github.com/sayakpaul/Denoised-Smoothing-TF/blob/main/models.tar.gz) in the `SavedModel` format. In the interest of reproducibility, the initial model weights are also provided.
37+
The models are available inside [`models.tar.gz`](https://github.com/sayakpaul/Denoised-Smoothing-TF/blob/main/models.tar.gz) in `SavedModel` format. In the interest of reproducibility, the initial model weights are also provided.
3838

3939
## Acknowledgements
4040

-20.4 MB
Binary file not shown.
Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,73 @@
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+
"""
47
from tensorflow.keras import layers
58
import tensorflow as tf
69

710
WEIGHT_DECAY = 1e-4
811

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

Comments
 (0)