forked from wwoo/tf_box_classify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtf_convnet_test.py
More file actions
286 lines (226 loc) · 10.3 KB
/
tf_convnet_test.py
File metadata and controls
286 lines (226 loc) · 10.3 KB
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
'''
Another Convolutional Network Example using TensorFlow Library
Author: Win Woo
Based off Aymeric Damien's example at:
https://github.com/aymericdamien/TensorFlow-Examples/
Shows how to use TensorFlow input queues and image decoding to
train a simple convolutional neural network.
'''
import tensorflow as tf
import numpy as np
import os
import time
import math
# hyper parameters to use for training
TRAIN_BATCH_SIZE = 10
TRAIN_EPOCHS = 5
VALID_BATCH_SIZE = 40
VALID_EPOCHS = None
SHUFFLE_BATCHES = True
LEARNING_RATE = 0.01
NUM_CLASSES = 4
KEEP_PROB = 0.75
VALID_STEPS = 10
# image parameters
IMAGE_SIZE = 64
IMAGE_CHANNELS = 3
def get_image_label_list(image_label_file):
filenames = []
labels = []
for line in open(image_label_file, "r"):
filename, label = line[:-1].split(' ')
filenames.append(filename)
labels.append(int(label))
# == debug ==
print "get_image_label_list: read " + str(len(filenames)) \
+ " items"
# == /debug ==
return filenames, labels
def read_image_from_disk(input_queue):
label = input_queue[1]
file_contents = tf.read_file(input_queue[0])
rgb_image = tf.image.decode_jpeg(file_contents, channels=IMAGE_CHANNELS,
name="decode_jpeg")
rgb_image = tf.image.resize_images(rgb_image, IMAGE_SIZE, IMAGE_SIZE)
return rgb_image, label
def inputs(train_file, batch_size=TRAIN_BATCH_SIZE, num_epochs=TRAIN_EPOCHS):
image_list, label_list = get_image_label_list(train_file)
input_queue = tf.train.slice_input_producer([image_list, label_list],
num_epochs=num_epochs, shuffle=SHUFFLE_BATCHES)
image, label = read_image_from_disk(input_queue)
image = tf.reshape(image, [IMAGE_SIZE, IMAGE_SIZE, IMAGE_CHANNELS])
image_batch, label_batch = tf.train.batch([image, label],
batch_size=batch_size)
return image_batch, tf.one_hot(tf.to_int64(label_batch),
NUM_CLASSES, on_value=1.0, off_value=0.0)
def conv2d(x, W, b, strides=1):
# Conv2D wrapper, with bias and relu activation
x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1],
padding='SAME')
x = tf.nn.bias_add(x, b)
return tf.nn.relu(x)
def maxpool2d(x, k=2, layer=""):
# MaxPool2D wrapper
return tf.nn.max_pool(x, ksize=[1, k, k, 1],
strides=[1, k, k, 1], padding='SAME')
def conv_net(x, weights, biases, image_size, keep_prob=KEEP_PROB):
# Convolution and max pooling layers
# Each max pooling layer reduces dimensionality by 2
with tf.name_scope('layer1'):
# Convolution and max pooling layer 1
conv1 = conv2d(x, weights['wc1'], biases['bc1'])
conv1 = maxpool2d(conv1, k=2)
with tf.name_scope('layer2'):
# Convolution and max pooling layer 2
conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
conv2 = maxpool2d(conv2, k=2)
with tf.name_scope('layer3'):
# Convolution and max pooling layer 3
conv3 = conv2d(conv2, weights['wc3'], biases['bc3'])
conv3 = maxpool2d(conv3, k=2)
with tf.name_scope('layer4'):
# Convolution and max pooling layer 4
conv4 = conv2d(conv3, weights['wc4'], biases['bc4'])
conv4 = maxpool2d(conv4, k=2)
with tf.name_scope('fully_connected'):
# Fully-connected layer
fc1 = tf.reshape(conv4, [-1, weights['wd1'].get_shape().as_list()[0]])
fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
fc1 = tf.nn.relu(fc1)
# Apply dropout
fc1 = tf.nn.dropout(fc1, keep_prob)
with tf.name_scope('output'):
# Output, class prediction
out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
return out
def generate_image_summary(x, weights, biases, step, image_size=IMAGE_SIZE):
with tf.name_scope('generate_image_summary'):
x = tf.slice(x, [0, 0, 0, 0],
[VALID_BATCH_SIZE, image_size, image_size, 3])
x = tf.nn.conv2d(x, weights['wc1'], strides=[1, 1, 1, 1],
padding='SAME')
# Nifty grid image summary via:
# http://stackoverflow.com/questions/33802336/visualizing-output-of-convolutional-layer-in-tensorflow
x = tf.slice(x, [0, 0, 0, 0], [1, -1, -1, -1])
x = tf.reshape(x, [IMAGE_SIZE, IMAGE_SIZE, 32])
pad_xy = image_size + 4
x = tf.image.resize_image_with_crop_or_pad(x, pad_xy, pad_xy)
x = tf.reshape(x, [pad_xy, pad_xy, 4, 8])
x = tf.transpose(x, [2, 0, 3, 1])
x = tf.reshape(x, [1, pad_xy * 4, pad_xy * 8, 1])
conv_summary = tf.image_summary("img_conv_{:05d}".format(step), x)
relu_summary = tf.image_summary("img_relu_{:05d}".format(step), tf.nn.relu(x))
return conv_summary, relu_summary
def main(argv=None):
# Read inventory of training images and labels
with tf.name_scope('batch_inputs'):
train_file = "./train.txt"
valid_file = "./valid.txt"
image_size = IMAGE_SIZE
train_image_batch, train_label_batch = inputs(train_file,
batch_size=TRAIN_BATCH_SIZE, num_epochs=TRAIN_EPOCHS)
valid_image_batch, valid_label_batch = inputs(valid_file,
batch_size=VALID_BATCH_SIZE, num_epochs=VALID_EPOCHS)
# These are image and label batch placeholders which we'll feed in during training
x_ = tf.placeholder("float32", shape=[None, image_size, image_size,
IMAGE_CHANNELS])
y_ = tf.placeholder("float32", shape=[None, NUM_CLASSES])
# k is the image size after 4 convolution layers
k = int(math.ceil(IMAGE_SIZE / 2.0 / 2.0 / 2.0 / 2.0))
# Store weights for our convolution & fully-connected layers
with tf.name_scope('weights'):
weights = {
# 5x5 conv, 3 input channel, 32 outputs each
'wc1': tf.Variable(tf.random_normal([5, 5, 1 * IMAGE_CHANNELS, 32])),
# 5x5 conv, 32 inputs, 64 outputs
'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),
# 5x5 conv, 64 inputs, 128 outputs
'wc3': tf.Variable(tf.random_normal([5, 5, 64, 128])),
# 5x5 conv, 128 inputs, 256 outputs
'wc4': tf.Variable(tf.random_normal([5, 5, 128, 256])),
# fully connected, k * k * 256 inputs, 1024 outputs
'wd1': tf.Variable(tf.random_normal([k * k * 256, 1024])),
# 1024 inputs, 2 class labels (prediction)
'out': tf.Variable(tf.random_normal([1024, NUM_CLASSES]))
}
# Store biases for our convolution and fully-connected layers
with tf.name_scope('biases'):
biases = {
'bc1': tf.Variable(tf.random_normal([32])),
'bc2': tf.Variable(tf.random_normal([64])),
'bc3': tf.Variable(tf.random_normal([128])),
'bc4': tf.Variable(tf.random_normal([256])),
'bd1': tf.Variable(tf.random_normal([1024])),
'out': tf.Variable(tf.random_normal([NUM_CLASSES]))
}
# Define dropout rate to prevent overfitting
keep_prob = tf.placeholder(tf.float32)
# Build our graph
pred = conv_net(x_, weights, biases, image_size, keep_prob)
# Calculate loss
with tf.name_scope('cross_entropy'):
# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y_))
cost_summary = tf.scalar_summary("cost_summary", cost)
# Run optimizer step
with tf.name_scope('train'):
train_step = tf.train.AdamOptimizer(learning_rate=LEARNING_RATE).minimize(cost)
# Evaluate model accuracy
with tf.name_scope('predict'):
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
accuracy_summary = tf.scalar_summary("accuracy_summary", accuracy)
w_summary = tf.histogram_summary("weights", weights['wc1'])
b_summary = tf.histogram_summary("biases", biases['bc1'])
sess = tf.Session()
writer = tf.train.SummaryWriter("./logs", sess.graph)
init_op = tf.initialize_all_variables()
# we need init_local_op step only on tensorflow 0.10rc due to a regression from 0.9
# https://github.com/tensorflow/models/pull/297
init_local_op = tf.initialize_local_variables()
saver = tf.train.Saver()
step = 0
with sess.as_default():
sess.run(init_op)
sess.run(init_local_op) # we need this only with tensorflow 0.10rc
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
try:
while not coord.should_stop():
step += 1
x, y = sess.run([train_image_batch, train_label_batch])
train_step.run(feed_dict={keep_prob: 0.75,
x_: x, y_: y})
if step % VALID_STEPS == 0:
x, y = sess.run([valid_image_batch, valid_label_batch])
conv_summary, relu_summary = generate_image_summary(x_, weights, biases, step, image_size)
result = sess.run([cost_summary, accuracy_summary, accuracy, conv_summary, relu_summary, w_summary, b_summary],
feed_dict={keep_prob: 1.0, x_: x, y_: y})
cost_summary_str = result[0]
accuracy_summary_str = result[1]
acc = result[2]
conv_summary_str = result[3]
relu_summary_str = result[4]
w_summary_str = result[5]
b_summary_str = result[6]
# write summaries for viewing in Tensorboard
writer.add_summary(accuracy_summary_str, step)
writer.add_summary(cost_summary_str, step)
writer.add_summary(conv_summary_str, step)
writer.add_summary(relu_summary_str, step)
writer.add_summary(w_summary_str, step)
writer.add_summary(b_summary_str, step)
print("Accuracy at step %s: %s" % (step, acc))
save_path = saver.save(sess, "./model.ckpt")
except tf.errors.OutOfRangeError:
x, y = sess.run([valid_image_batch, valid_label_batch])
result = sess.run([accuracy], feed_dict={keep_prob: 1.0,
x_: x, y_: y})
print("Validation accuracy: %s" % result[0])
finally:
coord.request_stop()
coord.join(threads)
sess.close()
return 0
if __name__ == '__main__':
tf.app.run()