-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
292 lines (226 loc) · 11.9 KB
/
main.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
281
282
283
284
285
286
287
288
289
290
291
292
import os.path
import tensorflow as tf
import helper
import warnings
from distutils.version import LooseVersion
import project_tests as tests
from tensorflow.python.platform import gfile
from tensorflow.core.protobuf import saved_model_pb2
from tensorflow.python.util import compat
LEARN_RATE = 9e-5
# Check TensorFlow Version
assert LooseVersion(tf.__version__) >= LooseVersion(
'1.0'), 'Please use TensorFlow version 1.0 or newer. You are using {}'.format(tf.__version__)
print('TensorFlow Version: {}'.format(tf.__version__))
# Check for a GPU
if not tf.test.gpu_device_name():
warnings.warn('No GPU found. Please use a GPU to train your neural network.')
else:
print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
def load_vgg(sess, vgg_path):
"""
Load Pretrained VGG Model into TensorFlow.
:param sess: TensorFlow Session
:param vgg_path: Path to vgg folder, containing "variables/" and "saved_model.pb"
:return: Tuple of Tensors from VGG model (image_input, keep_prob, layer3_out, layer4_out, layer7_out)
"""
# TODO: Implement function KK-DONE
# Use tf.saved_model.loader.load to load the model and weights
vgg_tag = 'vgg16'
vgg_input_tensor_name = 'image_input:0'
vgg_keep_prob_tensor_name = 'keep_prob:0'
vgg_layer3_out_tensor_name = 'layer3_out:0'
vgg_layer4_out_tensor_name = 'layer4_out:0'
vgg_layer7_out_tensor_name = 'layer7_out:0'
tf.saved_model.loader.load(sess, [vgg_tag], vgg_path)
graph = tf.get_default_graph()
w1 = graph.get_tensor_by_name(vgg_input_tensor_name)
keep = graph.get_tensor_by_name(vgg_keep_prob_tensor_name)
layer3 = graph.get_tensor_by_name(vgg_layer3_out_tensor_name)
layer4 = graph.get_tensor_by_name(vgg_layer4_out_tensor_name)
layer7 = graph.get_tensor_by_name(vgg_layer7_out_tensor_name)
return w1, keep, layer3, layer4, layer7
print("\n\nTesting load_vgg function......")
tests.test_load_vgg(load_vgg, tf)
def layers(vgg_layer3_out, vgg_layer4_out, vgg_layer7_out, num_classes):
"""
Create the layers for a fully convolutional network. Build skip-layers using the vgg layers.
:param vgg_layer7_out: TF Tensor for VGG Layer 3 output
:param vgg_layer4_out: TF Tensor for VGG Layer 4 output
:param vgg_layer3_out: TF Tensor for VGG Layer 7 output
:param num_classes: Number of classes to classify
:return: The Tensor for the last layer of output
"""
# TODO: Implement function KK-DONE
# KK Hyperparameters: Regularizer, Initializer, etc.
l2_value = 1e-3
kernel_reg = tf.contrib.layers.l2_regularizer(l2_value)
stddev = 1e-3
kernel_init = tf.random_normal_initializer(stddev=stddev)
# KK 1x1 convolution to preserve spatial information
conv_1x1_7 = tf.layers.conv2d(vgg_layer7_out, num_classes,
kernel_size=1,
strides=(1, 1),
padding='same',
kernel_regularizer=kernel_reg,
kernel_initializer=kernel_init)
# KK Print the shape of the 1x1
tf.Print(conv_1x1_7, [tf.shape(conv_1x1_7)[1:3]])
# KK Upsample by 2x so we can add it with layer4 in the skip layer to follow
conv7_2x = tf.layers.conv2d_transpose(conv_1x1_7, num_classes,
kernel_size=4,
strides=(2, 2),
padding='same',
kernel_regularizer=kernel_reg,
kernel_initializer=kernel_init)
# KK Print the shape of the upsample
print( '\n\nUpsampled layer 7 = ', tf.Print(conv7_2x, [tf.shape(conv7_2x)[1:3]]) )
# KK 1x1 convolution to preserve spatial information
conv_1x1_4 = tf.layers.conv2d(vgg_layer4_out, num_classes,
kernel_size=1,
strides=(1, 1),
padding='same',
kernel_regularizer=kernel_reg,
kernel_initializer=kernel_init)
# KK Add the layer4 with the upsampled 1x1 convolution as a skip layer
skip_4_to_7 = tf.add(conv7_2x, conv_1x1_4)
# KK Upsample the combined layer4 and 1x1 by 2x
upsample2x_skip_4_to_7 = tf.layers.conv2d_transpose(skip_4_to_7, num_classes,
kernel_size=4,
strides=(2, 2),
padding='same',
kernel_regularizer=kernel_reg,
kernel_initializer=kernel_init)
# KK Print the resulting shape
print( '\n\nUpsampled 4 and 7 = ', tf.Print(upsample2x_skip_4_to_7, [tf.shape(upsample2x_skip_4_to_7)[1:3]]))
# KK 1x1 convolution to preserve spatial information
conv_1x1_3 = tf.layers.conv2d(vgg_layer3_out, num_classes,
kernel_size=1,
strides=(1, 1),
padding='same',
kernel_regularizer=kernel_reg,
kernel_initializer=kernel_init)
# KK Add layer 3 with the upsampled skip1 layer
skip_3 = tf.add(upsample2x_skip_4_to_7, conv_1x1_3)
# KK Upsample by 8x to get to original image size
output = tf.layers.conv2d_transpose(skip_3, num_classes,
kernel_size=16,
strides=(8, 8),
padding='same',
kernel_regularizer=kernel_reg,
kernel_initializer=kernel_init)
# KK Print the resulting shape which should be the original image size
print('\n\nShape of output image = ', tf.Print(output, [tf.shape(output)[1:3]]))
return output
print("\n\nTesting layers function......")
tests.test_layers(layers)
def optimize(nn_last_layer, correct_label, learning_rate, num_classes):
"""
Build the TensorFLow loss and optimizer operations.
:param nn_last_layer: TF Tensor of the last layer in the neural network
:param correct_label: TF Placeholder for the correct label image
:param learning_rate: TF Placeholder for the learning rate
:param num_classes: Number of classes to classify
:return: Tuple of (logits, train_op, cross_entropy_loss)
"""
# TODO: Implement function KK-DONE
# KK Get the logits of the network
logits = tf.reshape(nn_last_layer, (-1, num_classes))
# KK Get the loss of the network
cross_entropy_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=correct_label))
#KK Regularization loss collector....Don't really understand this
reg_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
reg_constant = 0.01 # Choose an appropriate one.
loss = cross_entropy_loss + reg_constant * sum(reg_losses)
# KK Minimize the loss using Adam Optimizer
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-8)
train_op = optimizer.minimize(cross_entropy_loss)
return logits, train_op, loss
print("\n\nTesting optimize function......")
tests.test_optimize(optimize)
def train_nn(sess, epochs, batch_size, get_batches_fn, train_op, cross_entropy_loss, input_image,
correct_label, keep_prob, learning_rate):
"""
Train neural network and print out the loss during training.
:param sess: TF Session
:param epochs: Number of epochs
:param batch_size: Batch size
:param get_batches_fn: Function to get batches of training data. Call using get_batches_fn(batch_size)
:param train_op: TF Operation to train the neural network
:param cross_entropy_loss: TF Tensor for the amount of loss
:param input_image: TF Placeholder for input images
:param correct_label: TF Placeholder for label images
:param keep_prob: TF Placeholder for dropout keep probability
:param learning_rate: TF Placeholder for learning rate
"""
# TODO: Implement function KK-DONE
# KK loop through epochs
for epoch in range(epochs):
print('##############################################################')
print('........................Training Epoch # {}...................'.format(epoch))
print('##############################################################')
# KK loop through images and labels
for image, label in get_batches_fn(batch_size):
#DEBUG
print("\n\nTraining image shape = {}".format(tf.shape(image)))
print("\nTraining label shape = {}".format(tf.shape(label)))
# Training
_, loss = sess.run([train_op, cross_entropy_loss], feed_dict={input_image: image, correct_label: label, keep_prob: 0.5, learning_rate: LEARN_RATE})
print('\nTraining Loss = {:.3f}'.format(loss))
pass
print("\n\nTesting train_nn function......")
tests.test_train_nn(train_nn)
#KK Visualize the VGG16 model from Udacity reviewer
def graph_visualize():
# Path to vgg model
data_dir = './data'
vgg_path = os.path.join(data_dir, 'vgg')
with tf.Session() as sess:
model_filename = os.path.join(vgg_path, 'saved_model.pb')
with gfile.FastGFile(model_filename, 'rb') as f:
data = compat.as_bytes(f.read())
sm = saved_model_pb2.SavedModel()
sm.ParseFromString(data)
g_in = tf.import_graph_def(sm.meta_graphs[0].graph_def)
LOGDIR = '.'
train_writer = tf.summary.FileWriter(LOGDIR)
train_writer.add_graph(sess.graph)
#print("\n\nConverting .pb file to TF Summary and Saving Visualization of VGG16 graph..............")
#graph_visualize()
def run():
num_classes = 2
image_shape = (160, 576)
data_dir = './data'
runs_dir = './runs'
print("\n\nTesting for kitti datatset presence......")
tests.test_for_kitti_dataset(data_dir)
# Download pretrained vgg model
helper.maybe_download_pretrained_vgg(data_dir)
# OPTIONAL: Train and Inference on the cityscapes dataset instead of the Kitti dataset.
# You'll need a GPU with at least 10 teraFLOPS to train on.
# https://www.cityscapes-dataset.com/
with tf.Session() as sess:
# Path to vgg model
vgg_path = os.path.join(data_dir, 'vgg')
# Create function to get batches
get_batches_fn = helper.gen_batch_function(os.path.join(data_dir, 'data_road/training'), image_shape)
# OPTIONAL: Augment Images for better results
# https://datascience.stackexchange.com/questions/5224/how-to-prepare-augment-images-for-neural-network
# TODO: Build NN using load_vgg, layers, and optimize function KK-DONE
# TF placeholders
correct_label = tf.placeholder(tf.int32, [None, None, None, num_classes], name='correct_label')
learning_rate = tf.placeholder(tf.float32, name='learning_rate')
input_image, keep_prob, layer3_out, layer4_out, layer7_out = load_vgg(sess, vgg_path)
layer_output = layers(layer3_out, layer4_out, layer7_out, num_classes)
logits, train_op, cross_entropy_loss = optimize(layer_output, correct_label, learning_rate, num_classes)
# TODO: Train NN using the train_nn function KK-DONE
epochs = 25
batch_size = 4
sess.run(tf.global_variables_initializer())
train_nn(sess, epochs, batch_size, get_batches_fn, train_op, cross_entropy_loss, input_image,
correct_label, keep_prob, learning_rate)
# TODO: Save inference data using helper.save_inference_samples KK-DONE
helper.save_inference_samples(runs_dir, data_dir, sess, image_shape, logits, keep_prob, input_image)
# OPTIONAL: Apply the trained model to a video
if __name__ == '__main__':
run()