forked from bowang-lab/MedSAM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
342 lines (295 loc) · 10.9 KB
/
api.py
File metadata and controls
342 lines (295 loc) · 10.9 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import base64
import datetime
from io import BytesIO
from skimage import io
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from segment_anything.modeling import MaskDecoder, PromptEncoder, TwoWayTransformer
from tiny_vit_sam import TinyViT
from matplotlib import pyplot as plt
import cv2
#%% set seeds
torch.set_float32_matmul_precision('high')
torch.manual_seed(2024)
torch.cuda.manual_seed(2024)
lite_medsam_checkpoint_path = 'lite_medsam.pth'
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def resize_longest_side(image, target_length=256):
"""
Resize image to target_length while keeping the aspect ratio
Expects a numpy array with shape HxWxC in uint8 format.
"""
oldh, oldw = image.shape[0], image.shape[1]
scale = target_length * 1.0 / max(oldh, oldw)
newh, neww = oldh * scale, oldw * scale
neww, newh = int(neww + 0.5), int(newh + 0.5)
target_size = (neww, newh)
return cv2.resize(image, target_size, interpolation=cv2.INTER_AREA)
def pad_image(image, target_size=256):
"""
Pad image to target_size
Expects a numpy array with shape HxWxC in uint8 format.
"""
# Pad
h, w = image.shape[0], image.shape[1]
padh = target_size - h
padw = target_size - w
if len(image.shape) == 3: ## Pad image
image_padded = np.pad(image, ((0, padh), (0, padw), (0, 0)))
else: ## Pad gt mask
image_padded = np.pad(image, ((0, padh), (0, padw)))
return image_padded
class MedSAM_Lite(nn.Module):
def __init__(
self,
image_encoder,
mask_decoder,
prompt_encoder
):
super().__init__()
self.image_encoder = image_encoder
self.mask_decoder = mask_decoder
self.prompt_encoder = prompt_encoder
def forward(self, image, box_np):
image_embedding = self.image_encoder(image) # (B, 256, 64, 64)
# do not compute gradients for prompt encoder
with torch.no_grad():
box_torch = torch.as_tensor(box_np, dtype=torch.float32, device=image.device)
if len(box_torch.shape) == 2:
box_torch = box_torch[:, None, :] # (B, 1, 4)
sparse_embeddings, dense_embeddings = self.prompt_encoder(
points=None,
boxes=box_np,
masks=None,
)
low_res_masks, iou_predictions = self.mask_decoder(
image_embeddings=image_embedding, # (B, 256, 64, 64)
image_pe=self.prompt_encoder.get_dense_pe(), # (1, 256, 64, 64)
sparse_prompt_embeddings=sparse_embeddings, # (B, 2, 256)
dense_prompt_embeddings=dense_embeddings, # (B, 256, 64, 64)
multimask_output=False,
) # (B, 1, 256, 256)
return low_res_masks
@torch.no_grad()
def postprocess_masks(self, masks, new_size, original_size):
"""
Do cropping and resizing
Parameters
----------
masks : torch.Tensor
masks predicted by the model
new_size : tuple
the shape of the image after resizing to the longest side of 256
original_size : tuple
the original shape of the image
Returns
-------
torch.Tensor
the upsampled mask to the original size
"""
# Crop
masks = masks[..., :new_size[0], :new_size[1]]
# Resize
masks = F.interpolate(
masks,
size=(original_size[0], original_size[1]),
mode="bilinear",
align_corners=False,
)
return masks
def show_mask(mask, ax, mask_color=None, alpha=0.5):
"""
show mask on the image
Parameters
----------
mask : numpy.ndarray
mask of the image
ax : matplotlib.axes.Axes
axes to plot the mask
mask_color : numpy.ndarray
color of the mask
alpha : float
transparency of the mask
"""
if mask_color is not None:
color = np.concatenate([mask_color, np.array([alpha])], axis=0)
else:
color = np.array([251/255, 252/255, 30/255, alpha])
h, w = mask.shape[-2:]
mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
ax.imshow(mask_image)
def show_box(box, ax, edgecolor='blue'):
"""
show bounding box on the image
Parameters
----------
box : numpy.ndarray
bounding box coordinates in the original image
ax : matplotlib.axes.Axes
axes to plot the bounding box
edgecolor : str
color of the bounding box
"""
x0, y0 = box[0], box[1]
w, h = box[2] - box[0], box[3] - box[1]
ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor=edgecolor, facecolor=(0,0,0,0), lw=2))
def resize_box_to_256(box, original_size):
"""
the input bounding box is obtained from the original image
here, we rescale it to the coordinates of the resized image
Parameters
----------
box : numpy.ndarray
bounding box coordinates in the original image
original_size : tuple
the original size of the image
Returns
-------
numpy.ndarray
bounding box coordinates in the resized image
"""
new_box = np.zeros_like(box)
ratio = 256 / max(original_size)
for i in range(len(box)):
new_box[i] = int(box[i] * ratio)
return new_box
@torch.no_grad()
def medsam_inference(medsam_model, img_embed, box_256, new_size, original_size):
"""
Perform inference using the LiteMedSAM model.
Args:
medsam_model (MedSAMModel): The MedSAM model.
img_embed (torch.Tensor): The image embeddings.
box_256 (numpy.ndarray): The bounding box coordinates.
new_size (tuple): The new size of the image.
original_size (tuple): The original size of the image.
Returns:
tuple: A tuple containing the segmented image and the intersection over union (IoU) score.
"""
box_torch = torch.as_tensor(box_256[None, None, ...], dtype=torch.float, device=img_embed.device)
sparse_embeddings, dense_embeddings = medsam_model.prompt_encoder(
points = None,
boxes = box_torch,
masks = None,
)
low_res_logits, iou = medsam_model.mask_decoder(
image_embeddings=img_embed, # (B, 256, 64, 64)
image_pe=medsam_model.prompt_encoder.get_dense_pe(), # (1, 256, 64, 64)
sparse_prompt_embeddings=sparse_embeddings, # (B, 2, 256)
dense_prompt_embeddings=dense_embeddings, # (B, 256, 64, 64)
multimask_output=False
)
low_res_pred = medsam_model.postprocess_masks(low_res_logits, new_size, original_size)
low_res_pred = torch.sigmoid(low_res_pred)
low_res_pred = low_res_pred.squeeze().cpu().numpy()
medsam_seg = (low_res_pred > 0.5).astype(np.uint8)
return medsam_seg, iou
medsam_lite_image_encoder = TinyViT(
img_size=256,
in_chans=3,
embed_dims=[
64, ## (64, 256, 256)
128, ## (128, 128, 128)
160, ## (160, 64, 64)
320 ## (320, 64, 64)
],
depths=[2, 2, 6, 2],
num_heads=[2, 4, 5, 10],
window_sizes=[7, 7, 14, 7],
mlp_ratio=4.,
drop_rate=0.,
drop_path_rate=0.0,
use_checkpoint=False,
mbconv_expand_ratio=4.0,
local_conv_size=3,
layer_lr_decay=0.8
)
medsam_lite_prompt_encoder = PromptEncoder(
embed_dim=256,
image_embedding_size=(64, 64),
input_image_size=(256, 256),
mask_in_chans=16
)
medsam_lite_mask_decoder = MaskDecoder(
num_multimask_outputs=3,
transformer=TwoWayTransformer(
depth=2,
embedding_dim=256,
mlp_dim=2048,
num_heads=8,
),
transformer_dim=256,
iou_head_depth=3,
iou_head_hidden_dim=256,
)
medsam_lite_model = MedSAM_Lite(
image_encoder = medsam_lite_image_encoder,
mask_decoder = medsam_lite_mask_decoder,
prompt_encoder = medsam_lite_prompt_encoder
)
lite_medsam_checkpoint = torch.load(lite_medsam_checkpoint_path, map_location='cpu')
medsam_lite_model.load_state_dict(lite_medsam_checkpoint)
if device.type == "cpu":
# https://pytorch.org/tutorials/recipes/quantization.html#post-training-dynamic-quantization
medsam_lite_model = torch.quantization.quantize_dynamic(
medsam_lite_model, {torch.nn.Linear}, dtype=torch.qint8
)
medsam_lite_model.to(device)
medsam_lite_model.eval()
def blend_segment_with_original(image, box):
"""
- segments the specified rectangle of the image,
and blends the segmented area with the original image.
Parameters:
- image: Base64: The image as a OpenCV2 image.
- box: A tuple:(x1, y1, x2, y2) defining the rectangle box for segmentation.
Returns:
- output_image_bytesio: The blended image as a BytesIO.
"""
# Convert the image to 3 channels if it is not
# This handles different image formats like grayscale or single-channel
if image.ndim == 2: # Grayscale image
img_3c = np.stack((image, image, image), axis=-1)
elif image.shape[-1] == 1: # Single-channel image
img_3c = np.repeat(image, 3, axis=-1)
else: # Already a 3-channel image
img_3c = image
# Assert that the image data is within the expected range (0 to 255)
# This is important for ensuring the image is properly formatted
assert np.max(img_3c) < 256, f'Input data should be in range [0, 255], but got {np.unique(img_3c)}'
# Get the height, width
H, W, _ = img_3c.shape
## preprocessing
img_256 = resize_longest_side(img_3c, 256)
newh, neww = img_256.shape[:2]
img_256_norm = (img_256 - img_256.min()) / np.clip(
img_256.max() - img_256.min(), a_min=1e-8, a_max=None
)
img_256_padded = pad_image(img_256_norm, 256)
img_256_tensor = torch.tensor(img_256_padded).float().permute(2, 0, 1).unsqueeze(0).to(device)
with torch.no_grad():
image_embedding = medsam_lite_model.image_encoder(img_256_tensor)
box256 = resize_box_to_256(box, original_size=(H, W))
box256 = box256[None, ...] # (1, 4)
sam_mask, iou_pred = medsam_inference(medsam_lite_model, image_embedding, box256, (newh, neww), (H, W))
print(f'box: {box}, predicted iou: {np.round(iou_pred.item(), 4)}')
# Creating a figure with dimensions based on the image size.
fig = plt.figure(figsize=(W / 100, H / 100))
# Adding an axis to the figure without padding and margin.
ax = fig.add_axes([0, 0, 1, 1])
ax.imshow(img_3c)
ax.axis('off')
# Defining an RGB color for the box and mask (orange in this case).
color = np.array([1.0, 0.5, 0.0])
# Drawing the bounding box on the image.
show_box(box, ax, edgecolor=color)
# Displaying the segmentation mask over the image with some transparency.
show_mask((sam_mask > 0).astype(np.uint8), ax, mask_color=color, alpha=0.4)
output_image_bytesio = BytesIO()
# Saving the figure to a BytesIO object (in-memory buffer) as a PNG image.
plt.savefig(output_image_bytesio, format='png', dpi=100, bbox_inches='tight', pad_inches=0)
plt.close()
output_image_bytesio.seek(0)
return output_image_bytesio