# virennamo27/blood_segmentation architecture

> Architecture diagram of the GitHub repository virennamo27/blood_segmentation, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/virennamo27/blood_segmentation
- Repository: https://github.com/virennamo27/blood_segmentation

## Overview

#### 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: [`api/main.py`](https://github.com/virennamo27/blood_segmentation/blob/main/api/main.py) (FastAPI application), [`src/train.py`](https://github.com/virennamo27/blood_segmentation/blob/main/src/train.py) (Training loop), [`src/predict.py`](https://github.com/virennamo27/blood_segmentation/blob/main/src/predict.py) (Prediction pipeline), [`src/model.py`](https://github.com/virennamo27/blood_segmentation/blob/main/src/model.py) (ResNet34 UNet)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_training["Training Pipeline"]
  node_bccd[("BCCD dataset<br/>external dataset")]
  node_dataset["Dataset &amp; mask builder<br/>PyTorch dataset<br/>[dataset.py]"]
  node_train["Training loop<br/>training entry point<br/>[train.py]"]
  node_model["ResNet34 UNet<br/>segmentation model<br/>[model.py]"]
  node_metrics["Dice / IoU metrics<br/>evaluation<br/>[metrics.py]"]
  node_wandb["Weights &amp; Biases<br/>experiment service"]
  node_checkpoint["Best model checkpoint<br/>model artifact"]
  node_processed[("Processed data reservation<br/>data storage<br/>[.gitkeep]")]
end

subgraph group_serving["Inference Service"]
  node_frontend["Browser UI<br/>static SPA<br/>[index.html]"]
  node_api["FastAPI application<br/>HTTP API<br/>[main.py]"]
  node_schemas["Response schemas<br/>Pydantic contract<br/>[schemas.py]"]
  node_predict["Prediction pipeline<br/>inference service<br/>[predict.py]"]
  node_resident_model["Loaded model<br/>process state"]
  node_docker["Docker image<br/>deployment package"]
  node_runtime["Python runtime<br/>execution environment<br/>[requirements.txt]"]
  node_deployment_env["Deployment paths<br/>environment configuration<br/>[.env.example]"]
end

node_bccd -->|"images + VOC XML"| node_dataset
node_dataset -->|"binary masks"| node_train
node_model -->|"model definition"| node_train
node_metrics -->|"validation scores"| node_train
node_train -->|"writes best Dice"| node_checkpoint
node_train -.->|"logs experiments"| node_wandb
node_checkpoint -->|"loads at startup"| node_resident_model
node_model -->|"architecture"| node_resident_model
node_frontend -->|"POST /predict"| node_api
node_api -->|"serves UI and results"| node_frontend
node_api -->|"validates responses"| node_schemas
node_api -->|"uploaded image"| node_predict
node_resident_model -->|"UNet inference"| node_predict
node_predict -->|"overlay, count, latency"| node_api
node_runtime -->|"runs Uvicorn service"| node_api
node_docker -->|"packages"| node_runtime
node_deployment_env -.->|"frontend path"| node_api
node_deployment_env -.->|"checkpoint path"| node_checkpoint

click node_dataset "https://github.com/virennamo27/blood_segmentation/blob/main/src/dataset.py"
click node_train "https://github.com/virennamo27/blood_segmentation/blob/main/src/train.py"
click node_model "https://github.com/virennamo27/blood_segmentation/blob/main/src/model.py"
click node_metrics "https://github.com/virennamo27/blood_segmentation/blob/main/src/metrics.py"
click node_processed "https://github.com/virennamo27/blood_segmentation/blob/main/data/processed/.gitkeep"
click node_frontend "https://github.com/virennamo27/blood_segmentation/blob/main/frontend/index.html"
click node_api "https://github.com/virennamo27/blood_segmentation/blob/main/api/main.py"
click node_schemas "https://github.com/virennamo27/blood_segmentation/blob/main/api/schemas.py"
click node_predict "https://github.com/virennamo27/blood_segmentation/blob/main/src/predict.py"
click node_docker "https://github.com/virennamo27/blood_segmentation/blob/main/Dockerfile"
click node_runtime "https://github.com/virennamo27/blood_segmentation/blob/main/requirements.txt"
click node_deployment_env "https://github.com/virennamo27/blood_segmentation/blob/main/.env.example"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_bccd,node_dataset,node_train,node_model,node_metrics,node_wandb,node_checkpoint,node_processed toneBlue
class node_frontend,node_api,node_schemas,node_predict,node_resident_model,node_docker,node_runtime,node_deployment_env toneAmber
```

## Components

### Training Pipeline

- **BCCD dataset**: external dataset · Microscopy images and Pascal-VOC annotations.
- [Dataset & mask builder](https://github.com/virennamo27/blood_segmentation/blob/main/src/dataset.py): PyTorch dataset · Rasterizes VOC boxes into binary foreground masks. · `src/dataset.py`
- [Training loop](https://github.com/virennamo27/blood_segmentation/blob/main/src/train.py): training entry point · Applies augmentation, optimization, validation, and best-model selection. · `src/train.py`
- [ResNet34 UNet](https://github.com/virennamo27/blood_segmentation/blob/main/src/model.py): segmentation model · segmentation-models-pytorch UNet with ImageNet encoder initialization. · `src/model.py`
- [Dice / IoU metrics](https://github.com/virennamo27/blood_segmentation/blob/main/src/metrics.py): evaluation · `src/metrics.py`
- **Weights & Biases**: experiment service · Optional experiment logging.
- **Best model checkpoint**: model artifact · Best validation-Dice weights at runtime.
- [Processed data reservation](https://github.com/virennamo27/blood_segmentation/blob/main/data/processed/.gitkeep): data storage · Reserved but not populated. · `data/processed/.gitkeep`

### Inference Service

- [Browser UI](https://github.com/virennamo27/blood_segmentation/blob/main/frontend/index.html): static SPA · Drag-and-drop upload and prediction display. · `frontend/index.html`
- [FastAPI application](https://github.com/virennamo27/blood_segmentation/blob/main/api/main.py): HTTP API · Owns lifespan, health, multipart prediction, validation, and static delivery. · `api/main.py`
- [Response schemas](https://github.com/virennamo27/blood_segmentation/blob/main/api/schemas.py): Pydantic contract · `api/schemas.py`
- [Prediction pipeline](https://github.com/virennamo27/blood_segmentation/blob/main/src/predict.py): inference service · Preprocesses images and produces overlay, count, and latency. · `src/predict.py`
- **Loaded model**: process state · One checkpoint-backed PyTorch model is held in memory.
- [Docker image](https://github.com/virennamo27/blood_segmentation/blob/main/Dockerfile): deployment package · Python slim service image; checkpoint is mounted at runtime. · `Dockerfile`
- [Python runtime](https://github.com/virennamo27/blood_segmentation/blob/main/requirements.txt): execution environment · Python 3.11 with CUDA, MPS, or CPU fallback. · `requirements.txt`
- [Deployment paths](https://github.com/virennamo27/blood_segmentation/blob/main/.env.example): environment configuration · Configures checkpoint and frontend locations. · `.env.example`

## Connections

- 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
