✅ [Pass] test for v7 structure!
Browse files- tests/conftest.py +26 -2
- tests/test_model/test_yolo.py +10 -0
- tests/test_tools/test_solver.py +11 -1
- tests/test_utils/test_bounding_box_utils.py +20 -0
tests/conftest.py
CHANGED
|
@@ -8,7 +8,7 @@ from hydra import compose, initialize
|
|
| 8 |
project_root = Path(__file__).resolve().parent.parent
|
| 9 |
sys.path.append(str(project_root))
|
| 10 |
|
| 11 |
-
from yolo import Config, Vec2Box, create_model
|
| 12 |
from yolo.model.yolo import YOLO
|
| 13 |
from yolo.tools.data_loader import StreamDataLoader, YoloDataLoader
|
| 14 |
from yolo.tools.dataset_preparation import prepare_dataset
|
|
@@ -42,6 +42,11 @@ def inference_cfg():
|
|
| 42 |
return get_cfg(overrides=["task=inference"])
|
| 43 |
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
@pytest.fixture(scope="session")
|
| 46 |
def device():
|
| 47 |
return torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
@@ -65,12 +70,26 @@ def model(train_cfg: Config, device) -> YOLO:
|
|
| 65 |
return model.to(device)
|
| 66 |
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
@pytest.fixture(scope="session")
|
| 69 |
def vec2box(train_cfg: Config, model: YOLO, device) -> Vec2Box:
|
| 70 |
-
vec2box =
|
| 71 |
return vec2box
|
| 72 |
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
@pytest.fixture(scope="session")
|
| 75 |
def train_dataloader(train_cfg: Config):
|
| 76 |
prepare_dataset(train_cfg.dataset, task="train")
|
|
@@ -88,6 +107,11 @@ def file_stream_data_loader(inference_cfg: Config):
|
|
| 88 |
return StreamDataLoader(inference_cfg.task.data)
|
| 89 |
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
@pytest.fixture(scope="session")
|
| 92 |
def directory_stream_data_loader(inference_cfg: Config):
|
| 93 |
inference_cfg.task.data.source = "tests/data/images/train"
|
|
|
|
| 8 |
project_root = Path(__file__).resolve().parent.parent
|
| 9 |
sys.path.append(str(project_root))
|
| 10 |
|
| 11 |
+
from yolo import Anc2Box, Config, Vec2Box, create_converter, create_model
|
| 12 |
from yolo.model.yolo import YOLO
|
| 13 |
from yolo.tools.data_loader import StreamDataLoader, YoloDataLoader
|
| 14 |
from yolo.tools.dataset_preparation import prepare_dataset
|
|
|
|
| 42 |
return get_cfg(overrides=["task=inference"])
|
| 43 |
|
| 44 |
|
| 45 |
+
@pytest.fixture(scope="session")
|
| 46 |
+
def inference_v7_cfg():
|
| 47 |
+
return get_cfg(overrides=["task=inference", "model=v7"])
|
| 48 |
+
|
| 49 |
+
|
| 50 |
@pytest.fixture(scope="session")
|
| 51 |
def device():
|
| 52 |
return torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 70 |
return model.to(device)
|
| 71 |
|
| 72 |
|
| 73 |
+
@pytest.fixture(scope="session")
|
| 74 |
+
def model_v7(inference_v7_cfg: Config, device) -> YOLO:
|
| 75 |
+
model = create_model(inference_v7_cfg.model)
|
| 76 |
+
return model.to(device)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
@pytest.fixture(scope="session")
|
| 80 |
def vec2box(train_cfg: Config, model: YOLO, device) -> Vec2Box:
|
| 81 |
+
vec2box = create_converter(train_cfg.model.name, model, train_cfg.model.anchor, train_cfg.image_size, device)
|
| 82 |
return vec2box
|
| 83 |
|
| 84 |
|
| 85 |
+
@pytest.fixture(scope="session")
|
| 86 |
+
def anc2box(inference_v7_cfg: Config, model: YOLO, device) -> Anc2Box:
|
| 87 |
+
anc2box = create_converter(
|
| 88 |
+
inference_v7_cfg.model.name, model, inference_v7_cfg.model.anchor, inference_v7_cfg.image_size, device
|
| 89 |
+
)
|
| 90 |
+
return anc2box
|
| 91 |
+
|
| 92 |
+
|
| 93 |
@pytest.fixture(scope="session")
|
| 94 |
def train_dataloader(train_cfg: Config):
|
| 95 |
prepare_dataset(train_cfg.dataset, task="train")
|
|
|
|
| 107 |
return StreamDataLoader(inference_cfg.task.data)
|
| 108 |
|
| 109 |
|
| 110 |
+
@pytest.fixture(scope="session")
|
| 111 |
+
def file_stream_data_loader_v7(inference_v7_cfg: Config):
|
| 112 |
+
return StreamDataLoader(inference_v7_cfg.task.data)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
@pytest.fixture(scope="session")
|
| 116 |
def directory_stream_data_loader(inference_cfg: Config):
|
| 117 |
inference_cfg.task.data.source = "tests/data/images/train"
|
tests/test_model/test_yolo.py
CHANGED
|
@@ -36,6 +36,16 @@ def test_build_model_v9m():
|
|
| 36 |
assert len(model.model) == 39
|
| 37 |
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
@pytest.fixture
|
| 40 |
def cfg() -> Config:
|
| 41 |
with initialize(config_path="../../yolo/config", version_base=None):
|
|
|
|
| 36 |
assert len(model.model) == 39
|
| 37 |
|
| 38 |
|
| 39 |
+
def test_build_model_v7():
|
| 40 |
+
with initialize(config_path=config_path, version_base=None):
|
| 41 |
+
cfg: Config = compose(config_name=config_name, overrides=[f"model=v7"])
|
| 42 |
+
|
| 43 |
+
OmegaConf.set_struct(cfg.model, False)
|
| 44 |
+
cfg.weight = None
|
| 45 |
+
model = YOLO(cfg.model)
|
| 46 |
+
assert len(model.model) == 106
|
| 47 |
+
|
| 48 |
+
|
| 49 |
@pytest.fixture
|
| 50 |
def cfg() -> Config:
|
| 51 |
with initialize(config_path="../../yolo/config", version_base=None):
|
tests/test_tools/test_solver.py
CHANGED
|
@@ -11,7 +11,7 @@ from yolo.config.config import Config
|
|
| 11 |
from yolo.model.yolo import YOLO
|
| 12 |
from yolo.tools.data_loader import StreamDataLoader, YoloDataLoader
|
| 13 |
from yolo.tools.solver import ModelTester, ModelTrainer, ModelValidator
|
| 14 |
-
from yolo.utils.bounding_box_utils import Vec2Box
|
| 15 |
|
| 16 |
|
| 17 |
@pytest.fixture
|
|
@@ -41,6 +41,12 @@ def model_tester(inference_cfg: Config, model: YOLO, vec2box: Vec2Box, validatio
|
|
| 41 |
return tester
|
| 42 |
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def test_model_tester_initialization(model_tester: ModelTester):
|
| 45 |
assert isinstance(model_tester.model, YOLO)
|
| 46 |
assert hasattr(model_tester, "solve")
|
|
@@ -50,6 +56,10 @@ def test_model_tester_solve_single_image(model_tester: ModelTester, file_stream_
|
|
| 50 |
model_tester.solve(file_stream_data_loader)
|
| 51 |
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
@pytest.fixture
|
| 54 |
def model_trainer(train_cfg: Config, model: YOLO, vec2box: Vec2Box, train_progress_logger, device):
|
| 55 |
train_cfg.task.epoch = 2
|
|
|
|
| 11 |
from yolo.model.yolo import YOLO
|
| 12 |
from yolo.tools.data_loader import StreamDataLoader, YoloDataLoader
|
| 13 |
from yolo.tools.solver import ModelTester, ModelTrainer, ModelValidator
|
| 14 |
+
from yolo.utils.bounding_box_utils import Anc2Box, Vec2Box
|
| 15 |
|
| 16 |
|
| 17 |
@pytest.fixture
|
|
|
|
| 41 |
return tester
|
| 42 |
|
| 43 |
|
| 44 |
+
@pytest.fixture
|
| 45 |
+
def modelv7_tester(inference_v7_cfg: Config, model_v7: YOLO, anc2box: Anc2Box, validation_progress_logger, device):
|
| 46 |
+
tester = ModelTester(inference_v7_cfg, model_v7, anc2box, validation_progress_logger, device)
|
| 47 |
+
return tester
|
| 48 |
+
|
| 49 |
+
|
| 50 |
def test_model_tester_initialization(model_tester: ModelTester):
|
| 51 |
assert isinstance(model_tester.model, YOLO)
|
| 52 |
assert hasattr(model_tester, "solve")
|
|
|
|
| 56 |
model_tester.solve(file_stream_data_loader)
|
| 57 |
|
| 58 |
|
| 59 |
+
def test_modelv7_tester_solve_single_image(modelv7_tester: ModelTester, file_stream_data_loader_v7: StreamDataLoader):
|
| 60 |
+
modelv7_tester.solve(file_stream_data_loader_v7)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
@pytest.fixture
|
| 64 |
def model_trainer(train_cfg: Config, model: YOLO, vec2box: Vec2Box, train_progress_logger, device):
|
| 65 |
train_cfg.task.epoch = 2
|
tests/test_utils/test_bounding_box_utils.py
CHANGED
|
@@ -9,7 +9,9 @@ from torch import allclose, float32, isclose, tensor
|
|
| 9 |
project_root = Path(__file__).resolve().parent.parent.parent
|
| 10 |
sys.path.append(str(project_root))
|
| 11 |
from yolo import Config, NMSConfig, create_model
|
|
|
|
| 12 |
from yolo.utils.bounding_box_utils import (
|
|
|
|
| 13 |
Vec2Box,
|
| 14 |
bbox_nms,
|
| 15 |
calculate_iou,
|
|
@@ -125,6 +127,24 @@ def test_vec2box_autoanchor():
|
|
| 125 |
assert vec2box.scaler.shape == tuple([4200])
|
| 126 |
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
def test_bbox_nms():
|
| 129 |
cls_dist = tensor(
|
| 130 |
[[[0.1, 0.7, 0.2], [0.6, 0.3, 0.1]], [[0.4, 0.4, 0.2], [0.5, 0.4, 0.1]]] # Example class distribution
|
|
|
|
| 9 |
project_root = Path(__file__).resolve().parent.parent.parent
|
| 10 |
sys.path.append(str(project_root))
|
| 11 |
from yolo import Config, NMSConfig, create_model
|
| 12 |
+
from yolo.config.config import AnchorConfig
|
| 13 |
from yolo.utils.bounding_box_utils import (
|
| 14 |
+
Anc2Box,
|
| 15 |
Vec2Box,
|
| 16 |
bbox_nms,
|
| 17 |
calculate_iou,
|
|
|
|
| 127 |
assert vec2box.scaler.shape == tuple([4200])
|
| 128 |
|
| 129 |
|
| 130 |
+
def test_anc2box_autoanchor(inference_v7_cfg: Config):
|
| 131 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 132 |
+
model = create_model(inference_v7_cfg.model, weight_path=None).to(device)
|
| 133 |
+
anchor_cfg: AnchorConfig = inference_v7_cfg.model.anchor.copy()
|
| 134 |
+
del anchor_cfg.strides
|
| 135 |
+
anc2box = Anc2Box(model, anchor_cfg, inference_v7_cfg.image_size, device)
|
| 136 |
+
assert anc2box.strides == [8, 16, 32]
|
| 137 |
+
|
| 138 |
+
anc2box.update((320, 640))
|
| 139 |
+
anchor_grids_shape = [anchor_grid.shape for anchor_grid in anc2box.anchor_grids]
|
| 140 |
+
assert anchor_grids_shape == [
|
| 141 |
+
torch.Size([1, 1, 80, 80, 2]),
|
| 142 |
+
torch.Size([1, 1, 40, 40, 2]),
|
| 143 |
+
torch.Size([1, 1, 20, 20, 2]),
|
| 144 |
+
]
|
| 145 |
+
assert anc2box.anchor_scale.shape == torch.Size([3, 1, 3, 1, 1, 2])
|
| 146 |
+
|
| 147 |
+
|
| 148 |
def test_bbox_nms():
|
| 149 |
cls_dist = tensor(
|
| 150 |
[[[0.1, 0.7, 0.2], [0.6, 0.3, 0.1]], [[0.4, 0.4, 0.2], [0.5, 0.4, 0.1]]] # Example class distribution
|