-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnn.py
executable file
·340 lines (281 loc) · 10.7 KB
/
cnn.py
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""
Instruction:
In this section, you are asked to train a CNN with different hyperparameters.
To start with training, you need to fill in the incomplete code. There are two
places that you need to complete:
a) Backward pass equations for a convolutional layer.
b) Weight update equations with momentum.
After correctly fill in the code, modify the hyperparameters in "main()".
You can then run this file with the command: "python cnn.py" in your terminal.
The program will automatically check your gradient implementation before start.
The program will print out the training progress, and it will display the
training curve by the end. You can optionally save the model by uncommenting
the lines in "main()". You can also optionally load a trained model by
uncommenting the lines before the training.
Important Notes:
1. The Conv2D function has already been implemented. To implement the backward
pass, you should only need to call the existing Conv2D function, following the
equations from part 2 of the assignment. An efficient solution should be within
10 lines of code.
2. The Conv2D function accepts an optional parameter "pad", by default it will
pad the input with the size of the filter, so that the output has the same
dimension as the input. For example,
-----------------------------------------------------------------------------
Variable | Shape
-----------------------------------------------------------------------------
x (Inputs) | [N, H, W, C]
w (Filters) | [I, J, C, K]
Conv2D(x, w) | [N, H+I, W+J, C] * [I, J, C, K] = [N, H, W, K]
-----------------------------------------------------------------------------
(N=number of examples, H=height, W=width, C=number of channels,
I=filter height, J=filter width, K=number of filters)
You can also pass in a tuple (P, Q) to specify the amount you want to pad on
height and width dimension.
For example,
-----------------------------------------------------------------------------
Variable | Shape
-----------------------------------------------------------------------------
x (Inputs) | [N, H, W, C]
w (Filters) | [I, J, C, K]
Conv2D(x, w, pad=(P,Q)) | [N, H+P, W+Q, C] * [I, J, C, K] = [N, H+P-I, W+Q-J, K]
-----------------------------------------------------------------------------
3. It is maybe helpful to use "np.transpose" function to transpose the
dimensions of a variable. For example:
-----------------------------------------------------------------------------
Variable | Shape
-----------------------------------------------------------------------------
x (Inputs) | [N, H, W, C]
np.transpose(x, [3,1,2,0]) | [C, H, W, N]
-----------------------------------------------------------------------------
"""
from __future__ import division
from __future__ import print_function
from util import LoadData, Load, Save, DisplayPlot
from conv2d import conv2d as Conv2D
from fully_conn import Affine, ReLU, AffineBackward, ReLUBackward, Softmax, CheckGrad, Train, Evaluate
import sys
import numpy as np
def InitCNN(num_channels, filter_size, num_filters_1, num_filters_2,
num_outputs):
"""Initializes CNN parameters.
Args:
num_channels: Number of input channels.
filter_size: Filter size.
num_filters_1: Number of filters for the first convolutional layer.
num_filters_2: Number of filters for the second convolutional layer.
num_outputs: Number of output units.
Returns:
model: Randomly initialized network weights.
"""
W1 = 0.1 * np.random.randn(filter_size, filter_size,
num_channels, num_filters_1)
W2 = 0.1 * np.random.randn(filter_size, filter_size,
num_filters_1, num_filters_2)
W3 = 0.01 * np.random.randn(num_filters_2 * 64, num_outputs)
b1 = np.zeros((num_filters_1))
b2 = np.zeros((num_filters_2))
b3 = np.zeros((num_outputs))
model = {
'W1': W1,
'W2': W2,
'W3': W3,
'b1': b1,
'b2': b2,
'b3': b3,
'dE_dW1': 0,
'dE_dW2': 0,
'dE_dW3': 0,
'dE_db1': 0,
'dE_db2': 0,
'dE_db3': 0,
}
return model
def MaxPool(x, ratio):
"""Computes non-overlapping max-pooling layer.
Args:
x: Input values.
ratio: Pooling ratio.
Returns:
y: Output values.
"""
xs = x.shape
print("XS",xs)
print([xs[0], int(xs[1] / ratio), ratio,int(xs[2] / ratio), ratio, xs[3]])
h = x.reshape([xs[0], int(xs[1] / ratio), ratio,
int(xs[2] / ratio), ratio, xs[3]])
y = np.max(np.max(h, axis=4), axis=2)
return y
def MaxPoolBackward(grad_y, x, y, ratio):
"""Computes gradients of the max-pooling layer.
Args:
grad_y: Gradients wrt. the inputs.
x: Input values.
y: Output values.
Returns:
grad_x: Gradients wrt. the inputs.
"""
dy = grad_y
xs = x.shape
ys = y.shape
h = x.reshape([xs[0], int(xs[1] / ratio), ratio,
int(xs[2] / ratio), ratio, xs[3]])
y_ = np.expand_dims(np.expand_dims(y, 2), 4)
dy_ = np.expand_dims(np.expand_dims(dy, 2), 4)
dy_ = np.tile(dy_, [1, 1, ratio, 1, ratio, 1])
dx = dy_ * (y_ == h).astype('float')
dx = dx.reshape([ys[0], ys[1] * ratio, ys[2] * ratio, ys[3]])
return dx
def Conv2DBackward(grad_y, x, y, w):
"""Computes gradients of the convolutional layer.
Args:
grad_y: Gradients wrt. the inputs.
x: Input values.
y: Output values.
Returns:
grad_x: Gradients wrt. the inputs.
grad_w: Gradients wrt. the weights.
"""
###########################
# Insert your code here.
# Check all shapes coming in, and outgoing shapes to conv2D
I = np.shape(w)[0]
J = np.shape(w)[1]
#Grad x
w_T = np.transpose(np.copy(w), [0,1,3,2])
w_T = w_T[::-1,::-1,:,:]
grad_x = Conv2D(grad_y, w_T, [I-1,J-1])
#Grad w
x_T = np.transpose(np.copy(x), [3,1,2,0])
grad_y_T_hwnk = np.transpose(np.copy(grad_y), [1,2,0,3])
grad_w = Conv2D(x_T, grad_y_T_hwnk, [I-1,J-1]) #8,16,16,16 c,i,j,k
grad_w = np.transpose(grad_w, [1,2,0,3])
return grad_x, grad_w
###########################
raise Exception('Not implemented')
def CNNForward(model, x):
"""Runs the forward pass.
Args:
model: Dictionary of all the weights.
x: Input to the network.
Returns:
var: Dictionary of all intermediate variables.
"""
x = x.reshape([-1, 48, 48, 1])
h1c = Conv2D(x, model['W1']) + model['b1']
h1r = ReLU(h1c)
h1p = MaxPool(h1r, 3)
h2c = Conv2D(h1p, model['W2']) + model['b2']
h2r = ReLU(h2c)
h2p = MaxPool(h2r, 2)
h2p_ = np.reshape(h2p, [x.shape[0], -1])
y = Affine(h2p_, model['W3'], model['b3'])
var = {
'x': x,
'h1c': h1c,
'h1r': h1r,
'h1p': h1p,
'h2c': h2c,
'h2r': h2r,
'h2p': h2p,
'h2p_': h2p_,
'y': y
}
return var
def CNNBackward(model, err, var):
"""Runs the backward pass.
Args:
model: Dictionary of all the weights.
err: Gradients to the output of the network.
var: Intermediate variables from the forward pass.
"""
###########################
# Insert your code here.
dE_dh2p_, dE_dW3, dE_db3 = AffineBackward(err, var['h2p_'], model['W3'])
dE_dh2p = np.reshape(dE_dh2p_, var['h2p'].shape)
dE_dh2r = MaxPoolBackward(dE_dh2p, var['h2r'], var['h2p'], 2)
dE_dh2c = ReLUBackward(dE_dh2r, var['h2c'], var['h2r'])
dE_dh1p, dE_dW2 = Conv2DBackward(
dE_dh2c, var['h1p'], var['h2c'], model['W2'])
dE_db2 = dE_dh2c.sum(axis=2).sum(axis=1).sum(axis=0)
dE_dh1r = MaxPoolBackward(dE_dh1p, var['h1r'], var['h1p'], 3)
dE_dh1c = ReLUBackward(dE_dh1r, var['h1c'], var['h1r'])
_, dE_dW1 = Conv2DBackward(dE_dh1c, var['x'], var['h1c'], model['W1'])
dE_db1 = dE_dh1c.sum(axis=2).sum(axis=1).sum(axis=0)
#Keep old derivs for momentum update
model['dE_dW1_prev'] = model['dE_dW1']
model['dE_dW2_prev'] = model['dE_dW2']
model['dE_dW3_prev'] = model['dE_dW3']
model['dE_db1_prev'] = model['dE_db1']
model['dE_db2_prev'] = model['dE_db2']
model['dE_db3_prev'] = model['dE_db3']
model['dE_dW1'] = dE_dW1
model['dE_dW2'] = dE_dW2
model['dE_dW3'] = dE_dW3
model['dE_db1'] = dE_db1
model['dE_db2'] = dE_db2
model['dE_db3'] = dE_db3
return
###########################
def CNNUpdate(model, eps, momentum):
"""Update NN weights.
Args:
model: Dictionary of all the weights.
eps: Learning rate.
momentum: Momentum.
"""
###########################
# Insert your code here.
# Update the weights.
model['W1'] = model['W1'] - (eps*model['dE_dW1']
+ momentum*model['dE_dW1_prev'])
model['W2'] = model['W2'] - (eps*model['dE_dW2']
+ momentum*model['dE_dW2_prev'])
model['W3'] = model['W3'] - (eps*model['dE_dW3']
+ momentum*model['dE_dW3_prev'])
model['b1'] = model['b1'] - (eps*model['dE_db1']
+ momentum*model['dE_db1_prev'])
model['b2'] = model['b2'] - (eps*model['dE_db2']
+ momentum*model['dE_db2_prev'])
model['b3'] = model['b3'] - (eps*model['dE_db3']
+ momentum*model['dE_db3_prev'])
return
###########################
def main():
"""Trains a CNN."""
#model_fname = 'cnn_model.npz'
#stats_fname = 'cnn_stats.npz'
# Hyper-parameters. Modify them if needed.
eps = 0.025
momentum = 0.0
num_epochs = 50
filter_size = 5
num_filters_1 = 8
num_filters_2 = 16
batch_size = 100
# Input-output dimensions.
num_channels = 1
num_outputs = 8
# Initialize model.
model = InitCNN(num_channels, filter_size, num_filters_1, num_filters_2,
num_outputs)
# Uncomment to reload trained model here.
# model = Load(model_fname)
# Check gradient implementation.
#print('Checking gradients...')
#x = np.random.rand(10, 48, 48, 1) * 0.1
#CheckGrad(model, CNNForward, CNNBackward, 'W3', x)
#CheckGrad(model, CNNForward, CNNBackward, 'b3', x)
#CheckGrad(model, CNNForward, CNNBackward, 'W2', x)
#CheckGrad(model, CNNForward, CNNBackward, 'b2', x)
#CheckGrad(model, CNNForward, CNNBackward, 'W1', x)
#CheckGrad(model, CNNForward, CNNBackward, 'b1', x)
# Train model.
stats = Train(model, CNNForward, CNNBackward, CNNUpdate, eps,
momentum, num_epochs, batch_size)
# Uncomment if you wish to save the model.
# Save(model_fname, model)
# Uncomment if you wish to save the training statistics.
# Save(stats_fname, stats)
#raw_input()
if __name__ == '__main__':
main()