BASeg: Boundary-Aware Segmentation Model with Multi-Scale Fusion This repository contains the complete implementation including:
BASeg combines two complementary boundary-aware objectives:
Ensure you have the following installed:
# Using venv
python -m venv venv
source venv/bin/activate
# Or using conda
conda create -n baseg python=3.8
conda activate baseg
pip install -r requirements.txt
Key Dependencies:
To install PyTorch with CUDA support (example for CUDA 12.1):
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
Download the ImageNet-pretrained VMamba-Tiny checkpoint and place it at:
BASeg/pretrain/vmamba_tiny_e292.pth
The expected project structure is:
BASeg/
βββ pretrain/
β βββ vmamba_tiny_e292.pth
βββ train.py
βββ utils.py
Run the training command from inside the BASeg directory so that the
relative checkpoint path resolves correctly. The pretrained ResNet-18 and
DINOv3 weights are downloaded automatically by timm and Hugging Face on the
first run; only the VMamba-Tiny checkpoint must be placed manually.
Update the following parameters in utils.py:
MAIN_FOLDER = "/path/to/your/dataset/"
DATA_FOLDER = MAIN_FOLDER + "images_png/{}.png"
LABEL_FOLDER = MAIN_FOLDER + "masks_png/{}.png"
# Update train_ids and test_ids with your dataset IDs
train_ids = ["id1", "id2", ...]
test_ids = ["test_id1", "test_id2", ...]
Edit utils.py to adjust model hyperparameters:
# Model configuration
WINDOW_SIZE = (256, 256) # Patch size for training
STRIDE = 32 # Stride for testing
BATCH_SIZE = 32 # Batch size
IN_CHANNELS = 3 # RGB input
N_CLASSES = 7 # Number of segmentation classes
# Mode selection
MODE = "Train" # Change to "Test" for inference
DATASET = "Urban" # Dataset name
python train.py
The model auto detects and uses all available GPUs
Change the mode in utils.py:
MODE = "Test"
Then run:
python train.py
A small sample from GCD-25k is included in data/GCD-25k/. It contains 20
RGB image tiles (two tiles from each of ten cities) and 20 corresponding
single-channel masks. All tiles are 512 x 512 pixels. This sample is intended
for checking the data pipeline only; it is too small for a meaningful training
or evaluation experiment.
The sample uses the following correspondence:
data/GCD-25k/images_png/Abuja_0.png
data/GCD-25k/masks_png/Abuja_0_label.png
In general, an image named <id>.png corresponds to a mask named
<id>_label.png. The mask values are 0, 1, and 2, matching the configured
classes background, building, and road, respectively.
To use the included sample, configure utils.py as follows:
MAIN_FOLDER = "./data/GCD-25k/"
DATA_FOLDER = MAIN_FOLDER + "images_png/{}.png"
LABEL_FOLDER = MAIN_FOLDER + "masks_png/{}_label.png"
LABELS = ["background", "building", "road"]
N_CLASSES = len(LABELS)
WEIGHTS = torch.tensor([1, 1, 1], dtype=torch.float)
Populate train_ids and test_ids with the image names without .png, for
example "Abuja_0". Keep the training and test lists non-empty.
The included masks are already zero-based (0 to 2). Therefore, when using
this sample, the mask-loading code in dataset.__getitem__ must not subtract
one. Use:
label = np.asarray(io.imread(self.label_files[random_idx]), dtype="int64")
instead of loading the mask with - 1. The subtraction is only appropriate
for datasets whose stored class indices begin at 1.
Your dataset should be organized as follows:
dataset/
βββ images_png/
β βββ 1366.png
β βββ 1367.png
β βββ ...
βββ masks_png/
βββ 1366.png
βββ 1367.png
βββ ...
python train.py
Edit train.py to customize:
# Learning rate, optimizer, scheduler
# Loss function weights
# Number of epochs
# Checkpoint saving frequency
The training script supports automatic distributed training detection:
# Automatic setup (recommended)
python train.py
# Or explicit distributed launch
torchrun --nproc_per_node=4 train.py
The script prints:
Trained model checkpoints are saved during training. The model supports:
Switch to test mode and run:
# In utils.py
MODE = "Test"
python train.py
Adjust class weights in utils.py:
WEIGHTS = torch.tensor([1, 2, 2, 1, 1, 1, 1], dtype=torch.float)
1 commits
Python
100.0%
BASeg: Boundary-Aware Segmentation Model with Multi-Scale Fusion This repository contains the complete implementation including:
BASeg combines two complementary boundary-aware objectives:
Ensure you have the following installed:
# Using venv
python -m venv venv
source venv/bin/activate
# Or using conda
conda create -n baseg python=3.8
conda activate baseg
pip install -r requirements.txt
Key Dependencies:
To install PyTorch with CUDA support (example for CUDA 12.1):
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
Download the ImageNet-pretrained VMamba-Tiny checkpoint and place it at:
BASeg/pretrain/vmamba_tiny_e292.pth
The expected project structure is:
BASeg/
βββ pretrain/
β βββ vmamba_tiny_e292.pth
βββ train.py
βββ utils.py
Run the training command from inside the BASeg directory so that the
relative checkpoint path resolves correctly. The pretrained ResNet-18 and
DINOv3 weights are downloaded automatically by timm and Hugging Face on the
first run; only the VMamba-Tiny checkpoint must be placed manually.
Update the following parameters in utils.py:
MAIN_FOLDER = "/path/to/your/dataset/"
DATA_FOLDER = MAIN_FOLDER + "images_png/{}.png"
LABEL_FOLDER = MAIN_FOLDER + "masks_png/{}.png"
# Update train_ids and test_ids with your dataset IDs
train_ids = ["id1", "id2", ...]
test_ids = ["test_id1", "test_id2", ...]
Edit utils.py to adjust model hyperparameters:
# Model configuration
WINDOW_SIZE = (256, 256) # Patch size for training
STRIDE = 32 # Stride for testing
BATCH_SIZE = 32 # Batch size
IN_CHANNELS = 3 # RGB input
N_CLASSES = 7 # Number of segmentation classes
# Mode selection
MODE = "Train" # Change to "Test" for inference
DATASET = "Urban" # Dataset name
python train.py
The model auto detects and uses all available GPUs
Change the mode in utils.py:
MODE = "Test"
Then run:
python train.py
A small sample from GCD-25k is included in data/GCD-25k/. It contains 20
RGB image tiles (two tiles from each of ten cities) and 20 corresponding
single-channel masks. All tiles are 512 x 512 pixels. This sample is intended
for checking the data pipeline only; it is too small for a meaningful training
or evaluation experiment.
The sample uses the following correspondence:
data/GCD-25k/images_png/Abuja_0.png
data/GCD-25k/masks_png/Abuja_0_label.png
In general, an image named <id>.png corresponds to a mask named
<id>_label.png. The mask values are 0, 1, and 2, matching the configured
classes background, building, and road, respectively.
To use the included sample, configure utils.py as follows:
MAIN_FOLDER = "./data/GCD-25k/"
DATA_FOLDER = MAIN_FOLDER + "images_png/{}.png"
LABEL_FOLDER = MAIN_FOLDER + "masks_png/{}_label.png"
LABELS = ["background", "building", "road"]
N_CLASSES = len(LABELS)
WEIGHTS = torch.tensor([1, 1, 1], dtype=torch.float)
Populate train_ids and test_ids with the image names without .png, for
example "Abuja_0". Keep the training and test lists non-empty.
The included masks are already zero-based (0 to 2). Therefore, when using
this sample, the mask-loading code in dataset.__getitem__ must not subtract
one. Use:
label = np.asarray(io.imread(self.label_files[random_idx]), dtype="int64")
instead of loading the mask with - 1. The subtraction is only appropriate
for datasets whose stored class indices begin at 1.
Your dataset should be organized as follows:
dataset/
βββ images_png/
β βββ 1366.png
β βββ 1367.png
β βββ ...
βββ masks_png/
βββ 1366.png
βββ 1367.png
βββ ...
python train.py
Edit train.py to customize:
# Learning rate, optimizer, scheduler
# Loss function weights
# Number of epochs
# Checkpoint saving frequency
The training script supports automatic distributed training detection:
# Automatic setup (recommended)
python train.py
# Or explicit distributed launch
torchrun --nproc_per_node=4 train.py
The script prints:
Trained model checkpoints are saved during training. The model supports:
Switch to test mode and run:
# In utils.py
MODE = "Test"
python train.py
Adjust class weights in utils.py:
WEIGHTS = torch.tensor([1, 2, 2, 1, 1, 1, 1], dtype=torch.float)
1 commits
Python
100.0%