<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bhargava Sivateja on Medium]]></title>
        <description><![CDATA[Stories by Bhargava Sivateja on Medium]]></description>
        <link>https://medium.com/@bhargavasivateja?source=rss-8fb4401c2cea------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tHRfSuDcn4T9wjwplu1dHg.png</url>
            <title>Stories by Bhargava Sivateja on Medium</title>
            <link>https://medium.com/@bhargavasivateja?source=rss-8fb4401c2cea------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 13:03:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bhargavasivateja/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Self driving AI terrorizing the great city in NFS RIVALS]]></title>
            <link>https://medium.com/@bhargavasivateja/self-driving-ai-terrorizing-the-great-city-in-nfs-rivals-56d1d2fcda3e?source=rss-8fb4401c2cea------2</link>
            <guid isPermaLink="false">https://medium.com/p/56d1d2fcda3e</guid>
            <category><![CDATA[games]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Bhargava Sivateja]]></dc:creator>
            <pubDate>Fri, 03 Aug 2018 10:33:16 GMT</pubDate>
            <atom:updated>2018-08-08T00:54:45.228Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*-V_iXH1IlYwkIM_0r9q5sw.jpeg" /><figcaption>Machine plays Rivals</figcaption></figure><h4><strong><em>Introduction:</em></strong></h4><p>The overall goal of this project is to explore general artificial intelligence. When considering environments to employ AI models into, games are an obvious choice. I have personally chosen Need for Speed Rivals to begin with as I was looking for anarchic environment .The NFS Rivals is more visually impressive with better texture detail, improved lighting on cars and sharper graphics overall. One may also try other games that features realistic graphics and has a control over weather and time ,otherwise the CNN won’t recognize any useful features in this image.</p><p>With this method, since I also think we can teach an AI to play games by simply showing it how to play for a bit, using Convolutional Neural Network on that information, and then letting the AI poke around. To begin with , however, a more simple task with a definitive objective should be chosen. The starting task that I have chosen is to create a self-driving car.</p><p>While most actual self-driving cars use all sorts of sensors, such as LIDAR, my plans here are to only feed the neural network visual data. It’s understandable that real self-driving cars use everything to their advantage, lives are on the line and they want the best, safest product.</p><p>In this project, and game world, my personal goal is not to solve the challenge of self driving cars with some sort of a la carte solution specific to cars, it is to come up with a model that can be used to drive a car.</p><p>I may add more “senses” besides visual senses in the future, but for now, I am trying to see just how far we can get with vision alone.</p><h4>Objectives:</h4><ol><li>At this stage, my only objective is to see if I can just make a car drive at all with just visual inputs. If successful here, I will begin to fine-tune the objectives.</li><li>The goal is not to create a civil self-driving car. This is NFS after all. To me, the overall objective of the Agent is to drive as fast as possible while staying on road ,avoiding obstacles.</li></ol><h4>How I made the model:</h4><p>Accessing frames from the screen at decent FPS and mimic the key presses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/662/1*GB08Asi3szXlY8cWqEHL6g.png" /></figure><p>But building this system presents 2 major difficulties. First, both the game and the neural networks are GPU intensive processes. Running both on a single machine would require a lot of computational power. So one should take care of that.</p><p>Reading the game frames with Open CV:-After a lot of search, I found something that was suggested by Harrison and this gives me about ~20 Fps.</p><ol><li>Grabscreen.py</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/1*qSflkJWYMmu3A15f6kIOUg.png" /></figure><p>2. Getkeys.py</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/493/1*ANr3zYC1bwT6EPGw1OPOyA.png" /></figure><p>3.Direct Keys.py</p><p>The game expects the direct input keypresses .The code below provided saves W,S,A and D as constants, so we can easily call them later. DirectX codes for the keys:</p><p>W = 0x11; A = 0x1E; S = 0x1F;D = 0x20</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*fWxTYsYLCb0riDU_MgoPzg.png" /></figure><h4>Training Data:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*y18Iu39QTeQgNhwQ2Ac7ZA.png" /></figure><p>The training data will be saved in .npy files. Screenshots will be saved in X.npy and encode movements in Y.npy</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/477/1*lFSn3E9YZ0rzPVdkIdYsJQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*9306jnnqEUgTOuUBZqrNEQ.png" /></figure><blockquote><strong><em>Architecture 1 </em>:</strong></blockquote><ol><li>To begin, it’s fairly obvious that a Convolutional Neural Network should be used. Common sense would also suggest that we would need some sort of memory, whether that’s in the form of recurrent layers, or something else entirely. However, I decided to keep it simple and just do a ConvNet. At first, I tried a simple 2 and 4 layer ConvNet to no real success, as the models just simply wouldn’t train. I then grabbed AlexNet and tried that. It seemed to at least train, but plateaued too early. I finally ended on a modified AlexNet, which has many more layers.</li><li>Input: 120 x 80 grayscale pixel data from the game.</li><li>Output: Key presses in the form of W, S, A, or D.</li></ol><h4>Code:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*BX8edg9-sKNqY9Zbd35Bhg.png" /><figcaption>Modified Alex net</figcaption></figure><h4>Problems faced:</h4><ol><li>The weather is dynamic in NFS Rivals as heavy rain, sudden squalls and blinding early-morning sunshine ,so grey frames won’t help.</li><li>Every frame is mapped to the key presses W,A,S,D only. This created problems at both smoother and sharper turns.</li><li>The data was collected in third person view which created completely new issue.</li></ol><blockquote><strong><em>Architecture 2:</em></strong></blockquote><p>The main motivation to change the architecture was that we need a lot of training data to train on Alex net which was hectic. Also the results with this below architecture are quite similar with less amount of training data and less training time.</p><p>Another change here is, with the input layer, we now have 3 channels for R,G and B color values as it is expected that color will make feature creation and detection much easier for the network, resulting in more robust features as well as reducing mistakes in practice.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*tL9xSyYWG0nD8jUZQjA-tg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nBT4f0ax8mobeHOr2pWlQg.png" /><figcaption>Nvidia’s Self Driving Car Architecture and my modified bifocal architecture</figcaption></figure><p>Now first let me explain the <em>BIFOCAL</em> approach. Fancy name i know !!!</p><p>I split the image for the network to look at in cockpit view. It will not get the image at once put two halves of it. And the network will be forced to not focus on the car anymore and focus on entities like sidelines, markers, white tracks etc hopefully.</p><p>The idea of bifocal was to just have the screen split in two have two individual network decide which way to go the right or left. So one would specialize in right, the other one left, just like human eyes. Also that margin setting was for configuring how much of the portion of a scene does one side of the network sees.</p><p>First thing look at this article from <em>Nvidia </em><a href="https://devblogs.nvidia.com/deep-learning-self-driving-cars/"><em>End to end Deep learning Self Driving Car</em></a> I used their model for the convolution layers. But where they put the entire image through the sequential network ,I split the image in 50% from both side. i.e for a width of 400 ,i will have two image of 200 width .Then i took the output and simple concatenated then flattened and then dense layers. Also<em> key presses </em>in the form of W, S, A, or D, along with combinations like W+A, W+D, S+A, and S+D.</p><h4><em>Observations:</em></h4><p>Everything is improved. The driving data is biased, VERY MUCH BIASED and hence should be balanced. The agent still gets into a lot of crashes, and is nowhere near a decent driver, but still surprises me at times.</p><blockquote><strong>Architecture 3:</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PEyER6p6I8imMq81nkBFPA.png" /><figcaption>Inception V3 Net</figcaption></figure><p>Till now we have been juggling around the basic CNN architectures. Other alternative and more efficient advanced architectures was GoogLeNet with Inception Networks. I have used slightly modified version of Inception v3.</p><p><strong><em>Why Inception Net???</em></strong></p><p>Instead of deciding whether to use a 1x1 convolution, or a 3x3 or a 5x5 Convolution, or whether to use a Pooling layer — Why not use all of them?</p><ol><li>Until this point, the AI was trained to, in general, stay on the road and avoid obstacles, but never actually had a set objective. If the AI got into a collision, or bumped something, and the course was changed, then the AI would just simply look for a road again and continue a random journey.</li></ol><h4><em>Objectives:</em></h4><p>Increase the input resolution of the network to 480 x 270(common aspect ratio). Since at a lower resolution, it’s pretty challenging to read the game’s map and the task is to follow the map routes to various way points, like a GPS guidance system.Here’s an image comparing the resolutions:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/120/1*I-6m35mCcK3gyY4hX2Tjkg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*04bZhFA4qyy4gjYp2gmMsg.jpeg" /><figcaption>120x80 and 480x270</figcaption></figure><h4><strong><em>Training:</em></strong></h4><p>Training Data consists of 40,000 labelled color frames(This translates to around 5–6 hours of game play).</p><h4>Code:</h4><p>To implement this, I have used <strong><em>Transfer learning</em></strong><em> in keras</em> and the concept of fine tuning the top layers of a pre-trained network .The idea of<em> </em><strong><em>fine tuning</em></strong> basically was to freeze a bunch of layers i.e weights and kernels won’t change and to modify the last softmax layer of the network.Ensure that the learning rate was small.</p><p>One may also try just by ignoring the pre-existing models ,retrain the network from the scratch which is really expensive and it takes weeks together to train the neural network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*w3_qtaL3c7OeNcacDppkPQ.png" /><figcaption>InceptionV3 in Keras</figcaption></figure><p><strong><em>Output result:(Without giving way point )</em></strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F279828315&amp;dntp=1&amp;src_secure=1&amp;url=https%3A%2F%2Fvimeo.com%2Fuser87260725%2Freview%2F279828315%2F3d4f0b4f00&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Ffilter%2Foverlay%3Fsrc0%3Dhttps%253A%252F%252Fi.vimeocdn.com%252Fvideo%252F713147876_1280x720.jpg%26src1%3Dhttps%253A%252F%252Ff.vimeocdn.com%252Fimages_v6%252Fshare%252Fplay_icon_overlay.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="1280" height="720" frameborder="0" scrolling="no"><a href="https://medium.com/media/856aa02d223d497ae9e1627bef93f51e/href">https://medium.com/media/856aa02d223d497ae9e1627bef93f51e/href</a></iframe><p><strong><em>Output Result : (A Rainy night is no trouble for our AI)</em></strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F280488471%3Fapp_id%3D122963&amp;dntp=1&amp;url=https%3A%2F%2Fvimeo.com%2F280488471&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F714006244_640.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="640" height="360" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2e27f27d326604e32ccd96fdef2612b/href">https://medium.com/media/c2e27f27d326604e32ccd96fdef2612b/href</a></iframe><p><strong><em>Output Result:(AI in the dark):</em></strong></p><p>Our agent, will often try to get back on track, and is often successful</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F280487583%3Fapp_id%3D122963&amp;dntp=1&amp;url=https%3A%2F%2Fvimeo.com%2F280487583&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F714005235_1280.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="1280" height="720" frameborder="0" scrolling="no"><a href="https://medium.com/media/6105025639b25dfa85c9c660c9b821a1/href">https://medium.com/media/6105025639b25dfa85c9c660c9b821a1/href</a></iframe><p><strong><em>Output Result:(Given a way point)</em></strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F280489150%3Fapp_id%3D122963&amp;dntp=1&amp;url=https%3A%2F%2Fvimeo.com%2F280489150&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F714007117_1280.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="1280" height="720" frameborder="0" scrolling="no"><a href="https://medium.com/media/7249b8a4a3e880b48c2bd39d4b214be1/href">https://medium.com/media/7249b8a4a3e880b48c2bd39d4b214be1/href</a></iframe><p>AI also learns how to play in night time and when it rains. It was not at all hitting the cars for sure!!!</p><p>This continues to exceed my expectations.</p><p>Our Self Driving AI can play continuously for about 10–20 min. But I am looking to make it play for about 1 hr so that it can compete with the game controlled cars.</p><h4><em>Future Considerations:</em></h4><ol><li>Memory in some form is still important, but I have yet to find something that outperforms these architectures. One may even try LSTM’s- guess the idea of remembering past frames is a valid one.</li><li>Reinforcement learning, though of course the same rule for memory applies here.</li><li>Granular control is another area of interest, that performs better than the keyboard model.</li></ol><p>We are living in exciting times. Deep learning models can enable everyone to make novel combinations of algorithms and data to create applications that were not possible before. Imagination and experimentation are the fuel that will create intelligence that we have never seen before!!!</p><h4>References:</h4><ol><li><a href="https://sites.google.com/a/princeton.edu/arturf/self-driving-and-gta5">https://sites.google.com/a/princeton.edu/arturf/self-driving-and-gta5</a></li><li><a href="https://github.com/llSourcell/How_to_simulate_a_self_driving_car">https://github.com/llSourcell/How_to_simulate_a_self_driving_car</a></li><li><a href="https://github.com/rajatthepagal">https://github.com/rajat</a></li><li><a href="https://github.com/rggasoto/DriveGG">https://github.com/rggasoto/DriveGG</a></li><li><a href="https://github.com/sentdex">https://github.com/sentdex</a></li></ol><h4>More where this came from</h4><p>This story is published in <a href="http://blog.usejournal.com/">Noteworthy</a>, where thousands come every day to learn about the people &amp; ideas shaping the products we love.</p><p>Follow our publication to see more product &amp; design stories featured by the <a href="https://usejournal.com/?utm_source=usejournal.com&amp;utm_medium=blog&amp;utm_campaign=guest_post">Journal</a> team.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56d1d2fcda3e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>