# sacmehta/ynet architecture

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

- Interactive diagram: https://gitdiagram.com/sacmehta/ynet
- Repository: https://github.com/sacmehta/ynet

## Overview

##### Project type and purpose
- Y-Net is a deep-learning research codebase for joint tissue segmentation and diagnostic classification of breast biopsy images. It implements a two-stage training pipeline (segmentation only → joint segmentation+classification) plus a segmentation evaluation module.

##### File structure
- Top-level directories:
- stage1/ – code for training the segmentation network (branch 1)
- stage2/ – code for training the joint segmentation+classification network (branch 2)
- seg_eval/ – code for producing and evaluating segmentation masks with the trained Y-Net
- images/ – example input/output illustrations
- Within each stage:
- Data ingestion & augmentation: loadData.py, Transforms.py, DataSet.py
- Model definition: Model.py
- Loss & metrics: Criteria.py, IOUEval.py
- Training orchestration: main.py
- Visualization: VisualizeGraph.py
- Pretrained weights folder (pretrained_models_st1, pretrained_model_st2)

##### README for additional insights
- Two-stage training: stage1 trains segmentation branch, stage2 initializes from stage1 weights and jointly trains for segmentation + classification.
- seg_eval runs the trained model on new whole-slide images to produce segmentation masks.
- Dependencies: Python 3.6.2, PyTorch 0.2.0_4, OpenCV 3.3.0

##### Main components
a. Input Data
- Raw WSI patches (RGB) + ground-truth masks (annotations)
b. Data Pipeline (common to both stages)
- loadData.py → DataSet → Transforms → DataLoader
c. Stage 1: Segmentation Training
- Model Definition (U-Net-style branch)
- Loss & Metric (e.g., cross-entropy, IOU evaluation)
- Visualization (graph, training curves)
- Output: pretrained segmentation weights
d. Stage 2: Joint Segmentation + Classification Training
- Model Definition (Y-Net combining segmentation and classification heads)
- Initialization from stage1 weights
- Losses: segmentation + classification
- Metrics & Visualization
- Output: final joint model weights
e. seg_eval Module
- Load final model weights
- Inference engine (Eval_YNet.py)
- Produce masks on new WSI sets
- Compute evaluation metrics (IOU, etc.)
f. External Libraries & Frameworks
- PyTorch
- OpenCV
- Python runtime

##### Architectural patterns & design principles
- Modular “pipeline” stages (stage1, stage2, seg_eval)
- Reusable data ingestion & augmentation modules
- Clear separation of concerns: data, model, training loop, evaluation, visualization
- Checkpointing: intermediate and final model artifacts

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Data & I/O Layer
    subgraph "Data & I/O" 
        direction TB
        InputData["Input Data\n(Raw WSI patches & GT masks)"]:::data
        subgraph "Data Pipeline (Stage1)"
            direction TB
            Load1["loadData.py"]:::data
            DataSet1["DataSet.py"]:::data
            Transforms1["Transforms.py"]:::data
        end
        subgraph "Data Pipeline (Stage2)"
            direction TB
            Load2["loadData.py"]:::data
            DataSet2["DataSet.py"]:::data
            Transforms2["Transforms.py"]:::data
        end
    end

    %% Stage 1 Training
    subgraph "Stage1: Segmentation Training" 
        direction TB
        S1Model["Model.py\n(Unet branch)"]:::model
        S1Crit["Criteria.py"]:::model
        IOU1["IOUEval.py"]:::model
        S1Vis["VisualizeGraph.py"]:::vis
        S1Train["main.py"]:::train
        S1Out["model_C1.pth"]:::artifact
    end

    %% Stage 2 Training
    subgraph "Stage2: Joint Segm+Class Training"
        direction TB
        S2Init["Init from model_C1.pth"]:::artifact
        S2Model["Model.py\n(Y-Net)"]:::model
        S2Crit["Criteria.py"]:::model
        IOU2["IOUEval.py"]:::model
        S2Vis["VisualizeGraph.py"]:::vis
        S2Train["main.py"]:::train
        S2Out["ynet_c1.pth"]:::artifact
    end

    %% seg_eval Module
    subgraph "seg_eval Module"
        direction TB
        Eval["Eval_YNet.py"]:::eval
    end

    %% Outputs
    subgraph "Outputs"
        direction TB
        Masks["Segmentation Masks"]:::output
        Reports["Evaluation Reports"]:::output
    end

    %% External Dependencies
    subgraph "External Dependencies"
        direction TB
        PyTorch["PyTorch"]:::external
        OpenCV["OpenCV"]:::external
        Python["Python 3.6.2"]:::external
    end

    %% Connections
    InputData --> Load1
    Load1 --> DataSet1
    DataSet1 --> Transforms1
    Transforms1 --> S1Train
    S1Model --> S1Train
    S1Crit --> S1Train
    IOU1 --> S1Train
    S1Vis --> S1Train
    S1Train --> S1Out

    S1Out --> S2Init
    S2Init --> S2Train
    Load2 --> S2Train
    DataSet2 --> S2Train
    Transforms2 --> S2Train
    S2Model --> S2Train
    S2Crit --> S2Train
    IOU2 --> S2Train
    S2Vis --> S2Train
    S2Train --> S2Out

    InputData --> Eval
    S2Out --> Eval
    Eval --> Masks
    Eval --> Reports

    PyTorch -.-> S1Model
    PyTorch -.-> S2Model
    OpenCV -.-> Load1
    OpenCV -.-> Load2
    Python -.-> Load1
    Python -.-> Load2

    %% Click Events
    click InputData "https://github.com/sacmehta/ynet/tree/master/stage1/data/"
    click Load1 "https://github.com/sacmehta/ynet/blob/master/stage1/loadData.py"
    click DataSet1 "https://github.com/sacmehta/ynet/blob/master/stage1/DataSet.py"
    click Transforms1 "https://github.com/sacmehta/ynet/blob/master/stage1/Transforms.py"
    click S1Model "https://github.com/sacmehta/ynet/blob/master/stage1/Model.py"
    click S1Crit "https://github.com/sacmehta/ynet/blob/master/stage1/Criteria.py"
    click IOU1 "https://github.com/sacmehta/ynet/blob/master/stage1/IOUEval.py"
    click S1Vis "https://github.com/sacmehta/ynet/blob/master/stage1/VisualizeGraph.py"
    click S1Train "https://github.com/sacmehta/ynet/blob/master/stage1/main.py"
    click S1Out "https://github.com/sacmehta/ynet/blob/master/stage1/pretrained_models_st1/model_C1.pth"
    click Load2 "https://github.com/sacmehta/ynet/blob/master/stage2/loadData.py"
    click DataSet2 "https://github.com/sacmehta/ynet/blob/master/stage2/DataSet.py"
    click Transforms2 "https://github.com/sacmehta/ynet/blob/master/stage2/Transforms.py"
    click S2Init "https://github.com/sacmehta/ynet/blob/master/stage2/pretrained_model_st2/ynet_c1.pth"
    click S2Model "https://github.com/sacmehta/ynet/blob/master/stage2/Model.py"
    click S2Crit "https://github.com/sacmehta/ynet/blob/master/stage2/Criteria.py"
    click IOU2 "https://github.com/sacmehta/ynet/blob/master/stage2/IOUEval.py"
    click S2Vis "https://github.com/sacmehta/ynet/blob/master/stage2/VisualizeGraph.py"
    click S2Train "https://github.com/sacmehta/ynet/blob/master/stage2/main.py"
    click S2Out "https://github.com/sacmehta/ynet/blob/master/stage2/pretrained_model_st2/ynet_c1.pth"
    click Eval "https://github.com/sacmehta/ynet/blob/master/seg_eval/Eval_YNet.py"

    %% Styles
    classDef data fill:#D0E8FF,stroke:#0288D1,color:#000
    classDef train fill:#C8E6C9,stroke:#388E3C,color:#000
    classDef model fill:#A5D6A7,stroke:#2E7D32,color:#000
    classDef vis fill:#FFE0B2,stroke:#FB8C00,color:#000
    classDef eval fill:#FFCC80,stroke:#F57C00,color:#000
    classDef artifact fill:#E0E0E0,stroke:#757575,color:#000
    classDef external fill:#F5F5F5,stroke:#9E9E9E,color:#000
    classDef output fill:#FFF9C4,stroke:#FBC02D,color:#000
```
