henry000 commited on
Commit
5556058
·
1 Parent(s): ab407e8

✨ [New] load v9 model via yolo.py

Browse files
Files changed (1) hide show
  1. yolo/model/yolo.py +27 -15
yolo/model/yolo.py CHANGED
@@ -2,8 +2,9 @@ from typing import Any, Dict, List, Union
2
 
3
  import torch.nn as nn
4
  from loguru import logger
5
- from omegaconf import OmegaConf
6
 
 
7
  from yolo.tools.layer_helper import get_layer_map
8
 
9
 
@@ -18,7 +19,7 @@ class YOLO(nn.Module):
18
 
19
  def __init__(self, model_cfg: Model):
20
  super(YOLO, self).__init__()
21
- self.nc = model_cfg["nc"]
22
  self.layer_map = get_layer_map() # Get the map Dict[str: Module]
23
  self.build_model(model_cfg.model)
24
 
@@ -26,24 +27,30 @@ class YOLO(nn.Module):
26
  model_list = nn.ModuleList()
27
  output_dim = [3]
28
  layer_indices_by_tag = {}
 
29
  logger.info(f"🚜 Building YOLO")
30
  for arch_name in model_arch:
31
  logger.info(f" 🏗️ Building {arch_name}")
32
- for layer_idx, layer_spec in enumerate(model_arch[arch_name], start=1):
33
  layer_type, layer_info = next(iter(layer_spec.items()))
34
  layer_args = layer_info.get("args", {})
35
- source = layer_info.get("source", -1)
36
- output = layer_info.get("output", False)
37
 
 
 
38
  if isinstance(source, str):
39
  source = layer_indices_by_tag[source]
40
- if "Conv" in layer_type:
 
 
 
 
41
  layer_args["in_channels"] = output_dim[source]
42
- if "Detect" in layer_type:
43
- layer_args["nc"] = self.nc
44
- layer_args["ch"] = [output_dim[idx] for idx in source]
45
 
46
- layer = self.create_layer(layer_type, source, output, **layer_args)
 
47
  model_list.append(layer)
48
 
49
  if "tags" in layer_info:
@@ -53,13 +60,15 @@ class YOLO(nn.Module):
53
 
54
  out_channels = self.get_out_channels(layer_type, layer_args, output_dim, source)
55
  output_dim.append(out_channels)
 
 
56
  self.model = model_list
57
 
58
  def forward(self, x):
59
  y = [x]
60
  output = []
61
  for layer in self.model:
62
- if OmegaConf.is_list(layer.source):
63
  model_input = [y[idx] for idx in layer.source]
64
  else:
65
  model_input = y[layer.source]
@@ -70,8 +79,10 @@ class YOLO(nn.Module):
70
  return output
71
 
72
  def get_out_channels(self, layer_type: str, layer_args: dict, output_dim: list, source: Union[int, list]):
73
- if "Conv" in layer_type:
74
  return layer_args["out_channels"]
 
 
75
  if layer_type in ["Pool", "UpSample"]:
76
  return output_dim[source]
77
  if layer_type == "Concat":
@@ -79,11 +90,12 @@ class YOLO(nn.Module):
79
  if layer_type == "IDetect":
80
  return None
81
 
82
- def create_layer(self, layer_type: str, source: Union[int, list], output=False, **kwargs):
83
  if layer_type in self.layer_map:
84
  layer = self.layer_map[layer_type](**kwargs)
85
- layer.source = source
86
- layer.output = output
 
87
  return layer
88
  else:
89
  raise ValueError(f"Unsupported layer type: {layer_type}")
 
2
 
3
  import torch.nn as nn
4
  from loguru import logger
5
+ from omegaconf import ListConfig, OmegaConf
6
 
7
+ from yolo.config.config import Model
8
  from yolo.tools.layer_helper import get_layer_map
9
 
10
 
 
19
 
20
  def __init__(self, model_cfg: Model):
21
  super(YOLO, self).__init__()
22
+ self.num_classes = model_cfg["num_classes"]
23
  self.layer_map = get_layer_map() # Get the map Dict[str: Module]
24
  self.build_model(model_cfg.model)
25
 
 
27
  model_list = nn.ModuleList()
28
  output_dim = [3]
29
  layer_indices_by_tag = {}
30
+ layer_idx = 1
31
  logger.info(f"🚜 Building YOLO")
32
  for arch_name in model_arch:
33
  logger.info(f" 🏗️ Building {arch_name}")
34
+ for layer_idx, layer_spec in enumerate(model_arch[arch_name], start=layer_idx):
35
  layer_type, layer_info = next(iter(layer_spec.items()))
36
  layer_args = layer_info.get("args", {})
 
 
37
 
38
+ # Get input source
39
+ source = layer_info.get("source", -1)
40
  if isinstance(source, str):
41
  source = layer_indices_by_tag[source]
42
+ elif isinstance(source, ListConfig):
43
+ source = [layer_indices_by_tag[idx] if isinstance(idx, str) else idx for idx in source]
44
+
45
+ # Find in channels
46
+ if any(module in layer_type for module in ["Conv", "ELAN", "ADown", "CBLinear"]):
47
  layer_args["in_channels"] = output_dim[source]
48
+ if "Detection" in layer_type:
49
+ layer_args["in_channels"] = [output_dim[idx] for idx in source]
50
+ layer_args["num_classes"] = self.num_classes
51
 
52
+ # create layers
53
+ layer = self.create_layer(layer_type, source, layer_info, **layer_args)
54
  model_list.append(layer)
55
 
56
  if "tags" in layer_info:
 
60
 
61
  out_channels = self.get_out_channels(layer_type, layer_args, output_dim, source)
62
  output_dim.append(out_channels)
63
+ layer_idx += 1
64
+
65
  self.model = model_list
66
 
67
  def forward(self, x):
68
  y = [x]
69
  output = []
70
  for layer in self.model:
71
+ if isinstance(layer.source, list):
72
  model_input = [y[idx] for idx in layer.source]
73
  else:
74
  model_input = y[layer.source]
 
79
  return output
80
 
81
  def get_out_channels(self, layer_type: str, layer_args: dict, output_dim: list, source: Union[int, list]):
82
+ if any(module in layer_type for module in ["Conv", "ELAN", "ADown"]):
83
  return layer_args["out_channels"]
84
+ if layer_type == "CBFuse":
85
+ return output_dim[source[-1]]
86
  if layer_type in ["Pool", "UpSample"]:
87
  return output_dim[source]
88
  if layer_type == "Concat":
 
90
  if layer_type == "IDetect":
91
  return None
92
 
93
+ def create_layer(self, layer_type: str, source: Union[int, list], layer_info, **kwargs):
94
  if layer_type in self.layer_map:
95
  layer = self.layer_map[layer_type](**kwargs)
96
+ setattr(layer, "source", source)
97
+ setattr(layer, "output", layer_info.get("output", False))
98
+ setattr(layer, "tags", layer_info.get("tags", None))
99
  return layer
100
  else:
101
  raise ValueError(f"Unsupported layer type: {layer_type}")