/
train_tfrecord.py
281 lines (226 loc) · 11.2 KB
/
train_tfrecord.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
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import math
import os
import sys
from datetime import datetime
start = datetime.now()
# basic setting
feature, time_step = 13, 299
train_filename = "data/train.tfrecords"
val_filename = "data/val.tfrecords"
learning_rate = 0.0001
keep_prob = 1.0
default_stddev = 0.046875
n_hidden_1 = 32
n_hidden_2 = 64
n_hidden_3 = 128
n_hidden_4 = 256
epoch = 200
batch_size = 8
ckpt_keep = 1
samples = sum(1 for _ in tf.python_io.tf_record_iterator(train_filename))
batch_size_val = sum(1 for _ in tf.python_io.tf_record_iterator(val_filename))
training_steps = int(np.ceil(samples/batch_size))
min_after_dequeue = 2
# make checkpoint folder
ckpt_folder = start.strftime("%Y-%m-%d")
try:
os.mkdir('ckpt/'+ckpt_folder)
except OSError:
pass
checkpoint_dir = 'ckpt/'+ckpt_folder
# Graph Creation
def deepnn(x):
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='VALID')
# def max_pool_2x2(x, name):
# return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID', name=name)
def weight_variable(shape, name):
initial = tf.truncated_normal(shape, stddev=default_stddev)
return tf.Variable(initial, name=name)
def bias_variable(shape, name):
# initial = tf.constant(0.0, shape=shape)
initial = tf.truncated_normal(shape, stddev=default_stddev)
return tf.Variable(initial, name=name)
# Reshape to use within a convolutional neural net.
# Input shape: [batch_size, n_steps, n_input, 1]
x_image = tf.reshape(x, [-1, time_step, feature, 1])
# First convolutional layer
W_conv1 = weight_variable([5, 5, 1, n_hidden_1], name='w1')
b_conv1 = bias_variable([n_hidden_1], name="b1")
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1, name='h1')
# Pooling layer - downsamples by 2X.
# h_pool1 = max_pool_2x2(h_conv1, name='hp1')
layer_1 = tf.nn.dropout(h_conv1, keep_prob, name='layer1')
# Second convolutional layer
W_conv2 = weight_variable([5, 5, n_hidden_1, n_hidden_2], name='w2')
b_conv2 = bias_variable([n_hidden_2], name="b2")
h_conv2 = tf.nn.relu(conv2d(layer_1, W_conv2) + b_conv2, name='h2')
# Second pooling layer.
# h_pool2 = max_pool_2x2(h_conv2, name='hp2')
layer_2 = tf.nn.dropout(h_conv2, keep_prob, name='layer2')
# Third convolutional layer
W_conv3 = weight_variable([5, 5, n_hidden_2, n_hidden_3], name='w3')
b_conv3 = bias_variable([n_hidden_3], name="b3")
h_conv3 = tf.nn.relu(conv2d(layer_2, W_conv3) + b_conv3, name='h3')
# Third pooling layer.
# h_pool3 = max_pool_2x2(h_conv3, name='hp3')
layer_3 = tf.nn.dropout(h_conv3, keep_prob, name='layer3')
# Fourth convolutional layer
W_conv4 = weight_variable([5, 5, n_hidden_3, n_hidden_4], name='w4')
b_conv4 = bias_variable([n_hidden_4], name="b4")
h_conv4 = tf.nn.relu(conv2d(layer_3, W_conv4) + b_conv4, name='h4')
# Fourth pooling layer.
# h_pool4 = max_pool_2x2(h_conv4, name='hp4')
layer_4 = tf.nn.dropout(h_conv4, keep_prob, name='layer4')
# Fifth convolutional layer
#W_conv5 = weight_variable([5, 5, n_hidden_4, n_hidden_5], name='w5')
#b_conv5 = bias_variable([n_hidden_5], name="b5")
#h_conv5 = tf.nn.relu(conv2d(layer_4, W_conv5) + b_conv5, name='h5')
# Fifth pooling layer.
#h_pool5 = max_pool_2x2(h_conv5, name='hp5')
#layer_5 = tf.nn.dropout(h_pool5, keep_prob, name='layer5')
last_shape = layer_4.get_shape().as_list()
print('\nlast shape: {}\n'.format(last_shape))
last_shape = last_shape[1] * last_shape[2] * last_shape[3]
# Fully connected layer 1
W_fc1 = weight_variable([last_shape, 1024], name='w_fc1')
b_fc1 = bias_variable([1024], name="b_fc1")
h_pool_flat = tf.reshape(layer_4, [-1, last_shape], name='hp_flat')
h_fc1 = tf.nn.relu(tf.matmul(h_pool_flat, W_fc1) + b_fc1, name='h_fc1')
# Dropout - controls the complexity of the model, prevents co-adaptation of features.
# keep_prob = tf.placeholder(tf.float32, name='keep_prob')
# h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# Map the 1024 features to classes, one for each digit
W_fc2 = weight_variable([1024, 2], name='w_fc2')
b_fc2 = bias_variable([2], name="b_fc2")
y_conv = tf.matmul(h_fc1, W_fc2) + b_fc2
return y_conv
def restore_tfrecord(filename, mode):
filename_queue = tf.train.string_input_producer([filename], num_epochs=None, seed=4567)
reader = tf.TFRecordReader()
print(reader.num_records_produced())
print(reader.num_work_units_completed())
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example,
features={
'source': tf.FixedLenFeature([feature*time_step], tf.float32),
'target': tf.FixedLenFeature([2], tf.float32),
})
src = features['source']
tar = features['target']
if mode == 'val':
bz = batch_size_val
else:
bz = batch_size
capacity = min_after_dequeue+3*batch_size
source, target = tf.train.shuffle_batch([src, tar],
batch_size=bz,
num_threads=3,
capacity=capacity,
min_after_dequeue=min_after_dequeue)
return source, target
def train():
a = datetime.now()
train_accuracies, val_accuracies, x_range, loss_epoch, loss_epoch_val = [], [], [], [], []
tf.reset_default_graph()
train_x, train_y = restore_tfrecord(train_filename, mode='train')
val_x, val_y = restore_tfrecord(val_filename, mode='val')
print('training data prepared, data processing duration: {}'.format(str(datetime.now()-start)))
x = tf.placeholder(tf.float32, [None, feature * time_step], name='input_x')
y_true = tf.placeholder(tf.float32, [None, 2], name='input_y')
keep_prob_tf = tf.placeholder(tf.float32, name='keep_prob')
y_predict = deepnn(x)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=y_predict, labels=y_true)
cost = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate,
beta1=0.9,
beta2=0.999,
epsilon=1e-4,
name='AdamOptimizer').minimize(cost)
y_model = tf.argmax(y_predict, 1, name='op_to_restore')
y_real = tf.argmax(y_true, 1)
correct_prediction = tf.equal(y_model, y_real)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name='Accuracy')
init_op = tf.global_variables_initializer()
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
sess.run(init_op)
saver = tf.train.Saver(max_to_keep=1)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
acc_step, loss_step = [], []
try:
# while not coord.should_stop():
for i in range(training_steps * epoch):
input_x, input_y = sess.run([train_x, train_y])
loss, _, acc_eval = sess.run([cost, optimizer, accuracy],
feed_dict={x: input_x, y_true: input_y, keep_prob_tf: keep_prob})
acc_step.append(acc_eval)
loss_step.append(loss)
# Print an overview fairly often.
if i % training_steps == 0:
loss_avg = sum(loss_step) / len(loss_step)
acc_avg = sum(acc_step) / len(acc_step)
input_x, input_y = sess.run([val_x, val_y])
loss_val, val_acc = sess.run([cost, accuracy],
feed_dict={x: input_x, y_true: input_y, keep_prob_tf: 1.0})
ep = int(i/training_steps)
save_path = saver.save(sess, checkpoint_dir + "/kws-" +
datetime.now().strftime("%Y%m%d-%H%M"),
global_step=ep+1)
print("=" * 50)
print("Epoch:", ep+1)
x_range.append(ep+1)
train_accuracies.append(acc_avg)
val_accuracies.append(val_acc)
loss_epoch.append(loss_avg)
loss_epoch_val.append(loss_val)
print("Training Accuracy = {:.3f} % Training Loss = {:.6f}".format(acc_avg*100, loss_avg) +
"\nValidation Accuracy = {:.3f} % Validation Loss = {:.6f}".format(val_acc*100, loss_val))
print('Checkpoint:', save_path)
# # simple early stop # using "from collections import deque"
es_train, es_val = train_accuracies[-5:], val_accuracies[-5:]
stop_point = sum([es_train[i] >= es_val[i] for i in range(len(es_train))])
if stop_point >= 4 and loss_avg < 0.005 and es_val[-1]*100 >= 90.0:
print('\nEarly Stop at epoch {}, loss_avg={:.4f}'.format(ep+1, loss_avg))
break
acc_step, loss_step = [], []
b = datetime.now()
training_time = b - a
print('training duration: {}\n'.format(str(training_time)))
# print number of parameters
total_parameters = 0
for variable in tf.trainable_variables():
# shape is an array of tf.Dimension
print(variable)
shape = variable.get_shape()
variable_parameters = 1
for dim in shape:
variable_parameters *= dim.value
total_parameters += variable_parameters
print('\nTotal parameters: {}\n'.format(total_parameters))
except tf.errors.OutOfRangeError:
print('Done reading')
finally:
coord.request_stop()
coord.request_stop()
coord.join(threads)
return train_accuracies, val_accuracies, x_range, loss_epoch, loss_epoch_val, save_path
if __name__ == '__main__':
train_accuracies, validation_accuracies, x_range, loss_epoch, loss_epoch_val, save_path = train()
plt.figure(1)
plt.subplot(211) # the first one of 2x1
plt.plot(x_range, train_accuracies, 'black', label='Training Accuracy')
plt.plot(x_range, validation_accuracies, '-r', label='Validation Accuracy')
plt.legend(loc='lower right', frameon=False)
plt.ylim(ymax=1.1)
plt.xlabel('Epoch')
plt.subplot(212) # the second one og 2x1
plt.plot(x_range, loss_epoch, 'black', label='Training Loss')
plt.plot(x_range, loss_epoch_val, '-g', label='Validation Loss')
plt.legend(loc='lower right', frameon=False)
plt.ylim(ymin=-0.1)
plt.savefig('graph/' + datetime.now().strftime("%Y-%m-%d-%H-%M") + '_tfrecord.png')
plt.show()