# kousumi04/denoiser architecture

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

- Interactive diagram: https://gitdiagram.com/kousumi04/denoiser
- Repository: https://github.com/kousumi04/denoiser

## Overview

1. **System shape**
   This repo is an end-to-end image denoising system centered on **DRUNet**. It has three major layers: offline data/training code, a Python inference backend, and a Next.js frontend that sends user images to that backend.

2. **Offline data preparation**
   `preprocessing_pipeline/preprocess_sidd.py` turns raw **SIDD** scene folders into paired noisy/clean patch tensors saved as `.pt` files. `data_pipeline/drunet_datasets.py` builds the other training input by pairing **DIV2K** clean images with multiple noisy domains and computing a per-sample `sigma_hat`.

3. **Training boundary**
   `training/training_drunet.py` is the main training entry point, and `training/fine_tune_drunet.py` supports continued training from an existing checkpoint. The model architecture lives in `training/models/drunet.py`, so training is separated from the network definition.

4. **Training outputs and shared model artifact**
   Training writes checkpoints into `checkpoints/`, including the unified model weights used for deployment. That checkpoint file is the main artifact connecting the offline ML pipeline to the serving system.

5. **Evaluation and local inference**
   `models_eval/denoiser_eval.py` is the offline evaluation side, tracking quality with metrics such as PSNR. `inference/infer_drunet.py` is the local CLI path for denoising a single image with a chosen checkpoint, or estimating noise if a sigma is not provided.

6. **Backend serving layer**
   `app.py` is the production inference service: a **FastAPI** app packaged for a **Hugging Face Docker Space** via `Dockerfile` and `requirements-space.txt`. On startup it loads `checkpoints/DRUNET_DENOISER.pt`, exposes `GET /health`, and exposes `POST /denoise`.

7. **Request/response flow through the backend**
   The serving boundary is simple: the client uploads a noisy image as multipart form data using the field name `image`, and the backend returns a denoised **PNG** response. This means there is no persistent session state or database in the runtime path; inference is stateless and file/model-driven.

8. **Frontend boundary**
   The UI is a **Next.js** app deployed on **Vercel**. The frontend uses a proxy/API route under `app/api/denoise/route.ts` to forward image uploads to the Hugging Face backend, with deployment behavior controlled by environment values like `HF_IMAGE_API_URL`, request method, request mode, and form field name.

9. **Repository layout nuance**
   The repo contains a Next.js app structure both at the **root** and under `frontend/`. The root-level `app/`, `next.config.mjs`, and package files appear to be the active Vercel-facing app, while `frontend/` mirrors the same frontend structure for an alternate packaging/deployment layout.

10. **External services and runtime split**
   The live architecture is split across two hosted services: **Vercel** for the presentation layer and **Hugging Face Spaces** for GPU/CPU inference. The Python side uses PyTorch/FastAPI, while the frontend side uses TypeScript/Next.js; the only runtime dependency between them is the HTTP image-denoising endpoint.

Main entry points: [`app.py`](https://github.com/kousumi04/denoiser/blob/main/app.py) (FastAPI app), [`app/api/denoise/route.ts`](https://github.com/kousumi04/denoiser/blob/main/app/api/denoise/route.ts) (API route), [`training/training_drunet.py`](https://github.com/kousumi04/denoiser/blob/main/training/training_drunet.py) (Train DRUNet), [`frontend/app/api/denoise/route.ts`](https://github.com/kousumi04/denoiser/blob/main/frontend/app/api/denoise/route.ts) (Frontend API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_offline["Offline ML"]
  node_preprocess_sidd["SIDD prep<br/>preprocess<br/>[preprocess_sidd.py]"]
  node_drunet_datasets["Train data<br/>dataset builder<br/>[drunet_datasets.py]"]
  node_train_drunet["Train DRUNet<br/>trainer<br/>[training_drunet.py]"]
  node_fine_tune_drunet["Fine-tune<br/>trainer"]
  node_drunet_model["DRUNet<br/>network<br/>[drunet.py]"]
  node_denoiser_eval["Evaluate<br/>[denoiser_eval.py]"]
  node_infer_cli["CLI infer<br/>inference<br/>[infer_drunet.py]"]
  node_checkpoint_artifact[("Checkpoint<br/>artifact")]
end

subgraph group_serving["Backend Serving"]
  node_app_py["FastAPI app<br/>[app.py]"]
  node_dockerfile["Docker<br/>deploy"]
  node_requirements_space["Space deps<br/>env"]
end

subgraph group_web["Web Frontend"]
  node_root_route["API route<br/>proxy<br/>[route.ts]"]
  node_root_page["Home page<br/>ui<br/>[page.tsx]"]
  node_root_layout["App shell<br/>ui shell<br/>[layout.tsx]"]
  node_frontend_route["Frontend API<br/>proxy<br/>[route.ts]"]
  node_frontend_page["Frontend page<br/>ui<br/>[page.tsx]"]
  node_frontend_layout["Frontend shell<br/>ui shell<br/>[layout.tsx]"]
  node_frontend_pkg["Frontend pkg<br/>[package.json]"]
end

subgraph group_external["External"]
  node_vercel_host(("Vercel<br/>hosted runtime"))
  node_hf_space(("HF Space<br/>hosted runtime"))
  node_user_upload["User image<br/>input"]
end

node_preprocess_sidd -->|"creates training pairs"| node_drunet_datasets
node_drunet_datasets -->|"feeds training"| node_train_drunet
node_drunet_model -->|"defines network"| node_train_drunet
node_drunet_model -->|"loads model"| node_fine_tune_drunet
node_train_drunet -->|"writes weights"| node_checkpoint_artifact
node_fine_tune_drunet -->|"updates weights"| node_checkpoint_artifact
node_checkpoint_artifact -->|"evaluated by"| node_denoiser_eval
node_checkpoint_artifact -->|"used by"| node_infer_cli
node_checkpoint_artifact -->|"loaded on startup"| node_app_py
node_app_py -->|"runs on"| node_hf_space
node_dockerfile -->|"packages"| node_app_py
node_requirements_space -->|"depends on"| node_app_py
node_user_upload -->|"uploads image"| node_root_route
node_root_route -->|"forwards request"| node_app_py
node_root_route -->|"served by"| node_vercel_host
node_root_page -->|"calls"| node_root_route
node_root_layout -->|"wraps"| node_root_page
node_frontend_pkg -->|"contains"| node_frontend_route
node_frontend_page -->|"calls"| node_frontend_route
node_frontend_layout -->|"wraps"| node_frontend_page
node_frontend_route -->|"proxies to"| node_hf_space
node_frontend_pkg -->|"deploys on"| node_vercel_host

click node_preprocess_sidd "https://github.com/kousumi04/denoiser/blob/main/preprocessing_pipeline/preprocess_sidd.py"
click node_drunet_datasets "https://github.com/kousumi04/denoiser/blob/main/data_pipeline/drunet_datasets.py"
click node_train_drunet "https://github.com/kousumi04/denoiser/blob/main/training/training_drunet.py"
click node_fine_tune_drunet "https://github.com/kousumi04/denoiser/blob/main/training/fine_tune_drunet.py"
click node_drunet_model "https://github.com/kousumi04/denoiser/blob/main/training/models/drunet.py"
click node_denoiser_eval "https://github.com/kousumi04/denoiser/blob/main/models_eval/denoiser_eval.py"
click node_infer_cli "https://github.com/kousumi04/denoiser/blob/main/inference/infer_drunet.py"
click node_app_py "https://github.com/kousumi04/denoiser/blob/main/app.py"
click node_dockerfile "https://github.com/kousumi04/denoiser/tree/main/Dockerfile"
click node_requirements_space "https://github.com/kousumi04/denoiser/blob/main/requirements-space.txt"
click node_root_route "https://github.com/kousumi04/denoiser/blob/main/app/api/denoise/route.ts"
click node_root_page "https://github.com/kousumi04/denoiser/blob/main/app/page.tsx"
click node_root_layout "https://github.com/kousumi04/denoiser/blob/main/app/layout.tsx"
click node_frontend_route "https://github.com/kousumi04/denoiser/blob/main/frontend/app/api/denoise/route.ts"
click node_frontend_page "https://github.com/kousumi04/denoiser/blob/main/frontend/app/page.tsx"
click node_frontend_layout "https://github.com/kousumi04/denoiser/blob/main/frontend/app/layout.tsx"
click node_frontend_pkg "https://github.com/kousumi04/denoiser/blob/main/frontend/package.json"

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_preprocess_sidd,node_drunet_datasets,node_train_drunet,node_fine_tune_drunet,node_drunet_model,node_denoiser_eval,node_infer_cli,node_checkpoint_artifact toneBlue
class node_app_py,node_dockerfile,node_requirements_space toneAmber
class node_root_route,node_root_page,node_root_layout,node_frontend_route,node_frontend_page,node_frontend_layout,node_frontend_pkg toneMint
class node_vercel_host,node_hf_space,node_user_upload toneRose
```

## Components

### Offline ML

Data prep, training, evaluation, and local inference

- [SIDD prep](https://github.com/kousumi04/denoiser/blob/main/preprocessing_pipeline/preprocess_sidd.py): preprocess · Converts raw SIDD scenes into paired noisy/clean patch tensors · `preprocessing_pipeline/preprocess_sidd.py`
- [Train data](https://github.com/kousumi04/denoiser/blob/main/data_pipeline/drunet_datasets.py): dataset builder · Builds DIV2K plus noisy-domain training pairs and sigma estimates · `data_pipeline/drunet_datasets.py`
- [Train DRUNet](https://github.com/kousumi04/denoiser/blob/main/training/training_drunet.py): trainer · Main training entry point for DRUNet · `training/training_drunet.py`
- [Fine-tune](https://github.com/kousumi04/denoiser/blob/main/training/fine_tune_drunet.py): trainer · Continues training from an existing checkpoint · `training/fine_tune_drunet.py`
- [DRUNet](https://github.com/kousumi04/denoiser/blob/main/training/models/drunet.py): network · Core denoising model architecture · `training/models/drunet.py`
- [Evaluate](https://github.com/kousumi04/denoiser/blob/main/models_eval/denoiser_eval.py): eval · Offline quality evaluation and PSNR-style metrics · `models_eval/denoiser_eval.py`
- [CLI infer](https://github.com/kousumi04/denoiser/blob/main/inference/infer_drunet.py): inference · Single-image local denoising and noise estimation path · `inference/infer_drunet.py`
- **Checkpoint**: artifact · Shared trained model weights used by serving and CLI inference

### Backend Serving

FastAPI inference service and deployment packaging

- [FastAPI app](https://github.com/kousumi04/denoiser/blob/main/app.py): service · Production inference service exposing health and denoise endpoints · `app.py`
- [Docker](https://github.com/kousumi04/denoiser/tree/main/Dockerfile): deploy · Container image for the Hugging Face Space runtime · `Dockerfile`
- [Space deps](https://github.com/kousumi04/denoiser/blob/main/requirements-space.txt): env · Python dependencies for the deployed backend · `requirements-space.txt`

### Web Frontend

Next.js UI and API proxy layer

- [API route](https://github.com/kousumi04/denoiser/blob/main/app/api/denoise/route.ts): proxy · Root Next.js route that forwards image uploads to the backend · `app/api/denoise/route.ts`
- [Home page](https://github.com/kousumi04/denoiser/blob/main/app/page.tsx): ui · Root-level Next.js page for the Vercel-facing app · `app/page.tsx`
- [App shell](https://github.com/kousumi04/denoiser/blob/main/app/layout.tsx): ui shell · Root-level Next.js layout and shared page chrome · `app/layout.tsx`
- [Frontend API](https://github.com/kousumi04/denoiser/blob/main/frontend/app/api/denoise/route.ts): proxy · Mirrored Next.js API route for alternate frontend packaging · `frontend/app/api/denoise/route.ts`
- [Frontend page](https://github.com/kousumi04/denoiser/blob/main/frontend/app/page.tsx): ui · Mirrored Next.js page entrypoint · `frontend/app/page.tsx`
- [Frontend shell](https://github.com/kousumi04/denoiser/blob/main/frontend/app/layout.tsx): ui shell · Mirrored Next.js layout and shared chrome · `frontend/app/layout.tsx`
- [Frontend pkg](https://github.com/kousumi04/denoiser/blob/main/frontend/package.json): package · Standalone frontend package and build configuration · `frontend/package.json`

### External

Hosted runtime targets and third-party services

- **Vercel**: hosted runtime · Deployment target for the presentation layer
- **HF Space**: hosted runtime · Deployment target for the backend inference service
- **User image**: input · Noisy image uploaded from the browser

## Connections

- SIDD prep → Train data: creates training pairs · Preprocessed SIDD data complements the broader training set construction
- Train data → Train DRUNet: feeds training · Training consumes paired noisy/clean examples and sigma metadata
- DRUNet → Train DRUNet: defines network · Training instantiates the DRUNet architecture
- DRUNet → Fine-tune: loads model · Fine-tuning continues from the same network definition
- Train DRUNet → Checkpoint: writes weights · Primary training output is the deployable checkpoint
- Fine-tune → Checkpoint: updates weights · Fine-tuning refreshes the shared model artifact
- Checkpoint → Evaluate: evaluated by · Offline evaluation scores trained checkpoints
- Checkpoint → CLI infer: used by · CLI inference loads the selected checkpoint
- Checkpoint → FastAPI app: loaded on startup · Backend service initializes the deployed model weights
- FastAPI app → HF Space: runs on · The FastAPI backend is packaged for the hosted inference runtime
- Docker → FastAPI app: packages · Container image bundles the backend service
- Space deps → FastAPI app: depends on · Runtime dependencies support the service stack
- User image → API route: uploads image · Browser sends multipart image data to the web proxy
- API route → FastAPI app: forwards request · Next.js API route relays the upload to the backend denoising endpoint
- API route → Vercel: served by · Root frontend is deployed as the presentation layer
- Home page → API route: calls · UI submits denoise requests through the local API route
- App shell → Home page: wraps · Shared page shell surrounds the home page
- Frontend pkg → Frontend API: contains · Alternate frontend package includes its own API proxy
- Frontend page → Frontend API: calls · Mirrored UI submits requests through its proxy route
- Frontend shell → Frontend page: wraps · Mirrored shell surrounds the frontend page
- Frontend API → HF Space: proxies to · Alternate deployment forwards uploads to the same backend service
- Frontend pkg → Vercel: deploys on · Frontend package is intended for Vercel-style deployment
