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
| import gzip
import os
import tempfile
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.contrib.layers import flatten
from tensorflow.examples.tutorials.mnist import input_data
from sklearn.utils import shuffle
"""
Data Loading
"""
dataPath='../dataset/MNIST_data/'
mnist = input_data.read_data_sets(dataPath, one_hot=True)
# read the images and reformat the image shape from [img_num,img_height,img_width] to [img_num,img_height,img_width,1]
img_width = 28
img_height = 28
images = mnist.train.images
img_num, _ = images.shape
images = np.reshape(images,(img_num,img_height,img_width))
images = images[...,np.newaxis]
print('(Input to CNN) Images with shape {}'.format(images.shape))
# read the labels
labels1Hot = mnist.train.labels
print('(Input to CNN) labels1Hot.shape = {}'.format(labels1Hot.shape))
labels = np.argmax(labels1Hot,axis=1)
labels = labels[...,np.newaxis]
print('labels.shape = {}'.format(labels.shape))
n_classes = len(np.unique(labels))# read the labels
labels1Hot = mnist.train.labels
print('(Input to CNN) labels1Hot.shape = {}'.format(labels1Hot.shape))
labels = np.argmax(labels1Hot,axis=1)
labels = labels[...,np.newaxis]
print('labels.shape = {}'.format(labels.shape))
n_classes = len(np.unique(labels))
# load the validation set
images_valid = mnist.validation.images
img_num_valid = len(images_valid)
images_valid = np.reshape(images_valid,(img_num_valid,img_height,img_width))
images_valid = images_valid[...,np.newaxis]
labels1Hot_valid = mnist.validation.labels
print('Having %d number of validation images' % img_num_valid)
plt.figure(figsize=(15,5))
for i in np.arange(2*7):
random_idx = np.random.randint(0,img_num)
plt.subplot(2,7,i+1)
plt.imshow(images[random_idx][...,0],cmap='gray')
plt.title(labels[random_idx][0])
"""
First define the hyper-parameters
"""
# Hyper-parameters
EPOCHS = 30
BATCH_SIZE = 512
rate = 0.001
drop_out_keep_prob = 0.5
ksize = 5
cnn_depth_list = [16, 32]
mlp_depth_list = [256, 128]
cNum = 1
"""
Define the graph and construct it
"""
class MNISTCNN:
def __init__(self, ksize, cnn_depth_list, mlp_depth_list, img_height, img_width, cNum, n_classes):
self._ksize = ksize
self._cnn_depth_list = cnn_depth_list
self._mlp_depth_list = mlp_depth_list
self._img_height = img_height
self._img_width = img_width
self._cNum = cNum
self._n_classes = n_classes
self._mu = 0
self._sigma = 0.1
def create(self,x,keep_prob):
conv = self._conv(x, self._cNum, self._ksize, self._cnn_depth_list[0], 'conv1')
# Pooling. Input = 24x24xlayer_depth['layer_1']. Output = 12x12xlayer_depth['layer_1'].
conv = tf.nn.max_pool(conv, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
for lidx in range(1,len(self._cnn_depth_list)):
conv = self._conv(conv, self._cnn_depth_list[lidx-1], self._ksize, self._cnn_depth_list[lidx], 'conv{}'.format(lidx+1))
conv = tf.nn.max_pool(conv, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
fc = flatten(conv)
bsize, fc_in_dim = fc.shape
fc = self._fc(fc,fc_in_dim,self._mlp_depth_list[0],'fc1')
fc = tf.nn.dropout(fc, keep_prob) # dropout
for lidx in range(1,len(self._mlp_depth_list)):
fc = self._fc(fc,self._mlp_depth_list[lidx-1],self._mlp_depth_list[lidx],'fc{}'.format(lidx+1))
fc = tf.nn.dropout(fc, keep_prob) # dropout
with tf.variable_scope('logits') as scope:
with tf.device('/cpu:0'):
logits_w = tf.get_variable('logits_w', shape=[self._mlp_depth_list[-1],self._n_classes],\
initializer=tf.random_uniform_initializer(-0.1,0.1))
logits_b = tf.get_variable('logits_b', shape=[self._n_classes],\
initializer=tf.zeros_initializer)
logits = tf.nn.xw_plus_b(fc, logits_w, logits_b, name=scope.name)
print(logits.shape)
return logits
def _conv(self, x, in_depth, ksize, out_depth, scope_name, relu=True):
bsize,h,w,cNum = x.shape
assert(h-(ksize-1)>=1)
assert(w-(ksize-1)>=1)
with tf.variable_scope(scope_name) as scope:
with tf.device('/cpu:0'):
# Create tf variables for the weights and biases
weights = tf.get_variable('weights', shape=(ksize, ksize, in_depth, out_depth),\
initializer=tf.random_normal_initializer(self._mu,self._sigma))
biases = tf.get_variable('biases', shape=(out_depth),initializer=tf.zeros_initializer)
out = tf.nn.conv2d(x, weights, strides=[1, 1, 1, 1], padding='VALID',name=scope.name) + biases
if relu:
# Apply ReLu non linearity
relu = tf.nn.relu(out)
return relu
else:
return out
def _fc(self, x, num_in, num_out, scope_name, relu=True):
"""Create a fully connected layer."""
with tf.variable_scope(scope_name) as scope:
with tf.device('/cpu:0'):
# Create tf variables for the weights and biases
weights = tf.get_variable('weights', shape=[num_in, num_out],\
initializer=tf.random_uniform_initializer(-0.1,0.1))
biases = tf.get_variable('biases', [num_out],\
initializer=tf.zeros_initializer)
# Matrix multiply weights and inputs and add bias
out = tf.nn.xw_plus_b(x, weights, biases, name=scope.name)
if relu:
# Apply ReLu non linearity
relu = tf.nn.relu(out)
return relu
else:
return out
mnistCNN = MNISTCNN(ksize, cnn_depth_list, mlp_depth_list, img_height, img_width, cNum, n_classes)
# Averaging gradients for all tower models on GPU
def average_gradients(tower_grads):
"""Calculate the average gradient for each shared variable across all towers.
Note that this function provides a synchronization point across all towers.
Args:
tower_grads: List of lists of (gradient, variable) tuples. The outer list
is over individual gradients. The inner list is over the gradient
calculation for each tower.
Returns:
List of pairs of (gradient, variable) where the gradient has been averaged
across all towers.
"""
average_grads = []
for grad_and_vars in zip(*tower_grads):
# Note that each grad_and_vars looks like the following:
# ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN))
grads = []
for g, _ in grad_and_vars:
# Add 0 dimension to the gradients to represent the tower.
expanded_g = tf.expand_dims(g, 0)
# Append on a 'tower' dimension which we will average over below.
grads.append(expanded_g)
# Average over the 'tower' dimension.
grad = tf.concat(axis=0, values=grads)
grad = tf.reduce_mean(grad, 0)
# Keep in mind that the Variables are redundant because they are shared
# across towers. So .. we will just return the first tower's pointer to
# the Variable.
v = grad_and_vars[0][1]
grad_and_var = (grad, v)
average_grads.append(grad_and_var)
return average_grads
# Construct model for each GPU, where variables are shared/updated by CPU
with tf.device('/cpu:0'):
optimizer = tf.train.AdamOptimizer(learning_rate = rate)
# Calculate the gradients for each model tower.
tower_grads = []
logits_list = []
feed_x = []
feed_one_hot_y = []
keep_prob = tf.placeholder(tf.float32) # probability to keep units
with tf.variable_scope(tf.get_variable_scope()):
for i in range(2):
with tf.device('/gpu:%d' % i):
x = tf.placeholder(tf.float32, (None, img_height, img_width, cNum))
feed_x.append(x)
one_hot_y = tf.placeholder(tf.int32, (None, n_classes))
feed_one_hot_y.append(one_hot_y)
logits = mnistCNN.create(x,keep_prob)
logits_list.append(logits)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=one_hot_y,logits=logits)
loss_op = tf.reduce_mean(cross_entropy)
tf.get_variable_scope().reuse_variables()
# Calculate the gradients for each batch of data on this model tower.
grads = optimizer.compute_gradients(loss_op)
# Keep track of the gradients across all towers.
tower_grads.append(grads)
with tf.device('/cpu:0'):
# We must calculate the mean of each gradient. Note that this is the
# synchronization point across all towers.
grads = average_gradients(tower_grads)
# Apply the gradients to adjust the shared variables.
apply_gradient_op = optimizer.apply_gradients(grads)
training_op = apply_gradient_op
"""
Prediction/Inference Part
"""
# Define accuracy evaluation, calculate the average accuracy by calling evaluate(X_data, y_data)
# Using model that in the First GPU to calculate
correct_prediction = tf.equal(tf.argmax(logits_list[0], axis=1), tf.argmax(feed_one_hot_y[0], axis=1))
accuracy_operation = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
def evaluate(X_data, y_data):
num_examples = len(X_data)
total_accuracy = 0
sess = tf.get_default_session()
for offset in range(0, num_examples, BATCH_SIZE):
batch_x, batch_y = X_data[offset:offset+BATCH_SIZE], y_data[offset:offset+BATCH_SIZE]
accuracy = sess.run(accuracy_operation, feed_dict={feed_x[0]: batch_x, feed_one_hot_y[0]: batch_y, keep_prob: 1.0})
total_accuracy += (accuracy * len(batch_x))
return total_accuracy / num_examples
"""
Run Session
"""
# {feed_x[0]:batch_x_1, feed_x[1]:batch_x_2,\
# feed_one_hot_y[0]:batch_y_1, feed_one_hot_y[1]:batch_y_1, keep_prob:drop_out_keep_prob}
def gen_feed_dict(batch_x, batch_y, drop_out_keep_prob):
assert(len(batch_x)==len(batch_y))
assert(len(batch_x)%2==0)
data_num = int(len(batch_x)/2)
rtn_dict = {}
rtn_dict[feed_x[0]] = batch_x[:data_num]
rtn_dict[feed_x[1]] = batch_x[data_num:]
rtn_dict[feed_one_hot_y[0]] = batch_y[:data_num]
rtn_dict[feed_one_hot_y[1]] = batch_y[data_num:]
rtn_dict[keep_prob] = drop_out_keep_prob
return rtn_dict
### Train your model here.
import time
if not os.path.isdir('./models'):
os.makedirs('./models')
#saver = tf.train.Saver()
accumulate_time = 0.0
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
num_examples = img_num
print("Training...")
print()
train_accuracy = np.zeros(EPOCHS)
validation_accuracy = np.zeros(EPOCHS)
stime = time.time()
for i in range(EPOCHS):
stime = time.time()
acc_train_accuracy = 0
X_train, y_train = shuffle(images, labels1Hot)
for offset in range(0, num_examples, BATCH_SIZE):
end = offset + BATCH_SIZE
batch_x, batch_y = X_train[offset:end], y_train[offset:end]
feed_dict = gen_feed_dict(batch_x, batch_y, drop_out_keep_prob)
sess.run(training_op, feed_dict=feed_dict)
etime = time.time()
accumulate_time += etime - stime
validation_accuracy[i] = evaluate(images_valid, labels1Hot_valid)
print("EPOCH {} ...".format(i+1))
print("Validation Accuracy = {:.3f}".format(validation_accuracy[i]))
print('Cost time: ' + str(accumulate_time) + ' sec.')
|