Update inference.py
Browse files- inference.py +13 -61
inference.py
CHANGED
|
@@ -1,66 +1,18 @@
|
|
| 1 |
-
import base64
|
| 2 |
-
import io
|
| 3 |
-
import torch
|
| 4 |
-
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
-
|
| 7 |
-
from sam2.build_sam import build_sam2
|
| 8 |
-
from sam2.sam2_image_predictor import SAM2ImagePredictor
|
| 9 |
-
|
| 10 |
-
# Инициализация модели один раз
|
| 11 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
-
torch.autocast(device_type=device, dtype=torch.bfloat16).__enter__()
|
| 13 |
-
if torch.cuda.is_available() and torch.cuda.get_device_properties(0).major >= 8:
|
| 14 |
-
torch.backends.cuda.matmul.allow_tf32 = True
|
| 15 |
-
torch.backends.cudnn.allow_tf32 = True
|
| 16 |
-
|
| 17 |
-
# Загрузи путь к своей модели
|
| 18 |
-
medsam2_model = build_sam2('sam2_hiera_t', 'MedSAM2_pretrain_10ep_b1_AMD-SD_sam2_hiera_t.pth', device=device)
|
| 19 |
-
predictor = SAM2ImagePredictor(medsam2_model)
|
| 20 |
-
|
| 21 |
-
# --- Вспомогательные функции ---
|
| 22 |
-
def decode_image(base64_str):
|
| 23 |
-
img_bytes = base64.b64decode(base64_str)
|
| 24 |
-
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 25 |
-
return np.array(img)
|
| 26 |
-
|
| 27 |
-
def encode_mask_to_base64(mask_np):
|
| 28 |
-
mask_img = Image.fromarray((mask_np * 255).astype(np.uint8))
|
| 29 |
-
buffer = io.BytesIO()
|
| 30 |
-
mask_img.save(buffer, format="PNG")
|
| 31 |
-
return base64.b64encode(buffer.getvalue()).decode("utf-8")
|
| 32 |
-
|
| 33 |
-
# --- Главная функция вызова инференса ---
|
| 34 |
-
def predict(input_dict):
|
| 35 |
-
try:
|
| 36 |
-
image_b64 = input_dict["image"]
|
| 37 |
-
box = input_dict["box"] # [x1, y1, x2, y2]
|
| 38 |
-
|
| 39 |
-
image = decode_image(image_b64)
|
| 40 |
-
|
| 41 |
-
# Resize image to 1024x1024 for MedSAM2
|
| 42 |
-
img_3c = np.repeat(image[:, :, None], 3, axis=-1) if len(image.shape) == 2 else image
|
| 43 |
-
img_1024 = transform.resize(img_3c, (1024, 1024), order=3, preserve_range=True, anti_aliasing=True).astype(np.uint8)
|
| 44 |
-
|
| 45 |
-
# Normalize box
|
| 46 |
-
box_np = np.array(box)
|
| 47 |
-
box_1024 = box_np / np.array([img_3c.shape[1], img_3c.shape[0], img_3c.shape[1], img_3c.shape[0]]) * 1024
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
point_labels=None,
|
| 54 |
-
box=np.array([box_1024]),
|
| 55 |
-
multimask_output=False,
|
| 56 |
-
)
|
| 57 |
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
| 66 |
-
return {"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from PIL import Image
|
| 2 |
+
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
class PreTrainedPipeline:
|
| 5 |
+
def __init__(self, path=""):
|
| 6 |
+
# здесь можно подгрузить модель
|
| 7 |
+
print("Model initialized")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
def __call__(self, inputs):
|
| 10 |
+
image = inputs.get("image")
|
| 11 |
|
| 12 |
+
if isinstance(image, str):
|
| 13 |
+
from base64 import b64decode
|
| 14 |
+
from io import BytesIO
|
| 15 |
+
image = Image.open(BytesIO(b64decode(image)))
|
| 16 |
|
| 17 |
+
# Заглушка: возвращаем просто shape
|
| 18 |
+
return {"msg": "Got image", "size": image.size}
|