✅ [Pass] the test and rename the function of v2 arch
Browse files- tests/test_model/test_yolo.py +34 -25
- tests/test_utils/test_dataaugment.py +2 -1
- tests/test_utils/test_loss.py +27 -9
- yolo/model/README.md +0 -1
- yolo/tools/loss_functions.py +1 -1
- yolo/tools/solver.py +2 -2
tests/test_model/test_yolo.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import sys
|
2 |
from pathlib import Path
|
3 |
|
|
|
4 |
import torch
|
5 |
from hydra import compose, initialize
|
6 |
from omegaconf import OmegaConf
|
@@ -8,6 +9,7 @@ from omegaconf import OmegaConf
|
|
8 |
project_root = Path(__file__).resolve().parent.parent.parent
|
9 |
sys.path.append(str(project_root))
|
10 |
|
|
|
11 |
from yolo.model.yolo import YOLO, create_model
|
12 |
|
13 |
config_path = "../../yolo/config"
|
@@ -16,37 +18,44 @@ config_name = "config"
|
|
16 |
|
17 |
def test_build_model():
|
18 |
with initialize(config_path=config_path, version_base=None):
|
19 |
-
cfg = compose(config_name=config_name)
|
20 |
|
21 |
OmegaConf.set_struct(cfg.model, False)
|
22 |
cfg.weight = None
|
23 |
model = YOLO(cfg.model)
|
24 |
-
assert len(model.model) ==
|
25 |
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
30 |
cfg.weight = None
|
31 |
-
|
32 |
-
assert isinstance(model, YOLO)
|
33 |
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import sys
|
2 |
from pathlib import Path
|
3 |
|
4 |
+
import pytest
|
5 |
import torch
|
6 |
from hydra import compose, initialize
|
7 |
from omegaconf import OmegaConf
|
|
|
9 |
project_root = Path(__file__).resolve().parent.parent.parent
|
10 |
sys.path.append(str(project_root))
|
11 |
|
12 |
+
from yolo.config.config import Config
|
13 |
from yolo.model.yolo import YOLO, create_model
|
14 |
|
15 |
config_path = "../../yolo/config"
|
|
|
18 |
|
19 |
def test_build_model():
|
20 |
with initialize(config_path=config_path, version_base=None):
|
21 |
+
cfg: Config = compose(config_name=config_name)
|
22 |
|
23 |
OmegaConf.set_struct(cfg.model, False)
|
24 |
cfg.weight = None
|
25 |
model = YOLO(cfg.model)
|
26 |
+
assert len(model.model) == 39
|
27 |
|
28 |
|
29 |
+
@pytest.fixture
|
30 |
+
def cfg() -> Config:
|
31 |
+
with initialize(config_path="../../yolo/config", version_base=None):
|
32 |
+
cfg: Config = compose(config_name="config")
|
33 |
cfg.weight = None
|
34 |
+
return cfg
|
|
|
35 |
|
36 |
|
37 |
+
@pytest.fixture
|
38 |
+
def model(cfg: Config):
|
39 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
40 |
+
model = create_model(cfg.model)
|
41 |
+
return model.to(device)
|
42 |
+
|
43 |
+
|
44 |
+
def test_model_basic_status(model):
|
45 |
+
assert isinstance(model, YOLO)
|
46 |
+
assert len(model.model) == 39
|
47 |
+
|
48 |
+
|
49 |
+
def test_yolo_forward_output_shape(model):
|
50 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
51 |
+
# 2 - batch size, 3 - number of channels, 640x640 - image dimensions
|
52 |
+
dummy_input = torch.rand(2, 3, 640, 640, device=device)
|
53 |
+
|
54 |
+
# Forward pass through the model
|
55 |
+
output = model(dummy_input)
|
56 |
+
output_shape = [(cls.shape, anc.shape, box.shape) for cls, anc, box in output["Main"]]
|
57 |
+
assert output_shape == [
|
58 |
+
(torch.Size([2, 80, 80, 80]), torch.Size([2, 16, 4, 80, 80]), torch.Size([2, 4, 80, 80])),
|
59 |
+
(torch.Size([2, 80, 40, 40]), torch.Size([2, 16, 4, 40, 40]), torch.Size([2, 4, 40, 40])),
|
60 |
+
(torch.Size([2, 80, 20, 20]), torch.Size([2, 16, 4, 20, 20]), torch.Size([2, 4, 20, 20])),
|
61 |
+
]
|
tests/test_utils/test_dataaugment.py
CHANGED
@@ -43,8 +43,9 @@ def test_compose():
|
|
43 |
boxes = torch.tensor([[0, 0.2, 0.2, 0.8, 0.8]])
|
44 |
|
45 |
transformed_img, transformed_boxes = compose(img, boxes)
|
|
|
46 |
|
47 |
-
assert transformed_img ==
|
48 |
assert torch.equal(transformed_boxes, boxes), "Boxes should not be altered"
|
49 |
|
50 |
|
|
|
43 |
boxes = torch.tensor([[0, 0.2, 0.2, 0.8, 0.8]])
|
44 |
|
45 |
transformed_img, transformed_boxes = compose(img, boxes)
|
46 |
+
tensor_img = TF.pil_to_tensor(img).to(torch.float32) / 255
|
47 |
|
48 |
+
assert (transformed_img == tensor_img).all(), "Image should not be altered"
|
49 |
assert torch.equal(transformed_boxes, boxes), "Boxes should not be altered"
|
50 |
|
51 |
|
tests/test_utils/test_loss.py
CHANGED
@@ -8,32 +8,50 @@ from hydra import compose, initialize
|
|
8 |
project_root = Path(__file__).resolve().parent.parent.parent
|
9 |
sys.path.append(str(project_root))
|
10 |
|
11 |
-
from yolo.
|
|
|
|
|
|
|
12 |
|
13 |
|
14 |
@pytest.fixture
|
15 |
-
def cfg():
|
16 |
with initialize(config_path="../../yolo/config", version_base=None):
|
17 |
cfg = compose(config_name="config")
|
18 |
return cfg
|
19 |
|
20 |
|
21 |
@pytest.fixture
|
22 |
-
def
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
|
26 |
@pytest.fixture
|
27 |
def data():
|
28 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
29 |
targets = torch.zeros(1, 20, 5, device=device)
|
30 |
-
predicts = [torch.zeros(1,
|
31 |
return predicts, targets
|
32 |
|
33 |
|
34 |
def test_yolo_loss(loss_function, data):
|
35 |
predicts, targets = data
|
36 |
-
|
37 |
-
assert torch.isnan(
|
38 |
-
assert torch.isnan(
|
39 |
-
assert torch.
|
|
|
|
8 |
project_root = Path(__file__).resolve().parent.parent.parent
|
9 |
sys.path.append(str(project_root))
|
10 |
|
11 |
+
from yolo.config.config import Config
|
12 |
+
from yolo.model.yolo import create_model
|
13 |
+
from yolo.tools.loss_functions import DualLoss, create_loss_function
|
14 |
+
from yolo.utils.bounding_box_utils import Vec2Box
|
15 |
|
16 |
|
17 |
@pytest.fixture
|
18 |
+
def cfg() -> Config:
|
19 |
with initialize(config_path="../../yolo/config", version_base=None):
|
20 |
cfg = compose(config_name="config")
|
21 |
return cfg
|
22 |
|
23 |
|
24 |
@pytest.fixture
|
25 |
+
def model(cfg: Config):
|
26 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
27 |
+
model = create_model(cfg.model)
|
28 |
+
return model.to(device)
|
29 |
+
|
30 |
+
|
31 |
+
@pytest.fixture
|
32 |
+
def vec2box(cfg: Config, model):
|
33 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
34 |
+
print(device)
|
35 |
+
return Vec2Box(model, cfg.image_size, device)
|
36 |
+
|
37 |
+
|
38 |
+
@pytest.fixture
|
39 |
+
def loss_function(cfg, vec2box) -> DualLoss:
|
40 |
+
return create_loss_function(cfg, vec2box)
|
41 |
|
42 |
|
43 |
@pytest.fixture
|
44 |
def data():
|
45 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
46 |
targets = torch.zeros(1, 20, 5, device=device)
|
47 |
+
predicts = [torch.zeros(1, 8400, *cn, device=device) for cn in [(80,), (4, 16), (4,)]]
|
48 |
return predicts, targets
|
49 |
|
50 |
|
51 |
def test_yolo_loss(loss_function, data):
|
52 |
predicts, targets = data
|
53 |
+
loss, loss_dict = loss_function(predicts, predicts, targets)
|
54 |
+
assert torch.isnan(loss)
|
55 |
+
assert torch.isnan(loss_dict["BoxLoss"])
|
56 |
+
assert torch.isnan(loss_dict["DFLoss"])
|
57 |
+
assert torch.isinf(loss_dict["BCELoss"])
|
yolo/model/README.md
DELETED
@@ -1 +0,0 @@
|
|
1 |
-
main code
|
|
|
|
yolo/tools/loss_functions.py
CHANGED
@@ -132,7 +132,7 @@ class DualLoss:
|
|
132 |
return loss_sum, loss_dict
|
133 |
|
134 |
|
135 |
-
def
|
136 |
loss_function = DualLoss(cfg, vec2box)
|
137 |
logger.info("✅ Success load loss function")
|
138 |
return loss_function
|
|
|
132 |
return loss_sum, loss_dict
|
133 |
|
134 |
|
135 |
+
def create_loss_function(cfg: Config, vec2box) -> DualLoss:
|
136 |
loss_function = DualLoss(cfg, vec2box)
|
137 |
logger.info("✅ Success load loss function")
|
138 |
return loss_function
|
yolo/tools/solver.py
CHANGED
@@ -9,7 +9,7 @@ from yolo.config.config import Config, TrainConfig, ValidationConfig
|
|
9 |
from yolo.model.yolo import YOLO
|
10 |
from yolo.tools.data_loader import StreamDataLoader, create_dataloader
|
11 |
from yolo.tools.drawer import draw_bboxes
|
12 |
-
from yolo.tools.loss_functions import
|
13 |
from yolo.utils.bounding_box_utils import Vec2Box, bbox_nms, calculate_map
|
14 |
from yolo.utils.logging_utils import ProgressTracker
|
15 |
from yolo.utils.model_utils import (
|
@@ -27,7 +27,7 @@ class ModelTrainer:
|
|
27 |
self.device = device
|
28 |
self.optimizer = create_optimizer(model, train_cfg.optimizer)
|
29 |
self.scheduler = create_scheduler(self.optimizer, train_cfg.scheduler)
|
30 |
-
self.loss_fn =
|
31 |
self.progress = ProgressTracker(cfg.name, save_path, cfg.use_wandb)
|
32 |
self.num_epochs = cfg.task.epoch
|
33 |
|
|
|
9 |
from yolo.model.yolo import YOLO
|
10 |
from yolo.tools.data_loader import StreamDataLoader, create_dataloader
|
11 |
from yolo.tools.drawer import draw_bboxes
|
12 |
+
from yolo.tools.loss_functions import create_loss_function
|
13 |
from yolo.utils.bounding_box_utils import Vec2Box, bbox_nms, calculate_map
|
14 |
from yolo.utils.logging_utils import ProgressTracker
|
15 |
from yolo.utils.model_utils import (
|
|
|
27 |
self.device = device
|
28 |
self.optimizer = create_optimizer(model, train_cfg.optimizer)
|
29 |
self.scheduler = create_scheduler(self.optimizer, train_cfg.scheduler)
|
30 |
+
self.loss_fn = create_loss_function(cfg, vec2box)
|
31 |
self.progress = ProgressTracker(cfg.name, save_path, cfg.use_wandb)
|
32 |
self.num_epochs = cfg.task.epoch
|
33 |
|