-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtrain_brake.py
More file actions
78 lines (58 loc) · 2.91 KB
/
Copy pathtrain_brake.py
File metadata and controls
78 lines (58 loc) · 2.91 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
import os
import tensorflow as tf
from tensorflow.core.protobuf import saver_pb2
import driving_data
import scipy
import cv2
# standard step - reset computation graphs
tf.reset_default_graph()
saveDirectory = './save'
L2NormConst = 0.001
import model_brake
all_vars = tf.trainable_variables()
print(model_brake.y_brake_.shape)
print(model_brake.y_brake.shape)
loss_brake = tf.reduce_mean(tf.square(tf.subtract(model_brake.y_brake_, model_brake.y_brake))) #+ tf.add_n([tf.nn.l2_loss(v) for v in all_vars]) * L2NormConst
train_step_brake = tf.train.AdadeltaOptimizer(1., 0.95, 1e-6).minimize(loss_brake) # These are default parameters to the Adadelta optimizer
sess_brake = tf.Session()
sess_brake.run(tf.global_variables_initializer())
saver_brake = tf.train.Saver(all_vars)
# create a summary to monitor cost tensor
tf.summary.scalar("loss_brake", loss_brake)
# merge all summaries into a single op
merged_summary_op = tf.summary.merge_all()
# op to write logs to Tensorboard
logs_path = './logs'
summary_writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph())
epochs = 50
batch_size = 80
# train over the dataset about 30 times
for epoch in range(epochs):
for i in range(int(driving_data.num_images/batch_size)):
xs, ys, accels, brakes = driving_data.LoadTrainBatch(batch_size, False)
train_step_brake.run(feed_dict={model_brake.x_brake: xs, model_brake.y_brake_: brakes, model_brake.keep_prob_brake: 0.5, model_brake.keep_prob_brake_conv: 0.25}, session = sess_brake)
if i % 10 == 0:
xs, ys, accels, brakes = driving_data.LoadValBatch(batch_size, False)
loss_value_brake = loss_brake.eval(feed_dict={model_brake.x_brake:xs, model_brake.y_brake_: brakes, model_brake.keep_prob_brake: 1.0, model_brake.keep_prob_brake_conv: 1.0}, session = sess_brake)
print("Epoch: %d, Step: %d, Accel Loss: %g " % (epoch, epoch * batch_size + i, loss_value_brake))
if i % batch_size == 0:
if not os.path.exists(saveDirectory):
os.makedirs(saveDirectory)
brake_checkpoint_path = os.path.join(saveDirectory, "model_brake.ckpt")
filename_brake = saver_brake.save(sess_brake, brake_checkpoint_path)
print("Model saved in file: %s" % filename_brake)
### To predict the output based on the above trained model### - THE BUG IS IN THE CODE ABOVE!
xs = []
dataPath = "indian_dataset/"
fileNamePrefix = "circuit2_x264.mp4 "
with open(dataPath+"data.txt") as f:
for line in f:
xs.append(dataPath + fileNamePrefix + str(int(line.split()[0])).zfill(5)+".jpg")
i = 0
while(cv2.waitKey(10) != ord('q')):
full_image = scipy.misc.imread(xs[i], mode="RGB")
image = scipy.misc.imresize(full_image[-150:], [112, 112]) / 255.0
acceleration = model_brake.y_brake.eval(feed_dict={model_brake.x_brake: [image], model_brake.keep_prob_brake: 1.0, model_brake.keep_prob_brake_conv: 1.0}, session = sess_brake)[0][0]
print(i,acceleration * 180.0 / scipy.pi)
i += 1
sess_brake.close()