This repository was archived by the owner on Jan 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy path07_convnet_mnist.py
180 lines (141 loc) · 7.13 KB
/
07_convnet_mnist.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
""" Using convolutional net on MNIST dataset of handwritten digit
(http://yann.lecun.com/exdb/mnist/)
Author: Chip Huyen
Prepared for the class CS 20SI: "TensorFlow for Deep Learning Research"
cs20si.stanford.edu
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import time
import tensorflow as tf
import tf.contrib.layers as layers
from tensorflow.examples.tutorials.mnist import input_data
import utils
N_CLASSES = 10
# Step 1: Read in data
# using TF Learn's built in function to load MNIST data to the folder data/mnist
mnist = input_data.read_data_sets("/data/mnist", one_hot=True)
# Step 2: Define paramaters for the model
LEARNING_RATE = 0.001
BATCH_SIZE = 128
SKIP_STEP = 10
DROPOUT = 0.75
N_EPOCHS = 1
# Step 3: create placeholders for features and labels
# each image in the MNIST data is of shape 28*28 = 784
# therefore, each image is represented with a 1x784 tensor
# We'll be doing dropout for hidden layer so we'll need a placeholder
# for the dropout probability too
# Use None for shape so we can change the batch_size once we've built the graph
with tf.name_scope('data'):
X = tf.placeholder(tf.float32, [None, 784], name="X_placeholder")
Y = tf.placeholder(tf.float32, [None, 10], name="Y_placeholder")
dropout = tf.placeholder(tf.float32, name='dropout')
# Step 4 + 5: create weights + do inference
# the model is conv -> relu -> pool -> conv -> relu -> pool -> fully connected -> softmax
global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step')
with tf.variable_scope('conv1') as scope:
# first, reshape the image to [BATCH_SIZE, 28, 28, 1] to make it work with tf.nn.conv2d
images = tf.reshape(X, shape=[-1, 28, 28, 1])
kernel = tf.get_variable('kernel', [5, 5, 1, 32],
initializer=tf.truncated_normal_initializer())
biases = tf.get_variable('biases', [32],
initializer=tf.random_normal_initializer())
conv = tf.nn.conv2d(images, kernel, strides=[1, 1, 1, 1], padding='SAME')
conv1 = tf.nn.relu(conv + biases, name=scope.name)
# output is of dimension BATCH_SIZE x 28 x 28 x 32
# conv1 = layers.conv2d(images, 32, 5, 1, activation_fn=tf.nn.relu, padding='SAME')
with tf.variable_scope('pool1') as scope:
pool1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
padding='SAME')
# output is of dimension BATCH_SIZE x 14 x 14 x 32
with tf.variable_scope('conv2') as scope:
# similar to conv1, except kernel now is of the size 5 x 5 x 32 x 64
kernel = tf.get_variable('kernels', [5, 5, 32, 64],
initializer=tf.truncated_normal_initializer())
biases = tf.get_variable('biases', [64],
initializer=tf.random_normal_initializer())
conv = tf.nn.conv2d(pool1, kernel, strides=[1, 1, 1, 1], padding='SAME')
conv2 = tf.nn.relu(conv + biases, name=scope.name)
# output is of dimension BATCH_SIZE x 14 x 14 x 64
# layers.conv2d(images, 64, 5, 1, activation_fn=tf.nn.relu, padding='SAME')
with tf.variable_scope('pool2') as scope:
# similar to pool1
pool2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
padding='SAME')
# output is of dimension BATCH_SIZE x 7 x 7 x 64
with tf.variable_scope('fc') as scope:
# use weight of dimension 7 * 7 * 64 x 1024
input_features = 7 * 7 * 64
w = tf.get_variable('weights', [input_features, 1024],
initializer=tf.truncated_normal_initializer())
b = tf.get_variable('biases', [1024],
initializer=tf.constant_initializer(0.0))
# reshape pool2 to 2 dimensional
pool2 = tf.reshape(pool2, [-1, input_features])
fc = tf.nn.relu(tf.matmul(pool2, w) + b, name='relu')
# pool2 = layers.flatten(pool2)
# fc = layers.fully_connected(pool2, 1024, tf.nn.relu)
fc = tf.nn.dropout(fc, dropout, name='relu_dropout')
with tf.variable_scope('softmax_linear') as scope:
w = tf.get_variable('weights', [1024, N_CLASSES],
initializer=tf.truncated_normal_initializer())
b = tf.get_variable('biases', [N_CLASSES],
initializer=tf.random_normal_initializer())
logits = tf.matmul(fc, w) + b
# Step 6: define loss function
# use softmax cross entropy with logits as the loss function
# compute mean cross entropy, softmax is applied internally
with tf.name_scope('loss'):
entropy = tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=logits)
loss = tf.reduce_mean(entropy, name='loss')
with tf.name_scope('summaries'):
tf.summary.scalar('loss', loss)
tf.summary.histogram('histogram loss', loss)
summary_op = tf.summary.merge_all()
# Step 7: define training op
# using gradient descent with learning rate of LEARNING_RATE to minimize cost
optimizer = tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss,
global_step=global_step)
utils.make_dir('checkpoints')
utils.make_dir('checkpoints/convnet_mnist')
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
# to visualize using TensorBoard
writer = tf.summary.FileWriter('./graphs/convnet', sess.graph)
ckpt = tf.train.get_checkpoint_state(os.path.dirname('checkpoints/convnet_mnist/checkpoint'))
# if that checkpoint exists, restore from checkpoint
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
initial_step = global_step.eval()
start_time = time.time()
n_batches = int(mnist.train.num_examples / BATCH_SIZE)
total_loss = 0.0
for index in range(initial_step, n_batches * N_EPOCHS): # train the model n_epochs times
X_batch, Y_batch = mnist.train.next_batch(BATCH_SIZE)
_, loss_batch, summary = sess.run([optimizer, loss, summary_op],
feed_dict={X: X_batch, Y:Y_batch, dropout: DROPOUT})
writer.add_summary(summary, global_step=index)
total_loss += loss_batch
if (index + 1) % SKIP_STEP == 0:
print('Average loss at step {}: {:5.1f}'.format(index + 1, total_loss / SKIP_STEP))
total_loss = 0.0
saver.save(sess, 'checkpoints/convnet_mnist/mnist-convnet', index)
print("Optimization Finished!") # should be around 0.35 after 25 epochs
print("Total time: {0} seconds".format(time.time() - start_time))
# test the model
n_batches = int(mnist.test.num_examples/BATCH_SIZE)
total_correct_preds = 0
for i in range(n_batches):
X_batch, Y_batch = mnist.test.next_batch(BATCH_SIZE)
_, loss_batch, logits_batch = sess.run([optimizer, loss, logits],
feed_dict={X: X_batch, Y:Y_batch, dropout: 1.0})
preds = tf.nn.softmax(logits_batch)
correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(Y_batch, 1))
accuracy = tf.reduce_sum(tf.cast(correct_preds, tf.float32))
total_correct_preds += sess.run(accuracy)
print("Accuracy {0}".format(total_correct_preds/mnist.test.num_examples))