A Python library for image segmentation using OpenCV and deep learning models.
Project description
๐ผ๏ธ OC Image Segmentation
A comprehensive image segmentation library using deep learning, with support for U-Net and DeepLabV3+ on the Cityscapes dataset.
๐ Installation
git clone https://github.com/your-repo/oc-image-segmentation.git
cd oc-image-segmentation
pip install -e .
๐ Quick Start
Command Line Interface (CLI)
# Segment an image
python -m oc_image_segmentation.cli segment input.jpg -o output.png
# With custom model
python -m oc_image_segmentation.cli segment input.jpg -o output.png --model unet_attention
# Train a model
python -m oc_image_segmentation.cli model train unet /path/to/cityscapes --epochs 50
# Evaluate a model
python -m oc_image_segmentation.cli model eval unet /path/to/cityscapes --model-path model.h5
Python API
from oc_image_segmentation import UNetModel, CityscapesDataset, segment_image
# Create and train a model
unet = UNetModel(input_size=(512, 512))
model = unet.build_model(use_attention=True)
# Load a dataset
dataset = CityscapesDataset("/path/to/cityscapes", split="train")
# Segment an image
result = segment_image("input.jpg", model="unet", output_path="output.png")
๐๏ธ Model Architectures
U-Net
- Standard U-Net: Classic encoder-decoder architecture
- U-Net Attention: With attention gate mechanisms
- Optimized for urban image segmentation
- Support for skip connections and batch normalization
DeepLabV3+
- DeepLabV3+ ResNet50: Standard version with ResNet50 backbone
- DeepLabV3+ ResNet101: High-performance version with ResNet101
- DeepLabV3+ EfficientNetV2B4: Efficient version with EfficientNetV2B4 backbone
- ASPP (Atrous Spatial Pyramid Pooling) module
- Depthwise separable convolutions for efficiency
๐ Dataset and Preparation
Cityscapes Dataset
Required structure:
dataset/
โโโ leftImg8bit_trainvaltest/
โ โโโ leftImg8bit/
โ โโโ train/
โ โโโ val/
โ โโโ test/
โโโ gtFine_trainvaltest/
โโโ gtFine/
โโโ train/
โโโ val/
โโโ test/
Dataset Commands
# Load and explore a dataset
python -m oc_image_segmentation.cli dataset load /path/to/cityscapes
# Create all splits
python -m oc_image_segmentation.cli dataset create-all /path/to/cityscapes --batch-size 16
๐จ Data Augmentation
TensorFlow Augmentation (Standard)
Configuration in settings.yaml:
data_augmentation:
enabled: true
horizontal_flip: true
vertical_flip: false
rotation_range: 5
zoom_range: 0.05
brightness_range: [0.9, 1.1]
contrast_range: [0.9, 1.1]
noise_factor: 0.01
Albumentations Augmentation (Advanced)
For more robust and performant augmentations:
from oc_image_segmentation.datasets.preprocessing import (
augment_data_albumentations,
create_albumentations_config
)
# Use a predefined preset
config = create_albumentations_config("medium")
aug_image, aug_mask = augment_data_albumentations(image, mask, config)
# Custom configuration
custom_config = {
"enabled": True,
"horizontal_flip": True,
"rotation_range": 10,
"brightness_range": [0.8, 1.2],
"elastic_transform": True,
"weather_effects": True,
}
Albumentations Advantages:
- Consistent geometric transformations for image/mask
- Realistic weather effects
- Elastic and optical distortions
- Optimized performance
- Advanced segmentation support
Predefined profiles:
- Urban driving: Conservative augmentations
- High precision: Minimal augmentations
- Robustness: Extended augmentations
๐ Training and Evaluation
Training
# Standard U-Net
python -m oc_image_segmentation.cli model train unet /path/to/cityscapes
# U-Net with attention (more parameters)
python -m oc_image_segmentation.cli model train unet_attention /path/to/cityscapes --epochs 100
# High-performance DeepLabV3+
python -m oc_image_segmentation.cli model train deeplabv3plus_resnet101 /path/to/cityscapes
# EfficientNet-based DeepLabV3+
python -m oc_image_segmentation.cli model train deeplabv3plus_efficientnet /path/to/cityscapes
Metrics and Evaluation
- mIoU (mean Intersection over Union): Primary metric
- IoU per class: Detailed analysis per Cityscapes class
- Confusion matrix: Error diagnosis
- Callbacks: EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
# Complete evaluation
python -m oc_image_segmentation.cli model eval unet /path/to/cityscapes --model-path model.h5 --split val
# Prediction with mIoU
python -m oc_image_segmentation.cli model predict unet input.jpg output.png --model-path model.h5 --ground-truth gt.png
โ๏ธ Configuration
Flexible Configuration System
- settings.yaml: Main configuration
- Environment variables: Override with
OC_SEGMENT_prefix - Multiple files: Cascading configuration support
- Profiles: Predefined configurations (development, production, test)
Precision Profiles
- Ultra-fast: Maximum performance, reduced precision
- Balanced: Good speed/precision compromise
- High precision: Maximum precision for production
# View current configuration
python -m oc_image_segmentation.cli config
# Use specific profile
export OC_SEGMENT_SETTINGS_FILES="settings/prod_settings.yaml"
๐ง Advanced Features
Pre-trained Models
- Available pre-trained models
- Simplified transfer learning
- Resume training (
--resume-from)
Detailed IoU Analysis
- Per-class IoU with Cityscapes names
- Analysis of problematic classes
- Automatic improvement recommendations
Custom Callbacks
- Intelligent EarlyStopping on validation mIoU
- Automatic best model saving
- Training history in CSV
- Automatic visualizations
Category Management
- Support for trainId and categoryId
- Automatic Cityscapes class mapping
- Configurable ignored classes
๐ Complete CLI Commands
Segmentation
# Basic segmentation
segment input.jpg -o output.png
# With advanced options
segment input.jpg -o output.png --model deeplabv3plus --confidence-threshold 0.7 --no-overlay
Model Management
# Create a model
model create unet_attention --no-summary
# Train
model train unet /dataset --epochs 50 --output-dir ./models/ --resume-from weights.h5
# Evaluate
model eval unet /dataset --model-path model.h5 --split test
# Predict
model predict unet input.jpg output.png --model-path model.h5 --ground-truth gt.png
Dataset Management
# Load and explore
dataset load /path/to/cityscapes --split train --batch-size 32
# Create all splits
dataset create-all /path/to/cityscapes --batch-size 16
๐ Debugging and Optimization
Troubleshooting
- AutoGraph correction guide: Common TensorFlow issues
- Test migration: Updating existing tests
- Memory optimization: Memory usage reduction techniques
- Profiling: Detailed performance analysis
Logging and Monitoring
- Structured logs with configurable levels
- Real-time metrics during training
- Automatic performance profiling
- Test cleanup reports
๐ค Contributing
- Fork the project
- Create a feature branch (
git checkout -b feature/new-feature) - Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/new-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License. See the LICENSE file for details.
๐ Acknowledgments
- Cityscapes Dataset: cityscapes-dataset.com
- U-Net Architecture: Ronneberger et al.
- DeepLabV3+ Architecture: Chen et al.
- EfficientNet Architecture: Tan & Le
- TensorFlow/Keras for the deep learning framework
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file oc_image_segmentation-0.2.0.tar.gz.
File metadata
- Download URL: oc_image_segmentation-0.2.0.tar.gz
- Upload date:
- Size: 58.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b3a65273dec0912c4b4f9d501a9a663f02dd6e1c24857bb4d03cafc21ced152
|
|
| MD5 |
bbb3160baa926cbe94bc1f12ae57213b
|
|
| BLAKE2b-256 |
5de8fdcbde02ef0c3223f78a300a204426bae3dae5f62570d5e87d775e09c9c5
|
File details
Details for the file oc_image_segmentation-0.2.0-py3-none-any.whl.
File metadata
- Download URL: oc_image_segmentation-0.2.0-py3-none-any.whl
- Upload date:
- Size: 63.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98c843073940c3af44c9806eb9661cf480d87054747b2815a944d4370dc1da7b
|
|
| MD5 |
1a00eff7f0933a58cc1cb4961138f2c0
|
|
| BLAKE2b-256 |
6b2ccbe8de5c844c82d75d2babe39927e364a5d81236648804bc73964277fd47
|