MedSAM

文章目录

    • 一、关于 MedSAM
    • 二、安装
    • 三、快速上手
    • 四、模型训练
      • 1、数据处理
      • 2、在多 GPUs 上训练 (推荐)
      • 3、在单 GPU 上村联
    • Acknowledgements


一、关于 MedSAM

MedSAM: Segment Anything in Medical Images.

  • github : https://github.com/bowang-lab/MedSAM

二、安装

1、构建虚拟环境

conda create -n medsam python=3.10 -y
# 激活
conda activate medsam

2、安装 Pytorch 2.0

可前往:https://pytorch.org/get-started/locally/


3、下载/编译 MedSAM 源码

git clone https://github.com/bowang-lab/MedSAM
cd MedSAM
pip install -e .

三、快速上手

下载 model checkpoint 并将他放在 work_dir/MedSAM/medsam_vit_b
https://drive.google.com/drive/folders/1ETWmi4AiniJeWOt6HAsYgTjYv_fkgzoN?usp=drive_link


这里展示了三种方法,来在你的镜像上,快速测试模型。

1、命令行

python MedSAM_Inference.py # segment the demo image

Segment other images with the following flags

-i input_img
-o output path
--box bounding box of the segmentation target

2、Jupyter-notebook

在 CoLab 上提供了循序渐进的教程
https://colab.research.google.com/drive/19WNtRMbpsxeqimBlmJwtd1dzpaIvK2FZ?usp=sharing

你也可以在本地运行 MedSAM_Inference.ipynb 文件。


3、GUI

安装 PyQt5:

pip install PyQt5 

运行

python gui.py

Load the image to the GUI and specify segmentation targets by drawing bounding boxes.

MedSAM_第1张图片


四、模型训练


1、数据处理

  • 下载 SAM checkpoint 并放置到 work_dir/SAM/sam_vit_b_01ec64.pth .
  • 下载 demo dataset 并解压它到 data/FLARE22Train/ 文件夹下。

这个数据集包含 50 个腹部CT扫描,每个扫描件包含 13个器官的标注。
器官的名字等信息可查看 MICCAI FLARE2022.

MedSAM_第2张图片


运行预处理

安装 cc3d:

pip install connected-components-3d

运行代码

python pre_CT_MR.py

  • 切分数据及: 80% for training and 20% for testing
  • adjust CT scans to soft tissue window level (40) and width (400)
  • max-min normalization
  • resample image size to 1024x2014
  • save the pre-processed images and labels as npy files


2、在多 GPUs 上训练 (推荐)

The model was trained on five A100 nodes and each node has four GPUs (80G) (20 A100 GPUs in total). Please use the slurm script to start the training process.

sbatch train_multi_gpus.sh

When the training process is done, please convert the checkpoint to SAM’s format for convenient inference.

python utils/ckpt_convert.py # Please set the corresponding checkpoint path first

3、在单 GPU 上村联

python train_one_gpu.py

如果你只是想训练 mask decoder,可以查看0.1 branch 上的教程。


Acknowledgements

  • We highly appreciate all the challenge organizers and dataset owners for providing the public dataset to the community.
  • We thank Meta AI for making the source code of segment anything publicly available.
  • We also thank Alexandre Bonnet for sharing this great blog

2023-07-13(四)

你可能感兴趣的:(DL深度学习,MedSAM,Segment,医疗)