# darylfung96/self-supervised-ct-segmentation architecture

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

- Interactive diagram: https://gitdiagram.com/darylfung96/self-supervised-ct-segmentation
- Repository: https://github.com/darylfung96/self-supervised-ct-segmentation

## Overview

##### Main Components
- Data Layer:
- Input Datasets: Several dataset sources (e.g., medseg, deepglobe, spacenet) as seen in the “datasets” folder and the instructions on preparing the InfNet datasets.
- Data Preprocessing: Scripts and utilities such as “combine_dataset.py”, dataloader scripts (e.g., dataloader_LungInf.py, dataloader_MulClsLungInf_UNet.py), and additional preprocessing utilities in the “utils” folder.

- Model Architecture:
- Core Segmentation Models: Under the “InfNet/Code/model_lung_infection” directory we have multiple network implementations (e.g., InfNet_ResNet.py, InfNet_Res2Net.py, InfNet_UNet.py, InfNet_VGGNet.py) along with a “backbone” directory and “module” folder for attention and UNet parts.
- Self-Supervised Component: The training scripts (e.g., main_inf-net.py, main_multi-inf-net.py) also load pre-trained self-supervised weights (via load_net_path arguments) to fine-tune the segmentation models.

- Training Pipeline:
- Training Scripts: Multiple training scripts for both baseline and self-supervised versions (e.g., MyTrain_LungInf.py, MyTrain_MulClsLungInf_UNet.py).
- Hyperparameters and Data Augmentation: Command-line arguments manage aspects such as random cutout, focal loss, lookahead optimization, and cross-validation folds.
- External Tools: TensorBoard logs (with graphs being saved under a “graphs” directory) and checkpoint handling (saving weights and logging metrics).

- Evaluation & Results Generation:
- Evaluation Modules: Python evaluation scripts (e.g., rocs_generation.py, MyTest_LungInf.py, MyTest_MulClsLungInf_UNet.py) and MATLAB scripts in “InfNet/EvaluationTool” to compute measures like dice, jaccard, ROC curves, etc.
- Results Organization: Output directories (e.g., “InfNet/Results” subfolders) to store segmentation results by model type.

##### Relationships and Data Flow
- From the training pipelines, outputs (i.e., model checkpoints, training logs) are generated, which then serve as input for the evaluation modules.
- Evaluation outputs (e.g., segmentation masks, ROC curves, metric logs) are directed to dedicated “Results” folders and also visualized via TensorBoard and MATLAB-based tools.
- A block representing model comparison functionality (as seen with load_net_path_2) that takes outputs from different training experiments and compares evaluation metrics statistically.

##### Architectural Patterns and Design Principles
- Modularity: The clear separation between model definition (in the “Code” directory), data utilities, and training/evaluation scripts indicates a modular design.
- Reusability & Extensibility: The use of common utility modules and the separation of backbone and module components enable easy modification or addition of new network architectures.
- Experimentation and Cross-Validation: Support for cross-validation and different training regimes (baseline vs. self-supervised) suggests that the system is designed for empirical performance improvement.

##### Technologies and Libraries
- Deep Learning Libraries: Likely PyTorch or a similar deep learning framework given the structure of model scripts (e.g., “.py” files and checkpoint-based model saving).
- Data Visualization: TensorBoard for monitoring training graphs.
- Evaluation Tools: MATLAB scripts for evaluation measures and ROC generation.
- Utility Scripts: Python scripts for data format conversion, dataset splitting, and network evaluation.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Data Ingestion & Preprocessing"
        D1["Input Datasets"]:::data
        D2a["combine_dataset.py"]:::data
        D2b["Data Loader Utilities"]:::data
    end

    subgraph "Model Architecture"
        M1["Core Segmentation Models"]:::model
        M2["Backbone Modules"]:::model
        M3["Additional Network Modules"]:::model
        M4["Self-Supervised Component"]:::model
    end

    subgraph "Training Pipeline"
        T1["Baseline Training"]:::training
        T2["Self-Supervised Training"]:::training
        T3["Checkpoints & Logs"]:::training
    end

    subgraph "Evaluation & Results"
        E1["Evaluation Scripts"]:::evaluation
        E2["MATLAB Evaluation Tools"]:::evaluation
        E3["Results Directory"]:::evaluation
    end

    %% Data Flow from Input to Preprocessing
    D1 --> D2a
    D1 --> D2b

    %% Flow from Data Preprocessing to Training Pipeline
    D2a --> T1
    D2a --> T2
    D2b --> T1
    D2b --> T2

    %% Incorporating Model Architecture into Training
    M1 --> T1
    M1 --> T2
    M4 --> T1
    M4 --> T2

    %% Training produces checkpoints and logs
    T1 --> T3
    T2 --> T3

    %% Evaluation consumes training outputs
    T3 --> E1
    T3 --> E2
    T3 --> E3

    %% Click Events
    click D1 "https://github.com/darylfung96/self-supervised-ct-segmentation/tree/master/datasets"
    click D2a "https://github.com/darylfung96/self-supervised-ct-segmentation/blob/master/InfNet/combine_dataset.py"
    click D2b "https://github.com/darylfung96/self-supervised-ct-segmentation/tree/master/InfNet/Code/utils/"
    click M1 "https://github.com/darylfung96/self-supervised-ct-segmentation/tree/master/InfNet/Code/model_lung_infection/"
    click M2 "https://github.com/darylfung96/self-supervised-ct-segmentation/tree/master/InfNet/Code/model_lung_infection/backbone/"
    click M3 "https://github.com/darylfung96/self-supervised-ct-segmentation/tree/master/InfNet/Code/model_lung_infection/module/"
    click M4 "https://github.com/darylfung96/self-supervised-ct-segmentation/blob/master/main_inf-net.py"
    click T1 "https://github.com/darylfung96/self-supervised-ct-segmentation/blob/master/InfNet/MyTrain_LungInf.py"
    click T2 "https://github.com/darylfung96/self-supervised-ct-segmentation/blob/master/InfNet/MyTrain_MulClsLungInf_UNet.py"
    click T3 "https://github.com/darylfung96/self-supervised-ct-segmentation/tree/master/InfNet/Results/"
    click E1 "https://github.com/darylfung96/self-supervised-ct-segmentation/blob/master/InfNet/MyTest_LungInf.py"
    click E2 "https://github.com/darylfung96/self-supervised-ct-segmentation/tree/master/InfNet/EvaluationTool/"
    click E3 "https://github.com/darylfung96/self-supervised-ct-segmentation/tree/master/InfNet/Results/"

    %% Styles
    classDef data fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef model fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef training fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef evaluation fill:#f8d7da,stroke:#721c24,stroke-width:2px;
```
