Self-Driving AI Car Simulation in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
machine learning is awesome and one of the most exciting things that we can do with artificial intelligence on our own computers is running ai simulations in this video we're going to simulate the evolution of self-driving 2d cars in python for this we will use a technique called neural evolution of augmenting topologies short neat we will start with some very basic maps that are quite easy to learn and we're going to work our way up as we go on the cool thing about this is that we can draw our maps with simple tools like paint or we can try to design some smooth curves by using the path tool or we can just pick a brush if we lack the patience for that if we really want to challenge our models we can also draw some crazy maps that look like this our cars have five sensors that look out for the borders those are the five input neurons of our neural network this is the only thing that our ai actually sees also our model has four output neurons that represent the four actions it can take those are steering to the left steering to the right increasing the speed and decreasing the speed considering a certain predefined minimum speed so grab some popcorn hit the like button and let's get right into it [Music] first of all i want to mention that the whole code is available on github and heavily inspired by the youtuber cheesyai i basically optimized and changed his code so that it suits this video's needs the project consists of two major parts the python file and the need config file in the neat conflict file we can specify all sorts of parameters which will influence things like reproduction mutations the amount of species etc for those of you who know nothing about need or reinforcement learning in general i will quickly explain the basic idea of the whole algorithm without boring you too much with any mathematical details at least in this video as i already said our model is a neural network with five input neurons the sensors and four output neurons the actions it can take in between those we can optionally have some hidden layers with additional neurons those layers increase the complexity and the sophistication of our model but they also increase the training time and the likelihood of overfitting all those neurons are interconnected and those connections have certain weights depending on all those values our model will react in a certain way based on the inputs in the beginning all those reactions will be totally random there will be zero intelligence behind what our cars are doing however for each action our cars take they will either receive a reward or a penalty to implement this we use the so called fitness metric in our simple simulation the fitness of a car increases depending on the distance it covers without crashing after each generation we then evolve our cars the cars with the highest fitness value will probably survive and reproduce whereas the cars that didn't perform so well will go extinct after a while when a car reproduces it will not just duplicate the child car will be quite similar to its parent but not the same therefore it has a chance to become better cars that are very similar to each other form an owned species if a species doesn't see any improvements for a fixed number of generations it goes extinct given all those principles we can create an environment in which the best cars survive and reproduce whereas the worst cars go extinct and the model is forced to experiment around the basic principle is what works will probably persist and be replicated but i think that was enough theory let's start driving for the first simulation let us start with the most basic map possible which is just a ring going full circle once and the result is quite interesting since we have a population size of 30 by mere chance we already have a card that drives quite well in the first generation if we are interested in what's happening behind the scenes we can also show the radars to see what the car is seeing the only thing that the car has to do is steer left in the right moment and then it survives in the next generation we can already see that this car reproduced and we now thus have two cars were able to drive and not crash for 20 seconds but not only that one of the two cars is also way faster than the other one this might have happened by mere chance as well but remember the fitness value increases harder the more distance a car covers in those 20 seconds and our cars have the ability to accelerate therefore if a car figures out that increasing the speed in the right moment leads to better results we might see a new alpha species here and in fact in generation four we can already see four of those fast driving cars by generation five most of the cars are driving fast and you can even see one car making a very good attempt at speeding through the racing course but this map is pretty basic let us take a look at a map that has some more curves in it here as you can see every single car crashes right in the beginning of generation 1. this seems to be a bit harder in generation 2 we have some promising attempts but still all the cars crash and even though not a single car succeeds in the next couple of generations we can clearly see that the quote-unquote best cars reproduce and we see more and more good attempts over time [Music] in generation 7 we can then all of a sudden finally see two cars succeeding at going full circle one of them is going quite fast whereas the other one tries to maneuver carefully through the racetrack you can try and guess now which one will persist and reproduce over time and maybe this comes as a surprise but after 12 generations there are not too many differences between the two types of cars one explanation for this could be that even though the fast cars have a better fitness value the likelihood of a mutated version surviving at that speed is less likely than that of slower cars this might definitely change after a couple of more generations but let's go one level further since this map is still quite easy now this map looks way more challenging and it actually is we don't see any meaningful improvements until generation eight and even then it crashes soon after passing the starting curves and even though it tries to reproduce even slight changes in the offspring lead to crashes but then in generation 15 we see a super child this car does not only overcome the u-turn its parent failed add multiple times it even passes the full circuit in the first try very impressive since this is a longer track i increased the time limit to 40 seconds in the next generation we can already notice a reproduction a second car managed to pass the full race track however doing that requires fine rhetorical skills which is why many of the child cars fail after some curves then we noticed something interesting in generation 20 we have eight cars who manage to survive for 40 seconds and cover a pretty good distance however in generation 21 that number has decreased to 7. why is that see the reason for that is a parameter called elitism this parameter decides how many of the best cars will survive one hundred percent in our simulation this number is set to two which means that we guarantee that the best two cars will survive for sure all of the other cars might die without reproducing even if their results are quite good by choosing those settings we ensure that only the best of the best can survive and reproduce for sure since our model was able to beat every single map up until now we will now try to challenge it way harder we will put our car onto a map that looks like something that a guy having a stroke would scribble onto a canvas in his last seconds this thing is not only full of curves it also has some pretty narrow passages that are going to be a huge challenge for our cars and trust me they were naive as i was i just started the simulation with the same parameters and settings as before and it failed miserably i ran multiple simulations for hours and hours and the cars didn't seem to manage to pass through the whole track they made significant advancements and they even passed the narrow parts but in the end they all crashed before reaching the finish line so i thought why not add some more complexity to the model up until now we didn't use any hidden layers to increase the complexity of the model so maybe this could help but it didn't i again spent multiple hours starting and restarting simulations not a single car managed to go through the finish line it actually performed worse with a hidden layer now don't get me wrong if we would run that simulation overnight it would probably manage to cross the finish line sooner or later but i wanted to find settings that get the job done fast so after experimenting around with various parameters for a very long time i had an idea maybe instead of increasing the complexity of the neural network i could simplify the choices it has so what i did is i removed the hidden layer and also two of the output neurons those two output neurons were the ones which were responsible for increasing and decreasing the speed instead of letting the neural network decide when to speed up or slow down i decided to let the cars drive at a constant speed to be precise at the former minimum speed which was eight that way the only thing that the mall had to do was steering left and right notice that i didn't change any part of the simulation the car could have chosen that strategy on its own by just decreasing the speed down to the minimum and never increasing it again so technically speaking we're not cheating we just make a manual choice for the model so that it can focus on the steering and even though it still took quite a long time we actually managed to go through the finish line using that approach simplicity wins [Music] now of course we could go on and on and create more ridiculous maps over time but i'll leave that part to you the code for this project including the sprites is fully available on github in order to create your own map all you need to do is creating a 1920 times 1080 pixels image on which you draw some random tracks then you also need to make sure that the starting position that you set in the script is not on the white color every color except for white is okay so feel free to be creative i hope you enjoyed this video if so let me know by hitting the like button and leaving a comment in a comment section down below also don't forget to subscribe and hit the notification bell to not miss a single future video for free other than that thank you very much for watching see you next video and [Music] bye [Music] you
Info
Channel: NeuralNine
Views: 33,430
Rating: 4.9619474 out of 5
Keywords: python, coding, programming, ai, artificial intelligence, intelligent, neural network, neural networks, tensorflow, nlp, natural language processing, python tutorial, python ai, python chatbot, NEAT, reinforcement learning, self-driving, car, cars, tesla, self driving, autonomous, vehicles, simulation, deep learning
Id: Cy155O5R1Oo
Channel Id: undefined
Length: 11min 51sec (711 seconds)
Published: Thu Feb 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.