import tensorflow as tf import func_Training as ftr
#----------------------------------------------------------------------------------------------------------------------
img_height = 70 img_width = 280 img_channel = 3
learning_rate = 0.001 total_epochs = 100 tf.set_random_seed(777)
batch_size = 100
#----------------------------------------------------------------------------------------------------------------------
img_list = ftr.get_img_list() total_data = len(img_list) total_batch = int(total_data / batch_size)
#----------------------------------------------------------------------------------------------------------------------
x = tf.placeholder(tf.float32, [None, img_height, img_width, img_channel]) x_img = tf.reshape(x, [-1, img_height, img_width, img_channel]) y_steering = tf.placeholder(tf.float32, [None, 1])
#-------------------------------------------------------------------------------------------------1st Convolution Layer
W1 = tf.Variable(tf.random_normal([3, 3, 3, 8], stddev=0.01)) L1 = tf.nn.conv2d(x_img, W1, strides=[1, 1, 1, 1], padding='SAME') L1 = ftr.batch_norm_cnn(L1, 8) L1 = tf.nn.relu(L1) L1 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#-------------------------------------------------------------------------------------------------2nd Convolution Layer
W2 = tf.Variable(tf.random_normal([3, 3, 8, 16], stddev=0.01)) L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME') L2 = ftr.batch_norm_cnn(L2, 16) L2 = tf.nn.relu(L2) L2 = tf.nn.max_pool(L2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#-------------------------------------------------------------------------------------------------3rd Convolution Layer
W3 = tf.Variable(tf.random_normal([3, 3, 16, 32], stddev=0.01)) L3 = tf.nn.conv2d(L2, W3, strides=[1, 1, 1, 1], padding='SAME') L3 = ftr.batch_norm_cnn(L3, 32) L3 = tf.nn.relu(L3) L3 = tf.nn.max_pool(L3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') L3_flat = tf.reshape(L3, [-1, 9 * 35 * 32])
#---------------------------------------------------------------------------------------------4th Fully Connected Layer
W4 = tf.get_variable('W4', shape=[32 * 35 * 9, 128], initializer=tf.contrib.layers.xavier_initializer()) L4 = tf.matmul(L3_flat, W4) L4 = ftr.batch_norm_flat(L4) L4 = tf.nn.relu(L4)
#---------------------------------------------------------------------------------------------5th Fully Connected Layer
W5 = tf.get_variable('W5', shape=[128, 64], initializer=tf.contrib.layers.xavier_initializer()) L5 = tf.matmul(L4, W5) L5 = ftr.batch_norm_flat(L5) L5 = tf.nn.relu(L5)
#----------------------------------------------------------------------------------------------------------Output Layer
W6 = tf.get_variable('W6', shape=[64, 1], initializer=tf.contrib.layers.xavier_initializer()) b6= tf.Variable(tf.random_normal([1])) hypothesis = tf.matmul(L5, W6) + b6
#-----------------------------------------------------------------------------------------------Define cost & optimizer
cost = tf.reduce_mean(tf.square(hypothesis - y_steering)) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
sess = tf.Session() sess.run(tf.global_variables_initializer()) saver = tf.train.Saver()
#--------------------------------------------------------------------------------------------------------------Learning
print('\n' + 'Learning started...' + '\n')
for epoch in range(total_epochs) :
avg_cost = 0
for batch_index in range(total_batch):
training_img, training_label = ftr.read_batch(batch_index, batch_size) feed_dict = {x: training_img, y_steering: training_label} c, h, _ = sess.run([cost, hypothesis, optimizer], feed_dict=feed_dict) avg_cost += c
if (batch_index + 1) % 5 == 0 :
print('batch : %d ' % (batch_index + 1), 'cost = {:.4f} '.format(avg_cost / ((batch_index + 1) * batch_size)))
saver.save(sess, "~/my_train_model") print('\n' + 'Epoch : ' + '%d ' % (epoch + 1), 'cost = ', '{:.4f}'.format(avg_cost/total_data) + '\n')
print('Learning Finished!')
|