r/Python • u/Gautam-j • Sep 21 '20
Machine Learning End-to-End Self Driving Car - Need For Speed
119
u/punos_de_piedra Sep 21 '20
Bravo man that's really cool. In need of refinements, but nothing comes out perfect the first time, and I'm still really impressed.
56
u/Gautam-j Sep 21 '20
Thanks a lot! Yeah I'm still working on ways to improve this. The main restriction I found was to not able to have access to the game engine, and hence my only source of input is the screen image. That also does not let me use reinforcement learning :(
Any ideas as to how I can use reinforcement learning in this scenario?
42
u/caltas Sep 21 '20
If you want to go for RL approach you need to define transitions into "good" states. E.g. if your goal is to keep the car in line, it would be vital that a pair of frames shows that the middle of the screen/car stays in the lines. I haven't done this yet, but I would imagine that something like OpenCV could handle something like this.
On Google I found something like this: https://www.youtube.com/watch?v=0se6_UPNWVc.
I hope my answer helps you, it is more my intuition which speaks here as I have not so much experience in image processing.
15
u/Gautam-j Sep 21 '20
Thanks for the info! Your approach of defining the good states does really make a lot of sense! And yes, I have tried lane detection for this particular project, works good in highways, where the lanes are clearly marked, but not so much in random streets.
I guess I'll try to maybe create an ensemble that takes in the neural network (used in this video) plus lane detection.
21
u/cafriend Sep 21 '20
The fact that you don’t have much access and used the screen output makes this portable to actual vehicles. I would also recommend adding more views. This could be well on its way to a viable product in the market or at the very least a job offer.
→ More replies (7)9
u/bbqnacho Sep 21 '20
Are there any open source racing sims you could try this out on?
6
5
u/0rphon Sep 21 '20
if you were to get good at cheat engine you could write a basic trainer that tracks the cars relative position, speed, angle, etc etc. there are definitely more advanced ways to do it but cheat engine is probably going to be the easiest
→ More replies (3)4
2
2
u/MacGuyverism Sep 21 '20
Comma.ai's main way of input is a phone camera. I wonder if you could use their software to drive a car in a video game.
2
2
92
u/Owny33x Sep 21 '20
I imagine the minions from Despicable me trying to drive with one on each pedal, 2 at the wheel and 1 giving instructions...
→ More replies (1)16
u/Gautam-j Sep 21 '20
Best comment so far XD
15
u/wait--theresmore Sep 21 '20
That makes me wonder why isn't there a "Do nothing" label? (Meaning you just accelerate, no steering necessary)
Fantastic work btw
→ More replies (3)
24
13
Sep 21 '20
Hi, I'm new to this can you explain how you integrated the code with the game?
31
u/Gautam-j Sep 21 '20
I run the game in a windowed mode, and take a screen capture of the game window. Therefore, the input to the neural network is an image of the road, plus the mini map on the bottom left corner.
The neural network outputs whether the car should go straight, left or right. Based on that, I simulate a key press, and the car responds :)
12
8
u/Swyggles Sep 21 '20
How do you simulate a key press and get that to talk to the game?
9
u/Gautam-j Sep 21 '20
Get the game window in focus, and used a separate python script to simulate them.
4
u/dpalmade Sep 21 '20
whats the fps of the screen capture? is there an upper limit to that?
2
u/Gautam-j Sep 21 '20
Ig fps is around 15-20, I don't exactly remember. But no, there's no upper limit to it.
12
u/Methamputeemine Sep 21 '20
Off topic but I need to know is that NFSWorld or just my nostalgia? Great job btw!
Edit: typo
18
u/Gautam-j Sep 21 '20
Thanks! This is Need For Speed Most Wanted (2005) in Free Roam Mode :)
6
u/unnecessary_Fullstop Sep 21 '20
How do you access the game? Screen capture? Or is there any way to get "into" the game?
.
5
u/Gautam-j Sep 21 '20
Yeah it's just Screen Capture. I really really want to get into the game engine, but haven't found a solution yet.
5
2
18
17
u/renscy Sep 21 '20 edited Nov 09 '24
office fuzzy exultant vase gaze longing encourage gold aware oatmeal
This post was mass deleted and anonymized with Redact
8
8
u/familyofgorillas Sep 21 '20
Seems kinda jerky. Maybe add a control filter? You could just implement a simple low pass filter on the output.
3
6
5
5
u/appinv Python&OpenSource Sep 21 '20
That's pretty cool. I guess you mixed your two passions: games & python. It's fun how you can see the never-aging classic terminal alongside top graphics 👍
2
u/Gautam-j Sep 21 '20
Thanks! Yeah wanted to do something fun with python lol. Don't we all love terminal? (I know it's a windows cmd over there, I couldn't run the game on linux)
5
u/RippyTheGator Sep 21 '20
Kept watching expecting for it to improve itself haha. This is great!! Well done.
6
u/Gautam-j Sep 21 '20
Thanks a lot! What you say is totally true! I've spent hours watching it drive lol
4
u/iiMoe Sep 21 '20
This is actually an amazing model, Go tweet Elon
4
4
6
3
u/jingw222 Sep 21 '20
Great project. I've always wanted to do something similar but never got time to. Btw, is the AI cheating with the mini map on the lower left?
3
u/Gautam-j Sep 21 '20
Thanks! To answer your question, yes and no.
I created 2 models, with and without the mini-map as an input. The only difference I noticed was the model with the mini-map was able to make good turns when it comes to junctions. Otherwise, driving on highways and stuff, both models performed equally "good"
3
u/vEnoM_420 Sep 21 '20
This is so great.
I recently watched Sentdex's video on Self driving car in GTA 5. Totally amazing. He also ran into errors time to time, it was fun to see him fixing those quickly.
1
3
u/arcandor Sep 21 '20
Does the AI have a target? Or is it just trying not to crash? How are you managing speed?
I really like that this is from screengrabs only. It makes the problem it much more difficult and more similar to the real world.
1
u/Gautam-j Sep 21 '20
Thanks! Nope, the AI does not have any target. Managing speed by constantly pressing the gas, with restriction to use only the first gear.
3
u/vsvsvsvsvsvsvsvs Sep 21 '20
Maybe You should add "do nothing" as an action, so your AI has option to go in a straight line.
1
5
u/milozo1 Sep 21 '20
Pretty damn amazing!
5
u/Gautam-j Sep 21 '20
Thanks a lot!
4
u/milozo1 Sep 21 '20
Tried implementing something like that in rFactor several years ago
5
2
2
Sep 21 '20
this is how your BMW goes when it breaks down while racing Razor in the beginning of the game
2
2
u/agnarrarendelle Sep 21 '20
Are you self-taught or a CS student? This is simply amazing
6
u/Gautam-j Sep 21 '20
Thanks! At the time of writing this comment, I would say I'm self-taught.
Just finished high school, gonna pursue a Bachelors of Technology in Artificial Intelligence :)
4
u/agnarrarendelle Sep 21 '20
Holy sxxt. Would you share your learning resources with us, thanks
6
u/Gautam-j Sep 21 '20
Plus a LOT of YouTube Videos, Medium Blogs, towardsdatascience Blogs, etc.
→ More replies (2)3
u/pepoluan Sep 21 '20
Whoa, that's awesome dude!
Here's to your continued education and achievements in AI 🥂
→ More replies (1)
2
u/bpingel90 Sep 21 '20
You're really making it hard to "crawl,walk,run" with learning about Neural Nets.
2
u/Gautam-j Sep 21 '20
Haha. IMHO, I don't think there's a crawl, walk, run strategy when it comes to programming. You just sit and code (while learning what each line of code does, intuitively and mathematically). That's the only way you can learn CS.
2
2
2
u/FloydATC Sep 21 '20
Did you know that if you look closely, there are stripes painted on the road that tells you where you're supposed to drive? Just kidding, you've actually got it to sort of work, which is a far cry better than I ever have!
I bet this experience has affected your will to get into a real life "self driving" vehicle though. Imagine poor road quality, clusters of road signs and traffic lights, bad weather, pedestrians, bicycles everywhere and a confused driver with hazard blinkers making a right hand turn from the left lane :-/
6
u/Gautam-j Sep 21 '20
Thanks! Well to be honest, the fact that real life self-driving cars have a lot of inputs (like multiple cameras, radar, lidar, ultrasonic sensors, plus a powerful chip to compute all these data) unlike mine (which only takes an image of the road and an image of the mini-map as inputs) makes me believe the engineers will be able to develop a far better model, leading to a better experience :)
But yeah, human errors will definitely collapse the network!
2
u/Vivillon666 Sep 21 '20
Nice. Now just send it to elon to push it in next tesla update 😀 I think it is ready for test in prod
2
2
2
2
u/LatinoHeatps4 Sep 21 '20
Nice you used 2006 nfs most wanted.
2
2
2
u/QuantumCoder002 Sep 21 '20
Ah NFS MW 2005, u cut off when it detected cop presence, also how did u link code with the game ?
3
u/Gautam-j Sep 21 '20
I restricted it's top speed (by switching to manual transmission and staying in first gear) so didn't have the problem of cop chases.
I take a screen capture of the game window, feed it to the the network, which gives probabilities of key presses. Then a simple simulation of key presses :)
→ More replies (2)
2
u/R4_4S Sep 21 '20
Did you follow the self driving car in GTA tutorial or something?
2
u/Gautam-j Sep 21 '20
I would say I was inspired by Sentdex's series. That was kinda the foundation, I added a lot more of my custom stuff like having an auto encoder for anomaly detection, and hence the warnings.
2
2
u/SmoulderingHorizons Sep 21 '20
Can you give me a thought of how you managed to make the program "read" the screen?
2
u/Gautam-j Sep 21 '20
Initially I used cv2. But the frame rate at which I was reading the screen was too low. So then I switched to using win32 library for screen capture.
Have to say, it's not my code. Pretty sure it was from a StackOverflow thread lol
2
u/ReusedBoofWater Sep 21 '20
You're already prepped for working in a professional setting I'd say!
→ More replies (1)
2
2
Sep 21 '20
How long did it take you to build this ? So COOL
1
u/Gautam-j Sep 21 '20
Thank you! Well I didn't work on this continuously (GitHub commit messages have a range of 2-3 years) but if I had worked on this continuously, I would say a month maybe.
2
2
u/JayTurnr Sep 21 '20
Give it an option to go forward without turning you might have better luck
1
u/Gautam-j Sep 21 '20
It does have that option, but it wants to go zig zag lol
2
u/JayTurnr Sep 21 '20
Ah, that's strange! Keep it up though, it can only get better, right?
→ More replies (1)
2
u/devils-advocate164 Sep 21 '20
Hey Gautam-j this is excellent. Very neat! I couldn’t even make self driving dinosaur on google chrome offline page.
Yeah as discussed above having multiple views would be useful! You could use the different camera views for more data in each frame.
1
2
2
Sep 21 '20
Very cool stuff man. Very impressed. I'm just learning Python actually so it's really neat to see stuff like this.
You're gonna go far my man!
1
u/Gautam-j Sep 21 '20
Means a lot! Thank you! It's always fun to program in python! Wishing you all the best! Hoping to see your project here soon ;)
→ More replies (4)
2
u/15_Redstones Sep 21 '20
It'd be really nice if you could make an AI that could do multiple different driving simulators just based on what it sees on screen. Then your program might even be able to drive a real car based on camera input.
1
2
u/KillerKingTR Sep 21 '20
Ai be playing gta no traffic laws no nothing. Zero fucks given. Respect tho
1
2
u/swat8094 Sep 21 '20
Awesome man! I’m currently taking AI this semester, so this was fun for me to watch!
1
2
u/LazaroFilm Sep 21 '20
That reminds me of the time I worked for Nvidia quite a few years ago, filming a video of their first self driving car that only used one webcam to drive on the NJ streets. I was strapped to the open trunk of a van in front of the sled driving car prototype with my Steadicam sticking out the back. Every second I was thinking, please don’t go crazy on me. A couple of times I saw the car veering a bit and the driver rushed to grab the wheel.
1
2
2
2
u/BlackAkuma666 Sep 21 '20
Step 2: write it for gta5’s oppressor and just ruin servers.
2
u/Gautam-j Sep 21 '20
Nice idea!
2
2
u/thingythangabang Sep 21 '20
Reminds me of the time Sentdex created a self driving AI in GTA V: https://www.youtube.com/playlist?list=PLQVvvaa0QuDeETZEOy4VdocT7TOjfSA8a
Makes me want to go back and watch that again!
1
2
u/Sigg3net Sep 21 '20
You should consider adding a "drive straight" elif :)
1
2
u/AlexK- Sep 21 '20
I wish Teslas were able to show us the commands they “think” when driving Autonomously!!!
2
u/Gautam-j Sep 21 '20
As a tech enthusiastic, we might like it. But the average guy will probably freak out lol
2
u/AlexK- Sep 21 '20
Well, yeah. Could be an option to be enabled by the user. Not a permanent thing for sure!
2
u/Gautam-j Sep 21 '20
Sure! I remember watching a video on Youtube in which a guy kinda hacked his Tesla and we could see all the predictions and boundary boxes that the model outputted.
→ More replies (2)
2
u/Tengoles Sep 21 '20
Nice! At how many fps is the network running? I find it very nice that it's working only on screen captures.
1
2
2
2
Sep 21 '20
Bang-bang controls are... not ideal for that kind of thing. (the steerage seems to be using that scheme)
Is analog control an option in this game? If so, a PID controller is something you should look into!
1
u/Gautam-j Sep 21 '20
Very very true! Most of the self driving cars output a steering angle which makes a lot of sense, but unfortunately the game doesn't allow any other kind of control.
Edit: change of words
2
2
2
u/YodaCodar Sep 21 '20
Oh man i went on an uber with a driver that drove like that my stomach hurt so much after that ride. I couldn't even ask politely please drive a little smoother before I felt sick.
1
2
u/Blenderpro Sep 21 '20
Omg i wanna do this when i grow up! Tysm fr inspiring me w(°o°)w
2
u/Gautam-j Sep 22 '20
If you really wanna get into the field of programming and computer science, start right now. Your age doesn't matter. Trust me.
2
2
2
2
2
u/BAWRussell Sep 21 '20
Impressive man good job. Are you comfortable sharing what you do for a living?
3
u/Gautam-j Sep 22 '20
Thanks! I just finished high school. My college would have started like 3 months ago, but due to the pandemic, online classes began only last week :)
2
2
u/maifee Sep 22 '20
This just gave me goosebumps...
1
u/Gautam-j Sep 23 '20
Haha
2
u/maifee Sep 23 '20
I was working on the same project one year back.. I was trying to make dataset : https://www.youtube.com/watch?v=YLZycbAWr3s, https://www.youtube.com/watch?v=adu09CnksKY.
But a few months ago, I had to abandon this awesome cool project. But it's really cool to see it.
2
u/Gautam-j Sep 23 '20
Yeah we always do that, skip an on-going project and start a new one only to abandon it again
2
2
2
0
Sep 21 '20
[deleted]
122
Sep 21 '20
i hope when self driving cars go into service they project in big letters low validation score warnings for the passengers to see
→ More replies (2)29
→ More replies (10)24
0
0
Sep 21 '20
[deleted]
5
u/Gautam-j Sep 21 '20
I does have a straight option! Maybe I should tweak the thresholds a bit.
→ More replies (2)
0
137
u/Gautam-j Sep 21 '20
Link to code: GitHub