{"id":22546749,"url":"https://github.com/anshchoudhary/lane-detection-unet","last_synced_at":"2025-09-05T16:33:25.955Z","repository":{"id":248018491,"uuid":"827521846","full_name":"AnshChoudhary/Lane-Detection-UNet","owner":"AnshChoudhary","description":"This repository contains the implementation of a lane detection system using the UNet architecture. The model is trained on the BDD100K dataset, leveraging its diverse and large-scale data to ensure robust performance under various weather conditions and different times of day. ","archived":false,"fork":false,"pushed_at":"2024-07-23T08:32:49.000Z","size":30582,"stargazers_count":12,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-09T16:53:41.918Z","etag":null,"topics":["adas","autonomous-driving","bdd100k","deep-learning","lane-detection","machine-learning","opencv","pytorch","self-driving-car","torchvision","unet-image-segmentation"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AnshChoudhary.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-11T20:18:16.000Z","updated_at":"2025-02-18T03:21:07.000Z","dependencies_parsed_at":"2024-07-23T10:39:48.819Z","dependency_job_id":null,"html_url":"https://github.com/AnshChoudhary/Lane-Detection-UNet","commit_stats":null,"previous_names":["anshchoudhary/lane-detection-unet"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/AnshChoudhary/Lane-Detection-UNet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnshChoudhary%2FLane-Detection-UNet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnshChoudhary%2FLane-Detection-UNet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnshChoudhary%2FLane-Detection-UNet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnshChoudhary%2FLane-Detection-UNet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AnshChoudhary","download_url":"https://codeload.github.com/AnshChoudhary/Lane-Detection-UNet/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnshChoudhary%2FLane-Detection-UNet/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273783810,"owners_count":25167459,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-05T02:00:09.113Z","response_time":402,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["adas","autonomous-driving","bdd100k","deep-learning","lane-detection","machine-learning","opencv","pytorch","self-driving-car","torchvision","unet-image-segmentation"],"created_at":"2024-12-07T15:08:47.448Z","updated_at":"2025-09-05T16:33:25.905Z","avatar_url":"https://github.com/AnshChoudhary.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Lane Detection with UNet\n\n![Inference Videos](https://github.com/AnshChoudhary/Lane-Detection-UNet/blob/main/header1.gif)\n\nThis repository contains code for training and evaluating a UNet model for lane detection using the BDD100K dataset. The project leverages PyTorch for model implementation and training, and includes scripts for preprocessing data, running inference, and evaluating model performance.\n\n## Table of Contents\n\n- [Introduction](#introduction)\n- [Dataset](#dataset)\n- [Model Architecture](#model-architecture)\n- [Installation](#installation)\n- [Usage](#usage)\n  - [Training](#training)\n  - [Evaluation](#evaluation)\n  - [Inference](#inference)\n- [Results](#results)\n- [Streamlit App](#streamlit-app)\n- [Contributing](#contributing)\n- [License](#license)\n\n## Introduction\n\nLane detection is a crucial component of autonomous driving systems. This project implements a UNet model to accurately segment lane markings from images. The UNet architecture is well-suited for this task due to its encoder-decoder structure that captures contextual information at multiple scales.\n\n## Dataset\nOur lane detection model is trained on the BDD100K dataset, which is ideal for this task due to:\n\n- **Diversity**: It covers a wide range of driving scenarios, weather conditions, and times of day.\n- **Rich Annotations**: It includes detailed annotations for lane markings, drivable areas, and objects.\n- **Real-world Data**: Captured from real-world driving, ensuring the model generalizes well to actual driving conditions.\n- **High Quality**: Provides high-resolution images necessary for accurate detection.\n- **Community Support**: Widely used in the research community, providing benchmarks and continuous improvements.\n\nBy leveraging BDD100K, our model can perform lane detection effectively under various conditions, ensuring robust performance in all weather and lighting scenarios.\n- Download the dataset from [BDD100K website](https://bdd-data.berkeley.edu/)\n\n## Model Architecture\n\nThe UNet model is implemented with the following architecture:\n\n- **Encoder**: A series of convolutional layers followed by batch normalization and ReLU activation.\n- **Bottleneck**: A set of convolutional layers that capture the deepest features.\n- **Decoder**: A series of transposed convolutional layers that upsample the features back to the original image size.\n\n```python\nclass UNet(nn.Module):\n    def __init__(self, in_channels, out_channels):\n        super(UNet, self).__init__()\n\n        def CBR(in_channels, out_channels):\n            return nn.Sequential(\n                nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),\n                nn.BatchNorm2d(out_channels),\n                nn.ReLU(inplace=True),\n                nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1),\n                nn.BatchNorm2d(out_channels),\n                nn.ReLU(inplace=True)\n            )\n\n        self.enc1 = CBR(in_channels, 64)\n        self.enc2 = CBR(64, 128)\n        self.enc3 = CBR(128, 256)\n        self.enc4 = CBR(256, 512)\n        # Define other layers...\n\n    def forward(self, x):\n        # Implement forward pass...\n        pass\n```\n## Installation\n\n1. Clone the repository:\n```bash\ngit clone https://github.com/AnshChoudhary/Lane-Detection-UNet.git\ncd Lane-Detection-UNet\n```\n\n2. Create a virtual environment and activate it:\n```bash\npython -m venv venv\nsource venv/bin/activate  # On Windows use `venv\\Scripts\\activate`\n```\n3. Install the required dependencies:\n```bash\npip install -r requirements.txt\n```\n\n## Usage\n### Training\nThe model is trained on NVIDIA A6000 GPU with 48GB VRAM. The training takes approximately 10-12 hours on these specs. To train the model, run:\n```bash\nCUDA_VISIBLE_DEVICES=\u003cYOUR_GPU_ID\u003e nohup python train.py\n```\n\n### Evaluation\nOnce the model is trained, you can evaluate the model's performance on the validation set (10,000 images) in termms of metrics like the Jaccard Score (IoU), Accuracy, and F1-Score. You can make necessary changes to eval_lane.py and then run the following command in order to evaluate the model:\n```bash\nCUDA_VISIBLE_DEVICES=\u003cYOUR_GPU_ID\u003e nohup python eval-lane.py\n```\n\n### Inference \nTo run inference on a single image and save the predicted mask in the pred folder, use:\n```bash\nCUDA_VISIBLE_DEVICES=\u003cYOUR_GPU_ID\u003e nohup python inference.py\n```\n\nTo run inference on a video and overlay the lane detection mask, use:\n```bash\nCUDA_VISIBLE_DEVICES=\u003cYOUR_GPU_ID\u003e nohup python video_infer2.py\n```\n\nTo run inference on a video that would output an overlayed lane detection mask + YOLO detections, use:\n```bash\nCUDA_VISIBLE_DEVICES=\u003cYOUR_GPU_ID\u003e nohup python yolo_integrated.py\n```\n\n## Results\nThe model was evaluated on the following metrics over the validation set:\n- Validation Jaccard Score (IoU): 0.9934\n- Validation Accuracy: 0.9934\n- Validation F1 Score: 0.9967\n\nHere's a look at the model's predicted mask being compared to the ground truth mask on a sample image:\n\n![Single Inference](https://github.com/AnshChoudhary/Lane-Detection-UNet/blob/main/Inference-PredMask.png)\n\nHere's a look at a sample output video that overlays the lane detection mask from the trained model and performs YOLO object detections on cars, pedestrians, traffic lights, etc.:\n\n![Video Inference](https://github.com/AnshChoudhary/Lane-Detection-UNet/blob/main/output_input3_with_yolo_light.gif)\n\n### Post-processing Output using Dynamic Moving Average Filter \nAfter the masks generated by the model on a video input, The moving average filter is used to smooth out the detected lane mask over successive frames. This helps to reduce flicker and provide a more stable and coherent lane detection result over time.\n\n![Compare MAF](https://github.com/AnshChoudhary/Lane-Detection-UNet/blob/main/compare.png)\n\n```python\ndef moving_average_2d(data, window_size):\n    ret = np.cumsum(data, axis=0, dtype=float)\n    ret[window_size:] = ret[window_size:] - ret[:-window_size]\n    return ret[window_size - 1:] / window_size\n```\nThis function calculates the moving average along the first axis of the 2D data array, which could represent the mask or some other processed frame data, smoothing the transitions and making the lane detection more robust. You can also adjust the blending alpha parameter for blending the original and smoothed masks and the moving average window size to define the size of the window of frames used for calculating the average. \n\nA static moving average filter did not perform well on videos that had curved paths and it was averaging the lane lines to a different position. In order to tackle this problem, a dynamic window size adjustment was implemented. Now the window size would be inversely proportional to the number of pixels being detected in a frame. This would solve the averaging problem drastically as now only the frame with lesser detected pixels are being averaged out on bigger window sizes.\n\n```python\ndef dynamic_window_size_adjustment(mask, base_window_size, min_window_size, max_window_size):\n    detected_pixels = np.count_nonzero(mask)\n    total_pixels = mask.size\n    proportion = detected_pixels / total_pixels\n    \n    # Larger window size when fewer lane pixels are detected\n    window_size = int(max_window_size * (1 - proportion) + min_window_size * proportion)\n    \n    return max(min_window_size, min(max_window_size, window_size))\n```\n\n## Streamlit App\nThis project can be run on a streamlit web app in order to generate output videos that overlay the lane detection mask from the trained model and perform YOLO object detections. The user will be able to upload a video in avi, mov, mp4 formats and will have control over various parameters such as YOLO confidence threshold, detection transparency, interpolation factor,  etc. \n\n- **Moving Average Filtering** has also been added to the streamlit app and users can adjust the blending alpha parameter and the base, min, and max moving average window sizes within the streamlit web app controls.\n\nHere's a look at the web app UI:\n\n![Streamlit](https://github.com/AnshChoudhary/Lane-Detection-UNet/blob/main/streamlit-final.png)\n\nTo run the streamlit app, run the following in terminal:\n```bash\nstreamlit run streamlit-dynamic.py\n```\n\n## Contributing\nContributions are welcome! Please fork the repository and submit a pull request with your changes. For major changes, please open an issue first to discuss what you would like to change.\n1. Fork the Project\n2. Create your Feature Branch (`git checkout -b feature/YourFeature`)\n3. Commit your Changes (`git commit -m 'Add some YourFeature'`)\n4. Push to the Branch (`git push origin feature/YourFeature`)\n5. Open a Pull Request\n\n## License\nThis project is licensed under the MIT License - see the [LICENSE](https://github.com/AnshChoudhary/Lane-Detection-UNet/blob/main/LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanshchoudhary%2Flane-detection-unet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanshchoudhary%2Flane-detection-unet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanshchoudhary%2Flane-detection-unet/lists"}