# patball1/detectree2 architecture

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

- Interactive diagram: https://gitdiagram.com/patball1/detectree2
- Repository: https://github.com/patball1/detectree2

## Overview

##### Main Components
- Data Input & Preparation:
- Raw imagery (orthomosaics) and crown data used for training.
- Utilities for tiling images and processing crown polygons.
- Data Loading:
- “data_loading” module, which contains scripts for pulling data from Google Drive, quickstarts, and even GEE downloads. These scripts handle interfacing with external geospatial data.
- Preprocessing:
- “preprocessing” package used to tile images and possibly prepare crown annotations.
- Custom cleaning (e.g., cleanCrowns.R) and transformation utilities.
- Model Training & Tuning:
- “models” module where training (including train_tune.py), prediction, and evaluation scripts are located.
- Integration with deep learning frameworks such as PyTorch/Detectron2 and highlight that GPU access (with CUDA) is recommended.
- Model Evaluation & Testing:
- Evaluation components in both the “models” submodule (evaluation.py) and tests within the “tests” directory.
- Process of comparing predictions with manually labeled crowns.
- R Analysis and Additional Workflows:
- That the project also includes an “R” directory (with scripts like change_analysis_mortality_growth.R) which indicates a cross-language analytical pipeline.
- Notebooks & Documentation:
- Notebooks (colab, exploratory, etc.) as examples/demonstrations of the workflow.
- Documentation folder (“docs”) and report sections that explain workflow or results.
- External Dependencies & Deployment:
- External libraries (GDAL, PyTorch, torchvision, etc.) and the underlying deep learning framework (Detectron2).
- CI/CD workflows (GitHub workflows, Makefile, Dockerfile) for testing/delivery.

##### Relationships & Data Flow
- Data Flow:
- Move into the Preprocessing module, which tiles the images and formats annotation data.
- Branch out to the Model Training/Tuning component where preprocessed data is used to train Mask R-CNN models.
- Then, connect to the Evaluation and Prediction modules where trained models are applied over test tiles or full regions.
- A feedback loop for model performance evaluation (via integration tests and notebooks).
- Interaction between Modules:
- That the “data_loading”, “preprocessing”, and “models” modules are tightly integrated and follow a pipeline from raw data to predictions.
- Demonstrate that components from “tests” validate each stage of the pipeline.
- That additional R-based analyses integrate into the overall ecosystem for change analysis.

##### Architectural Patterns & Design Principles
- Modularity:
- Each core functionality (data loading, preprocessing, modeling) is separated into its own module.
- Pipeline Layout:
- The system essentially implements a data pipeline where input data is transformed step by step.
- Extensibility:
- Showing “model_garden” with pre-trained models helps indicate how users can swap or update models.
- Integration / CI/CD:
- The presence of GitHub workflows along with Makefile and Dockerfile imply continuous integration/deployment practices.

##### Final Assembly
1) Tiling (Preprocessing)
2) Training/Tuning (Model Component)
3) Evaluation/Prediction (Testing/Output)
4) Analysis (R-based post-processing, reports, and interactive notebooks)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Input Layer
    subgraph "Input Layer"
        A["Raw Aerial Imagery & Crown Annotations"]:::data
        B["Data Loading Module"]:::module
    end

    %% Preprocessing Layer
    subgraph "Preprocessing Layer"
        C["Preprocessing Module"]:::module
        CA["Tiling Utility"]:::module
    end

    %% Modeling Layer
    subgraph "Modeling Layer"
        D1["train.py"]:::module
        D2["train_tune.py"]:::module
    end

    %% Evaluation Layer
    subgraph "Evaluation & Testing Layer"
        E1["evaluation.py"]:::module
        F["Testing/Validation"]:::module
    end

    %% Analysis Layer
    subgraph "R Analysis Layer"
        G["R Analysis & Additional Workflows"]:::analysis
        G1["Crown Cleaning"]:::analysis
    end

    %% Reporting Layer
    subgraph "Reporting Layer"
        H1["Notebooks"]:::docs
        H2["Documentation"]:::docs
    end

    %% Deployment Layer
    subgraph "External Dependencies & Deployment"
        I1["CI/CD Workflows"]:::deployment
        I2["Docker Build"]:::deployment
        I3["Makefile"]:::deployment
    end

    %% Data Flow Connections
    A --> B
    B --> C
    C --> CA
    CA --> D1
    CA --> D2
    D1 --> E1
    D2 --> E1
    E1 --> F
    F -->|"feedback"| D1
    C --> G
    G --> G1
    E1 --> H1
    E1 --> H2
    H1 --> I1
    H1 --> I2
    H1 --> I3

    %% Styles
    classDef data fill:#f4cccc,stroke:#b45f06,stroke-width:2px;
    classDef module fill:#c9daf8,stroke:#1155cc,stroke-width:2px;
    classDef analysis fill:#fff2cc,stroke:#bf9000,stroke-width:2px;
    classDef docs fill:#d9ead3,stroke:#38761d,stroke-width:2px;
    classDef deployment fill:#f9cb9c,stroke:#e69138,stroke-width:2px;

    %% Click Events
    click B "https://github.com/patball1/detectree2/tree/master/detectree2/data_loading"
    click C "https://github.com/patball1/detectree2/tree/master/detectree2/preprocessing"
    click CA "https://github.com/patball1/detectree2/blob/master/detectree2/preprocessing/tiling.py"
    click D1 "https://github.com/patball1/detectree2/blob/master/detectree2/models/train.py"
    click D2 "https://github.com/patball1/detectree2/blob/master/detectree2/models/train_tune.py"
    click E1 "https://github.com/patball1/detectree2/blob/master/detectree2/models/evaluation.py"
    click F "https://github.com/patball1/detectree2/tree/master/detectree2/tests"
    click G "https://github.com/patball1/detectree2/tree/master/detectree2/R"
    click G1 "https://github.com/patball1/detectree2/blob/master/detectree2/R/cleanCrowns.R"
    click H1 "https://github.com/patball1/detectree2/tree/master/notebooks"
    click H2 "https://github.com/patball1/detectree2/tree/master/docs"
    click I1 "https://github.com/patball1/detectree2/tree/master/.github/workflows"
    click I2 "https://github.com/patball1/detectree2/tree/master/docker/Dockerfile"
    click I3 "https://github.com/patball1/detectree2/tree/master/Makefile"
```
