<?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 Armaan Sandhu on Medium]]></title>
        <description><![CDATA[Stories by Armaan Sandhu on Medium]]></description>
        <link>https://medium.com/@armaan.sandhu.2002?source=rss-9f7811932a9b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*8Rdwe6T3G6KqOHeB_bwi0A.jpeg</url>
            <title>Stories by Armaan Sandhu on Medium</title>
            <link>https://medium.com/@armaan.sandhu.2002?source=rss-9f7811932a9b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 16:32:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@armaan.sandhu.2002/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[Training YOLOv7 to detect vehicle braking]]></title>
            <link>https://medium.com/@armaan.sandhu.2002/training-yolov7-to-detect-vehicle-braking-e8e7e9db1b3b?source=rss-9f7811932a9b------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8e7e9db1b3b</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[yolov7]]></category>
            <category><![CDATA[yolo]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[object-detection]]></category>
            <dc:creator><![CDATA[Armaan Sandhu]]></dc:creator>
            <pubDate>Mon, 15 Aug 2022 04:21:55 GMT</pubDate>
            <atom:updated>2022-08-15T16:06:56.857Z</atom:updated>
            <content:encoded><![CDATA[<h4>Vehicle brake light status detection with YOLOv7 for ADAS / Autonomous Vehicle preemptive braking</h4><figure><img alt="Braking detection in video" src="https://cdn-images-1.medium.com/max/480/1*3kLyBNvv_Hgrk2QdI4MvAw.gif" /><figcaption>source: <a href="https://github.com/ArmaanSinghSandhu">https://github.com/ArmaanSinghSandhu</a></figcaption></figure><p>In this article, I am documenting and demonstrating the procedures and steps taken by me to build this project. The procedures followed might not be the most optimal but were helpful in bringing the project to quick implementation with acceptable results.</p><p>The aim of this project is to aid the ADAS / Autonomous Vehicles in predicting and preparing for braking even before the leading vehicle speed changes are detected by the radar. Even placing the foot on the brake pedal will convey the leading driver’s intent to brake, allowing the following vehicle to recalibrate and readjust it’s distance and speed even before the leading vehicle actually starts slowing down. This might be helpful to heavy vehicles such as trucks with long stopping distances and might prevent accidents that occur from sudden hard braking.</p><p>The details for setup, training and evaluation can be found in the main Google Colab file <a href="https://colab.research.google.com/drive/1uxMoyABEvXf6MOd2j0zMawT4u_xrbEKz?usp=sharing">YOLOv7 Braking Detection.ipynb</a> . The code and details for dataset preprocessing and augmentation can be found on the <a href="https://colab.research.google.com/drive/1HMgSZYyB3FT-9LmTVFBHszsHEg7h-Dk8?usp=sharing">Dataset Prep.ipynb</a></p><h3>Contents:</h3><ol><li><a href="#a66b">YOLOv7</a></li><li><a href="#3a49">Preparing the dataset</a></li><li><a href="#bf2a">Environment Setup</a></li><li><a href="#f5c1">Training</a></li><li><a href="#0b0c">Inference</a></li><li><a href="#5d5a">Future Enhancements</a></li><li><a href="#d93d">Credits</a></li></ol><h3>1. YOLOv7</h3><p>YOLO, also known as “You Only Look Once” is a popular real-time object detection algorithm. According to <a href="https://arxiv.org/abs/2207.02696">this</a> paper, it is the latest and highest performing iteration of YOLO so far. This iteration of YOLO has made improvements in areas like Model re-parameterization, Model Scaling, E-ELAN, and Planned re-parameterized convolution.</p><h3>2. Preparing the dataset</h3><h4>2.a Planning</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*6Np8Igtexaffx5agVv30FA.jpeg" /><figcaption>source: <a href="https://github.com/ArmaanSinghSandhu">https://github.com/ArmaanSinghSandhu</a></figcaption></figure><p>A thorough study and planning regarding the project requirements and the desired targets or outcomes is always required in advance. For this project, the ROI (region of interest) or the focus area needed to be worked out first. The dimensions of the ROI can be worked out, as shown in the image above. For example, its width can be decided by how many lanes, other than the current one, need to be included for detection and its depth by how far the detections need to be made from. This information can then help select and preprocess the dataset for optimal training and results. The view in the background images needed to be wide enough to match the width of the perceived ROI. The foreground images can then be overlayed on the background images at random positions along the x-axis within the perceived ROI width to create training images matching the real-world scenes, which the model will need to perform the detection on. Similarly, for the ROI depth, the foreground images of the sizes ranging between the sizes of the nearest and the farthest images (orange and blue boxes in the above image) might be selected. The resolution of the foreground and background images can determine if the smallest of these images contain enough information for the model to accurately infer the status of the vehicle’s tail lights. These images can then be overlayed on the background on the y axis according to their size (smaller images are placed higher along the y axis) to achieve the depth effect (z-axis). For a visual detection problem limited to a region in the field of vision, a training dataset focused around that ROI might be selected for efficient training and optimal results.</p><h4>2.b Dataset gathering and filtering</h4><p>The dataset for this project comprises of the rear signal dataset available <a href="http://vllab1.ucmerced.edu/~hhsu22/rear_signal/rear_signal">here</a>. This dataset contains cropped images of vehicle tail lights in various stages and combinations of braking and turning from a <a href="https://drive.google.com/file/d/13bCTSnB-29U83QgmLWihMXlqErMmJy-E/view">research</a> using CNN-LTSM model to classify vehicle tail lights. While the dataset is optimal for use with a classification model, it was decided to augment these images with some background images and then label them with the YOLO format annotations. For the background images, the GTSDB dataset was used.</p><p>Both the foreground and background images were manually selected by sifting through their datasets. The foreground images were filtered based on vehicle diversity, sizes, angles, lighting, resolution, miscategorizations, and duplicates. While the backgrounds were filtered based on lighting, diversity, shots of long &amp; open roads/highways, minimal to no leading traffic, etc. The vehicle images were then sorted into two main categories:</p><ol><li>All images with braking lights on (Braking, Braking &amp; Turning Right, Braking &amp; Turning Left) were combined under the ‘Braking’ category.</li><li>All images with braking lights off (All lights off, Turning Left, Turning Right) were combined under the ‘Normal’ category.</li></ol><h4>2.c Organizing dataset</h4><p>The steps for dataset structuring, preprocessing, augmentation and splitting were automated by a custom code detailed in this colab <a href="https://colab.research.google.com/drive/1HMgSZYyB3FT-9LmTVFBHszsHEg7h-Dk8?usp=sharing">notebook</a>. The script generates a directory structure within the ‘yolov7’ folder for the downloaded background and foreground images with folders bearing the name same as the predefined object classes while also creating folders for the backgrounds, merged images, and labels. It also creates a dataset folder with subfolders for training, validation, and testing, while each of these folders having their own subfolders for images and labels. The folder names and paths are predefined in the code and can be customized by changing the variables. The manually categorized and filtered foreground and background images are then uploaded to their newly created respective folders. The dataset directory structure for this project is shown in the following image:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/283/1*TMbzScK92OAsZeV4-rK2mg.jpeg" /><figcaption>source: <a href="https://github.com/ArmaanSinghSandhu">https://github.com/ArmaanSinghSandhu</a></figcaption></figure><h4>2.d Pre-processing</h4><p>The script then merges vehicle images with the background images and generates bounding box labels for the vehicles in the YOLO format. Keeping the number of images from each class equal, the vehicle images and background images were randomly selected and merged. Before merging, the background images were squared for YOLO training input optimization. Each foreground image is then positioned vertically on the background according to the foreground image size to achieve a depth effect as in the real world, while the horizontal position is randomly selected. The placement within bounds and predefined margins is ensured. Levels of margins, adjustment factors, and randomization can be tweaked for optimal results. A manual sifting of the merged images was then undertaken to remove images where the combination looked unrealistic and could negatively impact training. A ‘.txt’ bounding box label file in the YOLO format, bearing the same name as the image file is generated from the position of the foreground image in the background image.</p><h4>2.e Splitting the dataset</h4><p>The generated dataset of merged images and labels is then automatically and randomly split according to a split ratio preset in the code while ensuring the class equality in each split. The split data is copied into their respective folders under the ‘dataset’ folder.</p><h4>2.f Generating the data YAML file</h4><p>The script automatically generates a .yaml file containing the paths to the dataset training, validation, and testing folders along with the total number of classes and class names. The file is then saved in the ‘data’ folder inside the yolov7 folder. The file for this project was generated with the following data:</p><pre>train: ./dataset/train</pre><pre>val: ./dataset/valid</pre><pre>test: ./dataset/test</pre><pre>nc: 2</pre><pre>names: [&#39;Normal&#39;, &#39;Braking&#39;]</pre><h3>3. Environment setup</h3><p>All the steps from this step onwards are detailed in this colab <a href="https://colab.research.google.com/drive/1uxMoyABEvXf6MOd2j0zMawT4u_xrbEKz?usp=sharing">notebook</a>.</p><h4>3.a Mount Google Drive</h4><p>Mounted the google drive using the following code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*eryn3DaSDzQe4BtSFUOwWg.png" /><figcaption>source: <a href="https://github.com/ArmaanSinghSandhu">https://github.com/ArmaanSinghSandhu</a></figcaption></figure><h4>3.b Download YOLOv7 repository</h4><p>Downloaded the official YOLOv7 repository using the following code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*PqWIfi13lmtPOPYUaLL00g.png" /><figcaption>source: <a href="https://github.com/ArmaanSinghSandhu">https://github.com/ArmaanSinghSandhu</a></figcaption></figure><h4>3.c Install YOLOv7 dependencies</h4><p>Installed the YOLOv7 dependencies using the following commands:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*KoVWkh75OqS50HB9BrZ5cg.png" /><figcaption>source: <a href="https://github.com/ArmaanSinghSandhu">https://github.com/ArmaanSinghSandhu</a></figcaption></figure><h4>3.d Download the pre-trained model</h4><p>Downloaded the pre-trained model from the YOLOv7 repository using the following code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*w4aAqX_jM3RccSCV4aNOeA.png" /><figcaption>source: <a href="https://github.com/ArmaanSinghSandhu">https://github.com/ArmaanSinghSandhu</a></figcaption></figure><h3>4. Training</h3><h4>4.a Run training</h4><p>The training process uses YOLOv7 pre-trained weights <a href="https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt">downloaded</a> from the official YOLO repository. Details about all the train.py command line arguments can be found <a href="https://github.com/WongKinYiu/yolov7/blob/6ded32cc8d0ab40ea51f385876c143011ec69197/train.py#L526">here</a>. Few of the ones used here are:</p><p><strong>— weights</strong>: path to the pre-trained weights downloaded earlier</p><p><strong>— data</strong>: path to the yaml file containing details about dataset path and object classes.</p><p><strong>— batch-size</strong>: The number of subsets/parts the dataset is divided into to pass through the network per epoch. Depends on the size of the dataset and the GPU memory.</p><p><strong>— device</strong>: Processing hardware, 0 for single GPU, 0,1,.. for multiple parallel GPUs, CPU for CPU.</p><p><strong>— img-size</strong>: Image size for training.</p><p><strong>— cfg</strong>: Path to the yaml config file appropriate to the training model</p><p><strong>— epochs</strong>: Number of training epochs</p><p>The following command was used to run the training:</p><pre>!python train.py --weights yolov7.pt --data data/custom.yaml --batch-size 8 --device 0 --img-size 800 --cfg cfg/training/yolov7.yaml --epochs 40</pre><h4>4.b Training results</h4><p>Best results were observed after training for 40 epochs as further training led to overfitting and increase in objectness losses. The following are the training results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tu8SleHa2gNlrB0Vj14sXw.jpeg" /><figcaption>source: <a href="https://github.com/ArmaanSinghSandhu">https://github.com/ArmaanSinghSandhu</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5sahZ9BZWaO_xQCSWv0U6A.jpeg" /><figcaption>source: <a href="https://github.com/ArmaanSinghSandhu">https://github.com/ArmaanSinghSandhu</a></figcaption></figure><p>As we can see, the model’s mAP@0.5 is 0.995.</p><h3>5. Inference</h3><p>Inference is performed on images and videos using the weights with the best results of all the training epochs and a 50% confidence threshold. Since we are focusing on the detection of braking instances only, the ‘Normal’ (or non-braking) class has been excluded from the video detection to increase inference speed and reduce clutter, while both the classes were detected on image inputs. Details about all the detect.py command line arguments can be found <a href="https://github.com/WongKinYiu/yolov7/blob/main/detect.py#L166">here</a>.</p><p>The following command was used to run inference on the inputs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/825/1*1RRN8xYUbbfOb4L06RSlAg.png" /><figcaption>source: <a href="https://github.com/ArmaanSinghSandhu">https://github.com/ArmaanSinghSandhu</a></figcaption></figure><h4>Inference results</h4><p>Videos and images sourced from the internet were used to run inference on using the weights with best results of all the training epochs. As previously mentioned, the inference on the videos was run while excluding the ‘Normal’ class since the focus was mainly on the detection of the braking instances only. While the images were detected for both the class instances. The videos were split according to the normal weather and poor weather recording conditions. The results are as follows:</p><p><strong>Detection performed on clips recorded in normal weather conditions:</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FzWGofdE0gsw&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DzWGofdE0gsw&amp;image=http%3A%2F%2Fi.ytimg.com%2Fvi%2FzWGofdE0gsw%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b0193ab9adfa9294bb2311274acee24/href">https://medium.com/media/8b0193ab9adfa9294bb2311274acee24/href</a></iframe><p><strong>Detection performed on clips recorded in poor weather conditions:</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FiL30PDUZy3I%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DiL30PDUZy3I&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FiL30PDUZy3I%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7a271f1a1f98d55346861879f27b691/href">https://medium.com/media/e7a271f1a1f98d55346861879f27b691/href</a></iframe><p><strong>Output images from model inference:</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fdocs.google.com%2Fpresentation%2Fembed%3Fid%3D1X5t4wSMV6vGGi-UicdLAjbwXywVXehpZBLE86NquFBM%26size%3Dl&amp;display_name=Google+Docs&amp;url=https%3A%2F%2Fdocs.google.com%2Fpresentation%2Fd%2F1X5t4wSMV6vGGi-UicdLAjbwXywVXehpZBLE86NquFBM%2Fedit%3Fusp%3Dsharing&amp;image=https%3A%2F%2Flh3.googleusercontent.com%2F0bpezdnCsULhzN_imbo8MsESj2JtcYLLzy3aKY26S14C00Hsm8oWW-Me3KEm_cg-uv7rIcFeAlYJuA%3Dw1200-h630-p&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=google" width="700" height="559" frameborder="0" scrolling="no"><a href="https://medium.com/media/29b4feb0848aca4b1beadac27f279dbe/href">https://medium.com/media/29b4feb0848aca4b1beadac27f279dbe/href</a></iframe><h3>6. <strong>Future enhancements</strong></h3><p>The dataset and training needs to be enhanced to incorporate other vehicle classes such as bikes and heavy vehicles and various scenarios such as night driving, low lighting conditions, and glares and disruptions. Also, the detection of turn signals needs to be incorporated. The model performance on real-time data will also be tested. A lighter model could be trained and optimized for testing and deployment on edge devices. A future project might entail combining YOLO for detection and a classifier trained on this dataset for classification. The combination can also be used to generate an enhanced and annotated YOLO dataset.</p><h3>7. Credits</h3><ul><li>Backgrounds Dataset: Houben, S., Stallkamp, J., Salmen, J., Schlipsing, M., &amp; Igel, C. (2013, August). Detection of traffic signs in real-world images: The German Traffic Sign Detection Benchmark. In The 2013 international joint conference on neural networks (IJCNN) (pp. 1–8). Ieee. <a href="https://sid.erda.dk/public/archives/ff17dc924eba88d5d01a807357d6614c/published-archive.html">[link]</a></li><li>Rear Signal dataset: Hsu, H. K., Tsai, Y. H., Mei, X., Lee, K. H., Nagasaka, N., Prokhorov, D., &amp; Yang, M. H. (2017, October). Learning to tell brake and turn signals in videos using cnn-lstm structure. In 2017 IEEE 20th International Conference on Intelligent Transportation Systems (ITSC) (pp. 1–6). IEEE. <a href="http://vllab1.ucmerced.edu/~hhsu22/rear_signal/rear_signal">[Link]</a></li><li><a href="https://github.com/WongKinYiu/yolov7">YOLOv7 Official Repository</a></li><li><a href="https://github.com/ArmaanSinghSandhu">My Github</a></li><li><a href="https://www.linkedin.com/in/armaan-s-sandhu/">Connect with me on LinkedIn</a></li><li><a href="https://www.youtube.com/channel/UC2SckjYwSYGw2j3yp9ZakeA/videos">My YouTube</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8e7e9db1b3b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>