Update handler.py
Browse files- handler.py +16 -6
handler.py
CHANGED
@@ -21,8 +21,8 @@ class EndpointHandler():
|
|
21 |
def __init__(self, path=""):
|
22 |
# Preload all the elements you are going to need at inference.
|
23 |
self.dinov2_vits14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vits14')
|
24 |
-
device = torch.device('cuda' if torch.cuda.is_available() else "cpu")
|
25 |
-
self.dinov2_vits14.to(device)
|
26 |
print('Successfully load dinov2_vits14 model')
|
27 |
|
28 |
self.yolov8_model = YOLO(os.path.join(path, 'yolov8_2023-07-19_yolov8m.pt'))
|
@@ -40,6 +40,16 @@ class EndpointHandler():
|
|
40 |
|
41 |
with open(os.path.join(path, 'labels.txt'), 'r') as f:
|
42 |
self.labels = f.read().split(',') # loggerhead,green,leatherback...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
45 |
"""
|
@@ -73,16 +83,16 @@ class EndpointHandler():
|
|
73 |
else:
|
74 |
y1 = max(y1 - offset, 0)
|
75 |
y2 = min(y2 + offset, H)
|
76 |
-
|
77 |
|
78 |
new_image = self.transform_image(Image.fromarray(cropped))[:3].unsqueeze(0)
|
79 |
-
embedding = self.dinov2_vits14(new_image.to(device))
|
80 |
prediction = self.linear_model(embedding)
|
81 |
percentage = nn.Softmax(dim=1)(prediction).detach().numpy().round(2)[0].tolist()
|
82 |
result = {}
|
83 |
|
84 |
for i in range(len(self.labels)):
|
85 |
-
result[name_en2vi[self.labels[i]]] = percentage[i]
|
86 |
|
87 |
# Return the annotated original image with the square cropped and result dict
|
88 |
-
return annotated.tolist(), result
|
|
|
21 |
def __init__(self, path=""):
|
22 |
# Preload all the elements you are going to need at inference.
|
23 |
self.dinov2_vits14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vits14')
|
24 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else "cpu")
|
25 |
+
self.dinov2_vits14.to(self.device)
|
26 |
print('Successfully load dinov2_vits14 model')
|
27 |
|
28 |
self.yolov8_model = YOLO(os.path.join(path, 'yolov8_2023-07-19_yolov8m.pt'))
|
|
|
40 |
|
41 |
with open(os.path.join(path, 'labels.txt'), 'r') as f:
|
42 |
self.labels = f.read().split(',') # loggerhead,green,leatherback...
|
43 |
+
|
44 |
+
self.name_en2vi = {
|
45 |
+
"loggerhead": "Quản đồng",
|
46 |
+
"green": "Vích",
|
47 |
+
"leatherback": "Rùa da",
|
48 |
+
"hawksbill": "Đồi mồi",
|
49 |
+
"kemp_ridley": "Vích Kemp",
|
50 |
+
"olive_ridley": "Đồi mồi dứa",
|
51 |
+
"flatback": "Rùa lưng phẳng"
|
52 |
+
}
|
53 |
|
54 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
55 |
"""
|
|
|
83 |
else:
|
84 |
y1 = max(y1 - offset, 0)
|
85 |
y2 = min(y2 + offset, H)
|
86 |
+
cropped = img[y1:y2, x1:x2]
|
87 |
|
88 |
new_image = self.transform_image(Image.fromarray(cropped))[:3].unsqueeze(0)
|
89 |
+
embedding = self.dinov2_vits14(new_image.to(self.device))
|
90 |
prediction = self.linear_model(embedding)
|
91 |
percentage = nn.Softmax(dim=1)(prediction).detach().numpy().round(2)[0].tolist()
|
92 |
result = {}
|
93 |
|
94 |
for i in range(len(self.labels)):
|
95 |
+
result[self.name_en2vi[self.labels[i]]] = percentage[i]
|
96 |
|
97 |
# Return the annotated original image with the square cropped and result dict
|
98 |
+
return annotated.tolist(), result
|