How chenhongruixuan/changemamba works
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: registry.py Task registry · builder.py Data builder · ChangeMambaBCD.py Model wrappers · script Task scripts
Components 24
Runtime
CLI entry points, training/inference orchestration, and engine control.
- Task scripts · Training and inference scripts for each major task phase.
changedetection/script - Script utils · Shared argument parsing, checkpointing, logging, and device setup.
changedetection/script/script_utils.py - Engine · 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 · Registers and resolves task-specific behavior and metadata.
changedetection/tasks/registry.py - Task metadata · Shared task labels, class conventions, and problem definitions.
changedetection/tasks/metadata.py - BCD task · Binary change detection task definition.
changedetection/tasks/bcd.py - SCD task · Semantic change detection task definition.
changedetection/tasks/scd.py - BDA task · Building damage assessment task definition.
changedetection/tasks/bda.py - BRIGHT task · BRIGHT-specific task definition and conventions.
changedetection/tasks/bright.py
Data
Dataset loading, split lists, preprocessing, and dataloader construction.
- Data builder · Builds task-aware datasets and loaders from config and dataset names.
changedetection/datasets/builder.py - Data loader · Composes PyTorch dataloaders from dataset builders and split lists.
changedetection/datasets/make_data_loader.py - Dataset utils · Shared transforms, image utilities, normalization, and label handling.
changedetection/datasets/common.py - Change sets · Binary change detection dataset loader.
changedetection/datasets/change_detection.py - Semantic sets · Semantic change detection dataset loader.
changedetection/datasets/semantic_change_detection.py - Damage sets · Building damage assessment dataset loader.
changedetection/datasets/damage_assessment.py - Multi-modal damage · Multi-modal damage assessment dataset loader.
changedetection/datasets/multimodal_damage_assessment.py
Models
Shared backbone, decoders, and task wrappers.
- Configs · Config-driven VMamba variants and model/training settings.
changedetection/configs - Model builders · Selects and wires the correct backbone and decoder from config.
changedetection/models/builders.py - VMamba backbone · Shared Mamba/VMamba feature extractor for all tasks.
changedetection/models/Mamba_backbone.py - VMamba core · Core VMamba implementation used by the backbone.
changedetection/models/vmamba.py - Task decoders · Binary, semantic, and BRIGHT-specific decoders and fusion heads.
changedetection/models/ChangeDecoder.py - Model wrappers · 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 · Compiled selective-scan kernels used by the Mamba backbone.
kernels/selective_scan
Evaluation
Task-aligned metrics and evaluation helpers.
- Evaluators · Binary, multiclass, damage, and SCD evaluation logic.
changedetection/evaluation
Connections 27
- 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.