Skip to content

Replace tf.mul and tf.initialize_all_variables #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 46 commits into
base: revert-1-master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
1cd5dec
Merge pull request #2 from SullyChen/revert-1-master
SullyChen Oct 1, 2016
65e4a3b
Create License.txt
SullyChen Oct 1, 2016
d3bb3ee
Added smoother steering
Oct 2, 2016
c4406ff
Add check for folder to save checkpoints
huyouare Oct 2, 2016
30d6410
Fix display of dataset image colors and resize
huyouare Oct 2, 2016
6e16656
Merge pull request #4 from huyouare/add_save_folder
SullyChen Oct 2, 2016
7edf9eb
Merge pull request #7 from huyouare/image-resize-color-swap
SullyChen Oct 2, 2016
7742f84
Create run_dataset.py
SullyChen Oct 2, 2016
cc518e6
Added validation loss output
Oct 3, 2016
61ea801
Switched from SGD to Adam Optimizer
SullyChen Oct 3, 2016
c9ebaa4
Update README.md
SullyChen Oct 5, 2016
c5644c3
Added pretrained model files
Oct 12, 2016
0c520b8
Steering wheel background changed to black for better visualization
Nov 24, 2016
5292a2c
Tensorboard added for loss function visualization
Nov 24, 2016
09c03d1
Merge pull request #13 from gautamsharma7234/master
SullyChen Nov 24, 2016
6dded13
Update README.md
SullyChen Nov 24, 2016
745fdbb
Update README.md
SullyChen Nov 24, 2016
2948f1b
Update README.md
SullyChen Nov 24, 2016
97c37d7
Added L2 normalization
Dec 6, 2016
1e81a37
Delete .DS_Store
SullyChen Dec 6, 2016
bf82d38
Updated trained model
Dec 6, 2016
a6461fd
fixed L2 Norm typo
SullyChen Dec 21, 2016
2f94dc2
Fixed typo
SullyChen Jan 7, 2017
6e35e12
Update README.md
SullyChen Feb 14, 2017
8a54faa
Update README.md
SullyChen Mar 25, 2017
54444f9
Fixed backwards compatibility issue with model saver
SullyChen Apr 30, 2017
5755c0a
Fixed Tensorboard error
SullyChen May 14, 2017
7b98e43
Fixed Tensorboard error
SullyChen May 15, 2017
497672c
Fixed Tensorboard error
SullyChen May 17, 2017
f919250
Update README.md
SullyChen Jul 2, 2018
80e010b
Update README.md
SullyChen Aug 18, 2018
2e05dc9
Update README.md
SullyChen Aug 18, 2018
32a7242
Update README.md
SullyChen Dec 22, 2018
8850842
made compatible with recent tensorflow (1.12.0 tested)
esciara Jan 21, 2019
00ebb4c
Update README.md
SullyChen Apr 22, 2019
6a1b13b
Update README.md
SullyChen Apr 22, 2019
4133f59
Update README.md
SullyChen Sep 4, 2019
bd7f619
Merge pull request #32 from xebia-france/original
SullyChen Nov 18, 2019
a5bb98a
Fixed TF 2.0 incompatibilities, worked on better windows compatibility
SullyChen May 12, 2020
991be3a
Update README.md
SullyChen Sep 30, 2020
557c0eb
Update README.md
SullyChen Sep 30, 2020
924b736
Update README.md
SullyChen Sep 30, 2020
0256bf6
Update README.md
SullyChen Mar 28, 2022
2d08693
Update README.md
SullyChen Aug 18, 2022
7fe56c3
Update README.md
SullyChen Apr 17, 2023
33b9e5f
Update README.md
SullyChen Oct 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions License.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Sully Chen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# Nvidia-Autopilot-TensorFlow
A TensorFlow implementation of this [Nvidia paper](https://arxiv.org/pdf/1604.07316.pdf) with some changes.
# Autopilot-TensorFlow
A TensorFlow implementation of this [Nvidia paper](https://arxiv.org/pdf/1604.07316.pdf) with some changes. For a summary of the design process and FAQs, see [this medium article I wrote](https://medium.com/@sullyfchen/how-a-high-school-junior-made-a-self-driving-car-705fa9b6e860).

#How to Use
Download the [dataset](https://drive.google.com/file/d/0B-KJCaaF7ellem5pSVM2NTNQcDg/view?usp=sharing) and extract into the repository folder
# IMPORTANT
Absolutely, under NO circumstance, should one ever pilot a car using computer vision software trained with this code (or any home made software for that matter). It is extremely dangerous to use your own self-driving software in a car, even if you think you know what you're doing, not to mention it is quite illegal in most places and any accidents will land you in huge lawsuits.

This code is purely for research and statistics, absolutley NOT for application or testing of any sort.

# How to Use
Download the [dataset](https://github.com/SullyChen/driving-datasets) and extract into the repository folder

Use `python train.py` to train the model

Use `python run.py` to run the model on a live webcam feed

Use `python run_dataset.py` to run the model on the dataset

To visualize training using Tensorboard use `tensorboard --logdir=./logs`, then open http://0.0.0.0:6006/ into your web browser.
37 changes: 29 additions & 8 deletions driving_data.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import scipy.misc
import cv2
import random
import numpy as np

xs = []
ys = []

#points to the end of the last batch
batch_pointer = 0
train_batch_pointer = 0
val_batch_pointer = 0

#read data.txt
with open("driving_dataset/data.txt") as f:
Expand All @@ -14,7 +16,7 @@
#the paper by Nvidia uses the inverse of the turning radius,
#but steering wheel angle is proportional to the inverse of turning radius
#so the steering wheel angle in radians is used as the output
ys.append(float(line.split()[1]) * scipy.pi / 180)
ys.append(float(line.split()[1]) * 3.14159265 / 180)

#get number of images
num_images = len(xs)
Expand All @@ -24,12 +26,31 @@
random.shuffle(c)
xs, ys = zip(*c)

def LoadBatch(batch_size):
global batch_pointer
train_xs = xs[:int(len(xs) * 0.8)]
train_ys = ys[:int(len(xs) * 0.8)]

val_xs = xs[-int(len(xs) * 0.2):]
val_ys = ys[-int(len(xs) * 0.2):]

num_train_images = len(train_xs)
num_val_images = len(val_xs)

def LoadTrainBatch(batch_size):
global train_batch_pointer
x_out = []
y_out = []
for i in range(0, batch_size):
x_out.append(cv2.resize(cv2.imread(train_xs[(train_batch_pointer + i) % num_train_images])[-150:], (200, 66)) / 255.0)
y_out.append([train_ys[(train_batch_pointer + i) % num_train_images]])
train_batch_pointer += batch_size
return x_out, y_out

def LoadValBatch(batch_size):
global val_batch_pointer
x_out = []
y_out = []
for i in range(0, batch_size):
x_out.append(scipy.misc.imresize(scipy.misc.imread(xs[(batch_pointer + i) % num_images])[-150:], [66, 200]) / 255.0)
y_out.append([ys[(batch_pointer + i) % num_images]])
batch_pointer += batch_size
x_out.append(cv2.resize(cv2.imread(val_xs[(val_batch_pointer + i) % num_val_images])[-150:], (200, 66)) / 255.0)
y_out.append([val_ys[(val_batch_pointer + i) % num_val_images]])
val_batch_pointer += batch_size
return x_out, y_out
5 changes: 3 additions & 2 deletions model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import scipy

def weight_variable(shape):
Expand Down Expand Up @@ -85,4 +86,4 @@ def conv2d(x, W, stride):
W_fc5 = weight_variable([10, 1])
b_fc5 = bias_variable([1])

y = tf.mul(tf.atan(tf.matmul(h_fc4_drop, W_fc5) + b_fc5), 2) #scale the atan output
y = tf.multiply(tf.atan(tf.matmul(h_fc4_drop, W_fc5) + b_fc5), 2) #scale the atan output
24 changes: 18 additions & 6 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import tensorflow as tf
import scipy.misc
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import model
import cv2
from subprocess import call
import os

#check if on windows OS
windows = False
if os.name == 'nt':
windows = True

sess = tf.InteractiveSession()
saver = tf.train.Saver()
Expand All @@ -11,15 +17,21 @@
img = cv2.imread('steering_wheel_image.jpg',0)
rows,cols = img.shape

smoothed_angle = 0

cap = cv2.VideoCapture(0)
while(cv2.waitKey(10) != ord('q')):
ret, frame = cap.read()
image = scipy.misc.imresize(frame, [66, 200]) / 255.0
degrees = model.y.eval(feed_dict={model.x: [image], model.keep_prob: 1.0})[0][0] * 180 / scipy.pi
call("clear")
image = cv2.resize(frame, (200, 66)) / 255.0
degrees = model.y.eval(feed_dict={model.x: [image], model.keep_prob: 1.0})[0][0] * 180 / 3.14159265
if not windows:
call("clear")
print("Predicted steering angle: " + str(degrees) + " degrees")
cv2.imshow('frame', frame)
M = cv2.getRotationMatrix2D((cols/2,rows/2),-degrees,1)
#make smooth angle transitions by turning the steering wheel based on the difference of the current angle
#and the predicted angle
smoothed_angle += 0.2 * pow(abs((degrees - smoothed_angle)), 2.0 / 3.0) * (degrees - smoothed_angle) / abs(degrees - smoothed_angle)
M = cv2.getRotationMatrix2D((cols/2,rows/2),-smoothed_angle,1)
dst = cv2.warpAffine(img,M,(cols,rows))
cv2.imshow("steering wheel", dst)

Expand Down
27 changes: 20 additions & 7 deletions run_dataset.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import tensorflow as tf
import scipy.misc
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import model
import cv2
from subprocess import call
import os

#check if on windows OS
windows = False
if os.name == 'nt':
windows = True

sess = tf.InteractiveSession()
saver = tf.train.Saver()
Expand All @@ -11,14 +17,21 @@
img = cv2.imread('steering_wheel_image.jpg',0)
rows,cols = img.shape

smoothed_angle = 0

i = 0
while(cv2.waitKey(10) != ord('q')):
image = scipy.misc.imresize(scipy.misc.imread("driving_dataset/" + str(i) + ".jpg")[-150:], [66, 200]) / 255.0
degrees = model.y.eval(feed_dict={model.x: [image], model.keep_prob: 1.0})[0][0] * 180.0 / scipy.pi
call("clear")
full_image = cv2.imread("driving_dataset/" + str(i) + ".jpg")
image = cv2.resize(full_image[-150:], (200, 66)) / 255.0
degrees = model.y.eval(feed_dict={model.x: [image], model.keep_prob: 1.0})[0][0] * 180.0 / 3.14159265
if not windows:
call("clear")
print("Predicted steering angle: " + str(degrees) + " degrees")
cv2.imshow("frame", image)
M = cv2.getRotationMatrix2D((cols/2,rows/2),-degrees,1)
cv2.imshow("frame", full_image)
#make smooth angle transitions by turning the steering wheel based on the difference of the current angle
#and the predicted angle
smoothed_angle += 0.2 * pow(abs((degrees - smoothed_angle)), 2.0 / 3.0) * (degrees - smoothed_angle) / abs(degrees - smoothed_angle)
M = cv2.getRotationMatrix2D((cols/2,rows/2),-smoothed_angle,1)
dst = cv2.warpAffine(img,M,(cols,rows))
cv2.imshow("steering wheel", dst)
i += 1
Expand Down
2 changes: 2 additions & 0 deletions save/checkpoint
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
model_checkpoint_path: "model.ckpt"
all_model_checkpoint_paths: "model.ckpt"
Binary file added save/model.ckpt
Binary file not shown.
Binary file added save/model.ckpt.meta
Binary file not shown.
Binary file modified steering_wheel_image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 52 additions & 17 deletions train.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,57 @@
import tensorflow as tf
import os
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from tensorflow.core.protobuf import saver_pb2
import driving_data
import model

LOGDIR = './save'

sess = tf.InteractiveSession()

loss = tf.reduce_mean(tf.square(tf.sub(model.y_, model.y)))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
sess.run(tf.initialize_all_variables())

saver = tf.train.Saver()

#train over the dataset about 30 times
for i in range(int(driving_data.num_images * 0.3)):
xs, ys = driving_data.LoadBatch(100)
train_step.run(feed_dict={model.x: xs, model.y_: ys, model.keep_prob: 0.8})
if i % 10 == 0:
print("step %d, train loss %g"%(i, loss.eval(feed_dict={
model.x:xs, model.y_: ys, model.keep_prob: 1.0})))
if i % 100 == 0:
save_path = saver.save(sess, "save/model.ckpt")
print("Model saved in file: %s" % save_path)
L2NormConst = 0.001

train_vars = tf.trainable_variables()

loss = tf.reduce_mean(tf.square(tf.subtract(model.y_, model.y))) + tf.add_n([tf.nn.l2_loss(v) for v in train_vars]) * L2NormConst
train_step = tf.train.AdamOptimizer(1e-4).minimize(loss)
sess.run(tf.global_variables_initializer())

# create a summary to monitor cost tensor
tf.summary.scalar("loss", loss)
# merge all summaries into a single op
merged_summary_op = tf.summary.merge_all()

saver = tf.train.Saver(write_version = saver_pb2.SaverDef.V2)

# op to write logs to Tensorboard
logs_path = './logs'
summary_writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph())

epochs = 30
batch_size = 100

# 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 = driving_data.LoadTrainBatch(batch_size)
train_step.run(feed_dict={model.x: xs, model.y_: ys, model.keep_prob: 0.8})
if i % 10 == 0:
xs, ys = driving_data.LoadValBatch(batch_size)
loss_value = loss.eval(feed_dict={model.x:xs, model.y_: ys, model.keep_prob: 1.0})
print("Epoch: %d, Step: %d, Loss: %g" % (epoch, epoch * batch_size + i, loss_value))

# write logs at every iteration
summary = merged_summary_op.eval(feed_dict={model.x:xs, model.y_: ys, model.keep_prob: 1.0})
summary_writer.add_summary(summary, epoch * driving_data.num_images/batch_size + i)

if i % batch_size == 0:
if not os.path.exists(LOGDIR):
os.makedirs(LOGDIR)
checkpoint_path = os.path.join(LOGDIR, "model.ckpt")
filename = saver.save(sess, checkpoint_path)
print("Model saved in file: %s" % filename)

print("Run the command line:\n" \
"--> tensorboard --logdir=./logs " \
"\nThen open http://0.0.0.0:6006/ into your web browser")