File size: 1,006 Bytes
80efe00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
710e371
 
 
80efe00
 
 
 
 
 
 
 
 
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
import sys
from pathlib import Path

import pytest
import torch
from hydra import compose, initialize

project_root = Path(__file__).resolve().parent.parent.parent
sys.path.append(str(project_root))

from yolo.utils.loss import YOLOLoss


@pytest.fixture
def cfg():
    with initialize(config_path="../../yolo/config", version_base=None):
        cfg = compose(config_name="config")
    return cfg


@pytest.fixture
def loss_function(cfg) -> YOLOLoss:
    return YOLOLoss(cfg)


@pytest.fixture
def data():
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    targets = torch.zeros(20, 6, device=device)
    predicts = [[torch.zeros(1, 144, 80 // i, 80 // i, device=device) for i in [1, 2, 4]] for _ in range(2)]
    return predicts, targets


def test_yolo_loss(loss_function, data):
    predicts, targets = data
    loss_iou, loss_dfl, loss_cls = loss_function(predicts, targets)
    assert torch.isnan(loss_iou)
    assert torch.isnan(loss_dfl)
    assert torch.isinf(loss_cls)