This repository implements a semi-supervised change detection framework that leverages the powerful Segment Anything Model 2 (SAM2) as a foundation encoder. Our approach addresses the challenge of limited labeled data in remote sensing change detection tasks through:
- SAM2-Based Encoder: Utilizes the pre-trained Hiera backbone from SAM2 for robust feature extraction
- Scale-Aware Difference Attention Module (SADAM): Multi-scale difference feature extraction with attention mechanism
- Semi-Supervised Learning Framework: 1-weak-2-strong augmentation strategy with pseudo-labeling
- Parameter-Efficient Fine-Tuning: Both Adapter-based and LoRA-based implementations
Figure 1: Overall architecture of the proposed change detection network, consisting of SAM2 encoder with adapters, SADAM modules, and decoder.
Figure 2: (Left) Structure of the proposed SADAM module with multi-branch group convolutions and spatial-channel attention. (Right) Overall semi-supervised learning framework with supervised and unsupervised components.
# Clone repository
git clone https://github.com/xqa001/SAM2-UNet-SSCD.git
cd SAM2-UNet-SSCD
# Create environment
conda create -n sam2-sscd python=3.10
conda activate sam2-sscd
# Install dependencies
pip install -r requirements.txttorch~=2.4.0
torchvision
torchaudio
hydra-core==1.3.2
tqdm==4.65.2
imageio
pysodmetrics
peft # For LoRA implementation
Download SAM2 Hiera pretrained weights and place them in pretrained/:
SAM2-UNet-SSCD/
βββ sam2/ # SAM2 backbone
β βββ modeling/
β β βββ backbones/ # Hiera encoder
β β βββ sam/ # SAM components
β βββ build_sam.py
βββ sam2_configs/ # Model configs (T/S/B+/L)
βββ splits/ # Dataset splits
β βββ whu/
β β βββ train.txt
β β βββ test.txt
β β βββ 5%/
β β βββ 10%/
β β βββ 20%/
β βββ levir/
β βββ ...
βββ metrics/ # Evaluation metrics
β βββ metric_tool.py
βββ model.py # Network architectures
β βββ SAM2UNetCD # Adapter version
β βββ SAM2UNetCD_LoRA # LoRA version
βββ modules.py # Core modules
β βββ ScaleAwareDiffAttnModule # SADAM
β βββ AttentionalBlock # Spatial attention
β βββ DifferenceModule # Basic diff module
βββ cddataset.py # Dataset loader
βββ train-fully-ce.py # Fully-supervised training
βββ train-semi-1w2s-ce.py # Semi-supervised (1w2s)
βββ test.py # Testing
βββ eval.py # Evaluation
βββ *.sh # Shell scripts
- WHU-CD: imageA, imageB, and label
- LEVIR-CD: imageA, imageB, and label
- GZ-CD: aistudio
- EGY-BCD: baidu drive, password: EGYD. google drive
The data directory should follow the structure below:
βββ [Your WHU-CD/LEVIR-CD/GZ-CD/EGY-BCD Path]
βββ A
βββ B
βββ label
Train with 100% labeled data:
CUDA_VISIBLE_DEVICES="0" \
python train-fully-ce.py \
--hiera_path "pretrained/sam2_hiera_tiny.pt" \
--dataset_name "whu" \
--data_root "/path/to/WHU-CD256" \
--labeled_id_path "splits/whu/train.txt" \
--unlabeled_id_path "splits/whu/train.txt" \
--save_path "exp/whu-fully" \
--image_size 256 \
--epoch 80 \
--lr 3e-4 \
--batch_size 8Or simply:
bash train-fully.shTrain with limited labeled data (e.g., 5%):
CUDA_VISIBLE_DEVICES="0" \
python train-semi-1w2s-ce.py \
--hiera_path "pretrained/sam2_hiera_tiny.pt" \
--dataset_name "whu" \
--data_root "/path/to/WHU-CD256" \
--labeled_id_path "splits/whu/5%/labeled.txt" \
--unlabeled_id_path "splits/whu/5%/unlabeled.txt" \
--save_path "exp/whu-semi-5%" \
--image_size 256 \
--epoch 80 \
--lr 3e-4 \
--batch_size 4Or:
bash train-semi.shTest a trained model:
CUDA_VISIBLE_DEVICES="0" \
python test.py \
--checkpoint "exp/whu-semi-5%/best_model.pth" \
--dataset_name "whu" \
--data_root "/path/to/WHU-CD256" \
--save_path "exp/whu-semi-5%/results"Or:
bash test.shThis will:
- Load the trained model
- Run inference on the test set
- Save prediction masks to
save_path
Evaluate predictions against ground truth:
python eval.py \
--dataset_name "whu" \
--pred_path "exp/whu-semi-5%/results" \
--gt_path "/path/to/WHU-CD256/label"Or:
bash eval.shOur method is evaluated on four challenging change detection datasets (WHU-CD, LEVIR-CD, GZ-CD, and EGY-BCD) with different labeled ratios (5%, 10%, 20%, 40%).
Figure 3: Quantitative comparison on WHU-CD, LEVIR-CD, GZ-CD, and EGY-BCD datasets. The results show IoU and OA metrics across different labeled ratios (5%-40%), demonstrating consistent improvements over state-of-the-art semi-supervised methods.
Figure 4: Qualitative detection results of different methods on four CD datasets at 5% labeled ratio. White represents TP (true positives), black for TN (true negatives), red for FP (false positives), and green for FN (false negatives). Our method produces cleaner predictions with fewer false positives and false negatives.
If you find this work useful for your research, please cite:
@inproceedings{xu2025hierarchical,
title={HSACNet: Hierarchical Scale-Aware Consistency Regularized Semi-Supervised Change Detection},
author={Xu, Qi'ao and Wang, Pengfei and Li, Yanjun and Qian, Tianwen and Wang, Xiaoling},
booktitle={2025 IEEE International Conference on Multimedia and Expo (ICME)},
year={2025},
pages={1-6},
doi={10.1109/ICME59968.2025.11209170},
organization={IEEE}
}Thanks to the following projects for their excellent work:
- Segment Anything 2 - Foundation model and Hiera backbone
- SAM2-UNet - SAM2 adaptation for medical image segmentation
- UniMatch - Semi-supervised learning framework
We thank the authors for open-sourcing their code.