Spaces:
Sleeping
Sleeping
Commit
·
307cad5
1
Parent(s):
858a727
Update model.py
Browse files
model.py
CHANGED
|
@@ -5,7 +5,7 @@ import datetime
|
|
| 5 |
import requests
|
| 6 |
|
| 7 |
from google.cloud import storage
|
| 8 |
-
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
| 9 |
from label_studio_ml.model import LabelStudioMLBase
|
| 10 |
from lxml import etree
|
| 11 |
from uuid import uuid4
|
|
@@ -43,7 +43,10 @@ class Model(LabelStudioMLBase):
|
|
| 43 |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = get_credentials()
|
| 44 |
image_processor = AutoImageProcessor.from_pretrained("diegokauer/conditional-detr-coe-int")
|
| 45 |
model = AutoModelForObjectDetection.from_pretrained("diegokauer/conditional-detr-coe-int")
|
|
|
|
|
|
|
| 46 |
id2label = model.config.id2label
|
|
|
|
| 47 |
|
| 48 |
def predict(self, tasks, **kwargs):
|
| 49 |
""" This is where inference happens: model returns
|
|
@@ -71,6 +74,26 @@ class Model(LabelStudioMLBase):
|
|
| 71 |
for score, label, box in zip(results['scores'], results['labels'], results['boxes']):
|
| 72 |
label_id = str(uuid4())
|
| 73 |
x, y, x2, y2 = tuple(box)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
result_list.append({
|
| 75 |
'id': label_id,
|
| 76 |
'original_width': original_width,
|
|
@@ -107,7 +130,7 @@ class Model(LabelStudioMLBase):
|
|
| 107 |
|
| 108 |
predictions.append({
|
| 109 |
'score': results['scores'].mean().item(), # prediction overall score, visible in the data manager columns
|
| 110 |
-
'model_version': '
|
| 111 |
'result': result_list
|
| 112 |
})
|
| 113 |
print(predictions)
|
|
|
|
| 5 |
import requests
|
| 6 |
|
| 7 |
from google.cloud import storage
|
| 8 |
+
from transformers import AutoImageProcessor, AutoModelForObjectDetection, ViTImageProcessor, Swinv2ForImageClassification
|
| 9 |
from label_studio_ml.model import LabelStudioMLBase
|
| 10 |
from lxml import etree
|
| 11 |
from uuid import uuid4
|
|
|
|
| 43 |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = get_credentials()
|
| 44 |
image_processor = AutoImageProcessor.from_pretrained("diegokauer/conditional-detr-coe-int")
|
| 45 |
model = AutoModelForObjectDetection.from_pretrained("diegokauer/conditional-detr-coe-int")
|
| 46 |
+
seg_image_processor = ViTImageProcessor.from_pretrained("diegokauer/int-pet-classifier")
|
| 47 |
+
seg_model = SwinForImageClassification.from_pretrained("diegokauer/int-pet-classifier")
|
| 48 |
id2label = model.config.id2label
|
| 49 |
+
seg_id2label = seg_model.config.id2label
|
| 50 |
|
| 51 |
def predict(self, tasks, **kwargs):
|
| 52 |
""" This is where inference happens: model returns
|
|
|
|
| 74 |
for score, label, box in zip(results['scores'], results['labels'], results['boxes']):
|
| 75 |
label_id = str(uuid4())
|
| 76 |
x, y, x2, y2 = tuple(box)
|
| 77 |
+
|
| 78 |
+
if self.id2label[label.item()] == 'Propuesta':
|
| 79 |
+
pred_label_id = str(uuid4())
|
| 80 |
+
image = image.crop((x, y, x2, y2))
|
| 81 |
+
input = self.seg_image_processor(images=image, return_tensors="pt")
|
| 82 |
+
logits = self.seg_model(**inputs).logits
|
| 83 |
+
logits = torch.exp(logits)
|
| 84 |
+
preds = logits > 0.5
|
| 85 |
+
preds = [self.seg_id2label[i] for i, pred in enumerate(preds)]
|
| 86 |
+
preds = ["No Reportado"] if "No Reportado" in preds else preds
|
| 87 |
+
result_list.append({
|
| 88 |
+
"value": {
|
| 89 |
+
"choices": preds
|
| 90 |
+
},
|
| 91 |
+
"id": pred_label_id,
|
| 92 |
+
"from_name": "propuesta",
|
| 93 |
+
"to_name": "image",
|
| 94 |
+
"type": "choices"
|
| 95 |
+
})
|
| 96 |
+
|
| 97 |
result_list.append({
|
| 98 |
'id': label_id,
|
| 99 |
'original_width': original_width,
|
|
|
|
| 130 |
|
| 131 |
predictions.append({
|
| 132 |
'score': results['scores'].mean().item(), # prediction overall score, visible in the data manager columns
|
| 133 |
+
'model_version': 'cdetr_v2', # all predictions will be differentiated by model version
|
| 134 |
'result': result_list
|
| 135 |
})
|
| 136 |
print(predictions)
|