<?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 Ivan Kazakov on Medium]]></title>
        <description><![CDATA[Stories by Ivan Kazakov on Medium]]></description>
        <link>https://medium.com/@ivankazakov?source=rss-e00831d2bd09------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*KtUWVqP4Lx2UHYF6.</url>
            <title>Stories by Ivan Kazakov on Medium</title>
            <link>https://medium.com/@ivankazakov?source=rss-e00831d2bd09------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 21:58:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ivankazakov/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[Vehicle Detection and Tracking]]></title>
            <link>https://medium.com/data-science/vehicle-detection-and-tracking-44b851d70508?source=rss-e00831d2bd09------2</link>
            <guid isPermaLink="false">https://medium.com/p/44b851d70508</guid>
            <category><![CDATA[self-driving-cars]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[towards-data-science]]></category>
            <category><![CDATA[autonomous-vehicles]]></category>
            <category><![CDATA[udacity]]></category>
            <dc:creator><![CDATA[Ivan Kazakov]]></dc:creator>
            <pubDate>Sun, 14 May 2017 15:17:49 GMT</pubDate>
            <atom:updated>2017-05-29T09:33:53.223Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><a href="https://youtu.be/zIwLWfaAg-8?t=14m48s">“You can absolutely be super-human with just cameras”</a>. Elon Musk at TED.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*92cbnEUUwABYUiw-ItGfbQ.png" /></figure><p>This is the <a href="https://www.udacity.com/drive"><strong>Udacity’s Self-Driving Car Engineer Nanodegree Program</strong></a> final project for the 1st Term. Source code and a more technically elaborated writeup are available on <a href="https://github.com/antevis/CarND-Project5-Vehicle_Detection_and_Tracking"><strong>GitHub</strong></a></p><h4><strong>The Goal</strong></h4><p><strong><em>To write a software pipeline to identify vehicles in a video from a front-facing camera on a car.</em></strong></p><p>In my implementation, I used a Deep Learning approach to image recognition. Specifically, I leveraged the extraordinary power of Convolutional Neural Networks (CNNs) to recognize images.</p><p>However, the task at hand is not just to detect a vehicle’s presence, but rather to point to its location. It turns out CNNs are suitable for these type of problems as well. There is a <a href="https://youtu.be/wFG_JMQ6_Sk?list=PLLvH2FwAQhnpj1WEB-jHmPuUeQ8mX-XXG"><strong>lecture</strong></a> in <a href="http://cs231n.github.io/"><strong>CS231n Course</strong></a> dedicated specifically to localization, and the principle I’ve employed in my solution basically reflects the idea of a region proposal discussed in that lecture and implemented in the architectures such as <a href="https://arxiv.org/abs/1506.01497"><strong>Faster R-CNN</strong></a>.</p><p>The main idea is that since there is a binary classification problem (vehicle/non-vehicle), a model could be constructed in such a way that it would have an input size of a small training sample (e.g., 64x64) and a single-feature convolutional layer of 1x1 at the top, which output could be used as a probability value for classification.</p><p>Having trained this type of a model, the input’s width and height dimensions can be expanded arbitrarily, transforming the output layer’s dimensions from 1x1 to a <strong>map</strong> with an aspect ratio approximately matching that of a new large input.</p><p>Essentially, this would be somewhat equal to:</p><ol><li>Cutting new large input image into squares of the models’ initial input size (e.g., 64x64)</li><li>Detecting the subject in each of those squares</li><li>Stitching the resulting detections back, preserving the same order as the corresponding squares in the source input into a <strong>map</strong> with the aspect ratio of the sides approximately matching that of a new large input image.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kkgZZ-Tn5RX0imD3aKKlvQ.png" /><figcaption>Consider each of these squares to be processed individually by its own dedicated CNN, producing a 4x20 detection map</figcaption></figure><h4>Data</h4><p>Udacity equips students with the great resources for training the classifier. <a href="https://s3.amazonaws.com/udacity-sdc/Vehicle_Tracking/vehicles.zip"><strong>Vehicles</strong></a> and <a href="https://s3.amazonaws.com/udacity-sdc/Vehicle_Tracking/non-vehicles.zip"><strong>non-vehicles</strong></a> samples of the <a href="http://www.cvlibs.net/datasets/kitti/"><strong>KITTI vision benchmark suite</strong></a> have been used for training.</p><p>The Final model had difficulties in detecting the white Lexus in the Project video, so I augmented the dataset with about 200 samples of it. Additionally, I used the same random image augmentation technique as in <a href="https://github.com/antevis/CarND-Project2-Traffic-signs-classifier"><strong>Project 2 for Traffic Signs Classification</strong></a>, yielding about 1500 images of vehicles from the Project video. The total number of vehicle’s images used for training, validation, and testing was about 7500. Obviously each sample had been horizontally flipped, additionally inflating the dataset by a factor of 2. As a result, I had approximately 15000 data points.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/1*oqgTgfAfSRdo-hUTyHB0-Q.png" /><figcaption>Class IDs are vehicles and non-vehicles</figcaption></figure><p>An equal number of non-vehicle images has been added as negative examples.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jzOrbyN5THJe4dbzxvv7zA.png" /><figcaption>Typical vehicle and non-vehicle samples with their corresponding labels</figcaption></figure><h4>Model</h4><p>I borrowed the technique of constructing the top of the network from <a href="https://github.com/maxritter/SDC-Vehicle-Lane-Detection">the implementation of <strong>Max Ritter</strong></a>, who apparently employed the same approach.</p><p>A lot of model architectures with varying complexity have been tested to derive a final model.</p><p>I started with a transfer learning from <a href="http://www.robots.ox.ac.uk/%7Evgg/research/very_deep/"><strong>VGG16</strong></a> architecture with weights trained on <a href="http://www.image-net.org/"><strong>ImageNet</strong></a>. VGG is a great and well-tested architecture, and ImageNet weights apparently assume that it should have an idea of the vehicle’s features. I added my top <em>single-feature binary classifier</em> and fine-tuned the model. As expected, it yielded a pretty high test accuracy of about <strong>99,5%</strong>. The flip side with the VGG is that it is rather complex, making predictions computationally heavy.</p><p>I then tested some custom CNN configurations of varying number of layers and shapes, incrementally reducing complexity and evaluating test accuracy, and finally arrived at the model with as little as about <strong>28,000 trainable parameters</strong> with test accuracy of still about <strong>99.4%</strong>:</p><pre>Epoch 5/5<br>607/607 [==============================] - 48s - loss: 0.0063 - acc: 0.9923 - val_loss: 0.0073 - val_acc: 0.9926<br><br>Evaluating accuracy on test set.<br>test accuracy:  [0.0065823850340600764, 0.99373970345963758]</pre><p>Reducing the model’s complexity to the extreme is beneficial for both the <strong>computational cost</strong> of predictions and <strong>overfitting</strong>. Although the dataset may seem not too big, it’s hard to assume that the model of 28000 parameters may be able to memorize it. Additionally, I’ve aggressively employed <strong>Dropout</strong> to further mitigate the risk of overfitting.</p><p>The model has been implemented and trained using <a href="https://keras.io/"><strong>Keras</strong></a> with <a href="https://www.tensorflow.org/"><strong>TensorFlow</strong></a> backend.</p><p>Sample predictions results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/794/1*yKNXd4ixRf4Nv1S8-ECRwA.png" /></figure><h4>Using Trained Model for Vehicle Detection</h4><p>Original frame from the video stream looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*1v9U-qzGnSwfbCVLbp262g.png" /></figure><p>It is not strictly original, as it has already been subjected to undistortion, but that deserves a story of its <a href="https://github.com/antevis/CarND-Project4-Advanced_Lane_Finding/blob/master/writeup.md#undistorted-image">own</a>. For the task at hand, this is the image to be processed by the vehicle detection pipeline.</p><p>The region of interest for the vehicle detection starts at an approximately <strong>400th </strong>pixel from the top and spans vertically for about <strong>260</strong> pixels. Thus, we have a region of interest with the dimensions of <strong>260x1280</strong>, starting at <strong>400th</strong> pixel vertically.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*-InHI1Mc-lamry5ujrguQw.png" /></figure><p>This transforms the dimensionality of the top convolutional layer from <strong>(?,1,1,1) </strong>to <strong>(?,25,153,1)</strong>, where <strong>25 </strong>and<strong> 153</strong> are the <strong>height</strong> and <strong>width</strong> dimensions of a miniature <strong>map</strong> of predictions that, in turn, will ultimately be projected to the original high-resolution image.</p><p><strong>The vehicle scanning pipeline consists the following steps:</strong></p><ol><li>Obtain the region of interest (see above)</li></ol><p><strong>2. </strong>Produce the <strong>detection map </strong>using<strong> </strong>trained CNN model:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*w-uFNEXeAWdOVEF0Iqkh7g.png" /></figure><p><strong>3. </strong>Apply the confidence threshold to generate the binary map:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*90nzF1EP8Kc33TG_tIOtfw.png" /></figure><p>The predictions are <strong>very</strong> polarized, that is, they mostly stick to <strong>Ones</strong> and <strong>Zeros</strong> for vehicles and non-vehicle points. Therefore, even the midpoint of <strong>0.5</strong> for a confidence threshold might be a reliable choice. Just to be on safe side, I stick with 0<strong>.7</strong></p><p><strong>4. Label</strong> the obtained detection areas with the label() function of the scipy.ndimage.measurements package. This step allows outlining the boundaries of labels that will, in turn, help to keep each detected ‘island’ within its feature label&#39;s bounds when building the Heat Map.</p><blockquote><strong><em>This is also the first approximation of detected vehicles</em></strong><em>.</em></blockquote><p><strong>5. </strong>Project those featured labels of detection points to the coordinate space of the original image, transforming each point into a 64x64 square and keeping those squares within the features’ area bounds.</p><p>Just to illustrate the result of this points-to-squares transformation projected onto the original image:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*rseJQ3i-UiJMLQjOGl4UjQ.png" /></figure><p><strong>6. </strong>Create the <strong>Heat Map</strong>. Overlapping squares from the image above are essentially building-up the ‘heat’.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*dnxVy_OwJfwCscZqdEAKww.png" /></figure><p><strong>7. </strong>Label<strong> The Heat Map</strong> again, producing the final ‘islands’ for actual vehicles’ bounding boxes. Labeling of this particular heat map creates <strong>2</strong> ‘islands’ of detections. Obviously.</p><p><strong>8. </strong>Save labeled features of the <strong>Heat Map</strong> to the list of labels, where they would be kept for a certain number of consequent frames.</p><p><strong>9. </strong>The final step is getting the actual bounding boxes for the vehicles. <a href="http://opencv.org/"><strong>OpenCV</strong></a> provides the handy function cv2.groupRectangles(). As said in <a href="http://docs.opencv.org/3.0-beta/modules/objdetect/doc/cascade_classification.html?highlight=cv2.grouprectangles#cv2.groupRectangles">docs</a>: &quot;It clusters all the input rectangles using the rectangle equivalence criteria that combines rectangles with similar sizes and similar locations.&quot; Exactly what is needed. The function has a groupThreshold parameter responsible for &quot;<em>Minimum possible number of rectangles minus 1</em>&quot;. That is, it won&#39;t produce any result until the history accumulates bounding boxes from at least that number of frames.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*hmkm5e16IjHQkuifKbJmJw.png" /></figure><h4>Video implementation</h4><p>I’ve merged <strong>Vehicle</strong> and <a href="https://github.com/antevis/CarND-Project4-Advanced_Lane_Finding"><strong>Lane detections</strong></a> into a single pipeline to generate a combined footage with both the lane projection and vehicles bounding boxes.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FNXgV5hrHO2o%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DNXgV5hrHO2o&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FNXgV5hrHO2o%2Fhqdefault.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/a594b016db5f9ffa870cea6049693a83/href">https://medium.com/media/a594b016db5f9ffa870cea6049693a83/href</a></iframe><h4>Reflections</h4><p>I thoroughly studied the approach of applying <strong>SVM</strong> classifier to <strong>HOG</strong> features covered it the Project lessons, but still pretty confident that Deep Learning approach is way more suitable for the task of Vehicle detection. In a CS231n lecture that I referred to at the beginning, the <strong>HOGs</strong> are viewed only from a historical perspective. Furthermore, there is a <a href="https://arxiv.org/abs/1409.5403"><strong>paper</strong></a> which argues that the <strong>DPMs</strong> (those are based on <strong>HOGs</strong>) might be considered as a certain type of Convolutional Neural Networks.</p><p>It took some time figuring out how to derive a model that would produce the detection map of a reliable resolution when expanding it to accept the input image of a full-size region of interest.</p><p>Even the tiny model that I’ve finally picked takes about 0.75 seconds to produce a detection map for 260x1280 input image on a Mid-2014 3GHz quad-core i7 MacBook Pro. That is 1.33 frames per second.</p><h4><strong>Acknowledgements</strong></h4><p>I’d like to personally thank <a href="https://medium.com/u/8190c86ea791">David Silver</a> and <a href="https://medium.com/u/fd8bc37755d8">Oliver Cameron</a> for the great content they have developed for the Udacity’s Self-Driving Cars Nanodegree program, as well as all the Udacity team for the exceptional learning experience they provide.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=44b851d70508" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/vehicle-detection-and-tracking-44b851d70508">Vehicle Detection and Tracking</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>