MCPcopy Index your code
hub / github.com/bowang-lab/MedSAM2

github.com/bowang-lab/MedSAM2 @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
1,004 symbols 2,975 edges 82 files 443 documented · 44%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

MedSAM2

MedSAM2 - Logo

Segment Anything in 3D Medical Images and Videos

Paper Project Code HuggingFace Model
Dataset List CT_DeepLesion-MedSAM2 LLD-MMRI-MedSAM2 3D Slicer
Gradio App CT-Seg-Demo Video-Seg-Demo BibTeX

Welcome to join our mailing list to get updates. We’re also actively looking to collaborate on annotating new large-scale 3D datasets. If you have unlabeled medical images or videos and want to share them with the community, let’s connect!

Updates

  • 20250705: Release Efficient MedSAM2 baseline for FLARE 2025 Pan-cancer segmentation challenge RECIST-to-3D
  • 20250423: Release lung lesion segmentation dataset LUNA25-MedSAM2 for LUNA25

Installation

  • Create a virtual environment: conda create -n medsam2 python=3.12 -y and conda activate medsam2
  • Install PyTorch: pip install torch==2.5.1 torchvision==0.20.1 --index-url https://download.pytorch.org/whl/cu124 (Linux CUDA 12.4)
  • Download code git clone https://github.com/bowang-lab/MedSAM2.git && cd MedSAM2 and run pip install -e ".[dev]"
  • Download checkpoints: bash download.sh
  • Optional: Please install the following dependencies for gradio
sudo apt-get update
sudo apt-get install ffmpeg
pip install gradio==3.38.0
pip install numpy==1.26.3 
pip install ffmpeg-python 
pip install moviepy

Download annotated datasets

Note: Please also cite the raw DeepLesion, LLD-MMRI and RVENET papers when using these datasets.

Inference

3D medical image segmentation

python medsam2_infer_3D_CT.py -i CT_DeepLesion/images -o CT_DeepLesion/segmentation

Medical video segmentation

python medsam2_infer_video.py -i input_video_path -m input_mask_path -o output_video_path 

Gradio demo

python app.py

Training MedSAM2

Use FLARE25 pan-cancer CT dataset as an example. - Download sam2.1_hiera_tiny.pt to checkpoints - Add dataset information in sam2/configs/sam2.1_hiera_tiny512_FLARE_RECIST.yaml: data -> train -> datasets - Set train_video_batch_size based on the GPU memory

sh single_node_train_medsam2.sh
  • multi-node training
sbatch multi_node_train.sh
  • inference with RECIST marker (simulate a box prompt on middle slice)
python medsam2_infer_CT_lesion_npz_recist.py

Training Efficient MedSAM2

sh single_node_train_eff_medsam2_FLARE25.sh
npz = np.load('path to/CT_Lesion_FLARE23Ts_0057.npz', allow_pickle=True)
print(npz.keys())
imgs = npz['imgs'] # (D, W, H), [0, 255]
recist = npz['recist'] # (D, W, H), binary RECIST marker on tumor middle slice {0, 1}
gts = npz['gts'] # (D, W, H), 3D tumor ground truth mask. It will be not available in the testing set

simulate a box prompt on middle slice

python eff_medsam2_infer_CT_lesion_npz_recist.py

Acknowledgements

  • We highly appreciate all the challenge organizers and dataset owners for providing the public datasets to the community.
  • We thank Meta AI for making the source code of SAM2 and EfficientTAM publicly available. Please also cite these papers when using MedSAM2.

Bibtex

@article{MedSAM2,
    title={MedSAM2: Segment Anything in 3D Medical Images and Videos},
    author={Ma, Jun and Yang, Zongxin and Kim, Sumin and Chen, Bihui and Baharoon, Mohammed and Fallahpour, Adibvafa and Asakereh, Reza and Lyu, Hongwei and Wang, Bo},
    journal={arXiv preprint arXiv:2504.03600},
    year={2025}
}

Please also cite SAM2

@inproceedings{SAM2,
title={{SAM} 2: Segment Anything in Images and Videos},
    author={Nikhila Ravi and Valentin Gabeur and Yuan-Ting Hu and Ronghang Hu and Chaitanya Ryali and Tengyu Ma and Haitham Khedr and Roman R{\"a}dle and Chloe Rolland and Laura Gustafson and Eric Mintun and Junting Pan and Kalyan Vasudev Alwala and Nicolas Carion and Chao-Yuan Wu and Ross Girshick and Piotr Dollar and Christoph Feichtenhofer},
    booktitle={International Conference on Learning Representations},
    year={2025}
}

and EfficientTAM

@article{xiong2024efficienttam,
    title={Efficient Track Anything},
    author={Yunyang Xiong, Chong Zhou, Xiaoyu Xiang, Lemeng Wu, Chenchen Zhu, Zechun Liu, Saksham Suri, Balakrishnan Varadarajan, Ramya Akula, Forrest Iandola, Raghuraman Krishnamoorthi, Bilge Soran, Vikas Chandra},
    journal={preprint arXiv:2411.18933},
    year={2024}
}

Core symbols most depended-on inside this repo

items
called by 53
sam2/utils/amg.py
update
called by 48
training/utils/train_utils.py
cat
called by 38
sam2/utils/amg.py
cat
called by 35
efficient_track_anything/utils/amg.py
keys
called by 23
training/dataset/vos_segment_loader.py
load
called by 22
training/dataset/vos_segment_loader.py
_separate_heads
called by 12
efficient_track_anything/modeling/sam/transformer.py
start_process
called by 10
app.py

Shape

Method 588
Function 274
Class 142

Languages

Python100%

Modules by API surface

training/trainer.py45 symbols
training/dataset/transforms.py45 symbols
training/utils/train_utils.py34 symbols
training/utils/distributed.py34 symbols
app.py31 symbols
efficient_track_anything/efficienttam_video_predictor_npz.py30 symbols
efficient_track_anything/efficienttam_video_predictor.py30 symbols
training/dataset/vos_segment_loader.py27 symbols
sam2/sam2_video_predictor_npz.py27 symbols
sam2/sam2_video_predictor.py27 symbols
sam2/utils/amg.py26 symbols
efficient_track_anything/utils/amg.py26 symbols

For agents

$ claude mcp add MedSAM2 \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact