-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmnist.py
317 lines (277 loc) · 11.4 KB
/
dmnist.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
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from scipy.ndimage import interpolation
from scipy import misc
from cv2 import *
from load_data import load_mnist
from nn_utils import *
from spatialtransformer import *
import sys
def rotation(imgs):
# rotates individual images
img_np = imgs.get_value()
batch_size, img_size = img_np.shape
output = np.zeros((batch_size, 28, 28))
for i in range(batch_size):
val = np.random.uniform(low=90.0*-1, high=90.0)
output[i] = interpolation.rotate(img_np[i].reshape(28,28), val, reshape=False)
output = output.reshape(batch_size, img_size)
output_shared = theano.shared(np.asarray(output, dtype = theano.config.floatX), borrow = True)
return output_shared
def rts_combo(imgs):
# rotate, translate, and scale individual images
img_np = imgs.get_value()
batch_size, img_size = img_np.shape
output = np.zeros((batch_size, 28, 28))
for i in range(batch_size):
rotate = np.random.uniform(-45.0, 45.0)
scale = np.random.uniform(0.7, 1.2)
m = np.random.randint(-7,7, size = 2) # number of pixels to translate
M = getRotationMatrix2D((28/2,28/2),rotate,1)
dst = warpAffine(img_np[i].reshape(28,28), M, (28,28))
M2 = np.float32([[1,0,m[0]],[0,1,m[1]]])
output[i] = warpAffine(dst, M2, (28,28))
output = output.reshape(batch_size, img_size)
output_shared = theano.shared(np.asarray(output, dtype = theano.config.floatX), borrow = True)
return output_shared
def proj_transform(imgs):
img_np = imgs.get_value()
batch_size, img_size = img_np.shape
output = np.zeros((batch_size, 28, 28))
for i in range(batch_size):
scale = np.random.uniform(0.7, 1.0)
dst = resize(img_np[i].reshape(28, 28), None, fx = scale, fy = scale)
offset = 14 * scale
pst1 = np.float32([[14 - offset, 14 - offset], [14 + offset, 14 - offset], [14 - offset, 14 + offset], [14 + offset, 14 + offset]])
m = np.random.normal(0, 5, size = 4)
pst2 = np.float32([[14 - offset + m[0], 14 - offset + m[0]],
[14 + offset + m[1], 14 - offset + m[1]],
[14 - offset + m[2], 14 + offset + m[2]],
[14 + offset + m[3], 14 + offset + m[3]]])
M = getPerspectiveTransform(pst1,pst2)
output[i] = warpPerspective(dst,M,(28,28))
output = output.reshape(batch_size, img_size)
output_shared = theano.shared(np.asarray(output, dtype = theano.config.floatX), borrow = True)
return output_shared
def identity(img):
return img
def plot(img, dim=28):
# plots a single image
plt.imshow(np.reshape(img,(dim, dim)), cmap="gray")
plt.xticks([]); plt.yticks([])
plt.show()
def run(network_type='cnn', include_st=True, transformation = rotation, iterations=1.5e5, n_epochs=200,
batch_size=256, learning_rate=0.01, decay_param=0.1, activation=T.nnet.relu,
verbose=True, runallmode = False):
if runallmode == True:
num_modes = 2
else:
num_modes = 1
# load MNIST dataset from local directory
print('...loading the dataset')
dataset = load_mnist()
# partition into relevant datasets
train_set_x, train_set_y = dataset[0]
valid_set_x, valid_set_y = dataset[1]
test_set_x , test_set_y = dataset[2]
train_set_x
# Get transformed dataset
print('...Applying {0} to dataset'.format(transformation.__name__))
train_set_x = transformation(train_set_x)
valid_set_x = transformation(valid_set_x)
test_set_x = transformation(test_set_x)
# compute minibatches
n_train_batches = train_set_x.get_value(borrow=True).shape[0]
n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]
n_test_batches = test_set_x.get_value(borrow=True).shape[0]
n_train_batches //= batch_size
n_valid_batches //= batch_size
n_test_batches //= batch_size
# output dataset info
if verbose:
print('Current training data size is %i' %train_set_x.shape[0].eval())
print('Current validation data size is %i' %valid_set_x.shape[0].eval())
print('Current test data size is %i' %test_set_x.shape[0].eval())
print('...building the model')
rng = np.random.RandomState(23455)
x = T.matrix('x')
y = T.ivector('y')
index = T.lscalar()
############################
# FULLY CONNECTED NETWORK #
############################
fcn_params = {
'input_dim': (batch_size, 1, 28, 28),
'h_units': 128,
'h_layers': 2,
'theta_dim': 6,
'L1': 0.00,
'L2': 0.0001,
}
if network_type in ['fcn', 'FCN', 'fully-connected']:
for mode in range(num_modes):
print("...training fcn with include_st = {0}".format(include_st))
# check if spatial transformer should be included
if include_st:
st = STN_FCN(
input_dim = fcn_params['input_dim'],
input = x.reshape((batch_size, 1, 28, 28))
)
fcn_input = st.output
fcn_input = fcn_input.reshape((batch_size, 28*28))
else:
fcn_input = x
classifier = MultiLayerPerceptron(
rng=rng,
input=fcn_input,
n_in=28*28,
n_hidden=fcn_params['h_units'],
n_out=10,
n_hiddenLayers=fcn_params['h_layers'],
act_function=activation
)
# cost to minimize during training
cost = (classifier.negative_log_likelihood(y)
+ fcn_params['L1'] * classifier.L1
+ fcn_params['L2'] * classifier.L2_sqr
)
# testing
test_model = theano.function(
inputs=[index],
outputs=classifier.errors(y),
givens={
x: test_set_x[index * batch_size:(index + 1) * batch_size],
y: test_set_y[index * batch_size:(index + 1) * batch_size]
}
)
# validation
validate_model = theano.function(
inputs=[index],
outputs=classifier.errors(y),
givens={
x: valid_set_x[index * batch_size:(index + 1) * batch_size],
y: valid_set_y[index * batch_size:(index + 1) * batch_size]
}
)
if include_st:
classifier.params = classifier.params + st.params
# compute graident of cost with respect to parameters
gparams = [T.grad(cost, param) for param in classifier.params]
# specify how to update parameter
updates = [
(param, param - learning_rate * gparam)
for param, gparam in zip(classifier.params, gparams)
]
# training
train_model = theano.function(
inputs=[index],
outputs=cost,
updates=updates,
givens={
x: train_set_x[index * batch_size: (index + 1) * batch_size],
y: train_set_y[index * batch_size: (index + 1) * batch_size]
}
)
print('...training')
train_nn(train_model, validate_model, test_model,
n_train_batches, n_valid_batches, n_test_batches, n_epochs, verbose)
include_st = not include_st
print("----------------------------------------------------------------------\n\n")
############################
# CONVOLUTIONAL NETWORK #
############################
cnn_params = {
'input_dim': (batch_size, 1, 28, 28),
'filter': 32
}
if network_type in ['cnn', 'CNN', 'convolutional']:
for mode in range(num_modes):
print("...training cnn with include_st = {0}".format(include_st))
if include_st:
print "...Apply STN before CNN"
st = STN_CNN(
input_dim= cnn_params['input_dim'],
img=x.reshape((batch_size, 1, 28, 28)),
nconvs=[20,20],
downsampling=0.5,
scale=2
)
cnn_input = st.output
else:
cnn_input = x.reshape((batch_size, 1, 28, 28))
# first convolutional pooling layer
# filtering reduces to 20, maxpooling to 10
# 4D output tensor is thus of shape (batch_size, 32, 10, 10)
layer0 = ConvPoolLayer(
rng=rng,
input=cnn_input,
image_shape=(batch_size, 1, 28, 28),
filter_shape=(cnn_params['filter'], 1, 9, 9),
poolsize=(2,2)
)
# second convolutional pooling layer
# filter reduces to 4, maxpooling to 2
# 4D output tensor is thus of shape (batch_size, 32, 2, 2)
layer1 = ConvPoolLayer(
rng=rng,
input=layer0.output,
image_shape=(batch_size, cnn_params['filter'], 10, 10),
filter_shape=(cnn_params['filter'], cnn_params['filter'], 7, 7),
poolsize=(2,2)
)
# classification
layer2 = LogisticRegression(
input=layer1.output.flatten(2),
n_in=cnn_params['filter']*2*2,
n_out=10
)
# cost we minimize during training
cost = layer2.negative_log_likelihood(y)
# testing
test_model = theano.function(
[index],
layer2.errors(y),
givens={
x: test_set_x[index * batch_size: (index + 1) * batch_size],
y: test_set_y[index * batch_size: (index + 1) * batch_size]
}
)
# validation
validate_model = theano.function(
[index],
layer2.errors(y),
givens={
x: valid_set_x[index * batch_size: (index + 1) * batch_size],
y: valid_set_y[index * batch_size: (index + 1) * batch_size]
}
)
# list of model parameters to be fitted by gradient descent
params = (layer2.params + layer1.params + layer0.params)
if include_st:
params += st.params
# list of gradients for all model parameters
grads = T.grad(cost,params)
# specify how to update parameters
updates = [
(param_i, param_i - learning_rate * grad_i)
for param_i, grad_i in zip(params, grads)
]
# training
train_model = theano.function(
[index],
cost,
updates=updates,
givens={
x: train_set_x[index * batch_size: (index + 1) * batch_size],
y: train_set_y[index * batch_size: (index + 1) * batch_size]
}
)
print('...training')
train_nn(train_model, validate_model, test_model,
n_train_batches, n_valid_batches, n_test_batches, n_epochs, verbose)
include_st = not include_st
print("----------------------------------------------------------------------\n\n")
if __name__ == '__main__':
network = sys.argv[1]
run(network_type = network)