Spaces:
Sleeping
Sleeping
Commit
·
26c4598
1
Parent(s):
857c85b
Update model.py
Browse files
model.py
CHANGED
@@ -1,145 +1,48 @@
|
|
1 |
import os
|
|
|
2 |
|
3 |
-
from
|
4 |
-
from typing import List, Dict, Optional
|
5 |
-
from uuid import uuid4
|
6 |
-
from sam_predictor import SAMPredictor
|
7 |
from label_studio_ml.model import LabelStudioMLBase
|
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 |
-
input_box = [int(x), int(y), int(box_width + x), int(box_height + y)]
|
44 |
-
|
45 |
-
print(f'Point coords are {point_coords}, point labels are {point_labels}, input box is {input_box}')
|
46 |
-
|
47 |
-
img_path = tasks[0]['data'][value]
|
48 |
-
predictor_results = PREDICTOR.predict(
|
49 |
-
img_path=img_path,
|
50 |
-
point_coords=point_coords or None,
|
51 |
-
point_labels=point_labels or None,
|
52 |
-
input_box=input_box
|
53 |
-
)
|
54 |
-
|
55 |
-
predictions = self.get_results(
|
56 |
-
masks=predictor_results['masks'],
|
57 |
-
probs=predictor_results['probs'],
|
58 |
-
width=image_width,
|
59 |
-
height=image_height,
|
60 |
-
from_name=from_name,
|
61 |
-
to_name=to_name,
|
62 |
-
label=selected_label)
|
63 |
-
|
64 |
-
return predictions
|
65 |
-
|
66 |
-
def get_results(self, masks, probs, width, height, from_name, to_name, label):
|
67 |
-
results = []
|
68 |
-
for mask, prob in zip(masks, probs):
|
69 |
-
# creates a random ID for your label everytime so no chance for errors
|
70 |
-
label_id = str(uuid4())[:4]
|
71 |
-
# converting the mask from the model to RLE format which is usable in Label Studio
|
72 |
-
mask = mask * 255
|
73 |
-
rle = brush.mask2rle(mask)
|
74 |
-
|
75 |
-
results.append({
|
76 |
-
'id': label_id,
|
77 |
-
'from_name': from_name,
|
78 |
-
'to_name': to_name,
|
79 |
-
'original_width': width,
|
80 |
-
'original_height': height,
|
81 |
-
'image_rotation': 0,
|
82 |
-
'value': {
|
83 |
-
'format': 'rle',
|
84 |
-
'rle': rle,
|
85 |
-
'brushlabels': [label],
|
86 |
-
},
|
87 |
-
'score': prob,
|
88 |
-
'type': 'brushlabels',
|
89 |
-
'readonly': False
|
90 |
})
|
|
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
if __name__ == '__main__':
|
99 |
-
# test the model
|
100 |
-
model = SamMLBackend()
|
101 |
-
model.use_label_config('''
|
102 |
-
<View>
|
103 |
-
<Image name="image" value="$image" zoom="true"/>
|
104 |
-
<BrushLabels name="tag" toName="image">
|
105 |
-
<Label value="Banana" background="#FF0000"/>
|
106 |
-
<Label value="Orange" background="#0d14d3"/>
|
107 |
-
</BrushLabels>
|
108 |
-
<KeyPointLabels name="tag2" toName="image" smart="true" >
|
109 |
-
<Label value="Banana" background="#000000" showInline="true"/>
|
110 |
-
<Label value="Orange" background="#000000" showInline="true"/>
|
111 |
-
</KeyPointLabels>
|
112 |
-
<RectangleLabels name="tag3" toName="image" >
|
113 |
-
<Label value="Banana" background="#000000" showInline="true"/>
|
114 |
-
<Label value="Orange" background="#000000" showInline="true"/>
|
115 |
-
</RectangleLabels>
|
116 |
-
</View>
|
117 |
-
''')
|
118 |
-
results = model.predict(
|
119 |
-
tasks=[{
|
120 |
-
'data': {
|
121 |
-
'image': 'https://s3.amazonaws.com/htx-pub/datasets/images/125245483_152578129892066_7843809718842085333_n.jpg'
|
122 |
-
}}],
|
123 |
-
context={
|
124 |
-
'result': [{
|
125 |
-
'original_width': 1080,
|
126 |
-
'original_height': 1080,
|
127 |
-
'image_rotation': 0,
|
128 |
-
'value': {
|
129 |
-
'x': 49.441786283891545,
|
130 |
-
'y': 59.96810207336522,
|
131 |
-
'width': 0.3189792663476874,
|
132 |
-
'labels': ['Banana'],
|
133 |
-
'keypointlabels': ['Banana']
|
134 |
-
},
|
135 |
-
'is_positive': True,
|
136 |
-
'id': 'fBWv1t0S2L',
|
137 |
-
'from_name': 'tag2',
|
138 |
-
'to_name': 'image',
|
139 |
-
'type': 'keypointlabels',
|
140 |
-
'origin': 'manual'
|
141 |
-
}]}
|
142 |
-
)
|
143 |
-
import json
|
144 |
-
results[0]['result'][0]['value']['rle'] = f'...{len(results[0]["result"][0]["value"]["rle"])} integers...'
|
145 |
-
print(json.dumps(results, indent=2))
|
|
|
1 |
import os
|
2 |
+
import logging
|
3 |
|
4 |
+
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
|
|
|
|
|
|
5 |
from label_studio_ml.model import LabelStudioMLBase
|
6 |
+
from lxml import etree
|
7 |
+
|
8 |
+
|
9 |
+
class Model(LabelStudioMLBase):
|
10 |
+
|
11 |
+
image_processor = AutoImageProcessor.from_pretrained("diegokauer/conditional-detr-coe-int")
|
12 |
+
model = AutoModelForObjectDetection.from_pretrained("diegokauer/conditional-detr-coe-int")
|
13 |
+
|
14 |
+
def __init__(self, **kwargs):
|
15 |
+
# don't forget to call base class constructor
|
16 |
+
super(Model, self).__init__(**kwargs)
|
17 |
+
|
18 |
+
# you can preinitialize variables with keys needed to extract info from tasks and annotations and form predictions
|
19 |
+
self.model = model
|
20 |
+
self.tokenizer = image_processor
|
21 |
+
self.id2label = model.config.id2label
|
22 |
+
|
23 |
+
def predict(self, tasks, **kwargs):
|
24 |
+
""" This is where inference happens: model returns
|
25 |
+
the list of predictions based on input list of tasks
|
26 |
+
"""
|
27 |
+
predictions = []
|
28 |
+
for task in tasks:
|
29 |
+
predictions.append({
|
30 |
+
'score': 0.987, # prediction overall score, visible in the data manager columns
|
31 |
+
'model_version': 'delorean-20151021', # all predictions will be differentiated by model version
|
32 |
+
'result': [{
|
33 |
+
'from_name': self.from_name,
|
34 |
+
'to_name': self.to_name,
|
35 |
+
'type': 'choices',
|
36 |
+
'score': 0.5, # per-region score, visible in the editor
|
37 |
+
'value': {
|
38 |
+
'choices': [self.labels[0]]
|
39 |
+
}
|
40 |
+
}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
})
|
42 |
+
return predictions
|
43 |
|
44 |
+
def fit(self, annotations, **kwargs):
|
45 |
+
""" This is where training happens: train your model given list of annotations,
|
46 |
+
then returns dict with created links and resources
|
47 |
+
"""
|
48 |
+
return {'path/to/created/model': 'my/model.bin'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|