Skip to content

nick8592/lane-detection-unet-ncnn

Repository files navigation

UNet Lane Segmentation & NCNN Deployment

Demo

This repository provides a pipeline for lane segmentation using a UNet model trained on the BDD100K dataset. It includes modular scripts for training, validation, and inference, with configuration-driven workflows and TensorBoard logging. The project supports exporting PyTorch models for deployment with the NCNN framework, enabling efficient inference on ARM and x86 platforms via C++.

Features

  • UNet architecture for lane segmentation
  • BDD100K dataset integration
  • Configurable training and inference scripts
  • Checkpointing and metrics
  • TensorBoard support
  • Model export for NCNN (TorchScript → pnnx → NCNN)
  • C++ deployment example with NCNN and OpenCV

Table of Contents


Setup Instructions

1. Quick Start (Docker)

docker run -it --gpus all --shm-size=16g -v /home:/home <image_id>

2. Environment Setup

apt update && apt upgrade -y
apt install python3 python3-pip -y
apt install build-essential git cmake wget libprotobuf-dev protobuf-compiler libomp-dev libopencv-dev -y

3. PyTorch Installation

pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 --extra-index-url https://download.pytorch.org/whl/cu117
pip install -r requirements.txt

Dataset

BDD100K

BDD100K is a large-scale driving video dataset for diverse road scenes, widely used for autonomous driving research. This project uses the lane segmentation subset, which provides pixel-level lane annotations for training semantic segmentation models.

Download & Preparation

  • Official website: https://bdd-data.berkeley.edu/
  • Download the images and lane mask annotations from the BDD100K website or via their download scripts.
  • Organize the dataset as follows:
bdd100k/
├── images/
│   └── 100k/
│       ├── train/
│       ├── val/
│       └── test/
└── lane/
  ├── colormaps/
  ├── masks/
  │   ├── train/
  │   └── val/
  └── polygons/
  • images/100k/train, val, and test contain the raw images.
  • lane/masks/train and val contain binary lane segmentation masks.
  • lane/colormaps and lane/polygons provide additional annotation formats.

Pretrained Models on Hugging Face

Pretrained weights for all UNet variants are available on Hugging Face:

Model PyTorch (.pt) NCNN (.param, .bin) Repo Link
UNet pt param, bin repo
UNetDepthwise pt param, bin repo
UNetDepthwiseSmall pt param, bin repo
UNetDepthwiseNano pt param, bin repo

Each folder contains both PyTorch (.pt) and NCNN (.param, .bin) files for direct use in Python or C++ inference workflows.

Usage in This Project

  • Update config/train_config.yaml and config/test_config.yaml to point to your local BDD100K image and mask directories.
  • The dataset loader expects images and masks to be sorted and matched by filename.

Training Usage

1. Edit Training Config

Edit config/train_config.yaml to set dataset paths, batch size, epochs, learning rate, image size, and model parameters.

2. Run Training

python3 scripts/train.py

Checkpoints and logs will be saved in the checkpoints/ and runs/ folders.

TensorBoard logs are available in runs/. To view training progress:

tensorboard --logdir runs/

Inference Usage

1. Edit Inference Config

Edit config/test_config.yaml to set the trained checkpoint, input images directory, output directory, and model parameters.

2. Run Inference

python3 scripts/test.py

Output masks will be saved in the specified output directory. Progress is shown with tqdm.


Exporting Model for NCNN Deployment

1. Export PyTorch Model and NCNN Files

Run the export script to convert your trained UNet model to TorchScript and directly export NCNN .param and .bin files:

python export/export_to_ncnn.py

This will generate unet_jit.pt, unet_jit.param, and unet_jit.bin in the output directory specified in your config. No manual pnnx command is needed.

2. Deploy with NCNN

Use the generated .param and .bin files for the later NCNN C++ deployment. Refer to NCNN documentation for integration details.


NCNN Library Setup

To build NCNN from source:

git clone https://github.com/Tencent/ncnn.git
cd ncnn
mkdir build && cd build
cmake ..
make -j$(nproc)
make install

NCNN C++ Deployment

1. Build NCNN C++ Inference Code

Go to the deployment folder:

cd ncnn_deploy
mkdir build && cd build
cmake ..
make -j$(nproc)

If you see errors about missing NCNN or OpenCV, make sure to set the correct paths in CMakeLists.txt:

set(ncnn_DIR "/path/to/ncnn/install/lib/cmake/ncnn")
include_directories("/path/to/ncnn/install/include")

2. Run Inference (Flexible CLI)

You can now run the C++ inference with flexible command-line options:

./unet_ncnn key=value ...

Supported keys (all optional, defaults shown):

input_image=/path/to/image.jpg
model_param=../../checkpoints/exp_20250907_172056/ncnn_models/unet_jit.ncnn.param
model_bin=../../checkpoints/exp_20250907_172056/ncnn_models/unet_jit.ncnn.bin
output_mask=mask.jpg
output_overlay=overlay.jpg
input_w=256
input_h=256
input_layer=in0
output_layer=out0
mask_alpha=0.8

Example:

./unet_ncnn input_image=img.jpg mask_alpha=0.5 output_mask=mask.png output_overlay=overlay.png

This will save the output mask and overlay images to the specified paths.

3. Output Files

  • output_mask (default: mask.jpg): Lane mask image
  • output_overlay (default: overlay.jpg): Lane overlay visualization

4. Troubleshooting

  • If CMake cannot find protobuf, try:
    cmake .. -DProtobuf_INCLUDE_DIR=/usr/include -DProtobuf_LIBRARIES=/usr/lib/x86_64-linux-gnu/libprotobuf.so
  • If CMake cannot find NCNN, set ncnn_DIR to the folder containing ncnnConfig.cmake.
  • For ARM cross-compilation, set toolchain and paths as needed.

Gradio Lane Detection Demo

You can try the lane detection models interactively in your browser using Gradio:

Gradio App UI

Local Usage

To run the Gradio app locally:

pip install -r requirements.txt
python3 gradio_app/app.py
  • Upload a road image and select a model type (UNet, UNetDepthwise, UNetDepthwiseSmall, UNetDepthwiseNano).
  • The app will display the lane detection overlay result.

Model Architecture Comparison

Model Parameters FLOPs (GFLOPs) Estimated Size (MB)
UNet 31,043,521 54.79 700.10
UNetDepthwise 4,216,799 11.57 480.08
UNetDepthwiseSmall 253,919 5.88 385.58
UNetDepthwiseNano 52,191 3.00 279.92
  • All models use input size (1, 3, 256, 256).
  • FLOPs measured with ptflops (GMac).
  • Estimated size includes input, activations, and parameters.

Results Comparison

america_highway_x86_unet_comparison.mp4
hsinchu_to_taipei_1k_x86_unet_comparison.mp4
taichung_city_x86_unet_comparison.mp4

Codebase Directory Tree

lane-detection-unet-ncnn/
├── assets/
│   ├── demo.jpg
│   └──gradio_app.jpg
├── checkpoints
│   ├── exp_20250906_223222
│   │   ├── bdd100k_val_outputs
│   │   │   ├── config
│   │   │   └── inference_results
│   │   ├── config
│   │   │   └── train_config.yaml
│   │   ├── ncnn_models
│   │   │   ├── unet_jit.ncnn.bin
│   │   │   └── unet_jit.ncnn.param
│   │   └── weights
│   │       └── unet_best.pt
│   ├── exp_20250907_094745
|   └── ...   
├── config/
│   ├── eval_config.yaml
│   ├── export_config.yaml
│   ├── test_config.yaml
│   └── train_config.yaml
├── datasets/
│   └── bdd100k.py
├── export/
│   └── export_to_ncnn.py
├── gradio_app/
│   └── app.py
├── models/
│   ├── unet.py
│   ├── unet_depthwise.py
│   ├── unet_depthwise_small.py
│   └── unet_depthwise_nano.py
├── ncnn/
├── ncnn_deploy/
│   ├── build/
|   |   ├── input.jpg
│   |   └── unet_ncnn
│   ├── CMakeLists.txt
│   └── main.cpp
├── runs/
├── scripts/
│   ├── test.py
│   └── train.py
├── utils/
│   ├── checkpoint.py
│   └── metrics.py
├── .gitignore
├── CITATION.cff
├── LICENSE
├── README.md
└── requirements.txt

Citation

If you use this repository or models in your research, please cite as follows:

BibTeX:

@software{Pai_UNet_Lane_Segmentation_2025,
author = {Pai, Nick},
license = {MIT},
month = sep,
title = {{UNet Lane Segmentation \& NCNN Deployment}},
url = {https://github.com/nick8592/lane-detection-unet-ncnn},
version = {1.0.0},
year = {2025}
}

About

This repository provides a pipeline for lane segmentation using a UNet model trained on the BDD100K dataset.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages