This repository contains the official implementation of our ICIAP 2025 paper, FS-SAM2: Adapting Segment Anything Model 2 for Few-Shot Semantic Segmentation via Low-Rank Adaptation.
Authors: Bernardo Forni, Gabriele Lombardi, Federico Pozzi, Mirco Planamente.
Below are performance results for Few-Shot Semantic Segmentation (FSS) on the COCO-20i and PASCAL-5i datasets using mIoU metric, compared to other state-of-the-art models.
FS-SAM2 code requires python>=3.12, with torch>=2.8.0 and torchvision>=0.23.0.
Use following instructions to download the code and create a virtual environment with PyTorch:
git clone https://github.com/fornib/FS-SAM2.git && cd FS-SAM2
python3.12 -m venv .venv
source .venv/bin/activate
pip3 install torch==2.8 torchvision==0.23 --index-url https://download.pytorch.org/whl/cu126Install SAM2 and required dependencies:
cd segment-anything2/sam2
pip install -v -e .
cd ../..
pip install peft==0.17.1 tensorboardX==2.6Download the SAM2.1-B+ model checkpoint:
wget -P checkpoint https://dl.fbaipublicfiles.com/segment_anything_2/092824/sam2.1_hiera_base_plus.ptFollow VRP-SAM instruction for preparing Few-Shot Segmentation datasets: PASCAL-5i and COCO-20i.
Training:
python3 -m torch.distributed.run --nproc_per_node=$GPUs$ train.py \
--datapath $PATH_TO_YOUR_DATA$ \
--benchmark {coco, pascal, fss} \
--fold {0, 1, 2, 3} \
--epochs 50 \
--lr 1e-4 \
--bsz 8 Test a given training checkpoint:
python -m torch.distributed.run --nproc_per_node=$GPUs$ test.py \
--datapath $PATH_TO_YOUR_DATA$ \
--benchmark {coco, pascal, fss} \
--fold {0, 1, 2, 3}
--logpath $PATH_TO_YOUR_MODEL$This project is mainly built based on VRP-SAM and PFENet. Thanks for their great work!
Code is released for non-commercial and research purposes only. For commercial purposes, please contact the authors.


