# chenfei-liao/memorysam architecture

> Architecture diagram of the GitHub repository chenfei-liao/memorysam, 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/chenfei-liao/memorysam
- Repository: https://github.com/chenfei-liao/memorysam

## Overview

Based on your project’s file tree and README content, this is a multi-modal semantic segmentation framework that extends SAM2 with memory mechanisms for better semantic understanding. The purpose of the project is to apply a modified version of SAM2 to multi-modal segmentation, memorizing modality-agnostic features and semantic category prototypes. The project includes components for dataset preparation, model definition (with backbones, heads, layers, and modules), training routines, and a demo application with both backend and frontend elements.

##### Main Components
a. Data & Configuration:
- Dataset Module: Found under semseg/datasets (several specific dataset loaders such as deliver.py, kitti360.py, mcubes.py, nyu.py, etc.).
- Configuration Files: Located in the configs folder and SAM2 configuration subdirectories (under semseg/models/sam2/sam2/configs/ and similar). Also include environment.yaml and requirements.txt.
b. Model Architecture:
- Core Segmentation Models: Under semseg/models – which includes backbones (cmnext, cmx), heads (fcn, fpn, hem, etc.), layers, and modules (cross-attention, ppm, etc.).
- SAM2 Extension: The semseg/models/sam2 directory forms a separate module integrating SAM2 functionalities, with submodules for memory mechanisms (e.g., automatic_mask_generator, sam2_video_predictor) and Semantic Prototype Memory Module (SPMM).
c. Training & Inference:
- Training Scripts & Tools: Found in tools (train_mm.py, infer_mm.py, val_mm.py, train_sam2_lora.py) and semseg/models/sam2/training (with dataset transforms, loss functions, optimizer, trainer, and utility modules).
d. Demo Application:
- Full-Stack Demo: Under semseg/models/sam2/demo, there is both a backend (Flask/FastAPI based server with inference endpoints) and frontend (React/TSX code with components, layouts, and GraphQL schemas). This part demonstrates real-time segmentation or visualization.
e. Utilities & Supporting Modules:
- Augmentations, losses, metrics, custom optimizers, schedulers, and visualization utilities present in semseg/augmentations.py, semseg/losses.py, semseg/metrics.py, etc.

##### Relationships and Data Flow
- Dataset inputs (images/data from DELIVER/MCubes) are fed into the “Data Preparation” layer.
- Data augmentations and preprocessing (semseg/augmentations and dataset loaders) prepare the data.
- The main “Model” component contains layered modules: a backbone extracts features, while the head uses SAM2’s memory mechanism and integrates SPMM to encode semantic information.
- During training, loss functions (in semseg/losses.py) and metrics (in semseg/metrics.py) help supervise the adaptation process.
- The training engine (located in semseg/models/sam2/training) iteratively refines the model, while inference scripts (tools/infer_mm.py, etc.) serve the prediction.
- The demo application interfaces with the model through the backend service, and a user-friendly web-based frontend allows users to view predictions and, potentially, interact with model outputs (using GraphQL APIs for instance).

##### Design Patterns and Technologies
- That the architecture follows a modular design:
– Separation of concerns between datasets, model architecture, training utilities, and the demo (presentation layer).
- The system often uses patterns such as plug-and-play model components (backbones, heads, modules) and microservice-like structure for the demo (with separate backend and frontend folders).
- That Docker configuration files (e.g., Dockerfiles in the demo folders) support containerization for deployment.
- The project relies on PyTorch (as indicated by installation scripts) for model computation and training.
- It uses modern frontend tools (React, TypeScript, GraphQL) to build the interactive demo.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Data & Configuration"
        DS["Dataset Module"]:::data
        CF["Configuration Files"]:::data
    end

    subgraph "Modeling Layer"
        CM["Core Models"]:::model
        SAM["SAM2 Extension with Memory"]:::model
    end

    subgraph "Training & Inference"
        TI["Training & Inference"]:::inference
        UT["Utilities & Supporting Modules"]:::utils
    end

    subgraph "Application Demo"
        DB["Demo Backend"]:::demo
        DF["Demo Frontend"]:::demo
    end

    DS -->|"input"| CM
    CF -->|"config"| CM
    CM -->|"features"| SAM
    CM -->|"feedback"| TI
    SAM -->|"segmentation"| TI
    CF -->|"params"| TI
    UT -->|"support"| TI
    TI -->|"predictions"| DB
    DB -->|"API"| DF

    click DS "https://github.com/chenfei-liao/memorysam/tree/main/semseg/datasets"
    click CF "https://github.com/chenfei-liao/memorysam/tree/main/configs"
    click CM "https://github.com/chenfei-liao/memorysam/tree/main/semseg/models"
    click SAM "https://github.com/chenfei-liao/memorysam/tree/main/semseg/models/sam2"
    click TI "https://github.com/chenfei-liao/memorysam/tree/main/tools"
    click DB "https://github.com/chenfei-liao/memorysam/blob/main/semseg/models/sam2/demo/backend/server/app.py"
    click DF "https://github.com/chenfei-liao/memorysam/tree/main/semseg/models/sam2/demo/frontend"
    click UT "https://github.com/chenfei-liao/memorysam/tree/main/semseg/utils"

    classDef data fill:#ffcccc,stroke:#333,stroke-width:2px;
    classDef model fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef inference fill:#ccccff,stroke:#333,stroke-width:2px;
    classDef demo fill:#ffccff,stroke:#333,stroke-width:2px;
    classDef utils fill:#ffffcc,stroke:#333,stroke-width:2px;
```
