Skip to content

qaxuDev/HSACNet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

HSACNet: Hierarchical Scale-Aware Consistency Regularized Semi-Supervised Change Detection

arXiv IEEE

πŸ“– Overview

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

Network Architecture

Network Architecture Figure 1: Overall architecture of the proposed change detection network, consisting of SAM2 encoder with adapters, SADAM modules, and decoder.

SADAM and Framework 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.

πŸš€ Quick Start

Installation

# 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.txt

Requirements

torch~=2.4.0
torchvision
torchaudio
hydra-core==1.3.2
tqdm==4.65.2
imageio
pysodmetrics
peft  # For LoRA implementation

Download Pretrained Models

Download SAM2 Hiera pretrained weights and place them in pretrained/:

πŸ“ Project Structure

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

πŸ“Š Dataset

Download Links

Data Structure

The data directory should follow the structure below:

β”œβ”€β”€ [Your WHU-CD/LEVIR-CD/GZ-CD/EGY-BCD Path]
    β”œβ”€β”€ A
    β”œβ”€β”€ B
    └── label

πŸ‹οΈ Training

Fully-Supervised Training

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 8

Or simply:

bash train-fully.sh

Semi-Supervised Training

Train 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 4

Or:

bash train-semi.sh

πŸ§ͺ Testing

Test 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.sh

This will:

  1. Load the trained model
  2. Run inference on the test set
  3. Save prediction masks to save_path

πŸ“ˆ Evaluation

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.sh

πŸ“ˆ Results

Our 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%).

Quantitative Results

Quantitative Results 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.

Qualitative Results

Qualitative Results 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.

πŸ“š Citation

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}
}

πŸ™ Acknowledgements

Thanks to the following projects for their excellent work:

We thank the authors for open-sourcing their code.

About

Official code for IEEE ICME 2025 paper: "HSACNet: Hierarchical Scale-Aware Consistency Regularized Semi-Supervised Change Detection"

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors