henry000 commited on
Commit
58bac2b
Β·
2 Parent(s): d3c8b75 7c6ce21

πŸ”€ [Merge] branch 'SETUP' into DATASET

Browse files
Files changed (5) hide show
  1. config/config.py +13 -0
  2. config/config.yaml +8 -0
  3. model/module.py +1 -1
  4. model/yolo.py +32 -21
  5. train.py +9 -16
config/config.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dataclasses import dataclass
2
+ from typing import List, Dict, Union
3
+
4
+
5
+ @dataclass
6
+ class Model:
7
+ anchor: List[List[int]]
8
+ model: Dict[str, List[Dict[str, Union[Dict, List, int]]]]
9
+
10
+
11
+ @dataclass
12
+ class Config:
13
+ model: Model
config/config.yaml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ hydra:
2
+ run:
3
+ dir: ./runs
4
+
5
+ defaults:
6
+ - data_config: coco.yaml
7
+ - model: v7-base.yaml
8
+ - _self_
model/module.py CHANGED
@@ -297,7 +297,7 @@ class CSPELAN(nn.Module):
297
 
298
 
299
  class Concat(nn.Module):
300
- def __init__(self, dim=-1):
301
  super(Concat, self).__init__()
302
  self.dim = dim
303
 
 
297
 
298
 
299
  class Concat(nn.Module):
300
+ def __init__(self, dim=1):
301
  super(Concat, self).__init__()
302
  self.dim = dim
303
 
model/yolo.py CHANGED
@@ -1,10 +1,13 @@
 
 
 
 
1
  import torch.nn as nn
2
  from loguru import logger
3
- from typing import Dict, Any, List
4
- import inspect
5
- from utils.tools import load_model_cfg
6
 
7
  from model import module
 
8
 
9
 
10
  def get_layer_map():
@@ -23,7 +26,6 @@ def get_layer_map():
23
  class YOLO(nn.Module):
24
  """
25
  A preliminary YOLO (You Only Look Once) model class still under development.
26
- #TODO: Next: Finish forward proccess
27
 
28
  Parameters:
29
  model_cfg: Configuration for the YOLO model. Expected to define the layers,
@@ -33,19 +35,16 @@ class YOLO(nn.Module):
33
  def __init__(self, model_cfg: Dict[str, Any]):
34
  super(YOLO, self).__init__()
35
  self.nc = model_cfg["nc"]
36
- self.layer_map = get_layer_map() # Dynamically get the mapping
37
- self.build_model(model_cfg["model"])
38
- print(self.model)
39
- # raise NotImplementedError("Constructor not implemented.")
40
 
41
  def build_model(self, model_arch: Dict[str, List[Dict[str, Dict[str, Dict]]]]):
42
  model_list = nn.ModuleList()
43
  output_dim = [3]
44
  layer_indices_by_tag = {}
45
-
46
- for arch_name, arch in model_arch.items():
47
- logger.info(f"Building model-{arch_name}")
48
- for layer_idx, layer_spec in enumerate(arch, start=1):
49
  layer_type, layer_info = next(iter(layer_spec.items()))
50
  layer_args = layer_info.get("args", {})
51
  source = layer_info.get("source", -1)
@@ -56,8 +55,9 @@ class YOLO(nn.Module):
56
  layer_args["in_channels"] = output_dim[source]
57
  if "Detect" in layer_type:
58
  layer_args["nc"] = self.nc
 
59
 
60
- layer = self.create_layer(layer_type, **layer_args)
61
  model_list.append(layer)
62
 
63
  if "tags" in layer_info:
@@ -69,22 +69,32 @@ class YOLO(nn.Module):
69
  output_dim.append(out_channels)
70
  self.model = model_list
71
 
72
- def get_out_channels(self, layer_type, layer_args, output_dim, source):
 
 
 
 
 
 
 
 
 
 
 
73
  if "Conv" in layer_type:
74
  return layer_args["out_channels"]
 
 
75
  if layer_type == "Concat":
76
  return sum(output_dim[idx] for idx in source)
77
- if "Pool" in layer_type:
78
- return output_dim[source] // 2
79
- if layer_type == "UpSample":
80
- return output_dim[source] * 2
81
  if layer_type == "IDetect":
82
  return None
83
 
84
- def create_layer(self, layer_type: str, **kwargs):
85
- # Dictionary mapping layer names to actual layer classes
86
  if layer_type in self.layer_map:
87
- return self.layer_map[layer_type](**kwargs)
 
 
88
  else:
89
  raise ValueError(f"Unsupported layer type: {layer_type}")
90
 
@@ -99,6 +109,7 @@ def get_model(model_cfg: dict) -> YOLO:
99
  YOLO: An instance of the model defined by the given configuration.
100
  """
101
  model = YOLO(model_cfg)
 
102
  return model
103
 
104
 
 
1
+ import inspect
2
+ from typing import Any, Dict, List, Union
3
+
4
+ import torch
5
  import torch.nn as nn
6
  from loguru import logger
7
+ from omegaconf import OmegaConf
 
 
8
 
9
  from model import module
10
+ from utils.tools import load_model_cfg
11
 
12
 
13
  def get_layer_map():
 
26
  class YOLO(nn.Module):
27
  """
28
  A preliminary YOLO (You Only Look Once) model class still under development.
 
29
 
30
  Parameters:
31
  model_cfg: Configuration for the YOLO model. Expected to define the layers,
 
35
  def __init__(self, model_cfg: Dict[str, Any]):
36
  super(YOLO, self).__init__()
37
  self.nc = model_cfg["nc"]
38
+ self.layer_map = get_layer_map() # Get the map Dict[str: Module]
39
+ self.build_model(model_cfg.model)
 
 
40
 
41
  def build_model(self, model_arch: Dict[str, List[Dict[str, Dict[str, Dict]]]]):
42
  model_list = nn.ModuleList()
43
  output_dim = [3]
44
  layer_indices_by_tag = {}
45
+ for arch_name in model_arch:
46
+ logger.info(f"πŸ—οΈ Building model-{arch_name}")
47
+ for layer_idx, layer_spec in enumerate(model_arch[arch_name], start=1):
 
48
  layer_type, layer_info = next(iter(layer_spec.items()))
49
  layer_args = layer_info.get("args", {})
50
  source = layer_info.get("source", -1)
 
55
  layer_args["in_channels"] = output_dim[source]
56
  if "Detect" in layer_type:
57
  layer_args["nc"] = self.nc
58
+ layer_args["ch"] = [output_dim[idx] for idx in source]
59
 
60
+ layer = self.create_layer(layer_type, source, **layer_args)
61
  model_list.append(layer)
62
 
63
  if "tags" in layer_info:
 
69
  output_dim.append(out_channels)
70
  self.model = model_list
71
 
72
+ def forward(self, x):
73
+ y = [x]
74
+ for layer in self.model:
75
+ if OmegaConf.is_list(layer.source):
76
+ model_input = [y[idx] for idx in layer.source]
77
+ else:
78
+ model_input = y[layer.source]
79
+ x = layer(model_input)
80
+ y.append(x)
81
+ return x
82
+
83
+ def get_out_channels(self, layer_type: str, layer_args: dict, output_dim: list, source: Union[int, list]):
84
  if "Conv" in layer_type:
85
  return layer_args["out_channels"]
86
+ if layer_type in ["MaxPool", "UpSample"]:
87
+ return output_dim[source]
88
  if layer_type == "Concat":
89
  return sum(output_dim[idx] for idx in source)
 
 
 
 
90
  if layer_type == "IDetect":
91
  return None
92
 
93
+ def create_layer(self, layer_type: str, source: Union[int, list], **kwargs):
 
94
  if layer_type in self.layer_map:
95
+ layer = self.layer_map[layer_type](**kwargs)
96
+ layer.source = source
97
+ return layer
98
  else:
99
  raise ValueError(f"Unsupported layer type: {layer_type}")
100
 
 
109
  YOLO: An instance of the model defined by the given configuration.
110
  """
111
  model = YOLO(model_cfg)
112
+ logger.info("βœ… Success load model")
113
  return model
114
 
115
 
train.py CHANGED
@@ -2,25 +2,18 @@ import argparse
2
  from loguru import logger
3
  from model.yolo import get_model
4
  from utils.tools import load_model_cfg, custom_logger
 
 
 
5
 
6
 
7
- def parse_arguments() -> argparse.Namespace:
8
- """
9
- Parse command-line arguments to get the model configuration file.
10
-
11
- Returns:
12
- argparse.Namespace: The command-line arguments object with 'config' attribute.
13
- """
14
- parser = argparse.ArgumentParser(description="Load a YOLO model configuration and display the model.")
15
- parser.add_argument(
16
- "--model-config", type=str, default="v7-base", help="Name or path to the model configuration file."
17
- )
18
- return parser.parse_args()
19
 
20
 
21
  if __name__ == "__main__":
22
  custom_logger()
23
- args = parse_arguments()
24
- model_cfg = load_model_cfg(args.model_config)
25
- model = get_model(model_cfg)
26
- logger.info("Success load model: {}", model)
 
2
  from loguru import logger
3
  from model.yolo import get_model
4
  from utils.tools import load_model_cfg, custom_logger
5
+ import hydra
6
+ from config.config import Config
7
+ from omegaconf import OmegaConf
8
 
9
 
10
+ @hydra.main(config_path="config", config_name="config", version_base=None)
11
+ def main(cfg: Config):
12
+ OmegaConf.set_struct(cfg, False)
13
+ model = get_model(cfg.model)
14
+ logger.info("Success load model")
 
 
 
 
 
 
 
15
 
16
 
17
  if __name__ == "__main__":
18
  custom_logger()
19
+ main()