# zhangyh0502/pcasam architecture

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

- Interactive diagram: https://gitdiagram.com/zhangyh0502/pcasam
- Repository: https://github.com/zhangyh0502/pcasam

## Overview

##### Project type and purpose
- PCaSAM is a research-oriented, deep-learning segmentation toolkit specialized for prostate cancer on multi-modal MRI. It integrates a medical “foundation” model (Segment Anything Model, SAM) with custom networks (e.g., UNet fusion) to perform training and inference.

##### Main components of the system
a. Data Ingestion & Preprocessing
- datamodule/ (MRI_CancerSeg, picai_decomp, prostate158_decomp) for loading, splitting, and batching multi-modal MRI scans.
- utils/pre_CT_MR.py, utils/pre_grey_rgb.py for modality normalization and format conversion.
b. Core Model & Training
- OurModel.py & networks/ (unet.py, fusion.py, region_to_location.py) define model architectures.
- segment_anything/ subpackage holds the SAM backbone (image_encoder, prompt_encoder, mask_decoder, transformer).
- loss_function.py encapsulates task-specific loss metrics.
- train_*.py scripts (train_Ours.py, train_CoarseSeg.py, train_multi_gpus.py, train_Ours_TTA.py) orchestrate training loops, using config_args.py for hyperparameters.
c. Inference & Evaluation
- MedSAM_Inference.py & MedSAM_Interface.py wrap SAM-based inference.
- gui.py provides a simple UI for interactive segmentation.
- comparisons/ holds side-by-side inference and training code for DeepLabV3+, nnU-Net, vanilla SAM—used for benchmarking.
- utils/demo.py and plot_figure.py for visualizing results, and test_sam_for_ours.py for sanity checks.
d. Extensions
- extensions/point_prompt, text_prompt, seg_3dnii_sparse_marker each add specialized prompting or 3D label interpolation.
e. Configuration & Packaging
- setup.py and medsam.egg-info/ for packaging.
- config_args.py centralizes CLI flags for dataset paths, model settings, training hyperparameters.
f. Workspace & Outputs
- work_dir/MedSAM stores trained checkpoints, logs, and output masks.

##### Relationships and data flow
- Data Modules feed preprocessed MRI tensors into the Training pipeline.
- Training pipeline instantiates OurModel or SAM+fusion, computes losses, and writes checkpoints to work_dir.
- GUI calls inference APIs for live segmentation.
- Comparison scripts operate in parallel to benchmark other architectures; outputs feed into utils/plot_figure.py.

##### Architectural patterns & design principles
- Modular layering: data/preprocessing → core model → training/inference scripts → evaluation.
- Separation of concerns: comparison, extension, and core logic live in distinct directories.
- Reuse of external foundation model (SAM) via subpackage rather than reimplementation.
- Config-driven CLI for reproducibility.

##### Technologies & frameworks
- Python 3.x, PyTorch (in segment_anything modeling files and networks/), Numpy (npz data lists), Click or argparse (config_args.py), Jupyter notebooks for tutorials, simple Tkinter or PyQt (gui.py).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Data Ingestion & Preprocessing
    subgraph "Data Ingestion & Preprocessing"
        direction TB
        DM["datamodule/"]:::core
        MC["MRI_CancerSeg.py"]:::core
        PD["picai_decomp.py"]:::core
        PS["postate158_decomp.py"]:::core
        DL["data_list_*.npz"]:::core
        PCT["pre_CT_MR.py"]:::core
        PGR["pre_grey_rgb.py"]:::core
        FMT["format_convert.py"]:::core
        SPL["split.py"]:::core
    end

    %% Core Model & Training
    subgraph "Core Model & Training"
        direction TB
        OM["OurModel.py"]:::core
        subgraph "networks/"
            direction TB
            UN["unet.py"]:::core
            FU["fusion.py"]:::core
            RL["region_to_location.py"]:::core
        end
        subgraph "segment_anything/"
            direction TB
            AMG["automatic_mask_generator.py"]:::core
            BS["build_sam.py"]:::core
            PRED["predictor.py"]:::core
            SMODEL["modeling/*.py"]:::core
            SUTIL["utils/*.py"]:::core
        end
        LF["loss_function.py"]:::core
        CA["config_args.py"]:::core
        subgraph "Training Scripts"
            direction TB
            T1["train_Ours.py"]:::core
            T2["train_CoarseSeg.py"]:::core
            T3["train_multi_gpus.py"]:::core
            T3s["train_multi_gpus.sh"]:::core
            T4["train_Ours_TTA.py"]:::core
            T5["train_one_gpu.py"]:::core
        end
    end

    %% Workspace & Outputs
    WD["work_dir/MedSAM"]:::store

    %% Inference & Evaluation
    subgraph "Inference & Evaluation"
        direction TB
        MI["MedSAM_Inference.py"]:::core
        MX["MedSAM_Interface.py"]:::core
        UI["gui.py"]:::core
        DEMO["demo.py"]:::core
        PFIG["plot_figure.py"]:::core
        TS["test_sam_for_ours.py"]:::core
        subgraph "Comparisons/" 
            direction TB
            CP1["infer_deeplabv3_res50_2D.py"]:::comp
            CP2["infer_deeplabv3_res50_3D.py"]:::comp
            CP3["train_deeplabv3_res50.py"]:::comp
            CP4["infer_SAM_2D_npz.py"]:::comp
            CP5["infer_SAM_3D_npz.py"]:::comp
            CP6["infer_nnunet_2D.py"]:::comp
            CP7["infer_nnunet_3D.py"]:::comp
        end
    end

    %% Extensions
    subgraph "Extensions"
        direction TB
        EP["point_prompt/"]:::ext
        TP["text_prompt/"]:::ext
        SP["seg_3dnii_sparse_marker/"]:::ext
    end

    %% Configuration & Packaging
    subgraph "Configuration & Packaging"
        direction TB
        SPY["setup.py"]:::core
        EGG["medsam.egg-info/"]:::core
    end

    %% Connections
    DM -->|feeds tensors| OM
    MC --> DM
    PD --> DM
    PS --> DM
    DL --> DM
    PCT --> DM
    PGR --> DM
    FMT --> DM
    SPL --> DM

    OM --> T1
    OM --> T2
    OM --> T3
    OM --> T4
    OM --> T5
    UN --> OM
    FU --> OM
    RL --> OM
    AMG --> PRED
    BS --> PRED
    SMODEL --> PRED
    SUTIL --> PRED
    LF --> T1
    LF --> T2
    CA --> T1
    CA --> T2
    CA --> MI
    T1 --> WD
    T2 --> WD
    T3 --> WD
    T4 --> WD
    T5 --> WD

    WD --> MI
    MI --> MX
    MX --> UI
    UI --> MI
    MI --> FU
    FU --> MI
    MI --> OM
    DEMO --> PFIG
    TS --> MI
    CP1 --> PFIG
    CP2 --> PFIG
    CP3 --> PFIG
    CP4 --> PFIG
    CP5 --> PFIG
    CP6 --> PFIG
    CP7 --> PFIG

    MI <--> EP
    MI <--> TP
    MI <--> SP

    SPY --> T1
    SPY --> MI
    EGG --> T1
    EGG --> MI

    %% Styles
    classDef core fill:#D6E4FF,stroke:#1D4ED8
    classDef ext fill:#DCFCE7,stroke:#15803D
    classDef comp fill:#FEF3C7,stroke:#D97706
    classDef store fill:#E0E7FF,stroke:#4338CA

    %% Click Events
    click DM "https://github.com/zhangyh0502/pcasam/tree/master/datamodule/"
    click MC "https://github.com/zhangyh0502/pcasam/blob/master/datamodule/MRI_CancerSeg.py"
    click PD "https://github.com/zhangyh0502/pcasam/blob/master/datamodule/picai_decomp.py"
    click PS "https://github.com/zhangyh0502/pcasam/blob/master/datamodule/postate158_decomp.py"
    click DL "https://github.com/zhangyh0502/pcasam/blob/master/datamodule/data_list_*.npz"
    click PCT "https://github.com/zhangyh0502/pcasam/blob/master/utils/pre_CT_MR.py"
    click PGR "https://github.com/zhangyh0502/pcasam/blob/master/utils/pre_grey_rgb.py"
    click FMT "https://github.com/zhangyh0502/pcasam/blob/master/utils/format_convert.py"
    click SPL "https://github.com/zhangyh0502/pcasam/blob/master/utils/split.py"
    click OM "https://github.com/zhangyh0502/pcasam/blob/master/OurModel.py"
    click UN "https://github.com/zhangyh0502/pcasam/blob/master/networks/unet.py"
    click FU "https://github.com/zhangyh0502/pcasam/blob/master/networks/fusion.py"
    click RL "https://github.com/zhangyh0502/pcasam/blob/master/networks/region_to_location.py"
    click AMG "https://github.com/zhangyh0502/pcasam/blob/master/segment_anything/automatic_mask_generator.py"
    click BS "https://github.com/zhangyh0502/pcasam/blob/master/segment_anything/build_sam.py"
    click PRED "https://github.com/zhangyh0502/pcasam/blob/master/segment_anything/predictor.py"
    click SMODEL "https://github.com/zhangyh0502/pcasam/tree/master/segment_anything/modeling/"
    click SUTIL "https://github.com/zhangyh0502/pcasam/tree/master/segment_anything/utils/"
    click LF "https://github.com/zhangyh0502/pcasam/blob/master/loss_function.py"
    click CA "https://github.com/zhangyh0502/pcasam/blob/master/config_args.py"
    click T1 "https://github.com/zhangyh0502/pcasam/blob/master/train_Ours.py"
    click T2 "https://github.com/zhangyh0502/pcasam/blob/master/train_CoarseSeg.py"
    click T3 "https://github.com/zhangyh0502/pcasam/blob/master/train_multi_gpus.py"
    click T3s "https://github.com/zhangyh0502/pcasam/blob/master/train_multi_gpus.sh"
    click T4 "https://github.com/zhangyh0502/pcasam/blob/master/train_Ours_TTA.py"
    click T5 "https://github.com/zhangyh0502/pcasam/blob/master/train_one_gpu.py"
    click WD "https://github.com/zhangyh0502/pcasam/tree/master/work_dir/MedSAM"
    click MI "https://github.com/zhangyh0502/pcasam/blob/master/MedSAM_Inference.py"
    click MX "https://github.com/zhangyh0502/pcasam/blob/master/MedSAM_Interface.py"
    click UI "https://github.com/zhangyh0502/pcasam/blob/master/gui.py"
    click DEMO "https://github.com/zhangyh0502/pcasam/blob/master/utils/demo.py"
    click PFIG "https://github.com/zhangyh0502/pcasam/blob/master/plot_figure.py"
    click TS "https://github.com/zhangyh0502/pcasam/blob/master/test_sam_for_ours.py"
    click CP1 "https://github.com/zhangyh0502/pcasam/blob/master/comparisons/infer_deeplabv3_res50_2D.py"
    click CP2 "https://github.com/zhangyh0502/pcasam/blob/master/comparisons/infer_deeplabv3_res50_3D.py"
    click CP3 "https://github.com/zhangyh0502/pcasam/blob/master/comparisons/train_deeplabv3_res50.py"
    click CP4 "https://github.com/zhangyh0502/pcasam/blob/master/comparisons/infer_SAM_2D_npz.py"
    click CP5 "https://github.com/zhangyh0502/pcasam/blob/master/comparisons/infer_SAM_3D_npz.py"
    click CP6 "https://github.com/zhangyh0502/pcasam/blob/master/comparisons/infer_nnunet_2D.py"
    click CP7 "https://github.com/zhangyh0502/pcasam/blob/master/comparisons/infer_nnunet_3D.py"
    click EP "https://github.com/zhangyh0502/pcasam/tree/master/extensions/point_prompt/"
    click TP "https://github.com/zhangyh0502/pcasam/tree/master/extensions/text_prompt/"
    click SP "https://github.com/zhangyh0502/pcasam/tree/master/extensions/seg_3dnii_sparse_marker/"
    click SPY "https://github.com/zhangyh0502/pcasam/blob/master/setup.py"
    click EGG "https://github.com/zhangyh0502/pcasam/tree/master/medsam.egg-info/"
```
