# ๐Ÿš— Multi-Class Segmentation for Autonomous Vehicles ๐Ÿ“˜ Ce projet est รฉgalement disponible en [franรงais ๐Ÿ‡ซ๐Ÿ‡ท](./README.fr.md) **Source of truth** : [Forgejo](https://git.gregoiremureau.com/openclassroom-ai/oc-p08-street-vision) (self-hosted). ```bash git clone https://git.gregoiremureau.com/openclassroom-ai/oc-p08-street-vision.git git clone https://git.gregoiremureau.com/openclassroom-ai/oc-p08-street-vision.git ``` Prod: https://api.gregoiremureau.com/ml/street-vision/ Full dataset: `./fetch-data.sh` (Cityscapes **not** in git; a small sample is already in `notebooks/content/data/`). ## ๐Ÿ“‹ Project Description This project develops a semantic segmentation system for street images intended for autonomous vehicles. It enables the automatic identification and segmentation of various urban elements: vehicles, pedestrians, street furniture, road infrastructure, etc. The system is part of a complete embedded computer vision pipeline for autonomous vehicles, processing images in real time to feed decision-making systems. ## ๐Ÿ—‚๏ธ Project Structure ``` โ”œโ”€โ”€ notebooks/ โ”‚ โ”œโ”€โ”€ p8_notebook.ipynb # Main model training notebook โ”‚ โ””โ”€โ”€ content/ โ”‚ โ””โ”€โ”€ data/ โ”‚ โ”œโ”€โ”€ new_images_to_predict/ # Various images for inference tests โ”‚ โ””โ”€โ”€ test_images_sample/ # Cityscapes sample with masks โ”œโ”€โ”€ utils/ โ”‚ โ””โ”€โ”€ utils.py # Shared functions (preprocessing, loading, inference) โ”œโ”€โ”€ app/ # API and Web Interface โ”‚ โ”œโ”€โ”€ main.py # FastAPI API with segmentation routes โ”‚ โ”œโ”€โ”€ streamlit_app.py # Interactive web interface โ”‚ โ””โ”€โ”€ requirements.txt # Dependencies for the API and interface โ”œโ”€โ”€ cityscapes_config.json # Cityscapes class configuration โ”œโ”€โ”€ .env.example # Environment variables template โ”œโ”€โ”€ Dockerfile # Docker configuration โ”œโ”€โ”€ docker-compose.yml.example # Docker Compose template โ”œโ”€โ”€ nginx.conf # Reverse proxy configuration โ”œโ”€โ”€ start.sh # Services startup script โ”œโ”€โ”€ .dockerignore # Docker build optimization โ””โ”€โ”€ README.md ``` > **Note**: Trained models are stored and versioned via **MLFlow** with artifacts on **MinIO**, not locally. **Current development status:** - โœ… **Model training**: Complete (notebook + MLFlow + MinIO) - โœ… **Inference functions**: Developed and ready - โœ… **FastAPI API**: Complete API with segmentation endpoints - โœ… **Web Interface**: Streamlit interface for upload and visualization - โœ… **Deployment**: Full Docker containerization + nginx reverse proxy ## ๐Ÿท๏ธ Segmentation Classes The model identifies **8 main categories**: 1. **Road** - Roadway and driving surfaces 2. **Sidewalk** - Pedestrian areas 3. **Buildings** - Architectural structures 4. **Vegetation** - Trees, bushes, green spaces 5. **Vehicles** - Cars, trucks, buses 6. **People** - Pedestrians, cyclists 7. **Signage** - Signs, traffic lights 8. **Street furniture** - Poles, barriers, bus shelters ## ๐Ÿง  Technical Architecture ### Implemented Models Using the **[segmentation_models](https://github.com/qubvel/segmentation_models)** library by qubvel to implement the architectures. **Main architectures:** - **U-Net**: Classic encoder-decoder architecture - **FPN** (Feature Pyramid Network): Feature pyramid network **Tested backbones:** ```python # Models with default backbones ('U-Net', None) # Built-in U-Net backbone ('FPN', None) # Default VGG16 backbone # Models with pretrained backbones (frozen encoders) ('U-Net', 'mobilenetv2') ('U-Net', 'efficientnetb0') ('FPN', 'mobilenetv2') ('FPN', 'efficientnetb0') ('FPN', 'resnet34') # Selective fine-tuning (trainable encoders) ('U-Net', 'mobilenetv2') ('FPN', 'efficientnetb0') ('FPN', 'resnet34') ``` **Training strategies:** - **Frozen encoders**: Transfer learning with fixed pretrained features - **Fine-tuning**: Fine adjustment of pretrained encoders - **Training from scratch**: Without pretraining (default backbones) ### Technology Stack - **Framework**: Keras/TensorFlow - **Architectures**: [segmentation_models](https://github.com/qubvel/segmentation_models) (qubvel) - **Experiment management**: MLFlow - **Artifact storage**: MinIO (S3-compatible) - **API**: FastAPI with Pydantic validation - **Interface**: Streamlit for user interaction - **Reverse Proxy**: nginx for API/Interface routing - **Deployment**: Docker + personal NAS *(sovereign solutions)* ## ๐Ÿ“Š Dataset - **Source**: Cityscapes Dataset - **Local organization**: - `notebooks/content/data/test_images_sample/`: Sample with ground truth masks for inference tests - `notebooks/content/data/new_images_to_predict/`: Various test images for validation ## ๐Ÿš€ Installation and Configuration ### Option 1: Docker Deployment (Recommended) #### Quick configuration ```bash # Clone the project git clone https://git.gregoiremureau.com/openclassroom-ai/oc-p08-street-vision.git cd oc-p08-street-vision # Copy and adapt the configuration cp .env.example .env cp docker-compose.yml.example docker-compose.yml # Edit the environment variables nano .env ``` #### Required environment variables ```bash # Server configuration NGINX_PORT=8080 # MLflow configuration MLFLOW_TRACKING_URI=http://localhost:5000 MLFLOW_S3_ENDPOINT_URL=http://localhost:9000 AWS_ACCESS_KEY_ID=your_access_key AWS_SECRET_ACCESS_KEY=your_secret_key # API base URL (for Streamlit) MULTISEG_API_BASE_URL=http://localhost:8080/api ``` #### Deployment ```bash # Build and launch docker-compose up --build # Or in the background docker-compose up -d --build # Access the application # Web interface: http://localhost:8080 # API docs: http://localhost:8080/api/docs # Health check: http://localhost:8080/health ``` #### Local test without docker-compose ```bash # Launch with a custom port docker build -t multiseg-api . docker run -p 3000:3000 \ -e NGINX_PORT=3000 \ -e MLFLOW_TRACKING_URI=http://host.docker.internal:5000 \ -e MULTISEG_API_BASE_URL=http://localhost:3000/api \ multiseg-api ``` ### Option 2: Local Installation (Development) #### For experimentation and training ```bash cd notebooks pip install -r requirements.txt ``` #### For the API only ```bash cd app pip install -r requirements.txt # Manual service startup # Terminal 1: FastAPI uvicorn app.main:app --host 0.0.0.0 --port 8000 # Terminal 2: Streamlit streamlit run app/streamlit_app.py --server.port 8501 ``` ## ๐ŸŒ API Usage ### Main Endpoints #### Image segmentation ```bash # Upload and segmentation via curl curl -X POST "http://localhost:8080/api/segment" \ -F "file=@image.jpg" \ -F "model_name=unet_mobilenetv2" \ -F "confidence_threshold=0.5" ``` #### Model information ```bash # List of available models curl "http://localhost:8080/api/models" # Details of a model curl "http://localhost:8080/api/models/unet_mobilenetv2" ``` #### Monitoring ```bash # Health check curl "http://localhost:8080/health" # API metrics curl "http://localhost:8080/api/metrics" ``` ### Web Interface The Streamlit interface accessible at `http://localhost:8080` allows you to: - **Upload images** by drag-and-drop or selection - **Select the model** among the available models - **Adjust parameters** (confidence threshold, etc.) - **Visualize** results with segmentation overlay - **Download** segmentation masks ## ๐Ÿ”ง Deployment Architecture ### Service Structure ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ nginx:8080 โ”‚โ”€โ”€โ”€โ”€โ”‚ FastAPI:8000 โ”‚โ”€โ”€โ”€โ”€โ”‚ MLFlow/MinIO โ”‚ โ”‚ (Reverse Proxy)โ”‚ โ”‚ (API Backend) โ”‚ โ”‚ (Model Storage) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Streamlit:8501 โ”‚ โ”‚ (Web Interface) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` ### Volumes and Persistence - **Models**: Downloaded from MLFlow/MinIO at startup - **Temporary uploads**: In-memory storage (no persistence) - **Logs**: Accessible via `docker-compose logs` ### Monitoring and Logs ```bash # Real-time logs docker-compose logs -f # Logs for a specific service docker-compose logs -f multiseg-api # Performance metrics curl http://localhost:8080/api/metrics ``` ## ๐Ÿ”ฎ Future Improvements ### Models and Performance - **Adaptive preprocessing**: Backbone-specific preprocessing instead of generic normalization - **Real-time optimization**: Improved inference performance for embedded deployment - **Improved training resolution**: The API accepts any image resolution, but training was performed on images resized to 224x224. Retraining at higher resolutions (512x512, 1024x1024) could improve accuracy by capturing more fine detail, at the cost of increased computational power requirements - **Targeted transfer learning**: Using backbones pretrained on Mapillary Vistas ### Infrastructure and Deployment - **Horizontal scaling**: Kubernetes support for multi-instance deployment - **Smart caching**: Caching predictions for similar images - **API Gateway**: Integrating a gateway for authentication and rate limiting - **Advanced monitoring**: Prometheus/Grafana integration for detailed metrics ### Data and Generalization - **Condition generalization**: Extending beyond Cityscapes' urban conditions - **People segmentation**: Improving IoU via data augmentation (generative AI) - **Additional datasets**: Integrating complementary datasets for robustness --- *Project developed with a technological sovereignty approach, favoring open-source solutions and hosting on personal infrastructure.*