Git Product home page Git Product logo

reinforcement-learning-car's Introduction

Using reinforcement learning to train an autonomous vehicle to avoid obstacles

NOTE: If you're coming here from parts 1 or 2 of the Medium posts, you want to visit the releases section and check out version 1.0.0, as the code has evolved passed that.

This is a hobby project I created to learn the basics of reinforcement learning. It uses Python3, Pygame, Pymunk, Keras and Theanos. It employes a Q-learning (unsupervised) algorithm to learn how to move an object around a screen (drive itself) without running into obstacles.

The purpose of this project is to eventually use the learnings from the game to operate a real-life remote-control car, using distance sensors. I am carrying on that project in another GitHub repo here: https://github.com/harvitronix/rl-rc-car

This version of the code attempts to simulate the use of sensors to get us a step closer to being able to use this in the real world.

Full writeups that pertain to version 1.0.0 can be found here:

Part 1: https://medium.com/@harvitronix/using-reinforcement-learning-in-python-to-teach-a-virtual-car-to-avoid-obstacles-6e782cc7d4c6

Part 2: https://medium.com/@harvitronix/reinforcement-learning-in-python-to-teach-a-virtual-car-to-avoid-obstacles-part-2-93e614fcd238#.vbakopk4o

Part 3 (for this version of the code): https://medium.com/@harvitronix/reinforcement-learning-in-python-to-teach-an-rc-car-to-avoid-obstacles-part-3-a1d063ac962f

Installing

These instructions are for a fresh Ubuntu 16.04 box. Most of the same should apply to OS X. If you have issues installing, feel free to open an issue with your error and I'll do my best to help.

Basics

Recent Ubuntu releases come with python3 installed. I use pip3 for installing dependencies so install that with sudo apt install python3-pip. Install git if you don't already have it with sudo apt install git.

Then clone this repo with git clone https://github.com/harvitronix/reinforcement-learning-car.git. It has some pretty big weights files saved in past commits, so to just get the latest the fastest, do git clone https://github.com/harvitronix/reinforcement-learning-car.git --depth 1.

Python dependencies

pip3 install numpy keras h5py

That should install a slew of other libraries you need as well.

Install Pygame

Install Pygame's dependencies with:

sudo apt install mercurial libfreetype6-dev libsdl-dev libsdl-image1.2-dev libsdl-ttf2.0-dev libsmpeg-dev libportmidi-dev libavformat-dev libsdl-mixer1.2-dev libswscale-dev libjpeg-dev

Then install Pygame itself:

pip3 install hg+http://bitbucket.org/pygame/pygame

Install Pymunk

This is the physics engine used by the simulation. It just went through a pretty significant rewrite (v5) so you need to grab the older v4 version. v4 is written for Python 2 so there are a couple extra steps.

Go back to your home or downloads and get Pymunk 4:

wget https://github.com/viblo/pymunk/archive/pymunk-4.0.0.tar.gz

Unpack it:

tar zxvf pymunk-4.0.0.tar.gz

Update from Python 2 to 3:

cd pymunk-pymukn-4.0.0/pymunk

2to3 -w *.py

Install it:

cd .. python3 setup.py install

Now go back to where you cloned reinforcement-learning-car and make sure everything worked with a quick python3 learning.py. If you see a screen come up with a little dot flying around the screen, you're ready to go!

Training

First, you need to train a model. This will save weights to the saved-models folder. You may need to create this folder before running. You can train the model by running:

python3 learning.py

It can take anywhere from an hour to 36 hours to train a model, depending on the complexity of the network and the size of your sample. However, it will spit out weights every 25,000 frames, so you can move on to the next step in much less time.

Playing

Edit the playing.py file to change the path name for the model you want to load. Sorry about this, I know it should be a command line argument.

Then, watch the car drive itself around the obstacles!

python3 playing.py

That's all there is to it.

Plotting

Once you have a bunch of CSV files created via the learning, you can convert those into graphs by running:

python3 plotting.py

This will also spit out a bunch of loss and distance averages at the different parameters.

Credits

I'm grateful to the following people and the work they did that helped me learn how to do this:

reinforcement-learning-car's People

Contributors

carriojazz avatar harvitronix avatar microos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

reinforcement-learning-car's Issues

some confusion about the project

hello,I have not find where the “two-nets structure” is in your simulator and I want to change the
code to double dqn but have no idea how to do。could you discuss that with me?

Simulation won't run on OSX

image

Hi!

I am having trouble getting the simulation to run. I have pygame and pymunk properly installed and it seems to be working as I can run the exampels in both pymunk and pygame properly.

I am on OS X Mojave and am using a clean conda environment to run everything. Although it appears draw_screen is running, I am not seeing anything on the screen and the screen remains empty and I get a "spinning wheel" for loading and pygame doesn't respond. I have followed your installation guide to the dot. Any ideas?

AttributeError: module 'tensorflow' has no attribute 'python'

Hi, I get the following error when I execute python3 learning.py

Loading chipmunk for Linux (64bit) [/usr/local/lib/python3.5/dist-packages/pymunk/libchipmunk64.so]
Using TensorFlow backend.
Traceback (most recent call last):
File "learning.py", line 5, in
from nn import neural_net, LossHistory
File "/home/kiwis/reinforcement-learning-car/nn.py", line 15, in
tf.python.control_flow_ops = tf
AttributeError: module 'tensorflow' has no attribute 'python'

I am relatively new to python, your help would be greatly appreciated

Layer shape not compatible with weight shape

it is long time to train,however,after some time ,I get some moudles.when i run thepython3 playing.py,there are some errro that Exception: Layer shape (3, 164) not compatible with weight shape (6, 164).

Error in playing.py

When I run the playing.py file, I get the following error
ValueError: Dimension 0 in both shapes must be equal, but are 3 and 6. Shapes are [3,164] and [6,164]. for 'Assign' (op: 'Assign') with input shapes: [3,164], [6,164].

I am using version 2.0.0

Traceback (most recent call last):
File "/home/sudhamsa/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1659, in _create_c_op
c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimension 0 in both shapes must be equal, but are 3 and 6. Shapes are [3,164] and [6,164]. for 'Assign' (op: 'Assign') with input shapes: [3,164], [6,164].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "playing.py", line 38, in
model = neural_net(NUM_SENSORS, [164, 150], saved_model)
File "/home/sudhamsa/reinforcement-learning-car-2.0.0/nn.py", line 46, in neural_net
model.load_weights(load)
File "/home/sudhamsa/.local/lib/python3.6/site-packages/keras/engine/network.py", line 1166, in load_weights
f, self.layers, reshape=reshape)
File "/home/sudhamsa/.local/lib/python3.6/site-packages/keras/engine/saving.py", line 1058, in load_weights_from_hdf5_group
K.batch_set_value(weight_value_tuples)
File "/home/sudhamsa/.local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2465, in batch_set_value
assign_op = x.assign(assign_placeholder)
File "/home/sudhamsa/.local/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 1762, in assign
name=name)
File "/home/sudhamsa/.local/lib/python3.6/site-packages/tensorflow/python/ops/state_ops.py", line 223, in assign
validate_shape=validate_shape)
File "/home/sudhamsa/.local/lib/python3.6/site-packages/tensorflow/python/ops/gen_state_ops.py", line 64, in assign
use_locking=use_locking, name=name)
File "/home/sudhamsa/.local/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
op_def=op_def)
File "/home/sudhamsa/.local/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "/home/sudhamsa/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3300, in create_op
op_def=op_def)
File "/home/sudhamsa/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1823, in init
control_input_ops)
File "/home/sudhamsa/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1662, in _create_c_op
raise ValueError(str(e))
ValueError: Dimension 0 in both shapes must be equal, but are 3 and 6. Shapes are [3,164] and [6,164]. for 'Assign' (op: 'Assign') with input shapes: [3,164], [6,164].

Input data normalization

Hey Matt, I've seen that you don't normalize the sensor readings before feeding them to the network. Are you not normalizing for some reason? Also I wonder how does the LSTM perform, could you give any feedback?

Chipmunk abort

Hi, whilst running the code on Python 3.6.3 (Mac) whilst running python3 learning.py I was met with Aborting due to Chipmunk error: Mass must be positive and finite.
Failed condition: 0.0f <= mass && mass < INFINITY
Source:chipmunk_src/src/cpBody.c:257

Do you know how this might be fixed?

Thanks

pymunk windows

i did install using pip3 just like it mentioned.. but when i import it says OSError: [WinError 126] The specified module could not be found

Pygame window

Hi,
I tried to run the code in windows, the pygame window opens and closes immediately.. Do you know what to do?

normalized readings

I think that in the line 134 of carmun.py
normalized_readings = [(x-20.0)/20.0 for x in readings]
has to be changed to
normalized_readings = [(x-20.0)/40.0 for x in readings]

Error importing draw from pymunk.pygame_util

I wanted to run your code on my window 10 with 32bit python installed but its giving me an ImportError saying cannot import name draw which is occurring on carmunk.py file please help

pymunk.body.apply_impulse

When I run the learning.py file, it gives me the " AttributeError: 'Body' object has no attribute 'apply_impulse'" error. which version of pymunk used in the code. how can I fix the problem?

Error module import

:~/reinforcement-learning-car$ python3 learning.py
Loading chipmunk for Linux (64bit) [/usr/local/lib/python3.5/dist-packages/pymunk/libchipmunk64.so]
Using TensorFlow backend.
Traceback (most recent call last):
File "learning.py", line 5, in
from nn import neural_net, LossHistory
File "/home/archer/reinforcement-learning-car/nn.py", line 6, in
from keras.models import Sequential
File "/home/archer/.local/lib/python3.5/site-packages/keras/init.py", line 3, in
from . import activations
File "/home/archer/.local/lib/python3.5/site-packages/keras/activations.py", line 4, in
from . import backend as K
File "/home/archer/.local/lib/python3.5/site-packages/keras/backend/init.py", line 73, in
from .tensorflow_backend import *
File "/home/archer/.local/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 1, in
import tensorflow as tf
ImportError: No module named 'tensorflow'

pymunk 4.0 install in python 3.6

This error usually means that you don't have a compiled version of chipmunk in
the correct spot where pymunk can find it. pymunk does not include precompiled
chipmunk library files for all platforms.

Please found any solution let me know

error.pdf

Get access to the blog

Hi there, I'm very interested in your blog about this project. However, since I'm in China the website https://medium.com/ has been blocked. Could you please help save your blog in xml or pdf format and send it to me? I'm sorry if this request bothers you, but I really want to read and learn from your blog. Thank you very much.

Did you get this working?

Hi, I am trying to do something similar to this but I am having a hard time getting it to train correctly. Did you get this working?

Run for first time - detail request

Hi Matt,

What you have been able to do with this project is amazing! I would like to get this program running on my system but after working at if for 3 days nothing seems to work when it comes to environment set up. I was hoping you could add more detail on how you set up your environment on the readme.

What did you start with as your "base"? Was it purely python or did you start with something like Anaconda?

Also when I was recently trying to install pymunk 4.0.0 I got errors with Chipmunk and was wondering if you had any similar issues when you set up your environment.

Thanks,
MrZantag

P.S. If there is a better spot to post these questions please let me know!

Error while installing keras in open suse

Hi,
I did install python3 and got the repository using git. While installing kera using: pip3 install numpy keras h5py, am facing error. I have attached snapshot. I want to install the ubuntu box and give a try.. If you can provide me the link for ubuntu box version dvd it would be helpful.

python_install

Issue with cuda library

After following all steps, installing everything when i run the following command
python3 learning.py
i get the following... I have a question.. is cudalibrary necessary to run this project? since my laptop doesnt have a NVIDIA card, it has a AMD Radeon card
Loading chipmunk for Linux (64bit) [/usr/local/lib/python3.5/dist-packages/pymunk/libchipmunk64.so]
Using TensorFlow backend.
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/init.py", line 61, in
from tensorflow.python import pywrap_tensorflow
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in
_pywrap_tensorflow = swig_import_helper()
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
File "/usr/lib/python3.5/imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
File "/usr/lib/python3.5/imp.py", line 342, in load_dynamic
return _load(spec)
ImportError: libcudart.so.8.0: cannot open shared object file: No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "learning.py", line 5, in
from nn import neural_net, LossHistory
File "/home/saksham/8th/project_mam/reinforce/reinforcement-learning-car/nn.py", line 6, in
from keras.models import Sequential
File "/home/saksham/.local/lib/python3.5/site-packages/keras/init.py", line 3, in
from . import activations
File "/home/saksham/.local/lib/python3.5/site-packages/keras/activations.py", line 3, in
from . import backend as K
File "/home/saksham/.local/lib/python3.5/site-packages/keras/backend/init.py", line 73, in
from .tensorflow_backend import *
File "/home/saksham/.local/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 1, in
import tensorflow as tf
File "/usr/local/lib/python3.5/dist-packages/tensorflow/init.py", line 24, in
from tensorflow.python import *
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/init.py", line 72, in
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/init.py", line 61, in
from tensorflow.python import pywrap_tensorflow
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in
_pywrap_tensorflow = swig_import_helper()
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
File "/usr/lib/python3.5/imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
File "/usr/lib/python3.5/imp.py", line 342, in load_dynamic
return _load(spec)
ImportError: libcudart.so.8.0: cannot open shared object file: No such file or directory

Failed to load the native TensorFlow runtime.

See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/get_started/os_setup.md#import_error

for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.