# chenhongruixuan/changemamba architecture

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

- Interactive diagram: https://gitdiagram.com/chenhongruixuan/changemamba
- Repository: https://github.com/chenhongruixuan/changemamba

## Overview

1. **Repository purpose and scope**
   - ChangeMamba is a PyTorch-based remote-sensing change detection framework.
   - It supports four closely related problem families: binary change detection (BCD), semantic change detection (SCD), building damage assessment (BDA), and a BRIGHT variant.
   - The architecture is organized around one shared backbone family and multiple task-specific decoders, datasets, metrics, and entry scripts.

2. **Top-level execution flow**
   - The main flow is: dataset lists and image folders on disk → dataset builder / dataloader → task-specific training or inference script → model builder → backbone + decoder → task-specific loss/evaluation → saved checkpoints or prediction outputs.
   - Training and inference are separated into distinct scripts under `changedetection/script`, which keeps runtime entry points explicit per task.

3. **Task layer as the orchestration boundary**
   - `changedetection/tasks` is the high-level abstraction layer.
   - It contains task definitions for `bcd.py`, `scd.py`, `bda.py`, and `bright.py`, plus `registry.py` and `metadata.py`.
   - This layer likely binds together dataset conventions, label spaces, evaluation criteria, and model variants for each problem type.

4. **Dataset subsystem**
   - `changedetection/datasets` handles loading and preprocessing for multiple dataset formats.
   - Specific dataset modules include `change_detection.py`, `semantic_change_detection.py`, `damage_assessment.py`, and `multimodal_damage_assessment.py`.
   - `builder.py` and `make_data_loader.py` are the composition points that turn a task/dataset name into a PyTorch dataloader.
   - Dataset splits are driven by text files under `data_name_list`, so the repo separates file indexing from raw image storage.
   - `common.py`, `imutils.py`, and `utils`-style helpers indicate shared image transforms, cropping, normalization, and label handling across tasks.

5. **Model architecture: shared encoder, multiple heads**
   - `changedetection/models` contains the core network family.
   - `Mamba_backbone.py` and `vmamba.py` indicate that the encoder is VMamba/Mamba-based and serves as the shared feature extractor.
   - Task-specific model wrappers include `ChangeMambaBCD.py`, `ChangeMambaSCD.py`, `ChangeMambaBDA.py`, and `ChangeMambaMMBDA.py`.
   - This suggests a common spatiotemporal encoder with different decoders or heads for binary masks, semantic maps, and damage classes.

6. **Decoder and feature fusion layer**
   - `ChangeDecoder.py`, `ChangeDecoder_BRIGHT.py`, and `SemanticDecoder.py` are the main task heads.
   - `decoder_factory.py` and `builders.py` provide the glue between a config-selected backbone and the correct decoder.
   - `model_utils.py` likely contains shared blocks for feature fusion, upsampling, skip handling, or head construction.
   - Architecturally, this is the boundary where generic temporal features become task-specific predictions.

7. **Configuration and model selection**
   - `changedetection/configs` contains YAML configs for VMamba variants: tiny, mini, small, and base.
   - `configs/config.py` is the central config abstraction, likely mapping CLI arguments to model and training settings.
   - The model type, encoder checkpoint, and config file together determine the instantiated architecture.
   - This makes the repo config-driven rather than hardcoding network variants inside the scripts.

8. **Training and inference entry points**
   - The `script` directory contains one script per major task and phase:
     - `train_MambaBCD.py`
     - `train_MambaSCD.py`
     - `train_MambaBDA.py`
     - `train_MambaBDA_bright.py`
     - `infer_MambaBCD.py`
     - `infer_MambaSCD.py`
     - `infer_MambaBDA.py`
   - `script_utils.py` likely centralizes argument parsing, checkpoint loading, logging, and device setup.
   - This layout keeps each task’s runtime flags and output conventions separated while sharing the underlying model/dataset code.

9. **Evaluation and metrics**
   - `changedetection/evaluation` contains task-aligned evaluators:
     - `binary.py`
     - `multiclass.py`
     - `damage.py`
     - `scd.py`
     - `common.py`
   - Utility metrics in `changedetection/utils_func` include segmentation metrics, Lovasz loss, and mcd-related helpers.
   - This indicates evaluation is not just accuracy reporting; it is task-specific and may compute F1, IoU, class-wise scores, and damage-specific summaries.

10. **Engine and logging**
   - `changedetection/engine/base.py` likely implements the reusable training loop skeleton: epoch/iteration control, validation, checkpointing, and optimization steps.
   - `logging_utils.py` provides experiment logging and probably standardized console/file output.
   - The separation between `engine`, `tasks`, and `script` suggests the repo uses a layered structure:
     - scripts = CLI entry points
     - engine = generic train/test loop
     - tasks = problem-specific behavior
     - models/datasets/evaluation = reusable components

11. **Native CUDA boundary**
   - The `kernels/selective_scan` subtree is a compiled extension built via `setup.py`.
   - This is a critical architectural boundary because the Mamba/VMamba backbone depends on optimized selective scan CUDA kernels for performance.
   - The Python model code delegates part of sequence/state-space computation to this native module, so repository setup includes both Python dependencies and local kernel compilation.
   - The presence of multiple kernel variants (`cus`, `cusndstate`, `cusnrow`, `cusoflex`) suggests support for different scan modes or shapes in the encoder implementation.

12. **Data contract and filesystem assumptions**
   - The repo expects prearranged dataset directories and split lists rather than downloading data itself.
   - All tasks use on-disk pairs or triples of images and masks, with the exact naming convention varying by dataset.
   - Pretrained encoder checkpoints are external artifacts placed under `pretrained_weight/`, while trained task checkpoints are stored under a user-specified save path.
   - This means the architecture cleanly separates code, raw data, pretrained backbone weights, and task-specific learned weights.

13. **What an architecture diagram should show**
   - A strong diagram would include:
     - external datasets on disk
     - split-list files feeding dataloaders
     - task registry deciding BCD/SCD/BDA/BRIGHT behavior
     - shared VMamba/Mamba encoder
     - task-specific decoders/heads
     - evaluation/metrics and checkpoint storage
     - the CUDA selective-scan extension beneath the encoder
   - The key structural idea is one reusable spatiotemporal backbone with thin task-specific wrappers around it, not three unrelated models.

Main entry points: [`changedetection/tasks/registry.py`](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/tasks/registry.py) (Task registry), [`changedetection/datasets/builder.py`](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/datasets/builder.py) (Data builder), [`changedetection/models/ChangeMambaBCD.py`](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/models/ChangeMambaBCD.py) (Model wrappers), [`changedetection/script`](https://github.com/chenhongruixuan/changemamba/tree/master/changedetection/script) (Task scripts)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_runtime["Runtime"]
  node_n_scripts["Task scripts<br/>CLI entry points"]
  node_n_script_utils["Script utils<br/>runtime helpers<br/>[script_utils.py]"]
  node_n_engine["Engine<br/>train loop<br/>[base.py]"]
end

subgraph group_grp_tasks["Tasks"]
  node_n_task_registry["Task registry<br/>task dispatch<br/>[registry.py]"]
  node_n_task_meta["Task metadata<br/>task schema<br/>[metadata.py]"]
  node_n_bcd["BCD task<br/>binary change<br/>[bcd.py]"]
  node_n_scd["SCD task<br/>semantic change<br/>[scd.py]"]
  node_n_bda["BDA task<br/>damage assess<br/>[bda.py]"]
  node_n_bright["BRIGHT task<br/>bright variant<br/>[bright.py]"]
end

subgraph group_grp_data["Data"]
  node_n_data_builder["Data builder<br/>dataset factory<br/>[builder.py]"]
  node_n_dataloader["Data loader<br/>loader assembly"]
  node_n_dataset_common["Dataset utils<br/>preprocess helpers<br/>[common.py]"]
  node_n_cd_dataset["Change sets<br/>binary dataset"]
  node_n_scd_dataset["Semantic sets<br/>semantic dataset"]
  node_n_bda_dataset["Damage sets<br/>damage dataset"]
  node_n_mmbda_dataset["Multi-modal damage<br/>multimodal dataset"]
end

subgraph group_grp_models["Models"]
  node_n_configs["Configs"]
  node_n_builders["Model builders<br/>model factory<br/>[builders.py]"]
  node_n_backbone["VMamba backbone<br/>shared encoder<br/>[Mamba_backbone.py]"]
  node_n_vmamba["VMamba core<br/>state-space encoder<br/>[vmamba.py]"]
  node_n_decoders["Task decoders<br/>prediction heads<br/>[ChangeDecoder.py]"]
  node_n_model_wrappers["Model wrappers<br/>task models<br/>[ChangeMambaBCD.py]"]
end

subgraph group_grp_native["Native Kernels"]
  node_n_native_scan["Selective scan<br/>CUDA extension"]
end

subgraph group_grp_eval["Evaluation"]
  node_n_eval["Evaluators<br/>task metrics"]
end

node_n_scripts -->|"setup"| node_n_script_utils
node_n_scripts -->|"run"| node_n_engine
node_n_scripts -->|"select task"| node_n_task_registry
node_n_task_registry -->|"defines"| node_n_task_meta
node_n_task_registry -->|"maps"| node_n_bcd
node_n_task_registry -->|"maps"| node_n_scd
node_n_task_registry -->|"maps"| node_n_bda
node_n_task_registry -->|"maps"| node_n_bright
node_n_task_registry -->|"binds"| node_n_data_builder
node_n_data_builder -->|"compose"| node_n_dataloader
node_n_data_builder -->|"loads"| node_n_cd_dataset
node_n_data_builder -->|"loads"| node_n_scd_dataset
node_n_data_builder -->|"loads"| node_n_bda_dataset
node_n_data_builder -->|"loads"| node_n_mmbda_dataset
node_n_data_builder -->|"uses"| node_n_dataset_common
node_n_scripts -->|"configures"| node_n_configs
node_n_engine -->|"instantiates"| node_n_builders
node_n_builders -->|"builds"| node_n_backbone
node_n_backbone -->|"wraps"| node_n_vmamba
node_n_vmamba -->|"accelerates"| node_n_native_scan
node_n_builders -->|"attaches"| node_n_decoders
node_n_builders -->|"wraps"| node_n_model_wrappers
node_n_model_wrappers -->|"serves"| node_n_bcd
node_n_model_wrappers -->|"serves"| node_n_scd
node_n_model_wrappers -->|"serves"| node_n_bda
node_n_model_wrappers -->|"serves"| node_n_bright
node_n_engine -->|"measures"| node_n_eval

click node_n_scripts "https://github.com/chenhongruixuan/changemamba/tree/master/changedetection/script"
click node_n_script_utils "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/script/script_utils.py"
click node_n_engine "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/engine/base.py"
click node_n_task_registry "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/tasks/registry.py"
click node_n_task_meta "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/tasks/metadata.py"
click node_n_bcd "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/tasks/bcd.py"
click node_n_scd "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/tasks/scd.py"
click node_n_bda "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/tasks/bda.py"
click node_n_bright "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/tasks/bright.py"
click node_n_data_builder "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/datasets/builder.py"
click node_n_dataloader "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/datasets/make_data_loader.py"
click node_n_dataset_common "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/datasets/common.py"
click node_n_cd_dataset "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/datasets/change_detection.py"
click node_n_scd_dataset "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/datasets/semantic_change_detection.py"
click node_n_bda_dataset "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/datasets/damage_assessment.py"
click node_n_mmbda_dataset "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/datasets/multimodal_damage_assessment.py"
click node_n_configs "https://github.com/chenhongruixuan/changemamba/tree/master/changedetection/configs"
click node_n_builders "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/models/builders.py"
click node_n_backbone "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/models/Mamba_backbone.py"
click node_n_vmamba "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/models/vmamba.py"
click node_n_decoders "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/models/ChangeDecoder.py"
click node_n_model_wrappers "https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/models/ChangeMambaBCD.py"
click node_n_native_scan "https://github.com/chenhongruixuan/changemamba/tree/master/kernels/selective_scan"
click node_n_eval "https://github.com/chenhongruixuan/changemamba/tree/master/changedetection/evaluation"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n_scripts,node_n_script_utils,node_n_engine toneBlue
class node_n_task_registry,node_n_task_meta,node_n_bcd,node_n_scd,node_n_bda,node_n_bright toneAmber
class node_n_data_builder,node_n_dataloader,node_n_dataset_common,node_n_cd_dataset,node_n_scd_dataset,node_n_bda_dataset,node_n_mmbda_dataset toneMint
class node_n_configs,node_n_builders,node_n_backbone,node_n_vmamba,node_n_decoders,node_n_model_wrappers toneRose
class node_n_native_scan toneIndigo
class node_n_eval toneTeal
```

## Components

### Runtime

CLI entry points, training/inference orchestration, and engine control.

- [Task scripts](https://github.com/chenhongruixuan/changemamba/tree/master/changedetection/script): CLI entry points · Training and inference scripts for each major task phase. · `changedetection/script`
- [Script utils](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/script/script_utils.py): runtime helpers · Shared argument parsing, checkpointing, logging, and device setup. · `changedetection/script/script_utils.py`
- [Engine](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/engine/base.py): train loop · Reusable epoch/iteration loop, validation, optimization, and checkpoint flow. · `changedetection/engine/base.py`

### Tasks

Problem-specific bindings for BCD, SCD, BDA, and BRIGHT.

- [Task registry](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/tasks/registry.py): task dispatch · Registers and resolves task-specific behavior and metadata. · `changedetection/tasks/registry.py`
- [Task metadata](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/tasks/metadata.py): task schema · Shared task labels, class conventions, and problem definitions. · `changedetection/tasks/metadata.py`
- [BCD task](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/tasks/bcd.py): binary change · Binary change detection task definition. · `changedetection/tasks/bcd.py`
- [SCD task](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/tasks/scd.py): semantic change · Semantic change detection task definition. · `changedetection/tasks/scd.py`
- [BDA task](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/tasks/bda.py): damage assess · Building damage assessment task definition. · `changedetection/tasks/bda.py`
- [BRIGHT task](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/tasks/bright.py): bright variant · BRIGHT-specific task definition and conventions. · `changedetection/tasks/bright.py`

### Data

Dataset loading, split lists, preprocessing, and dataloader construction.

- [Data builder](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/datasets/builder.py): dataset factory · Builds task-aware datasets and loaders from config and dataset names. · `changedetection/datasets/builder.py`
- [Data loader](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/datasets/make_data_loader.py): loader assembly · Composes PyTorch dataloaders from dataset builders and split lists. · `changedetection/datasets/make_data_loader.py`
- [Dataset utils](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/datasets/common.py): preprocess helpers · Shared transforms, image utilities, normalization, and label handling. · `changedetection/datasets/common.py`
- [Change sets](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/datasets/change_detection.py): binary dataset · Binary change detection dataset loader. · `changedetection/datasets/change_detection.py`
- [Semantic sets](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/datasets/semantic_change_detection.py): semantic dataset · Semantic change detection dataset loader. · `changedetection/datasets/semantic_change_detection.py`
- [Damage sets](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/datasets/damage_assessment.py): damage dataset · Building damage assessment dataset loader. · `changedetection/datasets/damage_assessment.py`
- [Multi-modal damage](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/datasets/multimodal_damage_assessment.py): multimodal dataset · Multi-modal damage assessment dataset loader. · `changedetection/datasets/multimodal_damage_assessment.py`

### Models

Shared backbone, decoders, and task wrappers.

- [Configs](https://github.com/chenhongruixuan/changemamba/tree/master/changedetection/configs): yaml configs · Config-driven VMamba variants and model/training settings. · `changedetection/configs`
- [Model builders](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/models/builders.py): model factory · Selects and wires the correct backbone and decoder from config. · `changedetection/models/builders.py`
- [VMamba backbone](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/models/Mamba_backbone.py): shared encoder · Shared Mamba/VMamba feature extractor for all tasks. · `changedetection/models/Mamba_backbone.py`
- [VMamba core](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/models/vmamba.py): state-space encoder · Core VMamba implementation used by the backbone. · `changedetection/models/vmamba.py`
- [Task decoders](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/models/ChangeDecoder.py): prediction heads · Binary, semantic, and BRIGHT-specific decoders and fusion heads. · `changedetection/models/ChangeDecoder.py`
- [Model wrappers](https://github.com/chenhongruixuan/changemamba/blob/master/changedetection/models/ChangeMambaBCD.py): task models · Task-specific model wrappers for BCD, SCD, BDA, and multimodal BDA. · `changedetection/models/ChangeMambaBCD.py`

### Native Kernels

Compiled selective-scan extension used by the Mamba backbone.

- [Selective scan](https://github.com/chenhongruixuan/changemamba/tree/master/kernels/selective_scan): CUDA extension · Compiled selective-scan kernels used by the Mamba backbone. · `kernels/selective_scan`

### Evaluation

Task-aligned metrics and evaluation helpers.

- [Evaluators](https://github.com/chenhongruixuan/changemamba/tree/master/changedetection/evaluation): task metrics · Binary, multiclass, damage, and SCD evaluation logic. · `changedetection/evaluation`

## Connections

- Task scripts → Script utils: setup · Scripts share CLI and runtime setup helpers.
- Task scripts → Engine: run · Entry points invoke the generic training/inference engine.
- Task scripts → Task registry: select task · Scripts resolve task-specific behavior through the task layer.
- Task registry → Task metadata: defines · Registry uses shared metadata and label conventions.
- Task registry → BCD task: maps · Registers binary change detection behavior.
- Task registry → SCD task: maps · Registers semantic change detection behavior.
- Task registry → BDA task: maps · Registers building damage assessment behavior.
- Task registry → BRIGHT task: maps · Registers BRIGHT-specific behavior.
- Task registry → Data builder: binds · Task definitions drive dataset construction.
- Data builder → Data loader: compose · Builder feeds dataloader assembly.
- Data builder → Change sets: loads · Binary change datasets are one supported input family.
- Data builder → Semantic sets: loads · Semantic change datasets are one supported input family.
- Data builder → Damage sets: loads · Damage assessment datasets are one supported input family.
- Data builder → Multi-modal damage: loads · Multimodal damage datasets are one supported input family.
- Data builder → Dataset utils: uses · Shared preprocessing and transforms are reused across loaders.
- Task scripts → Configs: configures · Runtime behavior is driven by config files and variants.
- Engine → Model builders: instantiates · Engine asks model factory for the configured network.
- Model builders → VMamba backbone: builds · Factory wires in the shared backbone.
- VMamba backbone → VMamba core: wraps · Backbone delegates core feature extraction to VMamba.
- VMamba core → Selective scan: accelerates · Selective-scan kernels implement the optimized state-space computation.
- Model builders → Task decoders: attaches · Factory selects the appropriate task head or decoder.
- Model builders → Model wrappers: wraps · Task wrappers combine backbone and decoder into task models.
- Model wrappers → BCD task: serves · Binary change model wrapper serves the BCD task.
- Model wrappers → SCD task: serves · Semantic change model wrapper serves the SCD task.
- Model wrappers → BDA task: serves · Damage assessment model wrapper serves the BDA task.
- Model wrappers → BRIGHT task: serves · BRIGHT-specific wrapper serves the BRIGHT variant.
- Engine → Evaluators: measures · Training and inference use task-aligned evaluators and metrics.
