Skip to content

Commit ffa93bf

Browse files
committed
Updating yolo loss wrapper test to align with output of _translate_labels for yolo.
Signed-off-by: Kieran Fraser <[email protected]>
1 parent 82cbe6f commit ffa93bf

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

tests/estimators/object_detection/test_pytorch_yolo_loss_wrapper.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,20 @@ def test_yolov8_loss_wrapper():
4141
x = torch.randn((batch_size, 3, 640, 640)) # YOLOv8 expects (B, 3, 640, 640)
4242

4343
# Create targets
44-
targets = []
44+
"""targets = []
4545
for _ in range(batch_size):
4646
boxes = torch.tensor([[0.1, 0.1, 0.3, 0.3], [0.5, 0.5, 0.8, 0.8]]) # [x1, y1, x2, y2]
4747
labels = torch.zeros(2, dtype=torch.long) # Use class 0 for testing
48-
targets.append({"boxes": boxes, "labels": labels})
48+
targets.append({"boxes": boxes, "labels": labels})"""
49+
targets = torch.tensor([[ 0.0000, 20.0000, 0.7738, 0.3919, 0.4525, 0.7582],
50+
[ 0.0000, 20.0000, 0.2487, 0.4062, 0.4966, 0.5787],
51+
[ 0.0000, 20.0000, 0.5667, 0.2772, 0.0791, 0.2313],
52+
[ 0.0000, 20.0000, 0.1009, 0.1955, 0.2002, 0.0835],
53+
[ 1.0000, 20.0000, 0.7738, 0.3919, 0.4525, 0.7582],
54+
[ 1.0000, 20.0000, 0.2487, 0.4062, 0.4966, 0.5787],
55+
[ 1.0000, 20.0000, 0.5667, 0.2772, 0.0791, 0.2313],
56+
[ 1.0000, 20.0000, 0.1009, 0.1955, 0.2002, 0.0835]])
57+
4958

5059
# Test training mode
5160
losses = wrapper(x, targets)
@@ -94,11 +103,19 @@ def test_yolov10_loss_wrapper():
94103
x = torch.randn((batch_size, 3, 640, 640)) # Standard YOLO input size
95104

96105
# Create targets
97-
targets = []
106+
"""targets = []
98107
for _ in range(batch_size):
99108
boxes = torch.tensor([[0.1, 0.1, 0.3, 0.3], [0.5, 0.5, 0.8, 0.8]]) # [x1, y1, x2, y2]
100109
labels = torch.zeros(2, dtype=torch.long) # Use class 0 for testing
101-
targets.append({"boxes": boxes, "labels": labels})
110+
targets.append({"boxes": boxes, "labels": labels})"""
111+
targets = torch.tensor([[ 0.0000, 20.0000, 0.7738, 0.3919, 0.4525, 0.7582],
112+
[ 0.0000, 20.0000, 0.2487, 0.4062, 0.4966, 0.5787],
113+
[ 0.0000, 20.0000, 0.5667, 0.2772, 0.0791, 0.2313],
114+
[ 0.0000, 20.0000, 0.1009, 0.1955, 0.2002, 0.0835],
115+
[ 1.0000, 20.0000, 0.7738, 0.3919, 0.4525, 0.7582],
116+
[ 1.0000, 20.0000, 0.2487, 0.4062, 0.4966, 0.5787],
117+
[ 1.0000, 20.0000, 0.5667, 0.2772, 0.0791, 0.2313],
118+
[ 1.0000, 20.0000, 0.1009, 0.1955, 0.2002, 0.0835]])
102119

103120
# Test training mode
104121
losses = wrapper(x, targets)
@@ -219,7 +236,7 @@ def loss(self, items):
219236
wrapper.train()
220237
# Dummy input and targets
221238
x = torch.zeros((1, 3, 416, 416))
222-
targets = [{"boxes": torch.zeros((1, 4)), "labels": torch.zeros((1,))}]
239+
targets = torch.tensor([[ 0.0000, 20.0000, 0.7738, 0.3919, 0.4525, 0.7582]])
223240
losses = wrapper(x, targets)
224241
assert set(losses.keys()) == {"loss_total", "loss_box", "loss_cls", "loss_dfl"}
225242
assert losses["loss_total"].item() == 6.0 # sum([1.0, 2.0, 3.0])
@@ -264,7 +281,7 @@ def loss(self, items):
264281
wrapper = PyTorchYoloLossWrapper(test_model, name="yolov8n")
265282
wrapper.train()
266283
x = torch.zeros((1, 3, 416, 416))
267-
targets = [{"boxes": torch.zeros((1, 4)), "labels": torch.zeros((1,))}]
284+
targets = torch.tensor([[ 0.0000, 20.0000, 0.7738, 0.3919, 0.4525, 0.7582]])
268285
losses = wrapper(x, targets)
269286
assert set(losses.keys()) == {"loss_total", "loss_box", "loss_cls", "loss_dfl"}
270287
assert losses["loss_total"].item() == 6.0
@@ -439,9 +456,7 @@ def loss(self, items):
439456
for batch_size in batch_sizes:
440457
for box_count in box_counts:
441458
x = torch.zeros((batch_size, 3, 416, 416))
442-
targets = [
443-
{"boxes": torch.zeros((box_count, 4)), "labels": torch.zeros(box_count)} for _ in range(batch_size)
444-
]
459+
targets = torch.tensor([[ 0.0000, 20.0000, 0.7738, 0.3919, 0.4525, 0.7582]]*batch_size)
445460
losses = wrapper(x, targets)
446461

447462
# Verify loss structure

0 commit comments

Comments
 (0)