Hao Wang1,2, Limeng Qiao3, Zequn Jie3, Zhijian Huang1, Chengjian Feng3,
Qingfang Zheng2, Lin Ma3, Xiangyuan Lan2π§, Xiaodan Liang1π§
1 Sun Yat-sen University, 2 Peng Cheng Laboratory, 3 Meituan Inc.
π§ Corresponding author
Note: X-SAM is under active development, and we will continue to update the code and documentation. Please check TODO to get our development schedule.
Reproducing AAAI 2026 results: To reproduce the results reported in our AAAI 2026 submission, please switch to the
AAAI26_Archivebranch:git clone -b AAAI26_Archive https://github.com/wanghao9610/X-SAM.git
We strongly recommend that everyone uses English to communicate in issues. This helps developers from around the world discuss, share experiences, and answer questions together.
If you have any questions or would like to collaborate, please feel free to open an issue or reach out to me at wanghao9610@gmail.com.
2026-07-03: β¨ We update the codebase to align with X2SAM, along with a new training recipe based on Qwen3-VL.2026-06-18: πππ Congratulations! πππ X2SAM has been accepted by ECCV 2026!2026-04-28: We release X2SAM, a new project for any segmentation in images and videos. X-SAM training configs are also supported in X2SAM β welcome to try it!2026-01-29: We update the camera-ready version of our paper and uploaded the official paper poster for AAAI 2026.2025-11-21: We release the code for X-SAM with Qwen3-4B-Instruct-2507 and Qwen3-1.7B. We will release the weights soon.2025-11-19: We release the code for Training X-SAM. Welcome to try it! If you have any questions, please feel free to open an issue.2025-11-08: πππ Congratulations! πππ X-SAM has been accepted by AAAI 2026! We will release all the code in the coming week!2025-09-28: We update the Local Demo inference script β you can run local inference instead of on the Web Demo.2025-08-11: Thanks for your great attention to our work! We have deployed another Online Demo2. You can also try it if Online Demo1 is not available.2025-08-11: We released the effective code for Evaluation on Segmentation Benchmarks. We have updated all code except for Training X-SAM.2025-08-10: We released the detailed instructions for Demo Deployment.2025-08-08: We released the simple code for Evaluation on VLM Benchmarks.2025-08-06: We are excited to publish the Technical Report, please check it out for more technical details.2025-08-05: We provided the Model Weights on HuggingFaceπ€.2025-07-26: We deployed the Online Demo, you can try it now!
This repository provides the official PyTorch implementation, pre-trained models, training, evaluation, visualization, and demo code of X-SAM:
-
X-SAM introduces a unified multimodal large language model (MLLM) framework, extending the segmentation paradigm from segment anything to any segmentation, thereby enhancing pixel-level perceptual understanding.
-
X-SAM proposes a novel Visual GrounDed (VGD) segmentation task, which segments all instance objects using interactive visual prompts, empowering the model with visually grounded, pixel-wise interpretative capabilities.
-
X-SAM presents a unified training strategy that enables co-training across multiple datasets. Experimental results demonstrate that X-SAM achieves state-of-the-art performance on various image segmentation benchmarks, highlighting its efficiency in multimodal, pixel-level visual understanding.
β¨ This repository provides unified and effective code for training, evaluation, and visualization of segmentation MLLMs. We hope this repository will promote further research on MLLMs. For image-and-video any-segmentation, please also check out our follow-up project X2SAM.
Large Language Models (LLMs) demonstrate strong capabilities in broad knowledge representation, yet they are inherently deficient in pixel-level perceptual understanding. Although the Segment Anything Model (SAM) represents a significant advancement in visual-prompt-driven image segmentation, it exhibits notable limitations in multi-mask prediction and category-specific segmentation tasks, and it cannot integrate all segmentation tasks within a unified model architecture. To address these limitations, we present X-SAM, a streamlined Multimodal Large Language Model (MLLM) framework that extends the segmentation paradigm from segment anything to any segmentation. Specifically, we introduce a novel unified framework that enables more advanced pixel-level perceptual comprehension for MLLMs. Furthermore, we propose a new segmentation task, termed Visual GrounDed (VGD) segmentation, which segments all instance objects with interactive visual prompts and empowers MLLMs with visual grounded, pixel-wise interpretative capabilities. To enable effective training on diverse data sources, we present a unified training strategy that supports co-training across multiple datasets. Experimental results demonstrate that X-SAM achieves state-of-the-art performance on a wide range of image segmentation benchmarks, highlighting its efficiency for multimodal, pixel-level visual understanding.
Figure 1: Overview of X-SAM. The Vision Encoder extracts global visual representations, while the Mask Encoder captures fine-grained visual features. The Large Language Model generates the language response and produces the latent condition embedding, which guides the Mask Decoder in generating the segmentation mask.
π More benchmark results can be found in benchmarks.md.
π To reproduce these AAAI 2026 submission results, use the AAAI26_Archive branch (see Notice).
We provide a detailed project structure for X-SAM. Please follow this structure to organize the project.
π Project
X-SAM
βββ datas
β βββ img_chat
β βββ img_gcgseg
β βββ img_genseg
β βββ img_intseg
β βββ img_ovseg
β βββ img_reaseg
β βββ img_refseg
β βββ img_vgdseg
β βββ LMUData
βββ inits
β βββ huggingface
β βββ mask2former-swin-large-coco-panoptic
β βββ Phi-3-mini-4k-instruct
β βββ sam-vit-large
β βββ X-SAM
βββ xsam
β βββ requirements
β βββ xsam
β βββ configs
β βββ dataset
β βββ demo
β βββ engine
β βββ evaluation
β βββ model
β βββ structures
β βββ tools
β βββ utils
βββ wkdrs
β βββ s1_train
β β βββ ...
β βββ s2_train
β β βββ ...
β βββ s3_train
β β βββ ...
β βββ ...
...# 1) Clone X-SAM and enter project home directory
git clone https://github.com/wanghao9610/X-SAM.git
cd X-SAM
export PROJ_HOME="$(realpath ./)"
export PYTHONPATH="$PROJ_HOME/xsam:$PYTHONPATH"
# 2) Create and activate conda environment
conda create -n xsam python=3.10 -y
conda activate xsam
# 3) Install X-SAM dependencies
cd "$PROJ_HOME/xsam"
pip install -r requirements/runtime.txt
pip install -r requirements/deepspeed.txt
pip install -r requirements/xsam.txt
# 4) Compile Deformable-Attention
cd "$PROJ_HOME/xsam/xsam/model/ops"
bash make.sh
# 5) Setup .env (machine-local paths for tooling; .env is gitignored)
cd "$PROJ_HOME"
cp -n .env.example .env
# CONDA_HOME β conda installation root
# PYTHON_HOME β active env root (xsam); basename is the env name
sed -i "s|YOUR_CONDA_HOME|$(conda info --base)|; s|YOUR_PYTHON_HOME|${CONDA_PREFIX}|" .envexport CUDA_HOME="your_cuda12.4_path"
export PATH="$CUDA_HOME/bin:$PATH"
export LD_LIBRARY_PATH="$CUDA_HOME/lib64:$LD_LIBRARY_PATH"
echo -e "CUDA version:\n$(nvcc -V)"conda install gcc=11 gxx=11 -c conda-forge -ycd "$PROJ_HOME"
git clone -b v0.3rc1 https://github.com/open-compass/VLMEvalKit.git
cd VLMEvalKit
pip install -e .Please refer to datasets.md for detailed instructions on data preparation.
Please refer to models.md for detailed instructions on model preparation.
We provide a comprehensive script that covers the entire pipeline, including training, evaluation, and visualization. For detailed instructions, please refer to gpu_run.sh.
cd "$PROJ_HOME"
# Distributed training across multiple nodes.
# Set NODE_RANK to specify the rank (ID) of each node in distributed training.
# MASTER_ADDR and MASTER_PORT should be set to the IP address and port of your master node.
# Execute the following commands on every machine, updating NODE_RANK for each node accordingly.
# 1) Stage 1: Segmentor Fine-tuning
NUM_NODES=1 NODE_RANK=0 GPU_PER_NODE=8 MASTER_ADDR=127.0.0.1 MASTER_PORT=29510 \
bash runs/gpu_run.sh \
xsam/xsam/configs/xsam/s1_train/xsam_sam_vit_large_m2f_e36_gpu16.py \
"train"
# 2) Stage 2: Alignment Pre-training (Phi-3 example)
NUM_NODES=1 NODE_RANK=0 GPU_PER_NODE=8 MASTER_ADDR=127.0.0.1 MASTER_PORT=29510 \
bash runs/gpu_run.sh \
xsam/xsam/configs/xsam/s2_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_e1_gpu16.py \
"train"
# 3) Stage 3: Mixed Fine-tuning (Phi-3 example)
NUM_NODES=1 NODE_RANK=0 GPU_PER_NODE=8 MASTER_ADDR=127.0.0.1 MASTER_PORT=29510 \
bash runs/gpu_run.sh \
xsam/xsam/configs/xsam/s3_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py \
"train segeval vlmeval visualize"More Stage-3 configs
| Backbone | Config |
|---|---|
| Phi-3-mini-4k-instruct | xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py |
| Qwen3-4B-Instruct-2507 | xsam_qwen3_4b_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py |
| Qwen3-1.7B | xsam_qwen3_1x7b_wothinking_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py |
| Qwen3-VL-4B-Instruct (LoRA) | xsam_qwen3_vl_4b_instruct_sam_vit_large_m2f_e1_gpu16_lora.py |
| Qwen3-VL-2B-Instruct (LoRA) | xsam_qwen3_vl_2b_instruct_sam_vit_large_m2f_e1_gpu16_lora.py |
cd "$PROJ_HOME"
bash runs/gpu_run.sh \
xsam/xsam/configs/xsam/s3_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py \
"segeval"cd "$PROJ_HOME"
bash runs/gpu_run.sh \
xsam/xsam/configs/xsam/s3_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py \
"vlmeval"cd "$PROJ_HOME"
bash runs/gpu_run.sh \
xsam/xsam/configs/xsam/s3_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py \
"visualize"Dataset Exploration
We provide a tool for dataset exploration. You can use it to explore the dataset and get visualizations of the samples.
cd "$PROJ_HOME"
python xsam/xsam/tools/explore.py \
xsam/xsam/configs/xsam/s3_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py \
--output-dir "wkdrs/dataset_exploration" \
--subset train \
--max-samples 100Model Conversion
We provide a tool for model conversion. You can use it to convert the checkpoint to the Hugging Face format.
cd "$PROJ_HOME"
python xsam/xsam/tools/model_tools/pth_to_hf.py \
xsam/xsam/configs/xsam/s3_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py \
"wkdrs/s3_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16" \
--pth_model latestποΈ Inference
cd "$PROJ_HOME"
python xsam/xsam/demo/demo.py \
xsam/xsam/configs/xsam/s3_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py \
--pth_model "inits/X-SAM/s3_mixed_finetune/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_large_m2f_gpu16_mixed_finetune/pytorch_model.bin" \
--task-name TASK_NAME \
--image INPUT_IMAGE/INPUT_DIR \
--prompt INPUT_PROMPT \
--vprompt-masks INPUT_VPROMPT_MASKSποΈ Examples
# Example: img_chat
python xsam/xsam/demo/demo.py \
xsam/xsam/configs/xsam/s3_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py \
--pth_model "inits/X-SAM/s3_mixed_finetune/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_large_m2f_gpu16_mixed_finetune/pytorch_model.bin" \
--task-name img_chat \
--image xsam/xsam/demo/sample.jpg \
--prompt "What is unusal about this image?"
# Example: img_genseg
python xsam/xsam/demo/demo.py \
xsam/xsam/configs/xsam/s3_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py \
--pth_model "inits/X-SAM/s3_mixed_finetune/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_large_m2f_gpu16_mixed_finetune/pytorch_model.bin" \
--task-name img_genseg \
--image xsam/xsam/demo/sample.jpg \
--output-dir "wkdrs/demo_outputs" \
--prompt "ins: person, bird, boat; sem: water, sky"
# Example: img_refseg
python xsam/xsam/demo/demo.py \
xsam/xsam/configs/xsam/s3_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py \
--pth_model "inits/X-SAM/s3_mixed_finetune/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_large_m2f_gpu16_mixed_finetune/pytorch_model.bin" \
--task-name img_refseg \
--image xsam/xsam/demo/sample.jpg \
--output-dir "wkdrs/demo_outputs" \
--prompt "the ironing man"
# Example: img_reaseg
python xsam/xsam/demo/demo.py \
xsam/xsam/configs/xsam/s3_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py \
--pth_model "inits/X-SAM/s3_mixed_finetune/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_large_m2f_gpu16_mixed_finetune/pytorch_model.bin" \
--task-name img_reaseg \
--image xsam/xsam/demo/sample.jpg \
--output-dir "wkdrs/demo_outputs" \
--prompt "What can be used to warm clothes?"
# Example: img_gcgseg
python xsam/xsam/demo/demo.py \
xsam/xsam/configs/xsam/s3_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py \
--pth_model "inits/X-SAM/s3_mixed_finetune/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_large_m2f_gpu16_mixed_finetune/pytorch_model.bin" \
--task-name img_gcgseg \
--image xsam/xsam/demo/sample.jpg \
--output-dir "wkdrs/demo_outputs"
# Example: img_intseg (requires visual prompt masks)
python xsam/xsam/demo/demo.py \
xsam/xsam/configs/xsam/s3_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py \
--pth_model "inits/X-SAM/s3_mixed_finetune/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_large_m2f_gpu16_mixed_finetune/pytorch_model.bin" \
--task-name img_intseg \
--image xsam/xsam/demo/sample.jpg \
--output-dir "wkdrs/demo_outputs" \
--vprompt-masks PATH_TO_VPROMPT_MASK.png
# Example: img_vgdseg (requires visual prompt masks)
python xsam/xsam/demo/demo.py \
xsam/xsam/configs/xsam/s3_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py \
--pth_model "inits/X-SAM/s3_mixed_finetune/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_large_m2f_gpu16_mixed_finetune/pytorch_model.bin" \
--task-name img_vgdseg \
--image xsam/xsam/demo/sample.jpg \
--output-dir "wkdrs/demo_outputs" \
--vprompt-masks PATH_TO_VPROMPT_MASK1.png PATH_TO_VPROMPT_MASK2.pngπ οΈ Deployment
cd "$PROJ_HOME"
python xsam/xsam/demo/app.py \
xsam/xsam/configs/xsam/s3_train/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_vit_large_m2f_e1_gpu16.py \
--pth_model "inits/X-SAM/s3_mixed_finetune/xsam_phi3_mini_4k_instruct_siglip2_so400m_p14_384_sam_large_m2f_gpu16_mixed_finetune/pytorch_model.bin" \
--log-dir "wkdrs/app_logs" \
--seed 0 \
--port 7860Then, you can access the demo website at http://localhost:7860.
- Release the Online Demo.
- Release the Model Weights.
- Release the Technical Report.
- Release the code for Evaluation on VLM Benchmarks.
- Release the code for Demo Deployment.
- Release the code for Evaluation on Segmentation Benchmarks.
- Release the code for Training X-SAM.
- Release the code and weight for X-SAM with Qwen3.
- Release the code and weight for X-SAM with Qwen3-VL.
- Release the inference and demo code supporting transformers.
- Release the code and instructions for training with Ascend NPU.
This project has referenced some excellent open-sourced repos (xtuner, VLMEvalKit, Sa2VA). Thanks for their wonderful works and contributions to the community!
If you find X-SAM and X2SAM are helpful for your research or applications, please consider giving us a star π and citing the following papers by the following BibTex entry.
@inproceedings{wang2026xsam,
title={X-SAM: From segment anything to any segmentation},
author={Wang, Hao and Qiao, Limeng and Jie, Zequn and Huang, Zhijian and Feng, Chengjian and Zheng, Qingfang and Ma, Lin and Lan, Xiangyuan and Liang, Xiaodan},
booktitle={Proceedings of the AAAI Conference on Artificial Intelligence},
volume={40},
number={31},
pages={26187--26196},
year={2026}
}
@article{wang2026x2sam,
title={X2SAM: Any Segmentation in Images and Videos},
author={Wang, Hao and Qiao, Limeng and Zhang, Chi and Wan, Guanglu and Ma, Lin and Lan, Xiangyuan and Liang, Xiaodan},
journal={arXiv preprint arXiv:2605.00891},
year={2026}
}