-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_yolo.py
More file actions
139 lines (117 loc) · 5.12 KB
/
test_yolo.py
File metadata and controls
139 lines (117 loc) · 5.12 KB
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
import unittest
from unittest.mock import MagicMock, patch
from collections import OrderedDict
import numpy as np
import torch
from ultralytics import YOLO
from kabr_tools.utils.yolo import YOLOv8
# from yolov8x.pt
LABELS = {"zebra": 22, "horse": 17, "giraffe": 23, "bear": 21}
def rescale(box, width, height):
return [box[0] * width, box[1] * height, box[2] * width, box[3] * height]
class MockBox:
def __init__(self, box=[[0, 0, 0, 0]], cls=["zebra"], conf=[0.95]):
self.xyxyn = None
self.cls = None
self.conf = None
def mock(self, boxes, classes, confs):
self.xyxyn = torch.Tensor(boxes)
self.cls = torch.Tensor([LABELS[cls] for cls in classes])
self.conf = torch.Tensor(confs)
return self
class TestYolo(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.im = np.zeros((100, 101, 3), dtype=np.uint8)
cls.box = OrderedDict([("x1", 10), ("y1", 20), ("x2", 30), ("y2", 40)])
cls.box_values = list(cls.box.values())
@patch("kabr_tools.utils.yolo.YOLO")
def test_forward(self, yolo_mock):
im = TestYolo.im
yolo_model = MagicMock()
yolo_model.predict.return_value.__getitem__ = lambda x, _: x
yolo_model.names = YOLO("yolov8x.pt").names
yolo_mock.return_value = yolo_model
# horse -> zebra
points = [[0] * 4] * 3
labels = ["zebra", "horse", "giraffe"]
expect_labels = ["Zebra", "Zebra", "Giraffe"]
probs = [0.7, 0.8, 0.9]
yolo_boxes = MockBox().mock(points, labels, probs)
yolo_model.predict.return_value.boxes.cpu.return_value = yolo_boxes
yolo = YOLOv8()
preds = yolo.forward(im)
self.assertEqual(len(preds), 3)
for i, pred in enumerate(preds):
self.assertEqual(preds[i][0], points[i])
self.assertEqual(preds[i][1], probs[i])
self.assertEqual(preds[i][2], expect_labels[i])
# bear -> filtered
points = [[0] * 4] * 3
labels = ["bear", "horse", "giraffe"]
expect_labels = [None, "Zebra", "Giraffe"]
probs = [0.9, 0.8, 0.9]
yolo_boxes = MockBox().mock(points, labels, probs)
yolo_model.predict.return_value.boxes.cpu.return_value = yolo_boxes
yolo = YOLOv8()
preds = yolo.forward(im)
self.assertEqual(len(preds), 2)
index = 0
for pred in preds:
while expect_labels[index] is None:
index += 1
self.assertEqual(pred[0], rescale(points[index], im.shape[1], im.shape[0]))
self.assertEqual(pred[1], probs[index])
self.assertEqual(pred[2], expect_labels[index])
index += 1
# low prob -> filtered
points = [[i] * 4 for i in range(8)]
labels = ["bear", "horse", "zebra", "giraffe", "bear", "horse", "zebra", "giraffe"]
expect_labels = [None, "Zebra", None, "Giraffe", None, "Zebra", None, None]
probs = [0.5, 0.9, 0.4, 0.8, 0.7, 0.6, 0.3, 0.5]
yolo_boxes = MockBox().mock(points, labels, probs)
yolo_model.predict.return_value.boxes.cpu.return_value = yolo_boxes
yolo = YOLOv8()
preds = yolo.forward(im)
self.assertEqual(len(preds), 3)
index = 0
for pred in preds:
while expect_labels[index] is None:
index += 1
self.assertEqual(pred[0], rescale(points[index], im.shape[1], im.shape[0]))
self.assertEqual(pred[1], probs[index])
self.assertEqual(pred[2], expect_labels[index])
index += 1
@patch("kabr_tools.utils.yolo.YOLO")
def test_yolo_with_params(self, yolo_mock):
im = TestYolo.im
yolo_model = MagicMock()
yolo_model.predict.return_value.__getitem__ = lambda x, _: x
yolo_model.names = YOLO("yolov8x.pt").names
yolo_mock.return_value = yolo_model
points = [[i] * 4 for i in range(8)]
labels = ["bear", "horse", "zebra", "giraffe", "bear", "horse", "zebra", "giraffe"]
expect_labels = ["Panda", "Fish", None, None, None, None, None, "Giraffe"]
probs = [0.91, 0.99, 0.92, 0.55, 0.9, 0.89, 0.85, 0.93]
yolo_boxes = MockBox().mock(points, labels, probs)
yolo_model.predict.return_value.boxes.cpu.return_value = yolo_boxes
yolo = YOLOv8(weights="yolov8x.pt",
imgsz=640, conf=0.9,
target_labels=["bear", "horse", "giraffe"],
label_map={"bear": "panda", "horse": "fish"})
preds = yolo.forward(im)
self.assertEqual(len(preds), 3)
index = 0
for pred in preds:
while expect_labels[index] is None:
index += 1
self.assertEqual(pred[0], rescale(points[index], im.shape[1], im.shape[0]))
self.assertEqual(pred[1], probs[index])
self.assertEqual(pred[2], expect_labels[index])
index += 1
def test_get_centroid(self):
box = TestYolo.box
box_values = TestYolo.box_values
x, y = YOLOv8.get_centroid(box_values)
self.assertEqual(x, (box["x1"] + box["x2"]) // 2)
self.assertEqual(y, (box["y1"] + box["y2"]) // 2)