forked from bowang-lab/MedSAM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
326 lines (265 loc) · 9.09 KB
/
gui.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
# -*- coding: utf-8 -*-
import sys
import time
from PyQt5.QtGui import (
QBrush,
QPainter,
QPen,
QPixmap,
QKeySequence,
QPen,
QBrush,
QColor,
QImage,
)
from PyQt5.QtWidgets import (
QFileDialog,
QApplication,
QGraphicsEllipseItem,
QGraphicsItem,
QGraphicsRectItem,
QGraphicsScene,
QGraphicsView,
QGraphicsPixmapItem,
QHBoxLayout,
QPushButton,
QSlider,
QVBoxLayout,
QWidget,
QShortcut,
)
import numpy as np
from skimage import transform, io
import torch
import torch.nn as nn
from torch.nn import functional as F
from PIL import Image
from segment_anything import sam_model_registry
# freeze seeds
torch.manual_seed(2023)
torch.cuda.empty_cache()
torch.cuda.manual_seed(2023)
np.random.seed(2023)
SAM_MODEL_TYPE = "vit_b"
MedSAM_CKPT_PATH = "work_dir/MedSAM/medsam_vit_b.pth"
MEDSAM_IMG_INPUT_SIZE = 1024
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
@torch.no_grad()
def medsam_inference(medsam_model, img_embed, box_1024, height, width):
box_torch = torch.as_tensor(box_1024, dtype=torch.float, device=img_embed.device)
if len(box_torch.shape) == 2:
box_torch = box_torch[:, None, :] # (B, 1, 4)
sparse_embeddings, dense_embeddings = medsam_model.prompt_encoder(
points=None,
boxes=box_torch,
masks=None,
)
low_res_logits, _ = 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 = torch.sigmoid(low_res_logits) # (1, 1, 256, 256)
low_res_pred = F.interpolate(
low_res_pred,
size=(height, width),
mode="bilinear",
align_corners=False,
) # (1, 1, gt.shape)
low_res_pred = low_res_pred.squeeze().cpu().numpy() # (256, 256)
medsam_seg = (low_res_pred > 0.5).astype(np.uint8)
return medsam_seg
print("Loading MedSAM model, a sec.")
tic = time.perf_counter()
# set up model
medsam_model = sam_model_registry["vit_b"](checkpoint=MedSAM_CKPT_PATH).to(device)
medsam_model.eval()
print(f"Done, took {time.perf_counter() - tic}")
def np2pixmap(np_img):
height, width, channel = np_img.shape
bytesPerLine = 3 * width
qImg = QImage(np_img.data, width, height, bytesPerLine, QImage.Format_RGB888)
return QPixmap.fromImage(qImg)
colors = [
(255, 0, 0),
(0, 255, 0),
(0, 0, 255),
(255, 255, 0),
(255, 0, 255),
(0, 255, 255),
(128, 0, 0),
(0, 128, 0),
(0, 0, 128),
(128, 128, 0),
(128, 0, 128),
(0, 128, 128),
(255, 255, 255),
(192, 192, 192),
(64, 64, 64),
(255, 0, 255),
(0, 255, 255),
(255, 255, 0),
(0, 0, 127),
(192, 0, 192),
]
class Window(QWidget):
def __init__(self):
super().__init__()
# configs
self.half_point_size = 5 # radius of bbox starting and ending points
# app stats
self.image_path = None
self.color_idx = 0
self.bg_img = None
self.is_mouse_down = False
self.rect = None
self.point_size = self.half_point_size * 2
self.start_point = None
self.end_point = None
self.start_pos = (None, None)
self.embedding = None
self.prev_mask = None
self.view = QGraphicsView()
self.view.setRenderHint(QPainter.Antialiasing)
pixmap = self.load_image()
vbox = QVBoxLayout(self)
vbox.addWidget(self.view)
load_button = QPushButton("Load Image")
save_button = QPushButton("Save Mask")
hbox = QHBoxLayout(self)
hbox.addWidget(load_button)
hbox.addWidget(save_button)
vbox.addLayout(hbox)
self.setLayout(vbox)
# keyboard shortcuts
self.quit_shortcut = QShortcut(QKeySequence("Ctrl+Q"), self)
self.quit_shortcut.activated.connect(lambda: quit())
self.undo_shortcut = QShortcut(QKeySequence("Ctrl+Z"), self)
self.undo_shortcut.activated.connect(self.undo)
load_button.clicked.connect(self.load_image)
save_button.clicked.connect(self.save_mask)
def undo(self):
if self.prev_mask is None:
print("No previous mask record")
return
self.color_idx -= 1
bg = Image.fromarray(self.img_3c.astype("uint8"), "RGB")
mask = Image.fromarray(self.prev_mask.astype("uint8"), "RGB")
img = Image.blend(bg, mask, 0.2)
self.scene.removeItem(self.bg_img)
self.bg_img = self.scene.addPixmap(np2pixmap(np.array(img)))
self.mask_c = self.prev_mask
self.prev_mask = None
def load_image(self):
file_path, file_type = QFileDialog.getOpenFileName(
self, "Choose Image to Segment", ".", "Image Files (*.tif *.png *.jpg *.bmp)"
)
if file_path is None or len(file_path) == 0:
print("No image path specified, plz select an image")
exit()
img_np = np.array(Image.open(file_path))
if len(img_np.shape) == 2:
img_3c = np.repeat(img_np[:, :, None], 3, axis=-1)
else:
img_3c = img_np
self.img_3c = img_3c
self.image_path = file_path
self.get_embeddings()
pixmap = np2pixmap(self.img_3c)
H, W, _ = self.img_3c.shape
self.scene = QGraphicsScene(0, 0, W, H)
self.end_point = None
self.rect = None
self.bg_img = self.scene.addPixmap(pixmap)
self.bg_img.setPos(0, 0)
self.mask_c = np.zeros((*self.img_3c.shape[:2], 3), dtype="uint8")
self.view.setScene(self.scene)
# events
self.scene.mousePressEvent = self.mouse_press
self.scene.mouseMoveEvent = self.mouse_move
self.scene.mouseReleaseEvent = self.mouse_release
def mouse_press(self, ev):
x, y = ev.scenePos().x(), ev.scenePos().y()
self.is_mouse_down = True
self.start_pos = ev.scenePos().x(), ev.scenePos().y()
self.start_point = self.scene.addEllipse(
x - self.half_point_size,
y - self.half_point_size,
self.point_size,
self.point_size,
pen=QPen(QColor("red")),
brush=QBrush(QColor("red")),
)
def mouse_move(self, ev):
if not self.is_mouse_down:
return
x, y = ev.scenePos().x(), ev.scenePos().y()
if self.end_point is not None:
self.scene.removeItem(self.end_point)
self.end_point = self.scene.addEllipse(
x - self.half_point_size,
y - self.half_point_size,
self.point_size,
self.point_size,
pen=QPen(QColor("red")),
brush=QBrush(QColor("red")),
)
if self.rect is not None:
self.scene.removeItem(self.rect)
sx, sy = self.start_pos
xmin = min(x, sx)
xmax = max(x, sx)
ymin = min(y, sy)
ymax = max(y, sy)
self.rect = self.scene.addRect(
xmin, ymin, xmax - xmin, ymax - ymin, pen=QPen(QColor("red"))
)
def mouse_release(self, ev):
x, y = ev.scenePos().x(), ev.scenePos().y()
sx, sy = self.start_pos
xmin = min(x, sx)
xmax = max(x, sx)
ymin = min(y, sy)
ymax = max(y, sy)
self.is_mouse_down = False
H, W, _ = self.img_3c.shape
box_np = np.array([[xmin, ymin, xmax, ymax]])
print("bounding box:", box_np)
box_1024 = box_np / np.array([W, H, W, H]) * 1024
sam_mask = medsam_inference(medsam_model, self.embedding, box_1024, H, W)
self.prev_mask = self.mask_c.copy()
self.mask_c[sam_mask != 0] = colors[self.color_idx % len(colors)]
self.color_idx += 1
bg = Image.fromarray(self.img_3c.astype("uint8"), "RGB")
mask = Image.fromarray(self.mask_c.astype("uint8"), "RGB")
img = Image.blend(bg, mask, 0.2)
self.scene.removeItem(self.bg_img)
self.bg_img = self.scene.addPixmap(np2pixmap(np.array(img)))
def save_mask(self):
out_path = f"{self.image_path.split('.')[0]}_mask.png"
io.imsave(out_path, self.mask_c)
@torch.no_grad()
def get_embeddings(self):
print("Calculating embedding, gui may be unresponsive.")
img_1024 = transform.resize(
self.img_3c, (1024, 1024), order=3, preserve_range=True, anti_aliasing=True
).astype(np.uint8)
img_1024 = (img_1024 - img_1024.min()) / np.clip(
img_1024.max() - img_1024.min(), a_min=1e-8, a_max=None
) # normalize to [0, 1], (H, W, 3)
# convert the shape to (3, H, W)
img_1024_tensor = (
torch.tensor(img_1024).float().permute(2, 0, 1).unsqueeze(0).to(device)
)
# if self.embedding is None:
with torch.no_grad():
self.embedding = medsam_model.image_encoder(
img_1024_tensor
) # (1, 256, 64, 64)
print("Done.")
app = QApplication(sys.argv)
w = Window()
w.show()
app.exec()