forked from bowang-lab/MedSAM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CVPR24_LiteMedSAM_infer_accelerating.py
executable file
·578 lines (498 loc) · 18.7 KB
/
CVPR24_LiteMedSAM_infer_accelerating.py
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
from os import listdir, makedirs
from os.path import join, isfile, basename
from glob import glob
import PIL
from tqdm import tqdm
from time import time
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
import argparse
from collections import OrderedDict
import pandas as pd
#%% set seeds
torch.set_float32_matmul_precision('high')
torch.manual_seed(2024)
torch.cuda.manual_seed(2024)
np.random.seed(2024)
parser = argparse.ArgumentParser()
parser.add_argument(
'-i',
'--input_dir',
type=str,
default='test_demo/imgs/',
# required=True,
help='root directory of the data',
)
parser.add_argument(
'-o',
'--output_dir',
type=str,
default='test_demo/segs/',
help='directory to save the prediction',
)
parser.add_argument(
'-lite_medsam_checkpoint_path',
type=str,
default="work_dir/LiteMedSAM/lite_medsam.pth",
help='path to the checkpoint of MedSAM-Lite',
)
parser.add_argument(
'-device',
type=str,
default="cpu",
help='device to run the inference',
)
parser.add_argument(
'-num_workers',
type=int,
default=4,
help='number of workers for inference with multiprocessing',
)
parser.add_argument(
'--save_overlay',
default=True,
action='store_true',
help='whether to save the overlay image'
)
parser.add_argument(
'-png_save_dir',
type=str,
default='./overlay',
help='directory to save the overlay image'
)
args = parser.parse_args()
data_root = args.input_dir
pred_save_dir = args.output_dir
save_overlay = args.save_overlay
num_workers = args.num_workers
if save_overlay:
assert args.png_save_dir is not None, "Please specify the directory to save the overlay image"
png_save_dir = args.png_save_dir
makedirs(png_save_dir, exist_ok=True)
lite_medsam_checkpoint_path = args.lite_medsam_checkpoint_path
makedirs(pred_save_dir, exist_ok=True)
device = torch.device(args.device)
image_size = 256
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 get_bbox256(mask_256, bbox_shift=3):
"""
Get the bounding box coordinates from the mask (256x256)
Parameters
----------
mask_256 : numpy.ndarray
the mask of the resized image
bbox_shift : int
Add perturbation to the bounding box coordinates
Returns
-------
numpy.ndarray
bounding box coordinates in the resized image
"""
y_indices, x_indices = np.where(mask_256 > 0)
x_min, x_max = np.min(x_indices), np.max(x_indices)
y_min, y_max = np.min(y_indices), np.max(y_indices)
# add perturbation to bounding box coordinates and test the robustness
# this can be removed if you do not want to test the robustness
H, W = mask_256.shape
x_min = max(0, x_min - bbox_shift)
x_max = min(W, x_max + bbox_shift)
y_min = max(0, y_min - bbox_shift)
y_max = min(H, y_max + bbox_shift)
bboxes256 = np.array([x_min, y_min, x_max, y_max])
return bboxes256
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 model_size_in_bytes(model):
param_size = 0
for param in model.parameters():
param_size += param.nelement() * param.element_size()
return param_size
size_in_bytes = model_size_in_bytes(medsam_lite_model)
size_in_kb = size_in_bytes / 1024
print(f"MedSAM model size: {size_in_bytes} bytes ({size_in_kb:.2f} KB)")
def MedSAM_infer_npz_2D(img_npz_file):
npz_name = basename(img_npz_file)
npz_data = np.load(img_npz_file, 'r', allow_pickle=True) # (H, W, 3)
img_3c = npz_data['imgs'] # (H, W, 3)
assert np.max(img_3c)<256, f'input data should be in range [0, 255], but got {np.unique(img_3c)}'
# PIL.Image.fromarray(img_3c.astype('uint8'), 'RGB').save(join(png_save_dir, 'org_' +npz_name.split(".")[0] + '.png'))
H, W = img_3c.shape[:2]
boxes = npz_data['boxes']
segs = np.zeros(img_3c.shape[:2], dtype=np.uint8)
## 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)
for idx, box in enumerate(boxes, start=1):
box256 = resize_box_to_256(box, original_size=(H, W))
box256 = box256[None, ...] # (1, 4)
medsam_mask, iou_pred = medsam_inference(medsam_lite_model, image_embedding, box256, (newh, neww), (H, W))
segs[medsam_mask>0] = idx
print(f'{npz_name}, box: {box}, predicted iou: {np.round(iou_pred.item(), 4)}')
np.savez_compressed(
join(pred_save_dir, npz_name),
segs=segs,
)
# visualize image, mask and bounding box
if save_overlay:
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
ax[0].imshow(img_3c)
ax[1].imshow(img_3c)
ax[0].set_title("Image")
ax[1].set_title("LiteMedSAM Segmentation")
ax[0].axis('off')
ax[1].axis('off')
for i, box in enumerate(boxes):
color = np.random.rand(3)
box_viz = box
show_box(box_viz, ax[1], edgecolor=color)
show_mask((segs == i+1).astype(np.uint8), ax[1], mask_color=color)
plt.tight_layout()
plt.savefig(join(png_save_dir, npz_name.split(".")[0] + '.png'), dpi=300)
plt.close()
def MedSAM_infer_npz_3D(img_npz_file):
npz_name = basename(img_npz_file)
npz_data = np.load(img_npz_file, 'r', allow_pickle=True)
img_3D = npz_data['imgs'] # (D, H, W)
spacing = npz_data['spacing'] # not used in this demo because it treats each slice independently
segs = np.zeros_like(img_3D, dtype=np.uint8)
boxes_3D = npz_data['boxes'] # [[x_min, y_min, z_min, x_max, y_max, z_max]]
for idx, box3D in enumerate(boxes_3D, start=1):
x_min, y_min, z_min, x_max, y_max, z_max = box3D
assert z_min < z_max, f"z_min should be smaller than z_max, but got {z_min=} and {z_max=}"
mid_slice_bbox_2d = np.array([x_min, y_min, x_max, y_max])
z_middle = int((z_max - z_min)/2 + z_min)
# infer from middle slice to the z_max
print(npz_name, 'infer from middle slice to the z_max')
for z in tqdm(range(z_middle, z_max)):
img_2d = img_3D[z, :, :]
if len(img_2d.shape) == 2:
img_3c = np.repeat(img_2d[:, :, None], 3, axis=-1)
else:
img_3c = img_2d
H, W, _ = img_3c.shape
img_256 = resize_longest_side(img_3c, 256)
new_H, new_W = img_256.shape[:2]
img_256 = (img_256 - img_256.min()) / np.clip(
img_256.max() - img_256.min(), a_min=1e-8, a_max=None
) # normalize to [0, 1], (H, W, 3)
## Pad image to 256x256
img_256 = pad_image(img_256)
# convert the shape to (3, H, W)
img_256_tensor = torch.tensor(img_256).float().permute(2, 0, 1).unsqueeze(0).to(device)
# get the image embedding
with torch.no_grad():
image_embedding = medsam_lite_model.image_encoder(img_256_tensor) # (1, 256, 64, 64)
if z == z_middle:
box_256 = resize_box_to_256(mid_slice_bbox_2d, original_size=(H, W))
else:
pre_seg = segs[z-1, :, :]
if np.max(pre_seg) > 0:
pre_seg256 = resize_longest_side(pre_seg)
pre_seg256 = pad_image(pre_seg256)
box_256 = get_bbox256(pre_seg256)
else:
box_256 = resize_box_to_256(mid_slice_bbox_2d, original_size=(H, W))
img_2d_seg, iou_pred = medsam_inference(medsam_lite_model, image_embedding, box_256, [new_H, new_W], [H, W])
segs[z, img_2d_seg>0] = idx
# infer from middle slice to the z_max
print(npz_name, 'infer from middle slice to the z_min')
for z in tqdm(range(z_middle-1, z_min, -1)):
img_2d = img_3D[z, :, :]
if len(img_2d.shape) == 2:
img_3c = np.repeat(img_2d[:, :, None], 3, axis=-1)
else:
img_3c = img_2d
H, W, _ = img_3c.shape
img_256 = resize_longest_side(img_3c)
new_H, new_W = img_256.shape[:2]
img_256 = (img_256 - img_256.min()) / np.clip(
img_256.max() - img_256.min(), a_min=1e-8, a_max=None
) # normalize to [0, 1], (H, W, 3)
## Pad image to 256x256
img_256 = pad_image(img_256)
img_256_tensor = torch.tensor(img_256).float().permute(2, 0, 1).unsqueeze(0).to(device)
# get the image embedding
with torch.no_grad():
image_embedding = medsam_lite_model.image_encoder(img_256_tensor) # (1, 256, 64, 64)
pre_seg = segs[z+1, :, :]
if np.max(pre_seg) > 0:
pre_seg256 = resize_longest_side(pre_seg)
pre_seg256 = pad_image(pre_seg256)
box_256 = get_bbox256(pre_seg256)
else:
scale_256 = 256 / max(H, W)
box_256 = mid_slice_bbox_2d * scale_256
img_2d_seg, iou_pred = medsam_inference(medsam_lite_model, image_embedding, box_256, [new_H, new_W], [H, W])
segs[z, img_2d_seg>0] = idx
np.savez_compressed(
join(pred_save_dir, npz_name),
segs=segs,
)
# visualize image, mask and bounding box
if save_overlay:
idx = int(segs.shape[0] / 2)
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
ax[0].imshow(img_3D[idx], cmap='gray')
ax[1].imshow(img_3D[idx], cmap='gray')
ax[0].set_title("Image")
ax[1].set_title("LiteMedSAM Segmentation")
ax[0].axis('off')
ax[1].axis('off')
for i, box3D in enumerate(boxes_3D, start=1):
if np.sum(segs[idx]==i) > 0:
color = np.random.rand(3)
x_min, y_min, z_min, x_max, y_max, z_max = box3D
box_viz = np.array([x_min, y_min, x_max, y_max])
show_box(box_viz, ax[1], edgecolor=color)
show_mask(segs[idx]==i, ax[1], mask_color=color)
plt.tight_layout()
plt.savefig(join(png_save_dir, npz_name.split(".")[0] + '.png'), dpi=300)
plt.close()
if __name__ == '__main__':
img_npz_files = sorted(glob(join(data_root, '*.npz'), recursive=True))
efficiency = OrderedDict()
efficiency['case'] = []
efficiency['time'] = []
for img_npz_file in tqdm(img_npz_files):
start_time = time()
if basename(img_npz_file).startswith('3D'):
MedSAM_infer_npz_3D(img_npz_file)
else:
MedSAM_infer_npz_2D(img_npz_file)
end_time = time()
efficiency['case'].append(basename(img_npz_file))
efficiency['time'].append(end_time - start_time)
efficiency_df = pd.DataFrame(efficiency)
efficiency_df.to_csv(join(pred_save_dir, 'efficiency.csv'), index=False)