How virennamo27/blood_segmentation works
Purpose and Entry Points
This repository trains and serves a binary blood-cell segmentation model for microscopy images. Training is invoked as python -m src.train; serving starts with uvicorn api.main:app. The browser UI is static and is mounted by the API.
Paths: src/train.py, api/main.py, frontend/index.html.
Data Ingestion and Mask Construction
Training consumes the external BCCD dataset under data/raw/JPEGImages and data/raw/Annotations. Pascal-VOC XML bounding boxes are rasterized at load time into binary foreground masks, so the model learns cell-versus-background rather than cell class labels. Processed-data storage is reserved but not populated in the tree.
Paths: src/dataset.py, data/processed/.gitkeep.
Model and Optimization
The modeling boundary is a segmentation-models-pytorch UNet with an ImageNet-initialized ResNet34 encoder. The training loop applies albumentations, optimizes a combined Dice/BCE objective with AdamW and cosine annealing, evaluates Dice/IoU, and writes the best validation-Dice artifact.
Paths: src/model.py, src/metrics.py, src/train.py.
Training Artifacts and Experiment Service
Training is independent from online inference. It produces a checkpoint expected at checkpoints/best_model.pth (the directory is runtime-created/external rather than present in this tree) and logs experiments to Weights & Biases; W&B can be disabled by environment configuration. The BCCD dataset and trained checkpoint are material external inputs/artifacts.
Paths: src/train.py, .env.example.
Inference Pipeline
Inference loads one PyTorch model into process memory, preprocesses an uploaded image, runs UNet prediction, applies sigmoid and thresholding, then generates a mask overlay, cell count, and latency result. This isolates image/model logic from HTTP concerns.
Paths: src/predict.py, src/model.py.
API and Frontend Boundary
FastAPI owns application lifespan model loading, POST /predict multipart upload handling, GET /health, response validation, and static-file delivery. Pydantic schemas define the API result contract. The vanilla single-page frontend submits drag-and-drop uploads and displays returned overlays and statistics.
Paths: api/main.py, api/schemas.py, frontend/index.html.
Runtime and Deployment
Primary runtime is Python 3.11 with PyTorch, FastAPI/Uvicorn, OpenCV, albumentations, and Pydantic v2; execution supports CUDA, Apple MPS, or CPU fallback. Docker packages the service from a Python slim base while checkpoints are mounted at runtime. BCCD_CHECKPOINT and BCCD_FRONTEND_DIR decouple deployed model and UI locations from defaults.
Paths: Dockerfile, requirements.txt, .dockerignore.
Main entry points: main.py FastAPI application · train.py Training loop · predict.py Prediction pipeline · model.py ResNet34 UNet
Components 16
Training Pipeline
- BCCD dataset · Microscopy images and Pascal-VOC annotations.
- Dataset & mask builder · Rasterizes VOC boxes into binary foreground masks.
src/dataset.py - Training loop · Applies augmentation, optimization, validation, and best-model selection.
src/train.py - ResNet34 UNet · segmentation-models-pytorch UNet with ImageNet encoder initialization.
src/model.py - Dice / IoU metrics
src/metrics.py - Weights & Biases · Optional experiment logging.
- Best model checkpoint · Best validation-Dice weights at runtime.
- Processed data reservation · Reserved but not populated.
data/processed/.gitkeep
Inference Service
- Browser UI · Drag-and-drop upload and prediction display.
frontend/index.html - FastAPI application · Owns lifespan, health, multipart prediction, validation, and static delivery.
api/main.py - Response schemas
api/schemas.py - Prediction pipeline · Preprocesses images and produces overlay, count, and latency.
src/predict.py - Loaded model · One checkpoint-backed PyTorch model is held in memory.
- Docker image · Python slim service image; checkpoint is mounted at runtime.
Dockerfile - Python runtime · Python 3.11 with CUDA, MPS, or CPU fallback.
requirements.txt - Deployment paths · Configures checkpoint and frontend locations.
.env.example
Connections 18
- BCCD dataset → Dataset & mask builder: images + VOC XML
- Dataset & mask builder → Training loop: binary masks
- ResNet34 UNet → Training loop: model definition
- Dice / IoU metrics → Training loop: validation scores
- Training loop → Best model checkpoint: writes best Dice
- Training loop → Weights & Biases: logs experiments
- Best model checkpoint → Loaded model: loads at startup
- ResNet34 UNet → Loaded model: architecture
- Browser UI → FastAPI application: POST /predict
- FastAPI application → Browser UI: serves UI and results
- FastAPI application → Response schemas: validates responses
- FastAPI application → Prediction pipeline: uploaded image
- Loaded model → Prediction pipeline: UNet inference
- Prediction pipeline → FastAPI application: overlay, count, latency
- Python runtime → FastAPI application: runs Uvicorn service
- Docker image → Python runtime: packages
- Deployment paths → FastAPI application: frontend path
- Deployment paths → Best model checkpoint: checkpoint path