Tensorflow Template

2019-02-19

As we all know, working with tensorflow framework usually consists of 2 steps:

  1. Build the computational graph.
  2. Execute the graph you built.

This blog provides a general tensorflow code template. Including following features:

  1. Use low-level tensorflow APIs.
  2. Use tensorflow’s Dataset.
  3. Encapsulate the model into a class.
  4. Put all model parameters together.
  5. Support tensorboard visulization: graph and loss histogram.
  6. Support save and restore params from checkpoints.

Reference: Stanford CS20 lecture.

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
#!/usr/bin/python
#-*- coding:utf-8 -*-
""""
File: cnn_mnist_class.py
Date: 2019-01-31 11:30
Author: Amy
Use tensorflow to implement cnn.
Dataset: minist (train/test)
"""
import os
import tensorflow as tf
import utils
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
class ConvNet(object):
def __init__(self):
self.batch_size = 4
self.learning_rate = 0.001
self.n_epochs = 5
self.n_train = 55000
self.n_valid = 5000
self.n_test = 10000
self.train_init, self.valid_init, self.test_init = None, None, None
self.x, self.y_true, self.y_pred = None, None, None
self.logits = None
self.n_correct = None
self.loss = None
self.opt = None
self.training = False
self.summary_loss, self.summary_acc = None, None
self.global_step = tf.get_variable("global_step", initializer=tf.constant(0), trainable=False)
""" Build the graph. """
@staticmethod
def conv_relu(inputs, n_filters, filter_size, stride, padding, scope_name):
""" Conv - Relu layer. """
with tf.variable_scope(scope_name, reuse=tf.AUTO_REUSE) as scope:
in_channels = inputs.shape[-1]
filter_wgts = tf.get_variable("filter_weight",
shape=[filter_size, filter_size, in_channels, n_filters],
initializer=tf.truncated_normal_initializer())
filter_biases = tf.get_variable("filter_biases",
shape=[n_filters],
initializer=tf.random_normal_initializer())
conv = tf.nn.conv2d(inputs,
filter_wgts,
strides=[1, stride, stride, 1],
padding=padding)
return tf.nn.relu(conv + filter_biases, name=scope.name)
@staticmethod
def max_pooling(inputs, pool_size, stride, padding="VALID", scope_name="pool"):
""" Max Pooling layer. """
with tf.variable_scope(scope_name, reuse=tf.AUTO_REUSE) as scope:
pool = tf.nn.max_pool(inputs,
ksize=[1, pool_size, pool_size, 1],
strides=[1, stride, stride, 1],
padding=padding)
return pool
@staticmethod
def fully_connected(inputs, out_dim, scope_name="fc"):
""" Fully connected layer.
inputs is a streched out 1-d Tensor.
"""
in_dim = inputs.shape[-1]
with tf.variable_scope(scope_name, reuse=tf.AUTO_REUSE) as scope:
wgts = tf.get_variable("fc_weight",
shape=[in_dim, out_dim],
initializer=tf.random_normal_initializer())
biases = tf.get_variable("fc_bais",
shape=[out_dim],
initializer=tf.zeros_initializer())
out = tf.matmul(inputs, wgts) + biases
return out
def _import_data(self):
""" Prepare data. """
with tf.name_scope("data_scope"):
# step 1.1: prepare data
mnist_folder = "data/mnist"
utils.download_mnist(mnist_folder)
train, valid, test = utils.read_mnist(mnist_folder, flatten=True)
# step 1.2: build dataset and iterators
train_data = tf.data.Dataset.from_tensor_slices(train)
train_data = train_data.shuffle(buffer_size=10000)
train_data = train_data.batch(self.batch_size)
valid_data = tf.data.Dataset.from_tensor_slices(valid)
valid_data = valid_data.batch(self.batch_size)
test_data = tf.data.Dataset.from_tensor_slices(test)
test_data = test_data.batch(self.batch_size)
iterator = tf.data.Iterator.from_structure(train_data.output_types, \
train_data.output_shapes)
self.train_init = iterator.make_initializer(train_data)
self.valid_init = iterator.make_initializer(valid_data)
self.test_init = iterator.make_initializer(test_data)
# step 2: create X, y
self.x, self.y_true = iterator.get_next()
self.x = tf.reshape(self.x, [-1, 28, 28, 1])
print("x shape: {}, y shape: {}".format(self.x.shape, self.y_true.shape))
def inference(self):
""" Model structure. """
# step 3, 4: build model
conv1 = self.conv_relu(inputs=self.x,
n_filters=32,
filter_size=5,
stride=1,
padding="SAME",
scope_name="conv1")
pool1 = self.max_pooling(inputs=conv1,
pool_size=2,
stride=2,
padding="VALID",
scope_name="pool1")
conv2 = self.conv_relu(inputs=pool1,
n_filters=64,
filter_size=5,
stride=1,
padding="SAME",
scope_name="conv2")
pool2 = self.max_pooling(inputs=conv2,
pool_size=2,
stride=2,
padding="VALID",
scope_name="pool2")
in_dim = pool2.shape[1] * pool2.shape[2] * pool2.shape[3]
pool2 = tf.reshape(pool2, [-1, in_dim])
fc1 = self.fully_connected(inputs=pool2,
out_dim=1024,
scope_name="fc1")
# use dropout in train stage
if self.training:
dropout = tf.nn.dropout(tf.nn.relu(fc1),
keep_prob=0.75,
name="relu_dropout")
else:
dropout = tf.nn.dropout(tf.nn.relu(fc1),
keep_prob=1.0,
name="relu_dropout")
self.logits = self.fully_connected(inputs=dropout,
out_dim=10,
scope_name="logits")
def _loss(self):
""" Define loss function. """
with tf.name_scope("loss_scope"):
entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=self.logits,
labels=self.y_true,
name="entropy")
self.loss = tf.reduce_mean(entropy, name="loss")
def _optimize(self):
""" Optimization. """
# step 6: optimizer
with tf.name_scope("optimizer_scope"):
self.opt = tf.train.AdamOptimizer(learning_rate=self.learning_rate) \
.minimize(self.loss, global_step=self.global_step)
def _eval(self):
""" Evaluation. """
# step 7: evaluate
with tf.name_scope("eval_scope"):
self.y_pred = tf.nn.softmax(self.logits)
correct_preds = tf.equal(tf.argmax(self.y_pred, axis=1), tf.argmax(self.y_true, axis=1))
self.n_correct = tf.reduce_sum(tf.cast(correct_preds, tf.float32))
def _summarize(self):
""" Add summary. """
with tf.name_scope("summary_scope"):
self.summary_loss = tf.summary.scalar("loss", self.loss)
self.summary_acc = tf.summary.scalar("acc", self.n_correct / self.batch_size)
def build_graph(self):
""" Build computational graph. """
self._import_data()
self.inference()
self._loss()
self._optimize()
self._eval()
self._summarize()
def eval_once(self, sess, init, n_eval):
""" Eval once. """
self.training = False
sess.run(init)
total_correct = 0
try:
while True:
n_c = sess.run(self.n_correct)
total_correct += n_c
except tf.errors.OutOfRangeError:
pass
return total_correct / (n_eval * 1.0)
def train(self, sess):
""" Training. """
saver = tf.train.Saver()
initial_step = 0
utils.safe_mkdir("checkpointrs")
ckpt = tf.train.get_checkpoint_state(os.path.dirname("checkpoints/checkpoint"))
if ckpt and ckpt.model_checkpoint_path:
print("Restore from checkpoints!")
saver.restore(sess, ckpt.model_checkpoint_path)
writer = tf.summary.FileWriter("./graphs/my_convnet/lr_{}batch_{}".\
format(self.learning_rate, self.batch_size), sess.graph)
initial_step = self.global_step.eval()
# step 8.1: train model
step = initial_step
for epo in range(1, self.n_epochs + 1):
self.training = True
sess.run(self.train_init)
total_loss = 0.
total_batch = 0
try:
while True:
_, loss_val, summary_loss = sess.run([self.opt, self.loss, self.summary_loss])
writer.add_summary(summary_loss, global_step=step)
total_loss += loss_val
total_batch += 1
if (step + 1) % 1000 == 0:
print("step [{}] loss: {:.4f}".format(step + 1, total_loss / (total_batch * 1.0)))
saver.save(sess, "checkpoints/my_convnet", step)
step += 1
except tf.errors.OutOfRangeError:
print("Data out of range!")
sess.run(self.train_init)
pass
summary_acc = sess.run(self.summary_acc)
writer.add_summary(summary_acc, global_step=step)
if epo % 1 == 0:
# step 8.2: validate model
acc = self.eval_once(sess, self.valid_init, self.n_valid)
print("epoch: {:2d} valid acc: {:.4f}".format(epo, acc))
writer.close()
def test(self, sess):
""" Testing. """
# step 9. test model
acc = self.eval_once(sess, self.test_init, self.n_test)
print("Test acc: {:.4f}".format(acc))
def main():
model = ConvNet()
# build graph
model.build_graph()
# run graph
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# train model
model.train(sess)
# test model
model.test(sess)
if __name__ == "__main__":
main()