How chenfei-liao/memorysam works
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.