Bono93 commited on
Commit
d13628a
·
1 Parent(s): 8ad7724

fix: medsam model

Browse files
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import os
2
  import numpy as np
3
  import gradio as gr
4
  import torch
@@ -6,8 +5,9 @@ import cv2
6
  from segment_anything import SamPredictor, sam_model_registry
7
 
8
  # Global variables
 
9
  OFFICIAL_CHECKPOINT = "./models/sam_vit_b_01ec64.pth"
10
- MEDSAM_CHECKPOINT = "./models/medsam_vitb.pth"
11
  MODEL_TYPE = "vit_b"
12
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
13
 
@@ -22,6 +22,17 @@ MEDSAM.to(device=DEVICE)
22
  MEDSAM_PREDICTOR = SamPredictor(MEDSAM)
23
 
24
 
 
 
 
 
 
 
 
 
 
 
 
25
  def draw_contour(image: np.ndarray, mask: np.ndarray) -> np.ndarray:
26
  # draw contour
27
  contour_image = image.copy()
@@ -53,12 +64,12 @@ def inference(
53
  return contour_image
54
 
55
 
56
- def extract_object_by_event(
57
- image: np.ndarray, evt: gr.SelectData
58
- ):
59
  click_h, click_w = evt.index
60
 
61
- return inference(SAM_PREDICTOR, image, click_h, click_w)
62
 
63
 
64
  def get_coords(evt: gr.SelectData):
@@ -76,10 +87,11 @@ with gr.Blocks() as demo:
76
  )
77
  with gr.Row():
78
  # select model
79
- model_type = gr.Dropdown(
80
- ["oficial_sam", "breast-cancer-sam"],
81
- value="oficial_sam",
82
  label="Select Model",
 
 
 
83
  )
84
 
85
  # Segment image
@@ -96,7 +108,7 @@ with gr.Blocks() as demo:
96
  with gr.Row():
97
  coord_h = gr.Number(label="Mouse coords h")
98
  coord_w = gr.Number(label="Mouse coords w")
99
- input_image.select(extract_object_by_event, [input_image], output)
100
  input_image.select(get_coords, None, [coord_h, coord_w])
101
 
102
  demo.queue().launch(debug=True, enable_queue=True)
 
 
1
  import numpy as np
2
  import gradio as gr
3
  import torch
 
5
  from segment_anything import SamPredictor, sam_model_registry
6
 
7
  # Global variables
8
+ MODELS = ["./models/sam_vit_b_01ec64.pth", "./models/medsam_vitb.pth"]
9
  OFFICIAL_CHECKPOINT = "./models/sam_vit_b_01ec64.pth"
10
+ MEDSAM_CHECKPOINT = "./models/medsam_vitb_best.pth"
11
  MODEL_TYPE = "vit_b"
12
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
13
 
 
22
  MEDSAM_PREDICTOR = SamPredictor(MEDSAM)
23
 
24
 
25
+ def load_model(model_choice: int) -> SamPredictor:
26
+ """Load model."""
27
+ print("model_choice", model_choice)
28
+ if model_choice == 0:
29
+ return SAM_PREDICTOR
30
+ elif model_choice == 1:
31
+ return MEDSAM_PREDICTOR
32
+ else:
33
+ raise ValueError("Model choice must be 0 or 1")
34
+
35
+
36
  def draw_contour(image: np.ndarray, mask: np.ndarray) -> np.ndarray:
37
  # draw contour
38
  contour_image = image.copy()
 
64
  return contour_image
65
 
66
 
67
+ def extract_object_by_event(model_choice: int, image: np.ndarray, evt: gr.SelectData):
68
+ """Extract object by mouse click."""
69
+ predictor = load_model(model_choice)
70
  click_h, click_w = evt.index
71
 
72
+ return inference(predictor, image, click_h, click_w)
73
 
74
 
75
  def get_coords(evt: gr.SelectData):
 
87
  )
88
  with gr.Row():
89
  # select model
90
+ model_choice = gr.Dropdown(
 
 
91
  label="Select Model",
92
+ choices=[m for m in MODELS],
93
+ type="index",
94
+ interactive=True,
95
  )
96
 
97
  # Segment image
 
108
  with gr.Row():
109
  coord_h = gr.Number(label="Mouse coords h")
110
  coord_w = gr.Number(label="Mouse coords w")
111
+ input_image.select(extract_object_by_event, [model_choice, input_image], output)
112
  input_image.select(get_coords, None, [coord_h, coord_w])
113
 
114
  demo.queue().launch(debug=True, enable_queue=True)
models/{medsam_vitb.pth → medsam_vitb_best.pth} RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:ec2df62732614e57411cdcf32a23ffdf28910380d03139ee0f4fcbe91eb8c912
3
- size 375042383
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2b194d15811c1471edd68a65b19d76932f49450cb8e5f20cddae08ac142c8101
3
+ size 375063985