Spaces:
Sleeping
Sleeping
Commit ·
c0d1e9d
1
Parent(s): 131b946
Update model.py
Browse files
model.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 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):
|
|
@@ -26,23 +29,46 @@ class Model(LabelStudioMLBase):
|
|
| 26 |
"""
|
| 27 |
predictions = []
|
| 28 |
for task in tasks:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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'}
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
| 6 |
from label_studio_ml.model import LabelStudioMLBase
|
| 7 |
from lxml import etree
|
| 8 |
+
from uuid import uuid4
|
| 9 |
+
from PIL import Image
|
| 10 |
|
| 11 |
|
| 12 |
class Model(LabelStudioMLBase):
|
|
|
|
| 29 |
"""
|
| 30 |
predictions = []
|
| 31 |
for task in tasks:
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
image_path = task["data"]["image"]
|
| 35 |
+
image = Image(image_path)
|
| 36 |
+
original_width, original_height = image.size
|
| 37 |
+
with torch.no_grad():
|
| 38 |
+
|
| 39 |
+
inputs = image_processor(images=image, return_tensors="pt")
|
| 40 |
+
outputs = model(**inputs)
|
| 41 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 42 |
+
results = image_processor.post_process_object_detection(outputs, threshold=0.5, target_sizes=target_sizes)[0]
|
| 43 |
+
|
| 44 |
+
result_list = []
|
| 45 |
+
for score, label, box in zip(results['scores'], results['labels'], scores['boxes']):
|
| 46 |
+
label_id = str(uuid4())[:4]
|
| 47 |
+
x, y, x2, y2 = tuple(box)
|
| 48 |
+
result_list.append(
|
| 49 |
+
{
|
| 50 |
+
'id': id
|
| 51 |
+
'original_width': original_width,
|
| 52 |
+
'original_height': original_height,
|
| 53 |
+
'from_name': "label",
|
| 54 |
+
'to_name': "image",
|
| 55 |
+
'type': 'labels',
|
| 56 |
+
'score': score, # per-region score, visible in the editor
|
| 57 |
+
'value': {
|
| 58 |
+
'x': x,
|
| 59 |
+
'y': y,
|
| 60 |
+
'width': x2-x,
|
| 61 |
+
'height': y2-y,
|
| 62 |
+
'rotation': 0
|
| 63 |
+
'labels': [self.id2label[label]]
|
| 64 |
+
}
|
| 65 |
}
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
predictions.append({
|
| 70 |
+
'score': results['scores'].mean(), # prediction overall score, visible in the data manager columns
|
| 71 |
+
'model_version': 'diegokauer/conditional-detr-coe-int', # all predictions will be differentiated by model version
|
| 72 |
+
'result': result_list
|
| 73 |
})
|
| 74 |
+
return predictions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|