flaviagiammarino commited on
Commit
b00dede
1 Parent(s): 60164d3

Upload 2 files

Browse files
Files changed (2) hide show
  1. scripts/pt_example.png +0 -0
  2. scripts/pt_example.py +13 -9
scripts/pt_example.png CHANGED
scripts/pt_example.py CHANGED
@@ -7,14 +7,14 @@ from transformers import SamModel, SamProcessor
7
 
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
 
10
- model = SamModel.from_pretrained("flaviagiammarino/medsam-vit-base")
11
  processor = SamProcessor.from_pretrained("flaviagiammarino/medsam-vit-base")
12
 
13
  img_url = "https://raw.githubusercontent.com/bowang-lab/MedSAM/main/assets/img_demo.png"
14
  raw_image = Image.open(requests.get(img_url, stream=True).raw).convert("RGB")
15
- input_boxes = [95, 255, 190, 350]
16
 
17
- inputs = processor(raw_image, input_boxes=[input_boxes], return_tensors="pt").to(device)
18
  outputs = model(**inputs, multimask_output=False)
19
  masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu())
20
 
@@ -32,11 +32,15 @@ def show_box(box, ax):
32
  w, h = box[2] - box[0], box[3] - box[1]
33
  ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor="blue", facecolor=(0, 0, 0, 0), lw=2))
34
 
35
- plt.imshow(np.array(raw_image))
36
- ax = plt.gca()
37
- for mask in masks:
38
- show_mask(mask, ax=ax, random_color=False)
39
- show_box(input_boxes, ax)
40
- plt.axis("off")
 
 
 
 
41
  plt.tight_layout()
42
  plt.show()
 
7
 
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
 
10
+ model = SamModel.from_pretrained("flaviagiammarino/medsam-vit-base").to(device)
11
  processor = SamProcessor.from_pretrained("flaviagiammarino/medsam-vit-base")
12
 
13
  img_url = "https://raw.githubusercontent.com/bowang-lab/MedSAM/main/assets/img_demo.png"
14
  raw_image = Image.open(requests.get(img_url, stream=True).raw).convert("RGB")
15
+ input_boxes = [95., 255., 190., 350.]
16
 
17
+ inputs = processor(raw_image, input_boxes=[[input_boxes]], return_tensors="pt").to(device)
18
  outputs = model(**inputs, multimask_output=False)
19
  masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu())
20
 
 
32
  w, h = box[2] - box[0], box[3] - box[1]
33
  ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor="blue", facecolor=(0, 0, 0, 0), lw=2))
34
 
35
+ fig, ax = plt.subplots(1, 2, figsize=(10, 5))
36
+ ax[0].imshow(np.array(raw_image))
37
+ show_box(input_boxes, ax[0])
38
+ ax[0].set_title("Input Image and Bounding Box")
39
+ ax[0].axis("off")
40
+ ax[1].imshow(np.array(raw_image))
41
+ show_mask(masks[0], ax=ax[1], random_color=False)
42
+ show_box(input_boxes, ax[1])
43
+ ax[1].set_title("MedSAM Segmentation")
44
+ ax[1].axis("off")
45
  plt.tight_layout()
46
  plt.show()