Skip to content

Commit 76a19a4

Browse files
Merge pull request #1 from anonymousICLR2019submitter/doc
Add full documentation
2 parents d52c744 + a4dc0f9 commit 76a19a4

File tree

111 files changed

+4820
-1284
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+4820
-1284
lines changed

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
### OSX ###
2+
*.DS_Store
3+
.AppleDouble
4+
.LSOverride
5+
6+
### Python ###
7+
# Byte-compiled / optimized / DLL files
8+
__pycache__/
9+
*.py[cod]
10+
*$py.class
11+
12+
# Sphinx documentation
13+
docs/_build/
14+
15+
# Jupyter Notebook
16+
.ipynb_checkpoints

deepobs/cifar10/cifar10_3c3d.py

Lines changed: 113 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,34 @@
2020

2121

2222
class set_up:
23+
"""Class providing the functionality for a vanilla CNN architecture on `CIFAR-10`.
24+
25+
It consists of three convolutional layers with ReLU activations, each followed by max-pooling, followed by two fully-connected layer with ReLU activations and a 10-unit output layer with softmax. The model uses cross-entroy loss. A weight decay is used on the weights (but not the biases) which defaults to ``0.002``. The weight matrices are initialized using the `Xavier-Initializer` and the biases are initialized to ``0``.
26+
27+
Basis data augmentation (random crop, left-right flip, lighting augmentation) is done on the training images.
28+
29+
A suggested training settings is for ``100`` epochs with a batch size of ``128``.
30+
31+
Args:
32+
batch_size (int): Batch size of the data points. Defaults to ``128``.
33+
weight_decay (float): Weight decay factor. In this model weight decay is applied to the weights, but not the biases. Defaults to ``0.002``.
34+
35+
Attributes:
36+
data_loading (deepobs.data_loading): Data loading class for `CIFAR-10`, :class:`.cifar10_input.data_loading`.
37+
losses (tf.Tensor): Tensor of size ``batch_size`` containing the individual losses per data point.
38+
accuracy (tf.Tensor): Tensor containing the accuracy of the model.
39+
train_init_op (tf.Operation): A TensorFlow operation to be performed before starting every training epoch.
40+
train_eval_init_op (tf.Operation): A TensorFlow operation to be performed before starting every training eval epoch.
41+
test_init_op (tf.Operation): A TensorFlow operation to be performed before starting every test evaluation phase.
42+
"""
2343
def __init__(self, batch_size=128, weight_decay=0.002):
44+
"""Initializes the problem set_up class.
45+
46+
Args:
47+
batch_size (int): Batch size of the data points. Defaults to ``128``.
48+
weight_decay (float): Weight decay factor. In this model weight decay is applied to the weights, but not the biases. Defaults to ``0.002``.
49+
50+
"""
2451
self.data_loading = cifar10_input.data_loading(batch_size=batch_size)
2552
self.losses, self.accuracy = self.set_up(weight_decay)
2653

@@ -30,50 +57,65 @@ def __init__(self, batch_size=128, weight_decay=0.002):
3057
self.test_init_op = tf.group([self.data_loading.test_init_op])
3158

3259
def get(self):
60+
"""Returns the losses and the accuray of the model.
61+
62+
Returns:
63+
tupel: Tupel consisting of the losses and the accuracy.
64+
65+
"""
3366
return self.losses, self.accuracy
3467

3568
def set_up(self, weight_decay):
69+
"""Sets up the test problem.
70+
71+
Args:
72+
weight_decay (float): Weight decay factor. In this model weight decay is applied to the weights, but not the biases.
73+
74+
Returns:
75+
tupel: Tupel consisting of the losses and the accuracy.
76+
77+
"""
3678
X, y, phase = self.data_loading.load()
3779
print "X", X.get_shape()
3880

3981
W_conv1 = self._conv_filter("W_conv1", [5, 5, 3, 64])
40-
b_conv1 = self._bias_variable("b_conv1", [64], init_val=0.0)
41-
h_conv1 = tf.nn.relu(self._conv2d(X, W_conv1, padding="VALID") + b_conv1)
82+
b_conv1 = self.bias_variable("b_conv1", [64], init_val=0.0)
83+
h_conv1 = tf.nn.relu(self.conv2d(X, W_conv1, padding="VALID") + b_conv1)
4284
print "h_conv1", h_conv1.get_shape()
4385

44-
h_pool1 = self._max_pool_3x3(h_conv1)
86+
h_pool1 = self.max_pool_3x3(h_conv1)
4587
print "h_pool1", h_pool1.get_shape()
4688

4789
W_conv2 = self._conv_filter("W_conv2", [3, 3, 64, 96])
48-
b_conv2 = self._bias_variable("b_conv2", [96], init_val=0.0)
49-
h_conv2 = tf.nn.relu(self._conv2d(h_pool1, W_conv2, padding="VALID") + b_conv2)
90+
b_conv2 = self.bias_variable("b_conv2", [96], init_val=0.0)
91+
h_conv2 = tf.nn.relu(self.conv2d(h_pool1, W_conv2, padding="VALID") + b_conv2)
5092
print "h_conv2", h_conv2.get_shape()
5193

52-
h_pool2 = self._max_pool_3x3(h_conv2)
94+
h_pool2 = self.max_pool_3x3(h_conv2)
5395
print "h_pool2", h_pool2.get_shape()
5496

5597
W_conv3 = self._conv_filter("W_conv3", [3, 3, 96, 128])
56-
b_conv3 = self._bias_variable("b_conv3", [128], init_val=0.0)
57-
h_conv3 = tf.nn.relu(self._conv2d(h_pool2, W_conv3, padding="SAME") + b_conv3)
98+
b_conv3 = self.bias_variable("b_conv3", [128], init_val=0.0)
99+
h_conv3 = tf.nn.relu(self.conv2d(h_pool2, W_conv3, padding="SAME") + b_conv3)
58100
print "h_conv3", h_conv3.get_shape()
59101

60-
h_pool3 = self._max_pool_3x3(h_conv3)
102+
h_pool3 = self.max_pool_3x3(h_conv3)
61103
print "h_pool3", h_pool3.get_shape()
62104

63105
dim = 1152 # Shape of h_pool3 is [batch_size, 3, 3, 128]
64106
h_pool3_flat = tf.reshape(h_pool3, tf.stack([-1, dim]))
65107
print "h_pool3_flat", h_pool3_flat.get_shape()
66108

67-
W_fc1 = self._weight_matrix("W_fc1", [dim, 512])
68-
b_fc1 = self._bias_variable("b_fc1", [512], init_val=0.0)
109+
W_fc1 = self.weight_matrix("W_fc1", [dim, 512])
110+
b_fc1 = self.bias_variable("b_fc1", [512], init_val=0.0)
69111
h_fc1 = tf.nn.relu(tf.matmul(h_pool3_flat, W_fc1) + b_fc1)
70112

71-
W_fc2 = self._weight_matrix("W_fc2", [512, 256])
72-
b_fc2 = self._bias_variable("b_fc2", [256], init_val=0.0)
113+
W_fc2 = self.weight_matrix("W_fc2", [512, 256])
114+
b_fc2 = self.bias_variable("b_fc2", [256], init_val=0.0)
73115
h_fc2 = tf.nn.relu(tf.matmul(h_fc1, W_fc2) + b_fc2)
74116

75-
W_fc3 = self._weight_matrix("W_fc3", [256, 10])
76-
b_fc3 = self._bias_variable("b_fc3", [10], init_val=0.0)
117+
W_fc3 = self.weight_matrix("W_fc3", [256, 10])
118+
b_fc3 = self.bias_variable("b_fc3", [10], init_val=0.0)
77119
linear_outputs = tf.matmul(h_fc2, W_fc3) + b_fc3
78120

79121
losses = tf.nn.softmax_cross_entropy_with_logits_v2(
@@ -91,20 +133,72 @@ def set_up(self, weight_decay):
91133

92134
return losses, accuracy
93135

94-
def _weight_matrix(self, name, shape):
136+
def weight_matrix(self, name, shape):
137+
"""Creates a weight matrix, initialized by the `Xavier-initializer`.
138+
139+
Args:
140+
name (str): Name of the weight variable.
141+
shape (list): Dimensionality of the weight variable.
142+
143+
Returns:
144+
tf.Variable: Weight variable.
145+
146+
"""
95147
init = tf.contrib.layers.xavier_initializer()
96148
return tf.get_variable(name, shape, initializer=init)
97149

98150
def _conv_filter(self, name, shape):
151+
"""Creates a convolutional filter matrix, initialized by the `Xavier-initializer`.
152+
153+
Args:
154+
name (str): Name of the filter variable.
155+
shape (list): Dimensionality of the filter variable.
156+
157+
Returns:
158+
tf.Variable: Filter variable.
159+
160+
"""
99161
init = tf.contrib.layers.xavier_initializer_conv2d()
100162
return tf.get_variable(name, shape, initializer=init)
101163

102-
def _bias_variable(self, name, shape, init_val):
164+
def bias_variable(self, name, shape, init_val):
165+
"""Creates a bias variable of given shape and initialized to a given value.
166+
167+
Args:
168+
name (str): Name of the bias variable.
169+
shape (list): Dimensionality of the bias variable.
170+
init_val (float): Initial value of the bias variable.
171+
172+
Returns:
173+
tf.Variable: Bias variable.
174+
175+
"""
103176
init = tf.constant_initializer(init_val)
104177
return tf.get_variable(name, shape, initializer=init)
105178

106-
def _conv2d(self, x, W, stride=1, padding="VALID"):
179+
def conv2d(self, x, W, stride=1, padding="VALID"):
180+
"""Creates a two dimensional convolutional layer on top of a given input.
181+
182+
Args:
183+
x (tf.Variable): Input to the layer.
184+
W (tf.Variable): Weight variable of the convolutional layer.
185+
stride (int): Stride of the convolution. Defaults to ``1``.
186+
padding (str): Padding of the convolutional layers. Can be ``SAME`` or ``VALID``. Defaults to ``VALID``.
187+
188+
Returns:
189+
tf.Variable: Output after the convolutional layer.
190+
191+
"""
107192
return tf.nn.conv2d(x, W, strides=[1, stride, stride, 1], padding=padding)
108193

109-
def _max_pool_3x3(self, x):
194+
def max_pool_3x3(self, x):
195+
"""Creates a ``3`` by ``3`` max pool layer on top of a given input.
196+
197+
Args:
198+
x (tf.Variable): Input to the layer.
199+
200+
Returns:
201+
tf.Variable: Output after the max pool layer.
202+
203+
"""
110204
return tf.nn.max_pool(x, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding="SAME")

deepobs/cifar10/cifar10_input.py

Lines changed: 89 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,37 @@
99

1010

1111
class data_loading:
12+
"""Class providing the data loading functionality for the CIFAR-10 data set.
13+
14+
Args:
15+
batch_size (int): Batch size of the input-output pairs. No default value is given.
16+
data_augmentation (bool): Switch to turn basic data augmentation on or off while training. Defaults to ``true``.
17+
18+
Attributes:
19+
batch_size (int): Batch size of the input-output pairs.
20+
data_augmentation (bool): Switch to turn basic data augmentation on or off while training.
21+
train_eval_size (int): Number of data points to evaluate during the `train eval` phase. Currently set to ``10000`` the size of the test set.
22+
D_train (tf.data.Dataset): The training data set.
23+
D_train_eval (tf.data.Dataset): The training evaluation data set. It is the same data as `D_train` but we go through it separately.
24+
D_test (tf.data.Dataset): The test data set.
25+
phase (tf.Variable): Variable to describe which phase we are currently in. Can be "train", "train_eval" or "test". The phase variable can determine the behaviour of the network, for example deactivate dropout during evaluation.
26+
iterator (tf.data.Iterator): A single iterator for all three data sets. We us the initialization operators (see below) to switch this iterator to the data sets.
27+
X (tf.Tensor): Tensor holding the CIFAR-10 images. It has dimension `batch_size` x ``32`` (image size) x ``32`` (image size) x ``3`` (rgb).
28+
y (tf.Tensor): Label of the CIFAR-10 images. It has dimension `batch_size` x ``10`` (number of classes).
29+
train_init_op (tf.Operation): A TensorFlow operation to be performed before starting every training epoch. It sets the `phase` variable to "train" and initializes the iterator to the training data set.
30+
train_eval_init_op (tf.Operation): A TensorFlow operation to be performed before starting every training eval phase. It sets the `phase` variable to "train_eval" and initializes the iterator to the training eval data set.
31+
test_init_op (tf.Operation): A TensorFlow operation to be performed before starting every test evaluation phase. It sets the `phase` variable to "test" and initializes the iterator to the test data set.
32+
33+
"""
34+
1235
def __init__(self, batch_size, data_augmentation=True):
36+
"""Initializes the data loading class.
37+
38+
Args:
39+
batch_size (int): Batch size of the input-output pairs. No default value is given.
40+
data_augmentation (bool): Switch to turn basic data augmentation on or off while training. Defaults to ``true``.
41+
42+
"""
1343
self.train_eval_size = 10000 # The size of the test set
1444
self.batch_size = batch_size
1545
self.data_augmentation = data_augmentation
@@ -33,15 +63,29 @@ def __init__(self, batch_size, data_augmentation=True):
3363
self.D_test), tf.assign(self.phase, "test")], name="test_init_op")
3464

3565
def load(self):
66+
"""Returns the data (`X` and `y` ) and the phase variable.
67+
68+
Returns:
69+
tupel: Tupel consisting of the data points (`X`), (`y`) and the phase variable (`phase`).
70+
71+
"""
3672
return self.X, self.y, self.phase
3773

3874
def train_dataset(self, batch_size, data_augmentation=True):
39-
"""Create a ``tf.data.Dataset`` for the CIFAR-10 training data."""
75+
"""Creates the training data set.
4076
77+
Args:
78+
batch_size (int): Batch size of the input-output pairs.
79+
data_augmentation (bool): Switch to turn basic data augmentation on or off while training. Defaults to ``true``.
80+
81+
Returns:
82+
tf.data.Dataset: The training data set.
83+
84+
"""
4185
pattern = os.path.join(dataset_utils.get_data_dir(),
4286
"cifar-10", "data_batch_*.bin")
4387
if data_augmentation:
44-
D = self._make_dataset(
88+
D = self.make_dataset(
4589
binaries_fname_pattern=pattern,
4690
batch_size=batch_size,
4791
crop_size=32,
@@ -56,7 +100,7 @@ def train_dataset(self, batch_size, data_augmentation=True):
56100
num_prefetched_batches=3,
57101
num_preprocessing_threads=8)
58102
else:
59-
D = self._make_dataset(
103+
D = self.make_dataset(
60104
binaries_fname_pattern=pattern,
61105
batch_size=batch_size, crop_size=32,
62106
per_image_standardization=True,
@@ -72,12 +116,20 @@ def train_dataset(self, batch_size, data_augmentation=True):
72116
return D
73117

74118
def train_eval_dataset(self, batch_size, data_augmentation=True):
75-
"""Create a ``tf.data.Dataset`` for the CIFAR-10 training evaluation data."""
119+
"""Creates the train eval data set.
120+
121+
Args:
122+
batch_size (int): Batch size of the input-output pairs.
123+
data_augmentation (bool): Switch to turn basic data augmentation on or off while evaluating the training data set. Defaults to ``true``.
124+
125+
Returns:
126+
tf.data.Dataset: The train eval data set.
76127
128+
"""
77129
pattern = os.path.join(dataset_utils.get_data_dir(),
78130
"cifar-10", "data_batch_*.bin")
79131
if data_augmentation:
80-
D = self._make_dataset(
132+
D = self.make_dataset(
81133
binaries_fname_pattern=pattern,
82134
batch_size=batch_size,
83135
crop_size=32,
@@ -93,7 +145,7 @@ def train_eval_dataset(self, batch_size, data_augmentation=True):
93145
num_preprocessing_threads=8,
94146
data_set_size=self.train_eval_size)
95147
else:
96-
D = self._make_dataset(
148+
D = self.make_dataset(
97149
binaries_fname_pattern=pattern,
98150
batch_size=batch_size,
99151
crop_size=32,
@@ -111,11 +163,18 @@ def train_eval_dataset(self, batch_size, data_augmentation=True):
111163
return D
112164

113165
def test_dataset(self, batch_size):
114-
"""Create a ``tf.data.Dataset`` for the CIFAR-10 test data."""
166+
"""Creates the test data set.
115167
168+
Args:
169+
batch_size (int): Batch size of the input-output pairs.
170+
171+
Returns:
172+
tf.data.Dataset: The test data set.
173+
174+
"""
116175
pattern = os.path.join(dataset_utils.get_data_dir(),
117176
"cifar-10", "test_batch.bin")
118-
return self._make_dataset(
177+
return self.make_dataset(
119178
binaries_fname_pattern=pattern,
120179
batch_size=batch_size,
121180
crop_size=32,
@@ -130,23 +189,29 @@ def test_dataset(self, batch_size):
130189
num_prefetched_batches=3,
131190
num_preprocessing_threads=4)
132191

133-
def _make_dataset(self,
134-
binaries_fname_pattern,
135-
batch_size,
136-
crop_size=32,
137-
per_image_standardization=True,
138-
random_crop=False,
139-
pad_before_random_crop=0,
140-
random_flip_left_right=False,
141-
lighting_augmentation=False,
142-
one_hot=True,
143-
shuffle=True,
144-
shuffle_buffer_size=10000,
145-
num_prefetched_batches=3,
146-
num_preprocessing_threads=8,
147-
data_set_size=-1):
148-
"""Produce CIFAR dataset."""
192+
def make_dataset(self, binaries_fname_pattern, batch_size, crop_size=32, per_image_standardization=True, random_crop=False, pad_before_random_crop=0, random_flip_left_right=False, lighting_augmentation=False, one_hot=True, shuffle=True, shuffle_buffer_size=10000, num_prefetched_batches=3, num_preprocessing_threads=8, data_set_size=-1):
193+
"""Creates a data set from a pattern of the images and label files.
194+
195+
Args:
196+
binaries_fname_pattern (str): Pattern of the ``,bin`` files containing the images and labels.
197+
batch_size (int): Batch size of the input-output pairs.
198+
crop_size (int): Crop size of each image. Defaults to ``32``.
199+
per_image_standardization (bool): Switch to standardize each image to have zero mean and unit norm. Defaults to ``True``.
200+
random_crop (bool): Switch if random crops should be used. Defaults to ``False``.
201+
pad_before_random_crop (int): Defines the added padding before a random crop is applied. Defaults to ``0``.
202+
random_flip_left_right (bool): Switch to randomly flip the images horizontally. Defaults to ``False``.
203+
lighting_augmentation (bool): Switch to use random brightness, saturation and contrast on each image. Defaults to ``False``.
204+
one_hot (bool): Switch to turn on or off one-hot encoding of the labels. Defaults to ``True``.
205+
shuffle (bool): Switch to turn on or off shuffling of the data set. Defaults to ``True``.
206+
shuffle_buffer_size (int): Size of the shuffle buffer. Defaults to ``10000`` the size of the `test` and `train eval` data set, meaning that they will be completely shuffled.
207+
num_prefetched_batches (int): Number of prefeteched batches, defaults to ``3``.
208+
num_preprocessing_threads (int): The number of elements to process in parallel while applying the image transformations. Defaults to ``8``.
209+
data_set_size (int): Size of the data set to extract from the images and label files. Defaults to ``-1`` meaning that the full data set is used.
210+
211+
Returns:
212+
tf.data.Dataset: Data set object created from the images and label files.
149213
214+
"""
150215
# Set number of bytes to read
151216
label_bytes = 1
152217
label_offset = 0

0 commit comments

Comments
 (0)