Skip to main content

Ultra-lightweight real-time image segmentation with LoRA fine-tuning & Knowledge Distillation

Project description

FlashSeg Logo

FlashSeg

CI PyTorch Python ONNX License

Ultra-lightweight real-time semantic segmentation with LoRA fine-tuning & knowledge distillation

InstallUsageModelsSolutionsTrainingExamples


What is FlashSeg?

FlashSeg is an ultra-lightweight semantic segmentation framework built for speed and edge deployment. Using a ShuffleNetV2 backbone with FPN neck, it delivers real-time pixel-level predictions with models as small as 0.3M parameters.

pip install -e .
flashseg train --model-size m --num-classes 21 --train-images data/images --train-masks data/masks --val-images data/val_img --val-masks data/val_mask
flashseg predict --model best.pth --source images/

Installation

# From source
git clone https://github.com/FlashVision/FlashSeg.git
cd FlashSeg
pip install -e ".[all]"

Optional extras

pip install -e ".[export]"      # ONNX export
pip install -e ".[analytics]"   # Benchmarking, plots
pip install -e ".[solutions]"   # Background removal, lane detection
pip install -e ".[all]"         # Everything

Verify

flashseg check
flashseg settings
flashseg version

Usage

Python API

from flashseg import Trainer, Predictor, Exporter

# Train
trainer = Trainer(
    model_size="m",
    train_images="data/images",
    train_masks="data/masks",
    val_images="data/val_images",
    val_masks="data/val_masks",
    num_classes=21,
    epochs=100,
    device="cuda",
)
trainer.train()

# Predict
predictor = Predictor(model_path="workspace/best.pth", num_classes=21, device="cuda")
mask = predictor.predict("photo.jpg")

# Export
exporter = Exporter(model_path="workspace/best.pth", num_classes=21)
exporter.export(output="model.onnx", simplify=True)

CLI

flashseg train --model-size m --epochs 100 --device cuda \
  --train-images data/images --train-masks data/masks \
  --val-images data/val_img --val-masks data/val_mask --num-classes 21

flashseg predict --model best.pth --source images/ --save-dir output/

flashseg val --model best.pth --val-images data/val --val-masks data/val_mask

flashseg export --model best.pth --output model.onnx --simplify

Models

Model Params FP16 Size Input mIoU (VOC)
FlashSeg-n 0.3M ~0.7 MB 256
FlashSeg-s 0.8M ~1.6 MB 256
FlashSeg-m 1.5M ~3.0 MB 512
FlashSeg-l 3.2M ~6.4 MB 512

Config-driven Training

flashseg train --config configs/flashseg_m_512_voc.yaml
flashseg train --config configs/flashseg_s_256_cityscapes.yaml
flashseg train --config configs/flashseg_m_512_lora.yaml

Solutions

Built-in high-level applications:

from flashseg import Predictor
from flashseg.solutions import BackgroundRemover, LaneDetector, SceneParser, AreaCalculator

predictor = Predictor(model_path="best.pth", num_classes=21)

# Remove background
remover = BackgroundRemover(predictor, foreground_classes=[15])
result = remover.remove(image)

# Detect lanes
lanes = LaneDetector(predictor, lane_class_id=1)

# Parse scenes
parser = SceneParser(predictor, class_names=["bg", "road", "building", ...])
stats = parser.parse(image)  # {"road": 35.2, "building": 12.1, ...}

# Calculate areas
calc = AreaCalculator(predictor, pixels_per_meter=10.0)
areas = calc.calculate(image)  # {class_id: area_m2, ...}
Solution Description
BackgroundRemover Remove/replace backgrounds, generate alpha mattes
LaneDetector Detect road lanes from segmentation
SceneParser Break scene into labeled regions with area stats
AreaCalculator Measure real-world areas from masks

Training

Standard

flashseg train --model-size m --epochs 100 --num-classes 21 --device cuda

LoRA Fine-Tuning

flashseg train --model-size m --lora --config configs/flashseg_m_512_lora.yaml

Mixed Precision

flashseg train --model-size m --amp --device cuda

Examples

Script What it does
train_voc.py Train on Pascal VOC
predict_image.py Segment a single image
background_removal.py Remove image background
export_onnx.py Export to ONNX
benchmark_model.py Measure FPS and latency

Project Structure

FlashSeg/
├── flashseg/                  # Main package
│   ├── cfg/                   # Configuration + YAML loading
│   ├── data/                  # Datasets, transforms
│   ├── engine/                # Trainer, Predictor, Exporter, Validator
│   ├── models/                # ShuffleNetV2, FPN, SegHead
│   ├── losses/                # CE, Dice, Focal, Combined
│   ├── nn/                    # ConvBnRelu, ASPP, DepthwiseSeparable
│   ├── utils/                 # Metrics, visualization
│   ├── solutions/             # Background removal, lanes, scene parsing
│   └── analytics/             # Benchmark, profiler
├── configs/                   # YAML configs (pick & train)
├── examples/                  # Ready-to-run scripts
├── tests/                     # Unit tests
├── docker/                    # Dockerfile + compose
├── pyproject.toml             # Package config
└── LICENSE                    # MIT

Docker

docker build -t flashseg -f docker/Dockerfile .
docker run --gpus all -v $(pwd)/data:/app/data flashseg predict --model best.pth --source data/

Contributing

git clone https://github.com/FlashVision/FlashSeg.git
cd FlashSeg
pip install -e ".[dev,all]"
ruff check flashseg/
pytest tests/ -v

License

MIT — see LICENSE.


FlashVision — Open-source lightweight AI

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flashseg-1.0.0.tar.gz (24.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flashseg-1.0.0-py3-none-any.whl (30.6 kB view details)

Uploaded Python 3

File details

Details for the file flashseg-1.0.0.tar.gz.

File metadata

  • Download URL: flashseg-1.0.0.tar.gz
  • Upload date:
  • Size: 24.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for flashseg-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0a2b9b8457d7481554c93601c043aadb67f6da5ee035140543d314044fa1bb91
MD5 6e88594b36dbdcc3f14cbe9d065084ae
BLAKE2b-256 1ad3cfd4bb8b04ff15f7ea76a7fbe9817b105a1b8ccee72de3b6ffb3ea00eb75

See more details on using hashes here.

File details

Details for the file flashseg-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: flashseg-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 30.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for flashseg-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1798da485b56d891f7967277c1d1aba84966e34610331f5dea06d09ba9156623
MD5 6d76c358dac8b14b741f7c3380f3206d
BLAKE2b-256 38a6d71f496c36a6a1c2c91b895fa983a0d5951351667d9b28185daf588aad16

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page