-
Notifications
You must be signed in to change notification settings - Fork 296
/
Copy pathmodel.py
159 lines (133 loc) · 5.83 KB
/
model.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
import torch
import numpy as np
import os
import sys
import pathlib
from typing import List, Dict, Optional
from uuid import uuid4
from label_studio_ml.model import LabelStudioMLBase
from label_studio_ml.response import ModelResponse
from label_studio_sdk.converter import brush
from label_studio_sdk._extensions.label_studio_tools.core.utils.io import get_local_path
from PIL import Image
ROOT_DIR = os.getcwd()
sys.path.insert(0, ROOT_DIR)
from sam2.build_sam import build_sam2
from sam2.sam2_image_predictor import SAM2ImagePredictor
DEVICE = os.getenv('DEVICE', 'cuda')
MODEL_CONFIG = os.getenv('MODEL_CONFIG', 'configs/sam2.1/sam2.1_hiera_l.yaml')
MODEL_CHECKPOINT = os.getenv('MODEL_CHECKPOINT', 'sam2.1_hiera_large.pt')
if DEVICE == 'cuda':
# use bfloat16 for the entire notebook
torch.autocast(device_type="cuda", dtype=torch.bfloat16).__enter__()
if torch.cuda.get_device_properties(0).major >= 8:
# turn on tfloat32 for Ampere GPUs (https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices)
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
# build path to the model checkpoint
sam2_checkpoint = str(os.path.join(ROOT_DIR, "checkpoints", MODEL_CHECKPOINT))
sam2_model = build_sam2(MODEL_CONFIG, sam2_checkpoint, device=DEVICE)
predictor = SAM2ImagePredictor(sam2_model)
class NewModel(LabelStudioMLBase):
"""Custom ML Backend model
"""
def get_results(self, masks, probs, width, height, from_name, to_name, label):
results = []
total_prob = 0
for mask, prob in zip(masks, probs):
# creates a random ID for your label everytime so no chance for errors
label_id = str(uuid4())[:4]
# converting the mask from the model to RLE format which is usable in Label Studio
mask = mask * 255
rle = brush.mask2rle(mask)
total_prob += prob
results.append({
'id': label_id,
'from_name': from_name,
'to_name': to_name,
'original_width': width,
'original_height': height,
'image_rotation': 0,
'value': {
'format': 'rle',
'rle': rle,
'brushlabels': [label],
},
'score': prob,
'type': 'brushlabels',
'readonly': False
})
return [{
'result': results,
'model_version': self.get('model_version'),
'score': total_prob / max(len(results), 1)
}]
def set_image(self, image_url, task_id):
image_path = get_local_path(image_url, task_id=task_id)
image = Image.open(image_path)
image = np.array(image.convert("RGB"))
predictor.set_image(image)
def _sam_predict(self, img_url, point_coords=None, point_labels=None, input_box=None, task=None):
self.set_image(img_url, task.get('id'))
point_coords = np.array(point_coords, dtype=np.float32) if point_coords else None
point_labels = np.array(point_labels, dtype=np.float32) if point_labels else None
input_box = np.array(input_box, dtype=np.float32) if input_box else None
masks, scores, logits = predictor.predict(
point_coords=point_coords,
point_labels=point_labels,
box=input_box,
multimask_output=True
)
sorted_ind = np.argsort(scores)[::-1]
masks = masks[sorted_ind]
scores = scores[sorted_ind]
mask = masks[0, :, :].astype(np.uint8)
prob = float(scores[0])
# logits = logits[sorted_ind]
return {
'masks': [mask],
'probs': [prob]
}
def predict(self, tasks: List[Dict], context: Optional[Dict] = None, **kwargs) -> ModelResponse:
""" Returns the predicted mask for a smart keypoint that has been placed."""
from_name, to_name, value = self.get_first_tag_occurence('BrushLabels', 'Image')
if not context or not context.get('result'):
# if there is no context, no interaction has happened yet
return ModelResponse(predictions=[])
image_width = context['result'][0]['original_width']
image_height = context['result'][0]['original_height']
# collect context information
point_coords = []
point_labels = []
input_box = None
selected_label = None
for ctx in context['result']:
x = ctx['value']['x'] * image_width / 100
y = ctx['value']['y'] * image_height / 100
ctx_type = ctx['type']
selected_label = ctx['value'][ctx_type][0]
if ctx_type == 'keypointlabels':
point_labels.append(int(ctx.get('is_positive', 0)))
point_coords.append([int(x), int(y)])
elif ctx_type == 'rectanglelabels':
box_width = ctx['value']['width'] * image_width / 100
box_height = ctx['value']['height'] * image_height / 100
input_box = [int(x), int(y), int(box_width + x), int(box_height + y)]
print(f'Point coords are {point_coords}, point labels are {point_labels}, input box is {input_box}')
img_url = tasks[0]['data'][value]
predictor_results = self._sam_predict(
img_url=img_url,
point_coords=point_coords or None,
point_labels=point_labels or None,
input_box=input_box,
task=tasks[0]
)
predictions = self.get_results(
masks=predictor_results['masks'],
probs=predictor_results['probs'],
width=image_width,
height=image_height,
from_name=from_name,
to_name=to_name,
label=selected_label)
return ModelResponse(predictions=predictions)