File size: 3,973 Bytes
144de54
 
6967514
 
8344180
da443ca
 
 
 
02676e1
 
 
 
144de54
6967514
8344180
6967514
123b4bd
57b6b4d
 
 
 
 
 
 
 
 
 
 
68542c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7fada3
68542c0
 
 
 
 
 
 
 
 
 
 
 
 
cdbde3d
1c9f839
db21559
de6eb37
 
db21559
 
 
8344180
db21559
68542c0
8344180
6967514
 
 
8344180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68542c0
 
 
 
 
 
 
8344180
 
 
68542c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff6d376
1ca53ea
 
 
 
 
 
418f163
da443ca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
---
license: apache-2.0
pipeline_tag: image-segmentation
tags:
- medical
- segmentation
- ct
- mri
- image
- sam
language:
- en
library_name: adapter-transformers
---

Welcome to Medical Adapters Zoo (Med-Adpt Zoo)!

## Med-Adpt Zoo Map 🐘🐊🦍🦒🦨🦜🦥

Lung Nodule (CT)

Melanoma (Skin Photo)

OpticCup (Fundus Image)

OpticDisc (Fundus Image)

Thyroid Nodule (UltraSound)

Aorta (Abdominal Image)

Esophagus (Abdominal Image)

Gallbladder (Abdominal Image)

Inferior Vena Cava (Abdominal Image)

Left Adrenal Gland (Abdominal Image)

Right Adrenal Gland (Abdominal Image)

Left Kidney (Abdominal Image)

Right kidney (Abdominal Image)

Liver (Abdominal Image)

Pancreas (Abdominal Image)

Spleen (Abdominal Image)

Stomach (Abdominal Image)

Portal Vein and Splenic Vein (Abdominal Image)

Edematous Tissue (Brain Tumor mpMRI)

Enhancing Tumor (Brain Tumor mpMRI)

Necrotic (Brain Tumor mpMRI)

Inferior Alveolar Nerve (CBCT)

Surgical Instrument (Surgical Video)

Kidney Tumor (MRI)

Liver (Liver Tumor CE-MRI)

Tumor (Liver Tumor CE-MRI)

Mandible (XRay)

Retina Vessel (Fundus Image)

White Blood Cell (MicroScope)

Download the Adapters you need [here](https://huggingface.co/KidsWithTokens/Medical-Adapter-Zoo/tree/main)

## What
Here are the pre-trained Adapters to transfer [SAM](https://segment-anything.com) (Segment Anything Model) for segmenting various organs/lesions from the medical images.
Check our paper: [Medical SAM Adapter](https://arxiv.org/abs/2304.12620) for the details.

## Why

SAM (Segment Anything Model) is one of the most popular open models for image segmentation. Unfortunately, it does not perform well on the medical images. 
An efficient way to solve it is using Adapters, i.e., some layers with a few parameters to be added to the pre-trained SAM model to fine-tune it to the target down-stream tasks.
Medical image segmentation includes many different organs, lesions, and abnormalities as the targets.
So we are training different adapters for each of the targets, and sharing them here for the easy usage in the community.

Download an adapter for your target disease—trained on organs, lesions, and abnormalities—and effortlessly enhance SAM.

One adapter transfers your SAM to a medical domain expert. Give it a try! 

## How to Use

1. Download the code of our MedSAM-Adapter [here](https://github.com/KidsWithTokens/Medical-SAM-Adapter).
2. Download the weights of the original SAM model.
3. Load the original model and our adapter for downstream tasks.

```python
import torch
import torchvision.transforms as transforms

import cfg
from utils import *

# set your own configs
args = cfg.parse_args()
GPUdevice = torch.device('cuda', args.gpu_device)

# load the original SAM model 
net = get_network(args, args.net, use_gpu=args.gpu, gpu_device=GPUdevice, distribution = args.distributed)
net.eval()

sam_weights = 'checkpoint/sam/sam_vit_b_01ec64.pth'    # load the original SAM weight
with open(sam_weights, "rb") as f:
    state_dict = torch.load(f)
    new_state_dict = {k: v for k, v in state_dict.items() if k in net.state_dict() and net.state_dict()[k].shape == v.shape}
    net.load_state_dict(new_state_dict, strict = False)

# load task-specific adapter
adapter_path = 'OpticCup_Fundus_SAM_1024.pth'
checkpoint_file = os.path.join(adapter_path)
assert os.path.exists(checkpoint_file)
loc = 'cuda:{}'.format(args.gpu_device)
checkpoint = torch.load(checkpoint_file, map_location=loc)

state_dict = checkpoint['state_dict']
if args.distributed != 'none':
    from collections import OrderedDict
    new_state_dict = OrderedDict()
    for k, v in state_dict.items():
        # name = k[7:] # remove `module.`
        name = 'module.' + k
        new_state_dict[name] = v
    # load params
else:
    new_state_dict = state_dict

net.load_state_dict(new_state_dict,strict = False)
```

## Authorship

Ziyue Wang (NUS): Adapters Training

Junde Wu (Oxford): Lead the project

Yueming Jin (NUS): Supervisor