# sczhou/lednet architecture

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

- Interactive diagram: https://gitdiagram.com/sczhou/lednet
- Repository: https://github.com/sczhou/lednet

## Overview

##### Project type and purpose
- This is a research‐oriented, open-source computer-vision toolkit (“LEDNet”) for joint low-light image enhancement and deblurring.
- It is built on top of the BasicSR framework and provides:
- A core library (`basicsr/…`) of network architectures, data loaders, loss/metric functions, ops, and utilities.
- Training scripts (`basicsr/train.py`), inference scripts (`inference_lednet.py`, `predict.py`), and utility scripts (`scripts/*.py`).
- Configuration files in `options/`, model weights in `weights/`, sample inputs in `inputs/`, and outputs in `results/`.

##### File structure
- Top-level directories:
- `basicsr/` – the core library with submodules: `archs/`, `data/`, `losses/`, `metrics/`, `models/`, `ops/`, `utils/`.
- `options/` – YAML config files for training (LEDNet, LEDNetGAN).
- `scripts/` – helper scripts for downloading models, calculating IQA metrics, generating synthetic data.
- `assets/`, `inputs/`, `weights/` – static assets, example inputs, and model checkpoints.
- Entry‐point scripts: `basicsr/train.py`, `inference_lednet.py`, `predict.py`.
- Build/install: `basicsr/setup.py`, `requirements.txt`, `cog.yaml`.

##### Key points from the README
- The library relies on PyTorch & CUDA.
- Training is invoked via CLI, passing a YAML config.
- Inference loads a pretrained model, takes an input folder, saves outputs.
- Additional utilities for evaluation, data generation, and Cog integration.

##### Main components
A. External Dependencies
- PyTorch (GPU/CUDA)
- BasicSR framework (internal)
- Third-party packages (from `requirements.txt`)
B. Core Library (`basicsr/`)
1. Model Architectures (`archs/`)
2. Data Handling (`data/`)
3. Losses & Metrics (`losses/`, `metrics/`)
4. Model Wrappers & Schedulers (`models/`)
5. Custom CUDA Ops (`ops/`)
6. Utilities & Registry (`utils/`)
C. Configuration Layer
- YAML opts in `options/`
- CLI option parser in `basicsr/utils/options.py`
D. CLI Scripts
1. Training (`basicsr/train.py`)
2. Inference (`inference_lednet.py`, `predict.py`)
3. Evaluation & Data Utilities (`scripts/*.py`)
E. Data Stores
- Input datasets (LOL-Blur under `inputs/` or user-downloaded data)
- Pretrained model weights (`weights/`)
- Outputs (`results/`)
F. Optional Deployment
- Cog container defined in `cog.yaml`

##### Relationships & data flow
- Config YAML → CLI script → Options parser → Core library is initialized (models, dataloaders, optimizers).
- Training flow:
Dataset → DataLoader → Model (forward) → Loss → Backward → Optimizer step → Checkpoint & Logs
- Inference flow:
Load pretrained weights → DataLoader on test inputs → Model → Save output images
- Utility scripts may call the core library’s evaluation functions to compute PSNR/SSIM.
- Cog runs a container that executes the same inference pipeline.

##### Architectural patterns & design principles
- Modular registry pattern: models/architectures registered via a central registry.
- Separation of concerns: distinct subpackages for data, models, losses, metrics.
- CLI + config-driven design: behavior controlled by YAML options, no hardcoded paths.
- Extensibility: new architectures, losses or datasets can be plugged in via the registry.

##### Diagramming recommendations
- Rounded rectangles for scripts/entrypoints
- Labels:
- Name each module/package clearly, include file path hints (e.g., `basicsr/archs/`)
- Direction of training vs. inference flows
- Important IO points (e.g., reading YAML, writing checkpoints, saving images)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Dependencies
    subgraph "External Dependencies"
        PT["PyTorch & CUDA"]:::external
        TP["Third-party Packages"]:::external
    end

    %% Core Library Modules
    subgraph "Core Library"
        direction TB
        A["Model Architectures\n(basicsr/archs/)"]:::core
        B["Data Handling\n(basicsr/data/)"]:::core
        C["Loss Functions\n(basicsr/losses/)"]:::core
        D["Metric Functions\n(basicsr/metrics/)"]:::core
        E["Model Wrappers & Training Logic\n(basicsr/models/)"]:::core
        F["Custom CUDA Ops\n(basicsr/ops/)"]:::core
        G["Utility Modules & Registry\n(basicsr/utils/)"]:::core
    end

    %% Configuration Layer
    subgraph "Configuration Layer"
        CFG["YAML Configs\n(options/)"]:::config
        OP["CLI Option Parser\n(basicsr/utils/options.py)"]:::config
    end

    %% CLI Applications & Scripts
    subgraph "CLI Applications"
        direction TB
        T["Training Script\n(basicsr/train.py)"]:::app
        I1["Inference Script\n(inference_lednet.py)"]:::app
        I2["Inference Script\n(predict.py)"]:::app
        U1["calc IQA\n(scripts/calculate_iqa_pair.py)"]:::app
        U2["calc IQA Detail\n(scripts/calculate_iqa_pair_detail.py)"]:::app
        U3["download Models\n(scripts/download_pretrained_models.py)"]:::app
        U4["generate Low-light imgs\n(scripts/generate_low_light_imgs.py)"]:::app
    end

    %% Optional Deployment
    subgraph "Optional Deployment"
        Cog["Cog Container\n(cog.yaml)"]:::external
    end

    %% Data Stores
    subgraph "Data Stores"
        direction TB
        IN1["Inputs\n(inputs/real/)"]:::data
        IN2["Inputs\n(inputs/synthetic/)"]:::data
        W["Model Weights\n(weights/)"]:::data
        R["Results\n(results/)"]:::data
    end

    %% Relationships & Data Flow
    CFG --- OP
    CFG -.-> T
    OP -.-> T

    T --> A
    T --> B
    T --> C
    T --> D
    T --> E
    T --> F
    T --> G
    T --> IN1
    T --> IN2
    T --> W
    T --> R

    I1 --> B
    I1 --> E
    I1 --> W
    I1 --> R

    I2 --> B
    I2 --> E
    I2 --> W
    I2 --> R

    U1 --> C
    U1 --> D
    U1 --> B

    U2 --> C
    U2 --> D
    U2 --> B

    U3 --> W
    U4 --> B

    Cog --> I1
    Cog --> I2

    PT --> F
    G --> F
    TP --> G

    %% Click Events
    click A "https://github.com/sczhou/lednet/tree/master/basicsr/archs/"
    click B "https://github.com/sczhou/lednet/tree/master/basicsr/data/"
    click C "https://github.com/sczhou/lednet/tree/master/basicsr/losses/"
    click D "https://github.com/sczhou/lednet/tree/master/basicsr/metrics/"
    click E "https://github.com/sczhou/lednet/tree/master/basicsr/models/"
    click F "https://github.com/sczhou/lednet/tree/master/basicsr/ops/"
    click G "https://github.com/sczhou/lednet/tree/master/basicsr/utils/"
    click CFG "https://github.com/sczhou/lednet/blob/master/options/train_LEDNet.yml"
    click CFG "https://github.com/sczhou/lednet/blob/master/options/train_LEDNetGAN.yml"
    click OP "https://github.com/sczhou/lednet/blob/master/basicsr/utils/options.py"
    click T "https://github.com/sczhou/lednet/blob/master/basicsr/train.py"
    click I1 "https://github.com/sczhou/lednet/blob/master/inference_lednet.py"
    click I2 "https://github.com/sczhou/lednet/blob/master/predict.py"
    click U1 "https://github.com/sczhou/lednet/blob/master/scripts/calculate_iqa_pair.py"
    click U2 "https://github.com/sczhou/lednet/blob/master/scripts/calculate_iqa_pair_detail.py"
    click U3 "https://github.com/sczhou/lednet/blob/master/scripts/download_pretrained_models.py"
    click U4 "https://github.com/sczhou/lednet/blob/master/scripts/generate_low_light_imgs.py"
    click IN1 "https://github.com/sczhou/lednet/tree/master/inputs/real/"
    click IN2 "https://github.com/sczhou/lednet/tree/master/inputs/synthetic/"
    click W "https://github.com/sczhou/lednet/tree/master/weights/"
    click Cog "https://github.com/sczhou/lednet/blob/master/cog.yaml"

    %% Styles
    classDef external fill:#f2f2f2,stroke:#333,stroke-width:1px
    classDef core fill:#cfe2f3,stroke:#333,stroke-width:1px
    classDef config fill:#d9ead3,stroke:#333,stroke-width:1px
    classDef app fill:#fff2cc,stroke:#333,stroke-width:1px
    classDef data fill:#ead1dc,stroke:#333,stroke-width:1px
```
