# mic-dkfz/nndetection architecture

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

- Interactive diagram: https://gitdiagram.com/mic-dkfz/nndetection
- Repository: https://github.com/mic-dkfz/nndetection

## Overview

##### Main Components
- Data and Configuration Management:
- Configuration files in nndet/conf (with YAML files and prep/train configurations).
- Use of Hydra for dynamic configuration.
- Environment settings (e.g., det_data, det_models, OMP_NUM_THREADS).

- Core Libraries and Modules:
○ Architecture: modules for encoders, decoders, heads, blocks, layers, initialization.
○ Preprocessing and Planning: “nndet/preprocessing” and “nndet/planning” (with experiment and properties registries).
○ Losses: Separate module that implements segmentation, regression and classification losses.
○ PyTorch Lightning Module: “nndet/ptmodule” for training pipelines.
○ Inference modules: This includes assembling outputs via “nndet/inference”, predictor and ensembler components.

- External Registries and Plug-In Points:
- Registries (augmentation, dataloader, planner, module registries) which support modular extension.
- How these registries allow switching between underlying algorithms by reading configuration files.

- Data Input and I/O:
- Components in “nndet/io” covering augmentation, datamodule (data loading), transforms, and patching.
- Expected input data formats (nifti images, labels in JSON) and how data flows from raw to preprocessed stages.

- Scripts and Execution Flow:
- On the periphery, include execution scripts found in the “scripts” folder:
○ Data preparation (nndet_prep, preprocess.py)
○ Unpacking (nndet_unpack)
○ Training (nndet_train and training/optimizer)
○ Consolidation and parameter sweep (nndet_consolidate, nndet_sweep)
○ Inference (nndet_predict)
○ Evaluation (nndet_eval)
- That these are the command line interfaces that trigger pipelines within the core modules.

- Docker and Environment:
- Dockerfile and container setup as an “Infrastructure / Deployment” block. Show environment variables (det_data, det_models) and mounting strategies.
- Mention that the project supports both Docker and local conda installations with tailored environment setups.

##### Establish Relationships and Data Flow
- From preprocessing, indicate that processed data feeds into both planning and training pipelines.
- That the training module interacts with the core nnDetection module (using PyTorch Lightning wrappers) to produce trained models.
- Detail that trained models are then consolidated (via nndet_consolidate) and further utilized by inference modules for prediction/output.
- To show the dependency from configuration files (Hydra-driven) into nearly every component.

##### Architectural Patterns and Principles
- Modularity: Emphasize how the architecture leverages a well-organized package structure (separating encoders, decoders, heads, etc.) and registries to allow plug-and-play functionality.
- Data-Driven & Self-configuring: Reflect the automated configuration planning stage that analyzes data properties (refer to planning and preprocessing folders) and dynamically adapts network architectures.
- Pipeline Flow: The project uses sequential stages (prep → train → inference → evaluation) that are each represented as distinct blocks.

##### Technologies and Frameworks
- Use of PyTorch and its Lightning interface for training.
- CUDA-specific components in “nndet/csrc” (with both CPU and CUDA implementations of operations such as NMS).
- Hydra for configuration and possibly mention integration with external tools such as MLflow.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Top Layer: User Interaction
    subgraph "User Interaction (Top Layer)"
        K["Scripts & Execution Flow"]:::script
        R["Raw Medical Images"]:::io
        MC["Model Consolidation"]:::script
        ECLI["Evaluation (CLI)"]:::script
    end

    %% Middle Layer: Core Components
    subgraph "Core Components (Middle Layer)"
        A["Data & Configuration Management"]:::config
        B["Core Architecture Modules"]:::core
        C["Core Functionalities"]:::core
        D["Preprocessing & Planning"]:::io
        E["Losses Implementation"]:::core
        F["PyTorch Lightning Module (Training)"]:::core
        G["Inference Modules"]:::core
        H["Data I/O Management"]:::io
        I["External Registries (Utils)"]:::config
        J["External Registries (Evaluator)"]:::config
        HYD["Hydra Configuration"]:::config
    end

    %% Bottom Layer: Infrastructure / Deployment
    subgraph "Infrastructure / Deployment (Bottom Layer)"
        L["Docker & Environment"]:::infra
    end

    %% Data Flow Connections
    R -->|"ingests"| D
    K -->|"triggers"| D
    D -->|"plans&preps"| F
    F -->|"produces_models"| MC
    MC -->|"feeds_into"| G
    G -->|"supplies_results"| ECLI

    %% Configuration Influence
    HYD --> A
    A --> D
    A --> F
    A --> G
    HYD --> D
    HYD --> F

    %% Integration of Supporting Modules
    B --> F
    C --> F
    E --> F
    H --> D
    I --> D
    I --> F
    I --> G
    J --> G

    %% Execution via Scripts
    K -->|"executes"| F
    K -->|"executes"| MC
    K -->|"executes"| ECLI

    %% Infrastructure Impact
    L -->|"provides_env"| A
    L -->|"provides_env"| H

    %% Style Definitions
    classDef config fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef core fill:#A9DFBF,stroke:#145A32,stroke-width:2px;
    classDef io fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef script fill:#D2B4DE,stroke:#6C3483,stroke-width:2px;
    classDef infra fill:#D5DBDB,stroke:#7F8C8D,stroke-width:2px;

    %% Click Events for Component Mappings
    click A "https://github.com/mic-dkfz/nndetection/tree/main/nndet/conf"
    click B "https://github.com/mic-dkfz/nndetection/tree/main/nndet/arch"
    click C "https://github.com/mic-dkfz/nndetection/tree/main/nndet/core/boxes"
    click D "https://github.com/mic-dkfz/nndetection/tree/main/nndet/preprocessing"
    click E "https://github.com/mic-dkfz/nndetection/tree/main/nndet/losses"
    click F "https://github.com/mic-dkfz/nndetection/tree/main/nndet/ptmodule"
    click G "https://github.com/mic-dkfz/nndetection/tree/main/nndet/inference"
    click H "https://github.com/mic-dkfz/nndetection/tree/main/nndet/io"
    click I "https://github.com/mic-dkfz/nndetection/tree/main/nndet/utils/registry"
    click J "https://github.com/mic-dkfz/nndetection/tree/main/nndet/evaluator/registry"
    click K "https://github.com/mic-dkfz/nndetection/tree/main/scripts"
    click L "https://github.com/mic-dkfz/nndetection/tree/main/Dockerfile"
```
