DEEP LEARNING IN GAMES

DEEP LEARNING IN GAMES 2

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 .

  1. open CV for grabbing the screen as data .
  2. capturing the keys as inputs .
  3. creating a neural network that takes captured screen data as input .
  4. training the model and then putting it to test.

THE APPROACH

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 :

  1. POSITION OF OTHER CARS .
  2. THE CURVATURE OF THE ROAD AT THAT POINT .
  3. 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 :

deep learning in games 
machine learning course

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 !!!!

NVIDIA CUDA GPU

DEEP LEARNING IN GAMES -GPUs ROLE

HOW NVIDIA PREDICTED TEN YEARS BACK THE FUTURE OF AI AND HOW ITS GPUs ARE PLAYING A KEY ROLE IN DEVELOPMENT OF DEEP LEARNING IN GAMES .

YOU MUST BE FAMILIAR WITH CPU( CENTRAL PROCESSING UNIT) . AND IF YOU ARE EITHER A GAMING ENTHUSIAST OR SOMEONE WHO KEEPS HIMSELF UPDATED WITH AI ADVANCEMENTS CHANCES ARE HIGH THAT YOU HAVE HEARD OF GPU’S. THEY HAVE EMERGED AS A BOON FOR AI DEVELOPMENT . AND SURELY THEY ARE DOING THEIR PART IN GAMING /GRAPHICS ADVANCEMENTS. WITH THE INCREASING USAGE OF DEEP LEARNING IN GAMES ,WE MIGHT SEE REALLY DIFFERENT VERSIONS OF GAMES BY THE END OF THIS DECADE . SMART AND INTELLIGENT ONES !!!!

LETS ANSWER THE BASIC QUESTION FIRST

WHAT IS A GPU

GPU STANDS FOR GRAPHICS PROCESSING UNIT .THE CORE PRINCIPLE ON WHICH GPU WORKS IS ” DIVIDE AND CONQUER ” . IN TECHNICAL TERMS WE CALL IT PARALLEL COMPUTING . THIS IS WHAT MAKES A GPU SO GOOD IN WHAT IT DOES . THIS IS IN CONTRAST TO A CPU WHICH CAN DO ONLY A HANDFUL OF CALCULATIONS AT ONCE . BUT HOW DOES A GPU DOES SO .WELL , THE ANSWER SEEMS OBVIOUS!!

NUMBER OF CORES IN A GPU

IF YOU ARE READING THIS ON A LAPTOP , OR IF YOU OWN ONE , YOU MUST HAVE HEARD TERMS LIKE DUAL CORE ,QUADCORE AND OCTACORE . SO BASICALLY A CPU CONSISTS OF A FEW CORES WHICH CAN HANDLE ACTIVITIES AND SUPPORT MULTIPROCESSING . IN CONTRAST A GPU CONSISTS OF A HUNDREDS OF CORES WHICH ALLOWS IT TO PERFORM PARALLEL COMPUTING BY BREAKING THE PROBLEM INTO SUBPROBLEMS.

GPU VS CPU

SO HOW DOES THIS HELP IN TRAINING AI / DEEP LEARNING MODELS ?

I HOPE YOU ARE FAMILIAR WITH NEURAL NETWORKS. AND IF NOT ,SUGGEST READING THIS AND THEN COMING BACK TO UNDERSTAND BETTER . ITS EASY TO SEE HOW THE NUMBER OF CALCULATIONS NEEDED PER CYCLE OF BACKPROPOGATION /FEEDFORWARD INCREASES DRASTICALLY WITH THE INCREASING COMPLEXITY OF THE NETWORK. ( MORE NEURONS , HIDDEN LAYERS AND SO) . THE NUMBER OF CALCULATIONS SOON RISE TO MILLIONS . TRAINING SUCH A MODEL ON A CPU WOULD BE A TEST OF PATIENCE . GPUs , BECAUSE OF THEIR ABILITY TO BREAK UP THE PROBLEMS USES PARALLEL COMPUTING TO ITS AID WHICH SPEEDS UP THE PROCESS EXPONENTIALLY .

GPUs IN GAMING , COMPUTER VISION ,SUPERCOMPUTING

I THINK BY NOW YOU CAN REALISE HOW GPUs WOULD OUTPERFORM CPUs IN THESE APPLICATIONS . SURELY BY THE END OF NEXT DECADE THE AI WOULD EFFECT GAMES , HEALTHCARE AND MANY DOMAINS OF COMPUTER SCIENCE . CURRENTLY NVIDIA IS LEADING THE GPU PRODUCTION MARKET . NVIDIA GRAPHICS CARDS ARE FAIRLY POPULAR AMONG GAMERS . CURRENTLY THE GAMES THAT CLAIM TO USE ” AI ” ARE IN REALITY USING ALGORITHMS FOR OPTIMISING THE ENEMIES BEHAVIOUR /APPROACH . THEY WORK ON TECHNOLOGIES LIKE “FINITE STATE MACHINES” . BUT SOON WE EXPECT THINGS TO CHANGE . TILL THEN , GAME ON!!!!