HOW TO TRAIN AN AI TO DRIVE A CAR IN GAMES LIKE NFS , GTA USING DEEP LEARNING IN GAMES (TENSORFLOW AND PYTHON)
DEEP LEARNING IN GAMES: What better than an Aston Martin driving through the streets on its own . In this article we see how you can train a supervised neural network which learns how to drive using a convolutional neural network . You can train cars in games like GTA , NFS , to learn to drive . I would advice using the the maps where the road is clear in terms of lanes . I have provided the github link for the code you will need . The article is inspired by a model trained by sentdex( a youtube channel with python and AI content) . Remember the AI we train here is supervised and not reinforcement learning . YOU NEED TO HAVE KNOWLEDGE OF THE FOLLOWING IN ORDER TO TRAIN SUCH A MODEL .
- open CV for grabbing the screen as data .
- capturing the keys as inputs .
- creating a neural network that takes captured screen data as input .
- training the model and then putting it to test.
how do we play such games as a human? OUR EYES CAPTURE THE DATA (THE VISUALS OF THE ROAD AND THE OTHER CARS ) . SENDS IT TO THE BRAIN . THEN OUR BRAIN CONSIDERS THE FOLLOWING FACTS :
- POSITION OF OTHER CARS .
- THE CURVATURE OF THE ROAD AT THAT POINT .
- THE AMOUNT OF DRIFT A CERTAIN CAR PRODUCES ON ACCELERATION .
THE BRAIN PROCESSES THIS INFORMATION AND THEN ON THE EXPERIENCE IT HAS FROM THE PREVIOUS GAMES IT DECIDES WHICH KEY TO PRESS . SENDS THE SIGNAL TO YOUR HAND AND THIS REPEATS THROUGHOUT THE TIME YOU PLAY .
WE DO THE SAME FOR TRAINING A NEURAL NETWORK . WE GRAB THE SCREEN AS VISUAL DATA . THE ACCESS TO THE KEYS SERVE AS HANDS PRESSING THE KEYS . THE CONVOLUTIONAL NET REFERS TO THE BRAIN HERE . LETS LOOK AT EACH STEP IN DETAIL .
HERE IS THE GITHUB LINK to the code .
GRABBING THE SCREEN .
For this you need to have the game running in a windowed mode . this data we grab acts as the training data for your network . While you play the game the python program would have access to the keys you are pressing and hence every frame that is captured would be associated with a key hence making up the training data .
REGION OF INTEREST
When you decide whether you need to slow down , turn left or hit them accelerators there are many factors present on the screen that don’ t matter . Like the colour of the frame , the presence of tall buildings , extra graphics features and so . hence we are interested only in Black and white frames , with just the part of the road visible , we mask the other areas . using grey scale images instead of rgb reduces the amount of data that needs to be processed by eliminating unnecessary details.
PRESSING AND ACCESSING THE KEYS
We provide access to the program to “press ” the keys and also note when a key is being pressed . THE PRESS KEY , GET KEYS AND DIRECT KEYS.py serve the purpose . we define the up , right , left arrows ( w ,d ,a ) which the program will have access to .
BALANCING THE DATA
The data we create while playing has to be biased for pressing the forward “key ” as it is being pressed throughout the drive . the left and right keys are pressed somewhat fewer times . We create a balanced data set that helps to counter this kind of bias . the code file balance_data.py serves this purpose . it will also show the count of the times you pressed the specific key while creating training data.
THE NEURAL NETWORK
Our neural network is a convolutional network that takes the frames and the keys associated as data and creates a model that has 3 outputs , representing the “up ” , “left” and “right ” probability distribution . you will get the model in the github as alexnet . py . nevertheless lets see how our deep learning in games model looks like :
TESTING THE MODEL
Open the game in the windowed mode , and run the code. The code captures the screen data and the model predicts the best possible action ( key ) to perform and then the program executes it . this happens repeatedly as long as the program is running !! following is the recording of a neural net playing NFS (an older version) trained on just half an hour of captured data . you can play for 2-3 hours and your model would learn better !!
Game on !!!!
One comment on “DEEP LEARNING IN GAMES 2”