Dramb commited on
Commit
c8ed8e3
·
verified ·
1 Parent(s): 23e82bd

Upload folder using huggingface_hub

Browse files
MedSAM2_pretrain_10ep_b1_AMD-SD_sam2_hiera_t.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8018821b159767012d081db1f5cef2720aa495bad6861b8d6b1c63dce24a5a6e
3
+ size 155972738
README.md CHANGED
@@ -1,3 +0,0 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
config.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "pipeline_tag": "image-segmentation",
3
+ "base_model": "medsam2"
4
+ }
5
+
inference.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict
2
+ import torch
3
+ import numpy as np
4
+ from PIL import Image
5
+ from skimage import transform
6
+ from sam2.build_sam import build_sam2
7
+ from sam2.sam2_image_predictor import SAM2ImagePredictor
8
+
9
+ class PreTrainedModel:
10
+ def __init__(self):
11
+ self.model = build_sam2(
12
+ "sam2_hiera_t",
13
+ "MedSAM2_pretrain_10ep_b1_AMD-SD_sam2_hiera_t.pth",
14
+ device="cuda" if torch.cuda.is_available() else "cpu"
15
+ )
16
+ self.predictor = SAM2ImagePredictor(self.model)
17
+
18
+ def __call__(self, inputs: Dict):
19
+ image = Image.open(inputs["image"]).convert("RGB")
20
+ box = list(map(float, inputs["box"]))
21
+
22
+ image_np = np.array(image)
23
+ img_3c = image_np if image_np.shape[2] == 3 else np.repeat(image_np[:, :, None], 3, axis=-1)
24
+ img_1024 = transform.resize(img_3c, (1024, 1024), preserve_range=True).astype(np.uint8)
25
+
26
+ box_1024 = np.array(box) / [image_np.shape[1], image_np.shape[0], image_np.shape[1], image_np.shape[0]] * 1024
27
+ box_1024 = box_1024[None, :]
28
+
29
+ with torch.inference_mode(), torch.autocast("cuda" if torch.cuda.is_available() else "cpu", dtype=torch.bfloat16):
30
+ self.predictor.set_image(img_1024)
31
+ masks, _, _ = self.predictor.predict(
32
+ point_coords=None,
33
+ point_labels=None,
34
+ box=box_1024,
35
+ multimask_output=False
36
+ )
37
+
38
+ mask = masks[0].astype(np.uint8)
39
+ return {"mask": mask.tolist()}
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch
2
+ numpy
3
+ scikit-image
4
+ gradio
5
+ pillow
6
+ git+https://github.com/facebookresearch/sam2.git