henry000 commited on
Commit
d1477fc
·
1 Parent(s): 5044edc

✅ [Pass] the test and rename the function of v2 arch

Browse files
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) == 38
25
 
26
 
27
- def test_create_model():
28
- with initialize(config_path=config_path, version_base=None):
29
- cfg = compose(config_name=config_name)
 
30
  cfg.weight = None
31
- model = create_model(cfg)
32
- assert isinstance(model, YOLO)
33
 
34
 
35
- def test_yolo_forward_output_shape():
36
- with initialize(config_path=config_path, version_base=None):
37
- cfg = compose(config_name=config_name)
38
- model = create_model(cfg)
39
- # 2 - batch size, 3 - number of channels, 640x640 - image dimensions
40
- dummy_input = torch.rand(2, 3, 640, 640)
41
-
42
- # Forward pass through the model
43
- output = model(dummy_input)
44
- output_shape = [x.shape for x in output[-1]]
45
- assert output_shape == [
46
- torch.Size([2, 144, 80, 80]),
47
- torch.Size([2, 144, 40, 40]),
48
- torch.Size([2, 144, 20, 20]),
49
- torch.Size([2, 144, 80, 80]),
50
- torch.Size([2, 144, 40, 40]),
51
- torch.Size([2, 144, 20, 20]),
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 == img, "Image should not be altered"
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.tools.loss_functions import YOLOLoss
 
 
 
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 loss_function(cfg) -> YOLOLoss:
23
- return YOLOLoss(cfg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, 144, 80 // i, 80 // i, device=device) for i in [1, 2, 4]]
31
  return predicts, targets
32
 
33
 
34
  def test_yolo_loss(loss_function, data):
35
  predicts, targets = data
36
- loss_iou, loss_dfl, loss_cls = loss_function(predicts, targets)
37
- assert torch.isnan(loss_iou)
38
- assert torch.isnan(loss_dfl)
39
- assert torch.isinf(loss_cls)
 
 
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 get_loss_function(cfg: Config, vec2box) -> DualLoss:
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 get_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,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 = get_loss_function(cfg, vec2box)
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