realslimman
commited on
Commit
•
32d10db
1
Parent(s):
4ca8ef7
Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,159 @@
|
|
1 |
---
|
2 |
license: apache-2.0
|
|
|
|
|
|
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: apache-2.0
|
3 |
+
pipeline_tag: image-segmentation
|
4 |
+
tags:
|
5 |
+
- medical
|
6 |
---
|
7 |
+
|
8 |
+
<h1 align="center">● Medical SAM Adapter</h1>
|
9 |
+
|
10 |
+
<p align="center">
|
11 |
+
<a href="https://discord.gg/hXmQRUHvYc">
|
12 |
+
<img alt="Discord" src="https://img.shields.io/discord/1146610656779440188?logo=discord&style=flat&logoColor=white"/></a>
|
13 |
+
<img src="https://img.shields.io/static/v1?label=license&message=GPL&color=white&style=flat" alt="License"/>
|
14 |
+
</p>
|
15 |
+
|
16 |
+
Medical SAM Adapter, or say MSA, is a project to fineturn [SAM](https://github.com/facebookresearch/segment-anything) using [Adaption](https://lightning.ai/pages/community/tutorial/lora-llm/) for the Medical Imaging.
|
17 |
+
This method is elaborated in the paper [Medical SAM Adapter: Adapting Segment Anything Model for Medical Image Segmentation](https://arxiv.org/abs/2304.12620).
|
18 |
+
|
19 |
+
## A Quick Overview
|
20 |
+
<img width="880" height="380" src="https://github.com/WuJunde/Medical-SAM-Adapter/blob/main/figs/medsamadpt.jpeg">
|
21 |
+
|
22 |
+
## News
|
23 |
+
- [TOP] Join in our [Discord](https://discord.gg/EqbgSPEX) to ask questions and discuss with others.
|
24 |
+
- 23-05-10. This project is still quickly updating 🌝. Check TODO list to see what will be released next.
|
25 |
+
- 23-05-11. GitHub Dicussion opened. You guys can now talk, code and make friends on the playground 👨❤️👨.
|
26 |
+
- 23-12-22. Released data loader and example case on [REFUGE](https://refuge.grand-challenge.org/) dataset. Credit: @jiayuanz3
|
27 |
+
- 24-01-04. Released the Efficient Med-SAM-Adapter❗️ A new, faster, and more lightweight version incorporates Meta [EfficientSAM](https://yformer.github.io/efficient-sam/)🏇. Full credit goes to @shinning0821.
|
28 |
+
- 24-01-07. The image resolution now can be resized by ``-image_size``. Credit: @shinning0821
|
29 |
+
- 24-01-11. Added a detailed guide on utilizing the Efficient Med-SAM-Adapter, complete with a comparison of performance and speed. You can find this resource in [guidance/efficient_sam.ipynb](./guidance/efficient_sam.ipynb). Credit: @shinning0821
|
30 |
+
- 24-01-14. We've just launched our first official version, v0.1.0-alpha 🥳. This release includes support for [MobileSAM](https://github.com/ChaoningZhang/MobileSAM), which can be activated by setting ``-net mobile_sam``. Additionally, you now have the flexibility to use ViT, Tiny ViT, and Efficient ViT as encoders. Check the details [here](https://github.com/KidsWithTokens/Medical-SAM-Adapter/releases/tag/v0.1.0-alpha). Credit: @shinning0821
|
31 |
+
|
32 |
+
## Requirement
|
33 |
+
|
34 |
+
Install the environment:
|
35 |
+
|
36 |
+
``conda env create -f environment.yml``
|
37 |
+
|
38 |
+
``conda activate sam_adapt``
|
39 |
+
|
40 |
+
Then download [SAM checkpoint](https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth), and put it at ./checkpoint/sam/
|
41 |
+
|
42 |
+
You can run:
|
43 |
+
|
44 |
+
``wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth``
|
45 |
+
|
46 |
+
``mv sam_vit_b_01ec64.pth ./checkpoint/sam``
|
47 |
+
creat the folder if it does not exist
|
48 |
+
|
49 |
+
## Example Cases
|
50 |
+
|
51 |
+
### Melanoma Segmentation from Skin Images (2D)
|
52 |
+
|
53 |
+
1. Download ISIC dataset part 1 from https://challenge.isic-archive.com/data/. Then put the csv files in "./data/isic" under your data path. Your dataset folder under "your_data_path" should be like:
|
54 |
+
ISIC/
|
55 |
+
ISBI2016_ISIC_Part1_Test_Data/...
|
56 |
+
|
57 |
+
ISBI2016_ISIC_Part1_Training_Data/...
|
58 |
+
|
59 |
+
ISBI2016_ISIC_Part1_Test_GroundTruth.csv
|
60 |
+
|
61 |
+
ISBI2016_ISIC_Part1_Training_GroundTruth.csv
|
62 |
+
|
63 |
+
2. Begin Adapting! run: ``python train.py -net sam -mod sam_adpt -exp_name *msa_test_isic* -sam_ckpt ./checkpoint/sam/sam_vit_b_01ec64.pth -image_size 1024 -b 32 -dataset isic -data_path *../data*``
|
64 |
+
change "data_path" and "exp_name" for your own useage. you can change "exp_name" to anything you want.
|
65 |
+
|
66 |
+
You can descrease the ``image size`` or batch size ``b`` if out of memory.
|
67 |
+
|
68 |
+
3. Evaluation: The code can automatically evaluate the model on the test set during traing, set "--val_freq" to control how many epoches you want to evaluate once. You can also run val.py for the independent evaluation.
|
69 |
+
|
70 |
+
4. Result Visualization: You can set "--vis" parameter to control how many epoches you want to see the results in the training or evaluation process.
|
71 |
+
|
72 |
+
In default, everything will be saved at `` ./logs/``
|
73 |
+
|
74 |
+
### REFUGE: Optic-disc Segmentation from Fundus Images (2D)
|
75 |
+
[REFUGE](https://refuge.grand-challenge.org/) dataset contains 1200 fundus images with optic disc/cup segmentations and clinical glaucoma labels.
|
76 |
+
|
77 |
+
1. Dowaload the dataset manually from [here](https://huggingface.co/datasets/realslimman/REFUGE-MultiRater/tree/main), or using command lines:
|
78 |
+
|
79 |
+
``git lfs install``
|
80 |
+
|
81 |
+
``git clone [email protected]:datasets/realslimman/REFUGE-MultiRater``
|
82 |
+
|
83 |
+
unzip and put the dataset to the target folder
|
84 |
+
|
85 |
+
``unzip ./REFUGE-MultiRater.zip``
|
86 |
+
|
87 |
+
``mv REFUGE-MultiRater ./data``
|
88 |
+
|
89 |
+
2. For training the adapter, run: ``python train.py -net sam -mod sam_adpt -exp_name REFUGE-MSAdapt -sam_ckpt ./checkpoint/sam/sam_vit_b_01ec64.pth -image_size 1024 -b 32 -dataset REFUGE -data_path ./data/REFUGE-MultiRater``
|
90 |
+
you can change "exp_name" to anything you want.
|
91 |
+
|
92 |
+
You can descrease the ``image size`` or batch size ``b`` if out of memory.
|
93 |
+
|
94 |
+
### Abdominal Multiple Organs Segmentation (3D)
|
95 |
+
|
96 |
+
This tutorial demonstrates how MSA can adapt SAM to 3D multi-organ segmentation task using the BTCV challenge dataset.
|
97 |
+
For BTCV dataset, under Institutional Review Board (IRB) supervision, 50 abdomen CT scans of were randomly selected from a combination of an ongoing colorectal cancer chemotherapy trial, and a retrospective ventral hernia study. The 50 scans were captured during portal venous contrast phase with variable volume sizes (512 x 512 x 85 - 512 x 512 x 198) and field of views (approx. 280 x 280 x 280 mm3 - 500 x 500 x 650 mm3). The in-plane resolution varies from 0.54 x 0.54 mm2 to 0.98 x 0.98 mm2, while the slice thickness ranges from 2.5 mm to 5.0 mm.
|
98 |
+
Target: 13 abdominal organs including
|
99 |
+
Spleen
|
100 |
+
Right Kidney
|
101 |
+
Left Kidney
|
102 |
+
Gallbladder
|
103 |
+
Esophagus
|
104 |
+
Liver
|
105 |
+
Stomach
|
106 |
+
Aorta
|
107 |
+
IVC
|
108 |
+
Portal and Splenic Veins
|
109 |
+
Pancreas
|
110 |
+
Right adrenal gland
|
111 |
+
Left adrenal gland.
|
112 |
+
Modality: CT
|
113 |
+
Size: 30 3D volumes (24 Training + 6 Testing)
|
114 |
+
Challenge: BTCV MICCAI Challenge
|
115 |
+
The following figure shows image patches with the organ sub-regions that are annotated in the CT (top left) and the final labels for the whole dataset (right).
|
116 |
+
1. Prepare BTCV dataset following [MONAI](https://docs.monai.io/en/stable/index.html) instruction:
|
117 |
+
Download BTCV dataset from: https://www.synapse.org/#!Synapse:syn3193805/wiki/217752. After you open the link, navigate to the "Files" tab, then download Abdomen/RawData.zip.
|
118 |
+
After downloading the zip file, unzip. Then put images from RawData/Training/img in ../data/imagesTr, and put labels from RawData/Training/label in ../data/labelsTr.
|
119 |
+
Download the json file for data splits from this [link](https://drive.google.com/file/d/1qcGh41p-rI3H_sQ0JwOAhNiQSXriQqGi/view). Place the JSON file at ../data/dataset_0.json.
|
120 |
+
2. For the Adaptation, run: ``python train.py -net sam -mod sam_adpt -exp_name msa-3d-sam-btcv -sam_ckpt ./checkpoint/sam/sam_vit_b_01ec64.pth -image_size 1024 -b 8 -dataset decathlon -thd True -chunk 96 -dataset ../data -num_sample 4``
|
121 |
+
You can modify following parameters to save the memory usage: '-b' the batch size, '-chunk' the 3D depth (channel) for each sample, '-num_sample' number of samples for [Monai.RandCropByPosNegLabeld](https://docs.monai.io/en/stable/transforms.html#randcropbyposneglabeld), 'evl_chunk' the 3D channel split step in the evaluation, decrease it if out of memory in the evaluation.
|
122 |
+
## Run on your own dataset
|
123 |
+
It is simple to run MSA on the other datasets. Just write another dataset class following which in `` ./dataset.py``. You only need to make sure you return a dict with
|
124 |
+
{
|
125 |
+
'image': A tensor saving images with size [C,H,W] for 2D image, size [C, H, W, D] for 3D data.
|
126 |
+
D is the depth of 3D volume, C is the channel of a scan/frame, which is commonly 1 for CT, MRI, US data.
|
127 |
+
If processing, say like a colorful surgical video, D could the number of time frames, and C will be 3 for a RGB frame.
|
128 |
+
'label': The target masks. Same size with the images except the resolutions (H and W).
|
129 |
+
'p_label': The prompt label to decide positive/negative prompt. To simplify, you can always set 1 if don't need the negative prompt function.
|
130 |
+
'pt': The prompt. Should be the same as that in SAM, e.g., a click prompt should be [x of click, y of click], one click for each scan/frame if using 3d data.
|
131 |
+
'image_meta_dict': Optional. if you want save/visulize the result, you should put the name of the image in it with the key ['filename_or_obj'].
|
132 |
+
...(others as you want)
|
133 |
+
}
|
134 |
+
Welcome to open issues if you meet any problem. It would be appreciated if you could contribute your dataset extensions. Unlike natural images, medical images vary a lot depending on different tasks. Expanding the generalization of a method requires everyone's efforts.
|
135 |
+
|
136 |
+
### TODO LIST
|
137 |
+
|
138 |
+
- [ ] Jupyter tutorials.
|
139 |
+
- [x] Fix bugs in BTCV. Add BTCV example.
|
140 |
+
- [ ] Release REFUGE2, BraTs dataloaders and examples
|
141 |
+
- [x] Changable Image Resolution
|
142 |
+
- [ ] Fix bugs in Multi-GPU parallel
|
143 |
+
- [x] Sample and Vis in training
|
144 |
+
- [ ] Release general data pre-processing and post-processing
|
145 |
+
- [x] Release evaluation
|
146 |
+
- [ ] Deploy on HuggingFace
|
147 |
+
- [x] configuration
|
148 |
+
- [ ] Release SSL code
|
149 |
+
- [ ] Release Medical Adapter Zoo
|
150 |
+
|
151 |
+
## Cite
|
152 |
+
~~~
|
153 |
+
@article{wu2023medical,
|
154 |
+
title={Medical sam adapter: Adapting segment anything model for medical image segmentation},
|
155 |
+
author={Wu, Junde and Fu, Rao and Fang, Huihui and Liu, Yuanpei and Wang, Zhaowei and Xu, Yanwu and Jin, Yueming and Arbel, Tal},
|
156 |
+
journal={arXiv preprint arXiv:2304.12620},
|
157 |
+
year={2023}
|
158 |
+
}
|
159 |
+
~~~
|